libubox
C utility functions for OpenWrt.
udebug.h File Reference
#include <sys/types.h>
#include <stdint.h>
#include <stdarg.h>
#include "list.h"
#include "uloop.h"
#include "avl.h"

Go to the source code of this file.

Data Structures

struct  udebug_buf_flag
 
struct  udebug_buf_meta
 
struct  udebug_buf
 
struct  udebug_packet_info
 
struct  udebug_remote_buf
 
struct  udebug
 
struct  udebug_ptr
 
struct  udebug_snapshot
 
struct  udebug_iter
 

Macros

#define UDEBUG_SOCK_NAME   "/var/run/udebug.sock"
 
#define UDEBUG_TS_MSEC   1000ULL
 
#define UDEBUG_TS_SEC   (1000ULL * UDEBUG_TS_MSEC)
 

Enumerations

enum  udebug_format { UDEBUG_FORMAT_PACKET , UDEBUG_FORMAT_STRING , UDEBUG_FORMAT_BLOBMSG }
 
enum  {
  UDEBUG_DLT_ETHERNET = 1 , UDEBUG_DLT_PPP = 50 , UDEBUG_DLT_RAW_IP = 101 , UDEBUG_DLT_IEEE_802_11 = 105 ,
  UDEBUG_DLT_IEEE_802_11_RADIOTAP = 127 , UDEBUG_DLT_NETLINK = 253
}
 
enum  udebug_meta_type { UDEBUG_META_IFACE_NAME , UDEBUG_META_IFACE_DESC , __UDEBUG_META_MAX }
 

Functions

uint64_t udebug_timestamp (void)
 
void udebug_entry_init_ts (struct udebug_buf *buf, uint64_t timestamp)
 
static void udebug_entry_init (struct udebug_buf *buf)
 
void * udebug_entry_append (struct udebug_buf *buf, const void *data, uint32_t len)
 
