4 #include <sys/socket.h>
14 #include <libubox/usock.h>
16 #define ALIGN(i, sz) (((i) + (sz) - 1) & ~((sz) - 1))
19 #define MAP_ANONYMOUS MAP_ANON
22 #define UDEBUG_MIN_ALLOC_LEN 128
23 static struct blob_buf b
;
25 static void __randname(char *template)
31 clock_gettime(CLOCK_REALTIME
, &ts
);
32 r
= ts
.tv_sec
+ ts
.tv_nsec
;
33 for (i
=0; i
<6; i
++, r
>>=5)
34 template[i
] = 'A'+(r
&15)+(r
&16)*2;
37 int udebug_id_cmp(const void *k1
, const void *k2
, void *ptr
)
39 uint32_t id1
= (uint32_t)(uintptr_t)k1
, id2
= (uint32_t)(uintptr_t)k2
;
48 shm_open_anon(char *name
)
50 char *template = name
+ strlen(name
) - 6;
53 if (template < name
|| memcmp(template, "XXXXXX", 6) != 0)
56 for (int i
= 0; i
< 100; i
++) {
58 fd
= shm_open(name
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
60 if (shm_unlink(name
) < 0) {
67 if (fd
< 0 && errno
!= EEXIST
)
74 uint64_t udebug_timestamp(void)
79 clock_gettime(CLOCK_REALTIME
, &ts
);
83 val
+= ts
.tv_nsec
/ 1000;
89 __udebug_buf_map(struct udebug_buf
*buf
)
93 ptr
= mmap(NULL
, buf
->head_size
+ 2 * buf
->data_size
, PROT_NONE
,
94 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
95 if (ptr
== MAP_FAILED
)
98 ptr2
= mmap(ptr
, buf
->head_size
+ buf
->data_size
,
99 PROT_READ
| PROT_WRITE
, MAP_FIXED
| MAP_SHARED
, buf
->fd
, 0);
103 ptr2
= mmap(ptr
+ buf
->head_size
+ buf
->data_size
, buf
->data_size
,
104 PROT_READ
| PROT_WRITE
, MAP_FIXED
| MAP_SHARED
, buf
->fd
,
106 if (ptr2
!= ptr
+ buf
->head_size
+ buf
->data_size
)
110 buf
->data
= ptr
+ buf
->head_size
;
114 munmap(ptr
, buf
->head_size
+ 2 * buf
->data_size
);
119 writev_retry(int fd
, struct iovec
*iov
, int iov_len
, int sock_fd
)
121 uint8_t fd_buf
[CMSG_SPACE(sizeof(int))] = { 0 };
122 struct msghdr msghdr
= { 0 };
123 struct cmsghdr
*cmsg
;
127 msghdr
.msg_iov
= iov
,
128 msghdr
.msg_iovlen
= iov_len
,
129 msghdr
.msg_control
= fd_buf
;
130 msghdr
.msg_controllen
= sizeof(fd_buf
);
132 cmsg
= CMSG_FIRSTHDR(&msghdr
);
133 cmsg
->cmsg_type
= SCM_RIGHTS
;
134 cmsg
->cmsg_level
= SOL_SOCKET
;
135 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
137 pfd
= (int *) CMSG_DATA(cmsg
);
138 msghdr
.msg_controllen
= cmsg
->cmsg_len
;
144 msghdr
.msg_control
= NULL
;
145 msghdr
.msg_controllen
= 0;
150 cur_len
= sendmsg(fd
, &msghdr
, 0);
152 struct pollfd pfd
= {
173 while (cur_len
>= (ssize_t
) iov
->iov_len
) {
174 cur_len
-= iov
->iov_len
;
180 iov
->iov_base
+= cur_len
;
181 iov
->iov_len
-= cur_len
;
182 msghdr
.msg_iov
= iov
;
183 msghdr
.msg_iovlen
= iov_len
;
186 /* Should never reach here */
191 recv_retry(int fd
, struct iovec
*iov
, bool wait
, int *recv_fd
)
193 uint8_t fd_buf
[CMSG_SPACE(sizeof(int))] = { 0 };
194 struct msghdr msghdr
= { 0 };
195 struct cmsghdr
*cmsg
;
200 msghdr
.msg_iov
= iov
,
201 msghdr
.msg_iovlen
= 1,
202 msghdr
.msg_control
= fd_buf
;
203 msghdr
.msg_controllen
= sizeof(fd_buf
);
205 cmsg
= CMSG_FIRSTHDR(&msghdr
);
206 cmsg
->cmsg_type
= SCM_RIGHTS
;
207 cmsg
->cmsg_level
= SOL_SOCKET
;
208 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
210 pfd
= (int *) CMSG_DATA(cmsg
);
212 while (iov
->iov_len
> 0) {
214 msghdr
.msg_control
= fd_buf
;
215 msghdr
.msg_controllen
= cmsg
->cmsg_len
;
217 msghdr
.msg_control
= NULL
;
218 msghdr
.msg_controllen
= 0;
222 bytes
= recvmsg(fd
, &msghdr
, 0);
245 iov
->iov_len
-= bytes
;
246 iov
->iov_base
+= bytes
;
249 if (iov
->iov_len
> 0) {
250 struct pollfd pfd
= {
256 ret
= poll(&pfd
, 1, UDEBUG_TIMEOUT
);
257 } while (ret
< 0 && errno
== EINTR
);
259 if (!(pfd
.revents
& POLLIN
))
267 void udebug_send_msg(struct udebug
*ctx
, struct udebug_client_msg
*msg
,
268 struct blob_attr
*meta
, int fd
)
270 struct iovec iov
[2] = {
271 { .iov_base
= msg
, .iov_len
= sizeof(*msg
) },
276 blob_buf_init(&b
, 0);
280 iov
[1].iov_base
= meta
;
281 iov
[1].iov_len
= blob_pad_len(meta
);
282 writev_retry(ctx
->fd
.fd
, iov
, ARRAY_SIZE(iov
), fd
);
286 udebug_buf_msg(struct udebug_buf
*buf
, enum udebug_client_msg_type type
)
288 struct udebug_client_msg msg
= {
293 udebug_send_msg(buf
->ctx
, &msg
, NULL
, -1);
296 static size_t __udebug_headsize(unsigned int ring_size
, unsigned int page_size
)
298 ring_size
*= sizeof(struct udebug_ptr
);
299 return ALIGN(sizeof(struct udebug_hdr
) + ring_size
, page_size
);
302 int udebug_buf_open(struct udebug_buf
*buf
, int fd
, uint32_t ring_size
, uint32_t data_size
)
304 INIT_LIST_HEAD(&buf
->list
);
306 buf
->ring_size
= ring_size
;
307 buf
->head_size
= __udebug_headsize(ring_size
, sysconf(_SC_PAGESIZE
));
308 buf
->data_size
= data_size
;
310 if (buf
->ring_size
> (1U << 24) || buf
->data_size
> (1U << 29))
313 if (__udebug_buf_map(buf
))
316 if (buf
->ring_size
!= buf
->hdr
->ring_size
||
317 buf
->data_size
!= buf
->hdr
->data_size
) {
318 munmap(buf
->hdr
, buf
->head_size
+ 2 * buf
->data_size
);
326 int udebug_buf_init(struct udebug_buf
*buf
, size_t entries
, size_t size
)
328 uint32_t pagesz
= sysconf(_SC_PAGESIZE
);
329 char filename
[] = "/udebug.XXXXXX";
330 unsigned int order
= 12;
331 uint8_t ring_order
= 5;
335 INIT_LIST_HEAD(&buf
->list
);
338 while(size
> 1 << order
)
341 while (entries
> 1 << ring_order
)
343 entries
= 1 << ring_order
;
345 if (size
> (1U << 29) || entries
> (1U << 24))
348 head_size
= __udebug_headsize(entries
, pagesz
);
349 while (ALIGN(sizeof(*buf
->hdr
) + (entries
* 2) * sizeof(struct udebug_ptr
), pagesz
) == head_size
)
352 fd
= shm_open_anon(filename
);
356 if (ftruncate(fd
, head_size
+ size
) < 0)
359 buf
->head_size
= head_size
;
360 buf
->data_size
= size
;
361 buf
->ring_size
= entries
;
364 if (__udebug_buf_map(buf
))
367 buf
->hdr
->ring_size
= entries
;
368 buf
->hdr
->data_size
= size
;
370 /* ensure hdr changes are visible */
371 __sync_synchronize();
380 static void *udebug_buf_alloc(struct udebug_buf
*buf
, uint32_t ofs
, uint32_t len
)
382 struct udebug_hdr
*hdr
= buf
->hdr
;
384 hdr
->data_used
= u32_max(hdr
->data_used
, ofs
+ len
+ 1);
386 /* ensure that data_used update is visible before clobbering data */
387 __sync_synchronize();
389 return udebug_buf_ptr(buf
, ofs
);
392 uint64_t udebug_buf_flags(struct udebug_buf
*buf
)
394 struct udebug_hdr
*hdr
= buf
->hdr
;
400 flags
= hdr
->flags
[0];
401 if (sizeof(flags
) != sizeof(uintptr_t))
402 flags
|= ((uint64_t)hdr
->flags
[1]) << 32;
407 void udebug_entry_init_ts(struct udebug_buf
*buf
, uint64_t timestamp
)
409 struct udebug_hdr
*hdr
= buf
->hdr
;
410 struct udebug_ptr
*ptr
;
415 ptr
= udebug_ring_ptr(hdr
, hdr
->head
);
416 ptr
->start
= hdr
->data_head
;
418 ptr
->timestamp
= timestamp
;
421 void *udebug_entry_append(struct udebug_buf
*buf
, const void *data
, uint32_t len
)
423 struct udebug_hdr
*hdr
= buf
->hdr
;
424 struct udebug_ptr
*ptr
;
431 ptr
= udebug_ring_ptr(hdr
, hdr
->head
);
432 ofs
= ptr
->start
+ ptr
->len
;
433 if (ptr
->len
+ len
> buf
->data_size
/ 2)
436 ret
= udebug_buf_alloc(buf
, ofs
, len
);
438 memcpy(ret
, data
, len
);
444 int udebug_entry_printf(struct udebug_buf
*buf
, const char *fmt
, ...)
450 ret
= udebug_entry_vprintf(buf
, fmt
, ap
);
456 int udebug_entry_vprintf(struct udebug_buf
*buf
, const char *fmt
, va_list ap
)
458 struct udebug_hdr
*hdr
= buf
->hdr
;
459 struct udebug_ptr
*ptr
;
467 ptr
= udebug_ring_ptr(hdr
, hdr
->head
);
468 ofs
= ptr
->start
+ ptr
->len
;
469 if (ptr
->len
> buf
->data_size
/ 2)
472 str
= udebug_buf_alloc(buf
, ofs
, UDEBUG_MIN_ALLOC_LEN
);
473 len
= vsnprintf(str
, UDEBUG_MIN_ALLOC_LEN
, fmt
, ap
);
474 if (len
<= UDEBUG_MIN_ALLOC_LEN
)
477 if (ptr
->len
+ len
> buf
->data_size
/ 2)
480 udebug_buf_alloc(buf
, ofs
, len
+ 1);
481 len
= vsnprintf(str
, len
, fmt
, ap
);
488 void udebug_entry_add(struct udebug_buf
*buf
)
490 struct udebug_hdr
*hdr
= buf
->hdr
;
491 struct udebug_ptr
*ptr
= udebug_ring_ptr(hdr
, hdr
->head
);
495 /* ensure strings are always 0-terminated */
496 data
= udebug_buf_ptr(buf
, ptr
->start
+ ptr
->len
);
498 hdr
->data_head
= ptr
->start
+ ptr
->len
+ 1;
500 /* ensure that all data changes are visible before advancing head */
501 __sync_synchronize();
503 u32_set(&hdr
->head
, u32_get(&hdr
->head
) + 1);
504 if (!u32_get(&hdr
->head
))
505 u32_set(&hdr
->head_hi
, u32_get(&hdr
->head_hi
) + 1);
507 /* ensure that head change is visible */
508 __sync_synchronize();
510 notify
= __atomic_exchange_n(&hdr
->notify
, 0, __ATOMIC_RELAXED
);
512 struct udebug_client_msg msg
= {
513 .type
= CL_MSG_RING_NOTIFY
,
515 .notify_mask
= notify
,
517 blob_buf_init(&b
, 0);
519 udebug_send_msg(buf
->ctx
, &msg
, b
.head
, -1);
522 void udebug_buf_free(struct udebug_buf
*buf
)
524 struct udebug
*ctx
= buf
->ctx
;
526 if (!list_empty(&buf
->list
) && buf
->list
.prev
)
527 list_del(&buf
->list
);
529 if (ctx
&& ctx
->fd
.fd
>= 0)
530 udebug_buf_msg(buf
, CL_MSG_RING_REMOVE
);
532 munmap(buf
->hdr
, buf
->head_size
+ 2 * buf
->data_size
);
534 memset(buf
, 0, sizeof(*buf
));
538 __udebug_buf_add(struct udebug
*ctx
, struct udebug_buf
*buf
)
540 struct udebug_client_msg msg
= {
541 .type
= CL_MSG_RING_ADD
,
543 .ring_size
= buf
->hdr
->ring_size
,
544 .data_size
= buf
->hdr
->data_size
,
546 const struct udebug_buf_meta
*meta
= buf
->meta
;
549 blob_buf_init(&b
, 0);
550 blobmsg_add_string(&b
, "name", meta
->name
);
551 c
= blobmsg_open_array(&b
, "flags");
552 for (size_t i
= 0; i
< meta
->n_flags
; i
++) {
553 const struct udebug_buf_flag
*flag
= &meta
->flags
[i
];
554 void *e
= blobmsg_open_array(&b
, NULL
);
555 blobmsg_add_string(&b
, NULL
, flag
->name
);
556 blobmsg_add_u64(&b
, NULL
, flag
->mask
);
557 blobmsg_close_array(&b
, e
);
559 blobmsg_close_array(&b
, c
);
561 udebug_send_msg(ctx
, &msg
, b
.head
, buf
->fd
);
564 int udebug_buf_add(struct udebug
*ctx
, struct udebug_buf
*buf
,
565 const struct udebug_buf_meta
*meta
)
567 list_add_tail(&buf
->list
, &ctx
->local_rings
);
570 buf
->id
= ctx
->next_id
++;
571 buf
->hdr
->format
= meta
->format
;
572 buf
->hdr
->sub_format
= meta
->sub_format
;
575 __udebug_buf_add(ctx
, buf
);
580 void udebug_init(struct udebug
*ctx
)
582 INIT_LIST_HEAD(&ctx
->local_rings
);
583 avl_init(&ctx
->remote_rings
, udebug_id_cmp
, true, NULL
);
585 ctx
->poll_handle
= -1;
588 static void udebug_reconnect_cb(struct uloop_timeout
*t
)
590 struct udebug
*ctx
= container_of(t
, struct udebug
, reconnect
);
592 if (udebug_connect(ctx
, ctx
->socket_path
) < 0) {
593 uloop_timeout_set(&ctx
->reconnect
, 1000);
597 udebug_add_uloop(ctx
);
600 void udebug_auto_connect(struct udebug
*ctx
, const char *path
)
602 free(ctx
->socket_path
);
603 ctx
->reconnect
.cb
= udebug_reconnect_cb
;
604 ctx
->socket_path
= path
? strdup(path
) : NULL
;
608 udebug_reconnect_cb(&ctx
->reconnect
);
611 int udebug_connect(struct udebug
*ctx
, const char *path
)
613 struct udebug_remote_buf
*rb
;
614 struct udebug_buf
*buf
;
621 path
= UDEBUG_SOCK_NAME
;
623 ctx
->fd
.fd
= usock(USOCK_UNIX
, path
, NULL
);
627 list_for_each_entry(buf
, &ctx
->local_rings
, list
)
628 __udebug_buf_add(ctx
, buf
);
630 avl_for_each_element(&ctx
->remote_rings
, rb
, node
) {
635 udebug_remote_buf_set_poll(ctx
, rb
, true);
642 udebug_recv_msg(struct udebug
*ctx
, struct udebug_client_msg
*msg
, int *fd
,
647 .iov_len
= sizeof(*msg
)
651 ret
= recv_retry(ctx
->fd
.fd
, &iov
, wait
, fd
);
653 uloop_fd_delete(&ctx
->fd
);
655 return ret
== sizeof(*msg
);
658 struct udebug_client_msg
*__udebug_poll(struct udebug
*ctx
, int *fd
, bool wait
)
660 static struct udebug_client_msg msg
= {};
662 while (udebug_recv_msg(ctx
, &msg
, fd
, wait
)) {
663 struct udebug_remote_buf
*rb
;
666 if (msg
.type
!= CL_MSG_RING_NOTIFY
)
675 key
= (void *)(uintptr_t)msg
.id
;
676 rb
= avl_find_element(&ctx
->remote_rings
, key
, rb
, node
);
677 if (!rb
|| !rb
->poll
)
680 if (ctx
->poll_handle
>= 0)
681 __atomic_fetch_or(&rb
->buf
.hdr
->notify
,
682 1UL << ctx
->poll_handle
,
684 ctx
->notify_cb(ctx
, rb
);
690 void udebug_poll(struct udebug
*ctx
)
692 while (__udebug_poll(ctx
, NULL
, false));
695 static void udebug_fd_cb(struct uloop_fd
*fd
, unsigned int events
)
697 struct udebug
*ctx
= container_of(fd
, struct udebug
, fd
);
705 void udebug_add_uloop(struct udebug
*ctx
)
707 if (ctx
->fd
.registered
)
710 ctx
->fd
.cb
= udebug_fd_cb
;
711 uloop_fd_add(&ctx
->fd
, ULOOP_READ
);
714 void __udebug_disconnect(struct udebug
*ctx
, bool reconnect
)
716 uloop_fd_delete(&ctx
->fd
);
719 ctx
->poll_handle
= -1;
720 if (ctx
->reconnect
.cb
&& reconnect
)
721 uloop_timeout_set(&ctx
->reconnect
, 1);
724 void udebug_free(struct udebug
*ctx
)
726 struct udebug_remote_buf
*rb
, *tmp
;
727 struct udebug_buf
*buf
;
729 free(ctx
->socket_path
);
730 ctx
->socket_path
= NULL
;
732 __udebug_disconnect(ctx
, false);
733 uloop_timeout_cancel(&ctx
->reconnect
);
735 while (!list_empty(&ctx
->local_rings
)) {
736 buf
= list_first_entry(&ctx
->local_rings
, struct udebug_buf
, list
);
737 udebug_buf_free(buf
);
740 avl_for_each_element_safe(&ctx
->remote_rings
, rb
, node
, tmp
)
741 udebug_remote_buf_unmap(ctx
, rb
);