int udebug_entry_printf (struct udebug_buf *buf, const char *fmt,...) __attribute__((format(printf
 
int int udebug_entry_vprintf (struct udebug_buf *buf, const char *fmt, va_list ap) __attribute__((format(printf
 
int int uint16_t udebug_entry_trim (struct udebug_buf *buf, uint16_t len)
 
void udebug_entry_set_length (struct udebug_buf *buf, uint16_t len)
 
void udebug_entry_add (struct udebug_buf *buf)
 
int udebug_buf_init (struct udebug_buf *buf, size_t entries, size_t size)
 
int udebug_buf_add (struct udebug *ctx, struct udebug_buf *buf, const struct udebug_buf_meta *meta)
 
uint64_t udebug_buf_flags (struct udebug_buf *buf)
 
void udebug_buf_free (struct udebug_buf *buf)
 
static bool udebug_buf_valid (struct udebug_buf *buf)
 
struct udebug_remote_bufudebug_remote_buf_get (struct udebug *ctx, uint32_t id)
 
int udebug_remote_buf_map (struct udebug *ctx, struct udebug_remote_buf *rb, uint32_t id)
 
void udebug_remote_buf_unmap (struct udebug *ctx, struct udebug_remote_buf *rb)
 
int udebug_remote_buf_set_poll (struct udebug *ctx, struct udebug_remote_buf *rb, bool val)
 
void udebug_remote_buf_set_flags (struct udebug_remote_buf *rb, uint64_t mask, uint64_t set)
 
struct udebug_snapshotudebug_remote_buf_snapshot (struct udebug_remote_buf *rb)
 
bool udebug_snapshot_get_entry (struct udebug_snapshot *s, struct udebug_iter *it, unsigned int entry)
 
void udebug_remote_buf_set_start_time (struct udebug_remote_buf *rb, uint64_t ts)
 
void udebug_remote_buf_set_start_offset (struct udebug_remote_buf *rb, uint32_t idx)
 
void udebug_iter_start (struct udebug_iter *it, struct udebug_snapshot **s, size_t n)
 
bool udebug_iter_next (struct udebug_iter *it)
 
void udebug_init (struct udebug *ctx)
 
int udebug_connect (struct udebug *ctx, const char *path)
 
void udebug_auto_connect (struct udebug *ctx, const char *path)
 
void udebug_add_uloop (struct udebug *ctx)
 
void udebug_poll (struct udebug *ctx)
 
void udebug_free (struct udebug *ctx)
 
static bool udebug_is_connected (struct udebug *ctx)
 
int udebug_id_cmp (const void *k1, const void *k2, void *ptr)
 

Macro Definition Documentation

◆ UDEBUG_SOCK_NAME

#define UDEBUG_SOCK_NAME   "/var/run/udebug.sock"

Definition at line 29 of file udebug.h.

◆ UDEBUG_TS_MSEC

#define UDEBUG_TS_MSEC   1000ULL

Definition at line 52 of file udebug.h.

◆ UDEBUG_TS_SEC

#define UDEBUG_TS_SEC   (1000ULL * UDEBUG_TS_MSEC)

Definition at line 53 of file udebug.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
UDEBUG_DLT_ETHERNET 
UDEBUG_DLT_PPP 
UDEBUG_DLT_RAW_IP 
UDEBUG_DLT_IEEE_802_11 
UDEBUG_DLT_IEEE_802_11_RADIOTAP 
UDEBUG_DLT_NETLINK 

Definition at line 37 of file udebug.h.

37  {
39  UDEBUG_DLT_PPP = 50,
40  UDEBUG_DLT_RAW_IP = 101,
43  UDEBUG_DLT_NETLINK = 253,
44 };
@ UDEBUG_DLT_RAW_IP
Definition: udebug.h:40
@ UDEBUG_DLT_IEEE_802_11_RADIOTAP
Definition: udebug.h:42
@ UDEBUG_DLT_ETHERNET
Definition: udebug.h:38
@ UDEBUG_DLT_PPP
Definition: udebug.h:39
@ UDEBUG_DLT_NETLINK
Definition: udebug.h:43
@ UDEBUG_DLT_IEEE_802_11
Definition: udebug.h:41

◆ udebug_format

Enumerator
UDEBUG_FORMAT_PACKET 
UDEBUG_FORMAT_STRING 
UDEBUG_FORMAT_BLOBMSG 

Definition at line 31 of file udebug.h.

31  {
35 };
@ UDEBUG_FORMAT_STRING
Definition: udebug.h:33
@ UDEBUG_FORMAT_BLOBMSG
Definition: udebug.h:34
@ UDEBUG_FORMAT_PACKET
Definition: udebug.h:32

◆ udebug_meta_type

Enumerator
UDEBUG_META_IFACE_NAME 
UDEBUG_META_IFACE_DESC 
__UDEBUG_META_MAX 

Definition at line 46 of file udebug.h.

46  {
50 };
@ UDEBUG_META_IFACE_NAME
Definition: udebug.h:47
@ __UDEBUG_META_MAX
Definition: udebug.h:49
@ UDEBUG_META_IFACE_DESC
Definition: udebug.h:48

Function Documentation

◆ udebug_add_uloop()

void udebug_add_uloop ( struct udebug ctx)

Definition at line 827 of file udebug.c.

828 {
829  if (ctx->fd.registered)
830  return;
831 
832  ctx->fd.cb = udebug_fd_cb;
833  uloop_fd_add(&ctx->fd, ULOOP_READ);
834 }
struct uloop_fd fd
Definition: udebug.h:106
bool registered
Definition: uloop.h:68
uloop_fd_handler cb
Definition: uloop.h:64
static void udebug_fd_cb(struct uloop_fd *fd, unsigned int events)
Definition: udebug.c:817
int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
Definition: uloop.c:235
#define ULOOP_READ
Definition: uloop.h:47
Here is the call graph for this function:
Here is the caller graph for this function:

◆ udebug_auto_connect()

void udebug_auto_connect ( struct udebug ctx,
const char *  path 
)

Definition at line 763 of file udebug.c.

764 {
765  free(ctx->socket_path);
767  ctx->socket_path = path ? strdup(path) : NULL;
768  if (ctx->fd.fd >= 0)
769  return;
770 
772 }
char * socket_path
Definition: udebug.h:108
struct uloop_timeout reconnect
Definition: udebug.h:109
int fd
Definition: uloop.h:65
uloop_timeout_handler cb
Definition: uloop.h:77
static void udebug_reconnect_cb(struct uloop_timeout *t)
Definition: udebug.c:751
Here is the call graph for this function:

◆ udebug_buf_add()

int udebug_buf_add ( struct udebug ctx,
struct udebug_buf buf,
const struct udebug_buf_meta meta 
)

Definition at line 724 of file udebug.c.

726 {
727  if (!buf->hdr)
728  return -1;
729 
730  list_add_tail(&buf->list, &ctx->local_rings);
731  buf->ctx = ctx;
732  buf->meta = meta;
733  buf->id = ctx->next_id++;
734  buf->hdr->format = meta->format;
735  buf->hdr->sub_format = meta->sub_format;
736 
737  if (ctx->fd.fd >= 0)
738  __udebug_buf_add(ctx, buf);
739 
740  return 0;
741 }
static void list_add_tail(struct list_head *_new, struct list_head *head)
Definition: list.h:165
uint32_t sub_format
Definition: udebug.h:66
enum udebug_format format
Definition: udebug.h:65
struct udebug_hdr * hdr
Definition: udebug.h:78
struct list_head list
Definition: udebug.h:76
uint32_t id
Definition: udebug.h:74
struct udebug * ctx
Definition: udebug.h:72
const struct udebug_buf_meta * meta
Definition: udebug.h:73
uint32_t sub_format
Definition: udebug-proto.h:28
uint32_t format
Definition: udebug-proto.h:27
uint32_t next_id
Definition: udebug.h:105
struct list_head local_rings
Definition: udebug.h:103
static void __udebug_buf_add(struct udebug *ctx, struct udebug_buf *buf)
Definition: udebug.c:697
Here is the call graph for this function:

◆ udebug_buf_flags()

uint64_t udebug_buf_flags ( struct udebug_buf buf)

Definition at line 516 of file udebug.c.

517 {
518  struct udebug_hdr *hdr = buf->hdr;
519  uint64_t flags;
520 
521  if (!hdr)
522  return 0;
523 
524  flags = hdr->flags[0];
525  if (sizeof(flags) != sizeof(uintptr_t))
526  flags |= ((uint64_t)hdr->flags[1]) << 32;
527 
528  return flags;
529 }
uintptr_t flags[8/sizeof(uintptr_t)]
Definition: udebug-proto.h:30

◆ udebug_buf_free()

void udebug_buf_free ( struct udebug_buf buf)

Definition at line 681 of file udebug.c.

682 {
683  struct udebug *ctx = buf->ctx;
684 
685  if (!list_empty(&buf->list) && buf->list.prev)
686  list_del(&buf->list);
687 
688  if (ctx && ctx->fd.fd >= 0)
690 
691  munmap(buf->hdr, buf->head_size + 2 * buf->data_size);
692  close(buf->fd);
693  memset(buf, 0, sizeof(*buf));
694 }
static bool list_empty(const struct list_head *head)
Definition: list.h:69
static void list_del(struct list_head *entry)
Definition: list.h:96
struct list_head * prev
Definition: list.h:55
size_t head_size
Definition: udebug.h:81
int fd
Definition: udebug.h:83
size_t data_size
Definition: udebug.h:80
Definition: udebug.h:102
@ CL_MSG_RING_REMOVE
Definition: udebug-proto.h:41
static void udebug_buf_msg(struct udebug_buf *buf, enum udebug_client_msg_type type)
Definition: udebug.c:395
Here is the call graph for this function:
Here is the caller graph for this function:

◆ udebug_buf_init()

int udebug_buf_init ( struct udebug_buf buf,
size_t  entries,
size_t  size 
)

Definition at line 450 of file udebug.c.

451 {
452  char filename[] = "/udebug.XXXXXX";
453  unsigned int order = 12;
454  uint8_t ring_order = 5;
455  size_t head_size;
456  int fd;
457 
459  INIT_LIST_HEAD(&buf->list);
460  if (size < page_size)
461  size = page_size;
462  while(size > 1U << order)
463  order++;
464  size = 1 << order;
465  while (entries > 1U << ring_order)
466  ring_order++;
467  entries = 1 << ring_order;
468 
469  if (size > (1U << 29) || entries > (1U << 24))
470  return -1;
471 
472  head_size = __udebug_headsize(entries);
473  while (ALIGN(sizeof(*buf->hdr) + (entries * 2) * sizeof(struct udebug_ptr), page_size) == head_size)
474  entries *= 2;
475 
476  fd = shm_open_anon(filename);
477  if (fd < 0)
478  return -1;
479 
480  if (ftruncate(fd, head_size + size) < 0)
481  goto err_close;
482 
483  buf->head_size = head_size;
484  buf->data_size = size;
485  buf->ring_size = entries;
486 
487  if (__udebug_buf_map(buf, fd))
488  goto err_close;
489 
490  buf->fd = fd;
491  buf->hdr->ring_size = entries;
492  buf->hdr->data_size = size;
493 
494  /* ensure hdr changes are visible */
495  __sync_synchronize();
496 
497  return 0;
498 
499 err_close:
500  close(fd);
501  return -1;
502 }
static void INIT_LIST_HEAD(struct list_head *list)
Definition: list.h:63
size_t ring_size
Definition: udebug.h:82
uint32_t ring_size
Definition: udebug-proto.h:24
uint32_t data_size
Definition: udebug-proto.h:25
int fd
Definition: udebug-priv.h:27
static unsigned int page_size
Definition: udebug.c:40
static void udebug_init_page_size(void)
Definition: udebug.c:412
static int shm_open_anon(char *name)
Definition: udebug.c:65
static size_t __udebug_headsize(unsigned int ring_size)
Definition: udebug.c:406
static int __udebug_buf_map(struct udebug_buf *buf, int fd)
Definition: udebug.c:116
#define ALIGN(i, sz)
Definition: udebug.c:32
Here is the call graph for this function:

◆ udebug_buf_valid()

static bool udebug_buf_valid ( struct udebug_buf buf)
inlinestatic

Definition at line 169 of file udebug.h.

170 {
171  return buf->hdr;
172 }

◆ udebug_connect()

int udebug_connect ( struct udebug ctx,
const char *  path 
)

Definition at line 774 of file udebug.c.

775 {
776  struct udebug_remote_buf *rb;
777  struct udebug_buf *buf;
778 
779  if (ctx->fd.fd >= 0)
780  close(ctx->fd.fd);
781  ctx->fd.fd = -1;
782 
783  if (!path)
784  path = UDEBUG_SOCK_NAME;
785 
786  ctx->fd.fd = usock(USOCK_UNIX, path, NULL);
787  if (ctx->fd.fd < 0)
788  return -1;
789 
791  __udebug_buf_add(ctx, buf);
792 
794  if (!rb->poll)
795  continue;
796 
797  rb->poll = false;
798  udebug_remote_buf_set_poll(ctx, rb, true);
799  }
800 
801  return 0;
802 }
#define avl_for_each_element(tree, element, node_member)
Definition: avl.h:366
#define list_for_each_entry(p, h, field)
Definition: list.h:132
Definition: test-avl.c:13
struct avl_tree remote_rings
Definition: udebug.h:104
int udebug_remote_buf_set_poll(struct udebug *ctx, struct udebug_remote_buf *rb, bool val)
Definition: udebug-remote.c:89
#define UDEBUG_SOCK_NAME
Definition: udebug.h:29
int usock(int type, const char *host, const char *service)
Definition: usock.c:252
#define USOCK_UNIX
Definition: usock.h:31
Here is the call graph for this function:
Here is the caller graph for this function:

◆ udebug_entry_add()

void udebug_entry_add ( struct udebug_buf buf)

Definition at line 642 of file udebug.c.

643 {
644  struct udebug_hdr *hdr = buf->hdr;
645  struct udebug_ptr *ptr;
646  uint32_t notify;
647  uint8_t *data;
648 
649  if (!hdr)
650  return;
651 
652  ptr = udebug_ring_ptr(hdr, hdr->head);
653 
654  /* ensure strings are always 0-terminated */
655  data = udebug_buf_ptr(buf, ptr->start + ptr->len);
656  *data = 0;
657  hdr->data_head = ptr->start + ptr->len + 1;
658 
659  /* ensure that all data changes are visible before advancing head */
660  __sync_synchronize();
661 
662  u32_set(&hdr->head, u32_get(&hdr->head) + 1);
663  if (!u32_get(&hdr->head))
664  u32_set(&hdr->head_hi, u32_get(&hdr->head_hi) + 1);
665 
666  /* ensure that head change is visible */
667  __sync_synchronize();
668 
669  notify = __atomic_exchange_n(&hdr->notify, 0, __ATOMIC_RELAXED);
670  if (notify) {
671  struct udebug_client_msg msg = {
673  .id = buf->id,
674  .notify_mask = notify,
675  };
676  blob_buf_init(&b, 0);
677 
678  udebug_send_msg(buf->ctx, &msg, b.head, -1);
679  }
680 }
int blob_buf_init(struct blob_buf *buf, int id)
Definition: blob.c:91
char data[]
Definition: blob.h:1
struct blob_attr * head
Definition: blob.h:65
uintptr_t notify
Definition: udebug-proto.h:31
uint32_t head_hi
Definition: udebug-proto.h:33
uint32_t data_head
Definition: udebug-proto.h:35
uint32_t head
Definition: udebug-proto.h:34
uint32_t len
Definition: udebug.h:117
uint32_t start
Definition: udebug.h:116
static uint32_t u32_get(void *ptr)
Definition: udebug-priv.h:47
struct udebug_client_msg * msg
Definition: udebug-priv.h:29
static void u32_set(void *ptr, uint32_t val)
Definition: udebug-priv.h:41
@ CL_MSG_RING_NOTIFY
Definition: udebug-proto.h:42
static struct udebug_ptr * udebug_ring_ptr(struct udebug_hdr *hdr, uint32_t idx)
Definition: udebug-proto.h:61
static void * udebug_buf_ptr(struct udebug_buf *buf, uint32_t ofs)
Definition: udebug-proto.h:67
static struct blob_buf b
Definition: udebug.c:39
static void udebug_send_msg(struct udebug *ctx, struct udebug_client_msg *msg, struct blob_attr *meta, int fd)
Definition: udebug.c:303
Here is the call graph for this function:
Here is the caller graph for this function:

◆ udebug_entry_append()

void* udebug_entry_append ( struct udebug_buf buf,
const void *  data,
uint32_t  len 
)

Definition at line 545 of file udebug.c.

546 {
547  struct udebug_hdr *hdr = buf->hdr;
548  struct udebug_ptr *ptr;
549  uint32_t ofs;
550  void *ret;
551 
552  if (!hdr)
553  return NULL;
554 
555  ptr = udebug_ring_ptr(hdr, hdr->head);
556  ofs = ptr->start + ptr->len;
557  if (ptr->len + len > buf->data_size / 2)
558  return NULL;
559 
560  ret = udebug_buf_alloc(buf, ofs, len);
561  if (data)
562  memcpy(ret, data, len);
563  ptr->len += len;
564 
565  return ret;
566 }
static void * udebug_buf_alloc(struct udebug_buf *buf, uint32_t ofs, uint32_t len)
Definition: udebug.c:504
Here is the call graph for this function:

◆ udebug_entry_init()

static void udebug_entry_init ( struct udebug_buf buf)
inlinestatic

Definition at line 151 of file udebug.h.

152 {
154 }
uint64_t udebug_timestamp(void)
Definition: udebug.c:101
void udebug_entry_init_ts(struct udebug_buf *buf, uint64_t timestamp)
Definition: udebug.c:531
Here is the call graph for this function:
Here is the caller graph for this function:

◆ udebug_entry_init_ts()

void udebug_entry_init_ts ( struct udebug_buf buf,
uint64_t  timestamp 
)

Definition at line 531 of file udebug.c.

532 {
533  struct udebug_hdr *hdr = buf->hdr;
534  struct udebug_ptr *ptr;
535 
536  if (!hdr)
537  return;
538 
539  ptr = udebug_ring_ptr(hdr, hdr->head);
540  ptr->start = hdr->data_head;
541  ptr->len = 0;
542  ptr->timestamp = timestamp;
543 }
uint64_t timestamp
Definition: udebug.h:118
Here is the call graph for this function:
Here is the caller graph for this function:

◆ udebug_entry_printf()

int udebug_entry_printf ( struct udebug_buf buf,
const char *  fmt,
  ... 
)

◆ udebug_entry_set_length()

void udebug_entry_set_length ( struct udebug_buf buf,
uint16_t  len 
)

Definition at line 583 of file udebug.c.

584 {
585  struct udebug_hdr *hdr = buf->hdr;
586  struct udebug_ptr *ptr;
587 
588  if (!hdr)
589  return;
590 
591  ptr = udebug_ring_ptr(hdr, hdr->head);
592  ptr->len = len;
593 }
Here is the call graph for this function:

◆ udebug_entry_trim()

int int uint16_t udebug_entry_trim ( struct udebug_buf buf,
uint16_t  len 
)

Definition at line 568 of file udebug.c.

569 {
570  struct udebug_hdr *hdr = buf->hdr;
571  struct udebug_ptr *ptr;
572 
573  if (!hdr)
574  return 0;
575 
576  ptr = udebug_ring_ptr(hdr, hdr->head);
577  if (len)
578  ptr->len -= len;
579 
580  return ptr->len;
581 }
Here is the call graph for this function:

◆ udebug_entry_vprintf()

int int udebug_entry_vprintf ( struct udebug_buf buf,
const char *  fmt,
va_list  ap 
)

◆ udebug_free()

void udebug_free ( struct udebug ctx)

Definition at line 836 of file udebug.c.

837 {
838  struct udebug_remote_buf *rb, *tmp;
839  struct udebug_buf *buf;
840 
841  free(ctx->socket_path);
842  ctx->socket_path = NULL;
843 
844  __udebug_disconnect(ctx, false);
846 
847  while (!list_empty(&ctx->local_rings)) {
848  buf = list_first_entry(&ctx->local_rings, struct udebug_buf, list);
849  udebug_buf_free(buf);
850  }
851 
854 }
#define avl_for_each_element_safe(tree, element, node_member, ptr)
Definition: avl.h:503
#define list_first_entry(ptr, type, field)
Definition: list.h:121
void udebug_remote_buf_unmap(struct udebug *ctx, struct udebug_remote_buf *rb)
Definition: udebug-remote.c:77
void udebug_buf_free(struct udebug_buf *buf)
Definition: udebug.c:681
static void __udebug_disconnect(struct udebug *ctx, bool reconnect)
Definition: udebug.c:91
int uloop_timeout_cancel(struct uloop_timeout *timeout)
Definition: uloop.c:348
Here is the call graph for this function:

◆ udebug_id_cmp()

int udebug_id_cmp ( const void *  k1,
const void *  k2,
void *  ptr 
)

Definition at line 54 of file udebug.c.

55 {
56  uint32_t id1 = (uint32_t)(uintptr_t)k1, id2 = (uint32_t)(uintptr_t)k2;
57  if (id1 < id2)
58  return -1;
59  else if (id1 > id2)
60  return 1;
61  return 0;
62 }
Here is the caller graph for this function:

◆ udebug_init()

void udebug_init ( struct udebug ctx)

Definition at line 743 of file udebug.c.

744 {
746  avl_init(&ctx->remote_rings, udebug_id_cmp, true, NULL);
747  ctx->fd.fd = -1;
748  ctx->poll_handle = -1;
749 }
void avl_init(struct avl_tree *tree, avl_tree_comp comp, bool allow_dups, void *ptr)
Definition: avl.c:92
int poll_handle
Definition: udebug.h:107
int udebug_id_cmp(const void *k1, const void *k2, void *ptr)
Definition: udebug.c:54
Here is the call graph for this function:

◆ udebug_is_connected()

static bool udebug_is_connected ( struct udebug ctx)
inlinestatic

Definition at line 195 of file udebug.h.

196 {
197  return ctx->fd.fd >= 0;
198 }
Here is the caller graph for this function:

◆ udebug_iter_next()

bool udebug_iter_next ( struct udebug_iter it)

Definition at line 325 of file udebug-remote.c.

326 {
327  while (1) {
328  struct udebug_snapshot *s;
329  uint64_t cur_ts;
330  int cur = -1;
331 
332  for (size_t i = 0; i < it->n; i++) {
333  struct udebug_ptr *ptr;
334 
335  s = it->list[i];
336  if (s->iter_idx >= s->n_entries)
337  continue;
338 
339  ptr = &s->entries[s->iter_idx];
340  if (cur >= 0 && ptr->timestamp > cur_ts)
341  continue;
342 
343  cur = i;
344  cur_ts = ptr->timestamp;
345  }
346 
347  if (cur < 0)
348  return false;
349 
350  s = it->list[cur];
351  it->s_idx = cur;
352  if (!udebug_snapshot_get_entry(s, it, s->iter_idx++))
353  continue;
354 
355  return true;
356  }
357 }
unsigned int s_idx
Definition: udebug.h:141
size_t n
Definition: udebug.h:138
struct udebug_snapshot ** list
Definition: udebug.h:137
unsigned int n_entries
Definition: udebug.h:123
uint32_t iter_idx
Definition: udebug.h:128
struct udebug_ptr * entries
Definition: udebug.h:122
bool udebug_snapshot_get_entry(struct udebug_snapshot *s, struct udebug_iter *it, unsigned int entry)
Here is the call graph for this function:

◆ udebug_iter_start()

void udebug_iter_start ( struct udebug_iter it,
struct udebug_snapshot **  s,
size_t  n 
)

Definition at line 314 of file udebug-remote.c.

315 {
316  memset(it, 0, sizeof(*it));
317 
318  it->list = s;
319  it->n = n;
320 
321  for (size_t i = 0; i < it->n; i++)
322  it->list[i]->iter_idx = 0;
323 }

◆ udebug_poll()

void udebug_poll ( struct udebug ctx)

Definition at line 804 of file udebug.c.

805 {
806  while (__udebug_poll(ctx, NULL, false));
807 }
static struct udebug_client_msg * __udebug_poll(struct udebug *ctx, int *fd, bool wait)
Definition: udebug.c:339
Here is the call graph for this function:
Here is the caller graph for this function:

◆ udebug_remote_buf_get()

struct udebug_remote_buf* udebug_remote_buf_get ( struct udebug ctx,
uint32_t  id 
)

Definition at line 39 of file udebug-remote.c.

40 {
41  struct udebug_remote_buf *rb;
42  void *key = (void *)(uintptr_t)id;
43 
44  return avl_find_element(&ctx->remote_rings, key, rb, node);
45 }
#define avl_find_element(tree, key, element, node_element)
Definition: avl.h:240
uint32_t id
Definition: udebug-proto.h:2

◆ udebug_remote_buf_map()

int udebug_remote_buf_map ( struct udebug ctx,
struct udebug_remote_buf rb,
uint32_t  id 
)

Definition at line 47 of file udebug-remote.c.

48 {
49  void *key = (void *)(uintptr_t)id;
50  struct udebug_client_msg *msg;
51  struct udebug_client_msg send_msg = {
53  .id = id,
54  };
55  int fd = -1;
56 
57  if (rb->buf.data || !udebug_is_connected(ctx))
58  return -1;
59 
60  msg = udebug_send_and_wait(ctx, &send_msg, &fd);
61  if (!msg || fd < 0)
62  return -1;
63 
64  if (udebug_buf_open(&rb->buf, fd, msg->ring_size, msg->data_size)) {
65  fprintf(stderr, "failed to open fd %d, ring_size=%d, data_size=%d\n", fd, msg->ring_size, msg->data_size);
66  close(fd);
67  return -1;
68  }
69 
70  rb->pcap_iface = ~0;
71  rb->node.key = key;
72  avl_insert(&ctx->remote_rings, &rb->node);
73 
74  return 0;
75 }
int avl_insert(struct avl_tree *tree, struct avl_node *new)
Definition: avl.c:220
const void * key
Definition: avl.h:84
void * data
Definition: udebug.h:79
uint32_t ring_size
Definition: udebug-proto.h:54
uint32_t data_size
Definition: udebug-proto.h:54
uint32_t pcap_iface
Definition: udebug.h:97
struct udebug_buf buf
Definition: udebug.h:92
struct avl_node node
Definition: udebug.h:91
@ CL_MSG_RING_GET
Definition: udebug-proto.h:44
struct udebug_client_msg * udebug_send_and_wait(struct udebug *ctx, struct udebug_client_msg *msg, int *rfd)
Definition: udebug.c:810
int udebug_buf_open(struct udebug_buf *buf, int fd, uint32_t ring_size, uint32_t data_size)
Definition: udebug.c:424
static bool udebug_is_connected(struct udebug *ctx)
Definition: udebug.h:195
Here is the call graph for this function:

◆ udebug_remote_buf_set_flags()

void udebug_remote_buf_set_flags ( struct udebug_remote_buf rb,
uint64_t  mask,
uint64_t  set 
)

Definition at line 181 of file udebug-remote.c.

182 {
183  struct udebug_hdr *hdr = rb->buf.hdr;
184 
185  if (!hdr)
186  return;
187 
188  if ((uintptr_t)mask)
189  __atomic_and_fetch(&hdr->flags[0], (uintptr_t)~mask, __ATOMIC_RELAXED);
190  if ((uintptr_t)set)
191  __atomic_or_fetch(&hdr->flags[0], (uintptr_t)set, __ATOMIC_RELAXED);
192 
193  if (sizeof(mask) == sizeof(unsigned long))
194  return;
195 
196  mask >>= 32;
197  if ((uintptr_t)mask)
198  __atomic_and_fetch(&hdr->flags[1], (uintptr_t)~mask, __ATOMIC_RELAXED);
199  if ((uintptr_t)set)
200  __atomic_or_fetch(&hdr->flags[1], (uintptr_t)set, __ATOMIC_RELAXED);
201 }

◆ udebug_remote_buf_set_poll()

int udebug_remote_buf_set_poll ( struct udebug ctx,
struct udebug_remote_buf rb,
bool  val 
)

Definition at line 89 of file udebug-remote.c.

90 {
91  int handle;
92 
93  if (!rb->buf.data)
94  return -1;
95 
96  if (rb->poll == val)
97  return 0;
98 
99  rb->poll = val;
100  if (!val)
101  return 0;
102 
103  handle = udebug_remote_get_handle(ctx);
104  if (handle < 0)
105  return -1;
106 
107  __atomic_fetch_or(&rb->buf.hdr->notify, 1UL << handle, __ATOMIC_RELAXED);
108  return 0;
109 }
static int udebug_remote_get_handle(struct udebug *ctx)
Definition: udebug-remote.c:21
Here is the call graph for this function:
Here is the caller graph for this function:

◆ udebug_remote_buf_set_start_offset()

void udebug_remote_buf_set_start_offset ( struct udebug_remote_buf rb,
uint32_t  idx 
)

Definition at line 173 of file udebug-remote.c.

174 {
175  if (!rb->buf.hdr)
176  return;
177 
178  rb->head = rb->buf.hdr->head - idx;
179 }
uint32_t head
Definition: udebug.h:94

◆ udebug_remote_buf_set_start_time()

void udebug_remote_buf_set_start_time ( struct udebug_remote_buf rb,
uint64_t  ts 
)

Definition at line 148 of file udebug-remote.c.

149 {
150  struct udebug_hdr *hdr = rb->buf.hdr;
151  uint32_t head = u32_get(&hdr->head);
152  uint32_t start = rb->head, end = head;
153  uint32_t diff;
154 
155  if (!hdr)
156  return;
157 
158  rbuf_advance_read_head(rb, head, NULL);
159  while ((diff = u32_sub(end, start)) > 0) {
160  uint32_t cur = start + diff / 2;
161  struct udebug_ptr *ptr;
162 
163  ptr = udebug_ring_ptr(hdr, cur);
164  if (ptr->timestamp > ts)
165  end = cur - 1;
166  else
167  start = cur + 1;
168  }
169 
170  rb->head = start;
171 }
static int32_t u32_sub(uint32_t a, uint32_t b)
Definition: udebug-priv.h:31
static void rbuf_advance_read_head(struct udebug_remote_buf *rb, uint32_t head, uint32_t *data_start)
Here is the call graph for this function:

◆ udebug_remote_buf_snapshot()

struct udebug_snapshot* udebug_remote_buf_snapshot ( struct udebug_remote_buf rb)

Definition at line 204 of file udebug-remote.c.

205 {
206  struct udebug_hdr *hdr = rb->buf.hdr;
207  struct udebug_ptr *last_ptr;
208  uint32_t data_start, data_end, data_used;
209  struct udebug_snapshot *s = NULL;
210  struct udebug_ptr *ptr_buf, *first_ptr;
211  uint32_t data_size, ptr_size;
212  uint32_t head, first_idx;
213  uint32_t prev_read_head = rb->head;
214  void *data_buf;
215 
216  if (!hdr)
217  return NULL;
218 
219  head = u32_get(&hdr->head);
220  rbuf_advance_read_head(rb, head, &data_start);
221  if (rb->head == head)
222  return NULL;
223 
224  first_idx = rb->head;
225  first_ptr = udebug_ring_ptr(hdr, first_idx);
226  last_ptr = udebug_ring_ptr(hdr, head - 1);
227  data_end = last_ptr->start + last_ptr->len;
228 
229  data_size = data_end - data_start;
230  ptr_size = head - rb->head;
231  if (data_size > rb->buf.data_size || ptr_size > rb->buf.ring_size) {
232  fprintf(stderr, "Invalid data size: %x > %x, %x > %x\n", data_size, (int)rb->buf.data_size, ptr_size, (int)rb->buf.ring_size);
233  goto out;
234  }
235 
236  s = calloc_a(sizeof(*s),
237  &ptr_buf, ptr_size * sizeof(*ptr_buf),
238  &data_buf, data_size);
239 
240  s->data = memcpy(data_buf, udebug_buf_ptr(&rb->buf, data_start), data_size);
241  s->data_size = data_size;
242  s->entries = ptr_buf;
243  s->dropped = rb->head - prev_read_head;
244 
245  if (first_ptr > last_ptr) {
246  struct udebug_ptr *start_ptr = udebug_ring_ptr(hdr, 0);
247  struct udebug_ptr *end_ptr = udebug_ring_ptr(hdr, rb->buf.ring_size - 1) + 1;
248  uint32_t size = end_ptr - first_ptr;
249  memcpy(s->entries, first_ptr, size * sizeof(*s->entries));
250  memcpy(s->entries + size, start_ptr, (last_ptr + 1 - start_ptr) * sizeof(*s->entries));
251  } else {
252  memcpy(s->entries, first_ptr, (last_ptr + 1 - first_ptr) * sizeof(*s->entries));
253  }
254 
255  /* get a snapshot of the counter that indicates how much data has been
256  * clobbered by newly added entries */
257  __sync_synchronize();
258  data_used = u32_get(&hdr->data_used) - rb->buf.data_size;
259 
260  s->n_entries = head - first_idx;
261 
262  rbuf_advance_read_head(rb, head, NULL);
263  if (s->n_entries < rb->head - first_idx) {
264  free(s);
265  s = NULL;
266  goto out;
267  }
268 
269  s->entries += rb->head - first_idx;
270  s->n_entries -= rb->head - first_idx;
271  while (s->n_entries > 0 &&
272  u32_sub(s->entries[0].start, data_used) < 0) {
273  s->entries++;
274  s->n_entries--;
275  s->dropped++;
276  }
277 
278  for (size_t i = 0; i < s->n_entries; i++)
279  s->entries[i].start -= data_start;
280 
281  s->format = hdr->format;
282  s->sub_format = hdr->sub_format;
283  s->rbuf_idx = (uint32_t)(uintptr_t)rb->node.key;
284 
285 out:
286  rb->head = head;
287  return s;
288 }
uint32_t data_used
Definition: udebug-proto.h:36
enum udebug_format format
Definition: udebug.h:130
size_t data_size
Definition: udebug.h:126
unsigned int dropped
Definition: udebug.h:124
void * data
Definition: udebug.h:125
uint32_t rbuf_idx
Definition: udebug.h:133
uint32_t sub_format
Definition: udebug.h:131
int uint32_t uint32_t data_size
Definition: udebug-priv.h:27
#define calloc_a(len,...)
Definition: utils.h:39
Here is the call graph for this function:

◆ udebug_remote_buf_unmap()

void udebug_remote_buf_unmap ( struct udebug ctx,
struct udebug_remote_buf rb 
)

Definition at line 77 of file udebug-remote.c.

78 {
79  if (!rb->buf.data)
80  return;
81 
82  avl_delete(&ctx->remote_rings, &rb->node);
83  udebug_buf_free(&rb->buf);
84  rb->poll = 0;
85  rb->node.key = NULL;
86  rb->pcap_iface = ~0;
87 }
void avl_delete(struct avl_tree *tree, struct avl_node *node)
Definition: avl.c:307
Here is the call graph for this function:
Here is the caller graph for this function:

◆ udebug_snapshot_get_entry()

bool udebug_snapshot_get_entry ( struct udebug_snapshot s,
struct udebug_iter it,
unsigned int  entry 
)

Definition at line 290 of file udebug-remote.c.

291 {
292  struct udebug_ptr *ptr;
293 
294  it->len = 0;
295  if (entry >= s->n_entries)
296  goto error;
297 
298  ptr = &s->entries[entry];
299  if (ptr->start > s->data_size || ptr->len > s->data_size ||
300  ptr->start + ptr->len > s->data_size)
301  goto error;
302 
303  it->s = s;
304  it->data = s->data + ptr->start;
305  it->len = ptr->len;
306  it->timestamp = ptr->timestamp;
307  return true;
308 
309 error:
310  it->data = NULL;
311  return false;
312 }
void * data
Definition: udebug.h:144
struct udebug_snapshot * s
Definition: udebug.h:140
uint64_t timestamp
Definition: udebug.h:143
size_t len
Definition: udebug.h:145
Here is the caller graph for this function:

◆ udebug_timestamp()

uint64_t udebug_timestamp ( void  )

Definition at line 101 of file udebug.c.

102 {
103  struct timespec ts;
104  uint64_t val;
105 
106  clock_gettime(CLOCK_REALTIME, &ts);
107 
108  val = ts.tv_sec;
109  val *= UDEBUG_TS_SEC;
110  val += ts.tv_nsec / 1000;
111 
112  return val;
113 }
#define UDEBUG_TS_SEC
Definition: udebug.h:53
Here is the caller graph for this function: