udebug: add ulog support
[project/libubox.git] / udebug.c
1 /*
2 * udebug - debug ring buffer library
3 *
4 * Copyright (C) 2023 Felix Fietkau <nbd@nbd.name>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #define _GNU_SOURCE
19 #include <sys/types.h>
20 #include <sys/mman.h>
21 #include <sys/socket.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <poll.h>
28 #include <time.h>
29 #include "udebug-priv.h"
30 #include "usock.h"
31
32 #define ALIGN(i, sz) (((i) + (sz) - 1) & ~((sz) - 1))
33
34 #ifndef MAP_ANONYMOUS
35 #define MAP_ANONYMOUS MAP_ANON
36 #endif
37
38 #define UDEBUG_MIN_ALLOC_LEN 128
39 static struct blob_buf b;
40
41 static void __randname(char *template)
42 {
43 int i;
44 struct timespec ts;
45 unsigned long r;
46
47 clock_gettime(CLOCK_REALTIME, &ts);
48 r = ts.tv_sec + ts.tv_nsec;
49 for (i=0; i<6; i++, r>>=5)
50 template[i] = 'A'+(r&15)+(r&16)*2;
51 }
52
53 int udebug_id_cmp(const void *k1, const void *k2, void *ptr)
54 {
55 uint32_t id1 = (uint32_t)(uintptr_t)k1, id2 = (uint32_t)(uintptr_t)k2;
56 if (id1 < id2)
57 return -1;
58 else if (id1 > id2)
59 return 1;
60 return 0;
61 }
62
63 static inline int
64 shm_open_anon(char *name)
65 {
66 char *template = name + strlen(name) - 6;
67 int fd;
68
69 if (template < name || memcmp(template, "XXXXXX", 6) != 0)
70 return -1;
71
72 for (int i = 0; i < 100; i++) {
73 __randname(template);
74 fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
75 if (fd >= 0) {
76 if (shm_unlink(name) < 0) {
77 close(fd);
78 continue;
79 }
80 return fd;
81 }
82
83 if (fd < 0 && errno != EEXIST)
84 return -1;
85 }
86
87 return -1;
88 }
89
90 static void __udebug_disconnect(struct udebug *ctx, bool reconnect)
91 {
92 uloop_fd_delete(&ctx->fd);
93 close(ctx->fd.fd);
94 ctx->fd.fd = -1;
95 ctx->poll_handle = -1;
96 if (ctx->reconnect.cb && reconnect)
97 uloop_timeout_set(&ctx->reconnect, 1);
98 }
99
100 uint64_t udebug_timestamp(void)
101 {
102 struct timespec ts;
103 uint64_t val;
104
105 clock_gettime(CLOCK_REALTIME, &ts);
106
107 val = ts.tv_sec;
108 val *= UDEBUG_TS_SEC;
109 val += ts.tv_nsec / 1000;
110
111 return val;
112 }
113
114 static int
115 __udebug_buf_map(struct udebug_buf *buf)
116 {
117 void *ptr, *ptr2;
118
119 ptr = mmap(NULL, buf->head_size + 2 * buf->data_size, PROT_NONE,
120 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
121 if (ptr == MAP_FAILED)
122 return -1;
123
124 ptr2 = mmap(ptr, buf->head_size + buf->data_size,
125 PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, buf->fd, 0);
126 if (ptr2 != ptr)
127 goto err_unmap;
128
129 ptr2 = mmap(ptr + buf->head_size + buf->data_size, buf->data_size,
130 PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, buf->fd,
131 buf->head_size);
132 if (ptr2 != ptr + buf->head_size + buf->data_size)
133 goto err_unmap;
134
135 buf->hdr = ptr;
136 buf->data = ptr + buf->head_size;
137 return 0;
138
139 err_unmap:
140 munmap(ptr, buf->head_size + 2 * buf->data_size);
141 return -1;
142 }
143
144 static int
145 writev_retry(int fd, struct iovec *iov, int iov_len, int sock_fd)
146 {
147 uint8_t fd_buf[CMSG_SPACE(sizeof(int))] = { 0 };
148 struct msghdr msghdr = { 0 };
149 struct cmsghdr *cmsg;
150 int len = 0;
151 int *pfd;
152
153 msghdr.msg_iov = iov,
154 msghdr.msg_iovlen = iov_len,
155 msghdr.msg_control = fd_buf;
156 msghdr.msg_controllen = sizeof(fd_buf);
157
158 cmsg = CMSG_FIRSTHDR(&msghdr);
159 cmsg->cmsg_type = SCM_RIGHTS;
160 cmsg->cmsg_level = SOL_SOCKET;
161 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
162
163 pfd = (int *) CMSG_DATA(cmsg);
164 msghdr.msg_controllen = cmsg->cmsg_len;
165
166 do {
167 ssize_t cur_len;
168
169 if (sock_fd < 0) {
170 msghdr.msg_control = NULL;
171 msghdr.msg_controllen = 0;
172 } else {
173 *pfd = sock_fd;
174 }
175
176 cur_len = sendmsg(fd, &msghdr, 0);
177 if (cur_len < 0) {
178 struct pollfd pfd = {
179 .fd = fd,
180 .events = POLLOUT
181 };
182
183 switch(errno) {
184 case EAGAIN:
185 poll(&pfd, 1, -1);
186 break;
187 case EINTR:
188 break;
189 default:
190 return -1;
191 }
192 continue;
193 }
194
195 if (len > 0)
196 sock_fd = -1;
197
198 len += cur_len;
199 while (cur_len >= (ssize_t) iov->iov_len) {
200 cur_len -= iov->iov_len;
201 iov_len--;
202 iov++;
203 if (!iov_len)
204 return len;
205 }
206 iov->iov_base += cur_len;
207 iov->iov_len -= cur_len;
208 msghdr.msg_iov = iov;
209 msghdr.msg_iovlen = iov_len;
210 } while (1);
211
212 /* Should never reach here */
213 return -1;
214 }
215
216 static int
217 recv_retry(int fd, struct iovec *iov, bool wait, int *recv_fd)
218 {
219 uint8_t fd_buf[CMSG_SPACE(sizeof(int))] = { 0 };
220 struct msghdr msghdr = { 0 };
221 struct cmsghdr *cmsg;
222 int total = 0;
223 int bytes;
224 int *pfd;
225
226 msghdr.msg_iov = iov,
227 msghdr.msg_iovlen = 1,
228 msghdr.msg_control = fd_buf;
229 msghdr.msg_controllen = sizeof(fd_buf);
230
231 cmsg = CMSG_FIRSTHDR(&msghdr);
232 cmsg->cmsg_type = SCM_RIGHTS;
233 cmsg->cmsg_level = SOL_SOCKET;
234 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
235
236 pfd = (int *) CMSG_DATA(cmsg);
237
238 while (iov->iov_len > 0) {
239 if (recv_fd) {
240 msghdr.msg_control = fd_buf;
241 msghdr.msg_controllen = cmsg->cmsg_len;
242 } else {
243 msghdr.msg_control = NULL;
244 msghdr.msg_controllen = 0;
245 }
246
247 *pfd = -1;
248 bytes = recvmsg(fd, &msghdr, 0);
249 if (!bytes)
250 return -2;
251 if (bytes < 0) {
252 bytes = 0;
253 if (errno == EINTR)
254 continue;
255
256 if (errno != EAGAIN)
257 return -2;
258 }
259 if (!wait && !bytes)
260 return 0;
261
262 if (recv_fd)
263 *recv_fd = *pfd;
264 else if (*pfd >= 0)
265 close(*pfd);
266
267 if (bytes > 0)
268 recv_fd = NULL;
269
270 wait = true;
271 iov->iov_len -= bytes;
272 iov->iov_base += bytes;
273 total += bytes;
274
275 if (iov->iov_len > 0) {
276 struct pollfd pfd = {
277 .fd = fd,
278 .events = POLLIN
279 };
280 int ret;
281 do {
282 ret = poll(&pfd, 1, UDEBUG_TIMEOUT);
283 } while (ret < 0 && errno == EINTR);
284
285 if (!(pfd.revents & POLLIN))
286 return -1;
287 }
288 }
289
290 return total;
291 }
292
293 void udebug_send_msg(struct udebug *ctx, struct udebug_client_msg *msg,
294 struct blob_attr *meta, int fd)
295 {
296 struct iovec iov[2] = {
297 { .iov_base = msg, .iov_len = sizeof(*msg) },
298 {}
299 };
300
301 if (!meta) {
302 blob_buf_init(&b, 0);
303 meta = b.head;
304 }
305
306 iov[1].iov_base = meta;
307 iov[1].iov_len = blob_pad_len(meta);
308 writev_retry(ctx->fd.fd, iov, ARRAY_SIZE(iov), fd);
309 }
310
311 static void
312 udebug_buf_msg(struct udebug_buf *buf, enum udebug_client_msg_type type)
313 {
314 struct udebug_client_msg msg = {
315 .type = type,
316 .id = buf->id,
317 };
318
319 udebug_send_msg(buf->ctx, &msg, NULL, -1);
320 }
321
322 static size_t __udebug_headsize(unsigned int ring_size, unsigned int page_size)
323 {
324 ring_size *= sizeof(struct udebug_ptr);
325 return ALIGN(sizeof(struct udebug_hdr) + ring_size, page_size);
326 }
327
328 int udebug_buf_open(struct udebug_buf *buf, int fd, uint32_t ring_size, uint32_t data_size)
329 {
330 INIT_LIST_HEAD(&buf->list);
331 buf->fd = fd;
332 buf->ring_size = ring_size;
333 buf->head_size = __udebug_headsize(ring_size, sysconf(_SC_PAGESIZE));
334 buf->data_size = data_size;
335
336 if (buf->ring_size > (1U << 24) || buf->data_size > (1U << 29))
337 return -1;
338
339 if (__udebug_buf_map(buf))
340 return -1;
341
342 if (buf->ring_size != buf->hdr->ring_size ||
343 buf->data_size != buf->hdr->data_size) {
344 munmap(buf->hdr, buf->head_size + 2 * buf->data_size);
345 buf->hdr = NULL;
346 return -1;
347 }
348
349 return 0;
350 }
351
352 int udebug_buf_init(struct udebug_buf *buf, size_t entries, size_t size)
353 {
354 uint32_t pagesz = sysconf(_SC_PAGESIZE);
355 char filename[] = "/udebug.XXXXXX";
356 unsigned int order = 12;
357 uint8_t ring_order = 5;
358 size_t head_size;
359 int fd;
360
361 INIT_LIST_HEAD(&buf->list);
362 if (size < pagesz)
363 size = pagesz;
364 while(size > 1U << order)
365 order++;
366 size = 1 << order;
367 while (entries > 1U << ring_order)
368 ring_order++;
369 entries = 1 << ring_order;
370
371 if (size > (1U << 29) || entries > (1U << 24))
372 return -1;
373
374 head_size = __udebug_headsize(entries, pagesz);
375 while (ALIGN(sizeof(*buf->hdr) + (entries * 2) * sizeof(struct udebug_ptr), pagesz) == head_size)
376 entries *= 2;
377
378 fd = shm_open_anon(filename);
379 if (fd < 0)
380 return -1;
381
382 if (ftruncate(fd, head_size + size) < 0)
383 goto err_close;
384
385 buf->head_size = head_size;
386 buf->data_size = size;
387 buf->ring_size = entries;
388 buf->fd = fd;
389
390 if (__udebug_buf_map(buf))
391 goto err_close;
392
393 buf->hdr->ring_size = entries;
394 buf->hdr->data_size = size;
395
396 /* ensure hdr changes are visible */
397 __sync_synchronize();
398
399 return 0;
400
401 err_close:
402 close(fd);
403 return -1;
404 }
405
406 static void *udebug_buf_alloc(struct udebug_buf *buf, uint32_t ofs, uint32_t len)
407 {
408 struct udebug_hdr *hdr = buf->hdr;
409
410 hdr->data_used = u32_max(hdr->data_used, ofs + len + 1);
411
412 /* ensure that data_used update is visible before clobbering data */
413 __sync_synchronize();
414
415 return udebug_buf_ptr(buf, ofs);
416 }
417
418 uint64_t udebug_buf_flags(struct udebug_buf *buf)
419 {
420 struct udebug_hdr *hdr = buf->hdr;
421 uint64_t flags;
422
423 if (!hdr)
424 return 0;
425
426 flags = hdr->flags[0];
427 if (sizeof(flags) != sizeof(uintptr_t))
428 flags |= ((uint64_t)hdr->flags[1]) << 32;
429
430 return flags;
431 }
432
433 void udebug_entry_init_ts(struct udebug_buf *buf, uint64_t timestamp)
434 {
435 struct udebug_hdr *hdr = buf->hdr;
436 struct udebug_ptr *ptr;
437
438 if (!hdr)
439 return;
440
441 ptr = udebug_ring_ptr(hdr, hdr->head);
442 ptr->start = hdr->data_head;
443 ptr->len = 0;
444 ptr->timestamp = timestamp;
445 }
446
447 void *udebug_entry_append(struct udebug_buf *buf, const void *data, uint32_t len)
448 {
449 struct udebug_hdr *hdr = buf->hdr;
450 struct udebug_ptr *ptr;
451 uint32_t ofs;
452 void *ret;
453
454 if (!hdr)
455 return NULL;
456
457 ptr = udebug_ring_ptr(hdr, hdr->head);
458 ofs = ptr->start + ptr->len;
459 if (ptr->len + len > buf->data_size / 2)
460 return NULL;
461
462 ret = udebug_buf_alloc(buf, ofs, len);
463 if (data)
464 memcpy(ret, data, len);
465 ptr->len += len;
466
467 return ret;
468 }
469
470 int udebug_entry_printf(struct udebug_buf *buf, const char *fmt, ...)
471 {
472 va_list ap;
473 size_t ret;
474
475 va_start(ap, fmt);
476 ret = udebug_entry_vprintf(buf, fmt, ap);
477 va_end(ap);
478
479 return ret;
480 }
481
482 int udebug_entry_vprintf(struct udebug_buf *buf, const char *fmt, va_list ap)
483 {
484 struct udebug_hdr *hdr = buf->hdr;
485 struct udebug_ptr *ptr;
486 uint32_t ofs;
487 uint32_t len;
488 char *str;
489
490 if (!hdr)
491 return -1;
492
493 ptr = udebug_ring_ptr(hdr, hdr->head);
494 ofs = ptr->start + ptr->len;
495 if (ptr->len > buf->data_size / 2)
496 return -1;
497
498 str = udebug_buf_alloc(buf, ofs, UDEBUG_MIN_ALLOC_LEN);
499 len = vsnprintf(str, UDEBUG_MIN_ALLOC_LEN, fmt, ap);
500 if (len <= UDEBUG_MIN_ALLOC_LEN)
501 goto out;
502
503 if (ptr->len + len > buf->data_size / 2)
504 return -1;
505
506 udebug_buf_alloc(buf, ofs, len + 1);
507 len = vsnprintf(str, len, fmt, ap);
508
509 out:
510 ptr->len += len;
511 return 0;
512 }
513
514 void udebug_entry_add(struct udebug_buf *buf)
515 {
516 struct udebug_hdr *hdr = buf->hdr;
517 struct udebug_ptr *ptr = udebug_ring_ptr(hdr, hdr->head);
518 uint32_t notify;
519 uint8_t *data;
520
521 /* ensure strings are always 0-terminated */
522 data = udebug_buf_ptr(buf, ptr->start + ptr->len);
523 *data = 0;
524 hdr->data_head = ptr->start + ptr->len + 1;
525
526 /* ensure that all data changes are visible before advancing head */
527 __sync_synchronize();
528
529 u32_set(&hdr->head, u32_get(&hdr->head) + 1);
530 if (!u32_get(&hdr->head))
531 u32_set(&hdr->head_hi, u32_get(&hdr->head_hi) + 1);
532
533 /* ensure that head change is visible */
534 __sync_synchronize();
535
536 notify = __atomic_exchange_n(&hdr->notify, 0, __ATOMIC_RELAXED);
537 if (notify) {
538 struct udebug_client_msg msg = {
539 .type = CL_MSG_RING_NOTIFY,
540 .id = buf->id,
541 .notify_mask = notify,
542 };
543 blob_buf_init(&b, 0);
544
545 udebug_send_msg(buf->ctx, &msg, b.head, -1);
546 }
547 }
548 void udebug_buf_free(struct udebug_buf *buf)
549 {
550 struct udebug *ctx = buf->ctx;
551
552 if (!list_empty(&buf->list) && buf->list.prev)
553 list_del(&buf->list);
554
555 if (ctx && ctx->fd.fd >= 0)
556 udebug_buf_msg(buf, CL_MSG_RING_REMOVE);
557
558 munmap(buf->hdr, buf->head_size + 2 * buf->data_size);
559 close(buf->fd);
560 memset(buf, 0, sizeof(*buf));
561 }
562
563 static void
564 __udebug_buf_add(struct udebug *ctx, struct udebug_buf *buf)
565 {
566 struct udebug_client_msg msg = {
567 .type = CL_MSG_RING_ADD,
568 .id = buf->id,
569 .ring_size = buf->hdr->ring_size,
570 .data_size = buf->hdr->data_size,
571 };
572 const struct udebug_buf_meta *meta = buf->meta;
573 void *c;
574
575 blob_buf_init(&b, 0);
576 blobmsg_add_string(&b, "name", meta->name);
577 c = blobmsg_open_array(&b, "flags");
578 for (size_t i = 0; i < meta->n_flags; i++) {
579 const struct udebug_buf_flag *flag = &meta->flags[i];
580 void *e = blobmsg_open_array(&b, NULL);
581 blobmsg_add_string(&b, NULL, flag->name);
582 blobmsg_add_u64(&b, NULL, flag->mask);
583 blobmsg_close_array(&b, e);
584 }
585 blobmsg_close_array(&b, c);
586
587 udebug_send_msg(ctx, &msg, b.head, buf->fd);
588 }
589
590 int udebug_buf_add(struct udebug *ctx, struct udebug_buf *buf,
591 const struct udebug_buf_meta *meta)
592 {
593 list_add_tail(&buf->list, &ctx->local_rings);
594 buf->ctx = ctx;
595 buf->meta = meta;
596 buf->id = ctx->next_id++;
597 buf->hdr->format = meta->format;
598 buf->hdr->sub_format = meta->sub_format;
599
600 if (ctx->fd.fd >= 0)
601 __udebug_buf_add(ctx, buf);
602
603 return 0;
604 }
605
606 void udebug_init(struct udebug *ctx)
607 {
608 INIT_LIST_HEAD(&ctx->local_rings);
609 avl_init(&ctx->remote_rings, udebug_id_cmp, true, NULL);
610 ctx->fd.fd = -1;
611 ctx->poll_handle = -1;
612 }
613
614 static void udebug_reconnect_cb(struct uloop_timeout *t)
615 {
616 struct udebug *ctx = container_of(t, struct udebug, reconnect);
617
618 if (udebug_connect(ctx, ctx->socket_path) < 0) {
619 uloop_timeout_set(&ctx->reconnect, 1000);
620 return;
621 }
622
623 udebug_add_uloop(ctx);
624 }
625
626 void udebug_auto_connect(struct udebug *ctx, const char *path)
627 {
628 free(ctx->socket_path);
629 ctx->reconnect.cb = udebug_reconnect_cb;
630 ctx->socket_path = path ? strdup(path) : NULL;
631 if (ctx->fd.fd >= 0)
632 return;
633
634 udebug_reconnect_cb(&ctx->reconnect);
635 }
636
637 int udebug_connect(struct udebug *ctx, const char *path)
638 {
639 struct udebug_remote_buf *rb;
640 struct udebug_buf *buf;
641
642 if (ctx->fd.fd >= 0)
643 close(ctx->fd.fd);
644 ctx->fd.fd = -1;
645
646 if (!path)
647 path = UDEBUG_SOCK_NAME;
648
649 ctx->fd.fd = usock(USOCK_UNIX, path, NULL);
650 if (ctx->fd.fd < 0)
651 return -1;
652
653 list_for_each_entry(buf, &ctx->local_rings, list)
654 __udebug_buf_add(ctx, buf);
655
656 avl_for_each_element(&ctx->remote_rings, rb, node) {
657 if (!rb->poll)
658 continue;
659
660 rb->poll = false;
661 udebug_remote_buf_set_poll(ctx, rb, true);
662 }
663
664 return 0;
665 }
666
667 static bool
668 udebug_recv_msg(struct udebug *ctx, struct udebug_client_msg *msg, int *fd,
669 bool wait)
670 {
671 struct iovec iov = {
672 .iov_base = msg,
673 .iov_len = sizeof(*msg)
674 };
675 int ret;
676
677 ret = recv_retry(ctx->fd.fd, &iov, wait, fd);
678 if (ret == -2)
679 __udebug_disconnect(ctx, true);
680
681 return ret == sizeof(*msg);
682 }
683
684 struct udebug_client_msg *__udebug_poll(struct udebug *ctx, int *fd, bool wait)
685 {
686 static struct udebug_client_msg msg = {};
687
688 while (udebug_recv_msg(ctx, &msg, fd, wait)) {
689 struct udebug_remote_buf *rb;
690 void *key;
691
692 if (msg.type != CL_MSG_RING_NOTIFY)
693 return &msg;
694
695 if (fd && *fd >= 0)
696 close(*fd);
697
698 if (!ctx->notify_cb)
699 continue;
700
701 key = (void *)(uintptr_t)msg.id;
702 rb = avl_find_element(&ctx->remote_rings, key, rb, node);
703 if (!rb || !rb->poll)
704 continue;
705
706 if (ctx->poll_handle >= 0)
707 __atomic_fetch_or(&rb->buf.hdr->notify,
708 1UL << ctx->poll_handle,
709 __ATOMIC_RELAXED);
710 ctx->notify_cb(ctx, rb);
711 }
712
713 return NULL;
714 }
715
716 void udebug_poll(struct udebug *ctx)
717 {
718 while (__udebug_poll(ctx, NULL, false));
719 }
720
721 static void udebug_fd_cb(struct uloop_fd *fd, unsigned int events)
722 {
723 struct udebug *ctx = container_of(fd, struct udebug, fd);
724
725 if (fd->eof)
726 __udebug_disconnect(ctx, true);
727
728 udebug_poll(ctx);
729 }
730
731 void udebug_add_uloop(struct udebug *ctx)
732 {
733 if (ctx->fd.registered)
734 return;
735
736 ctx->fd.cb = udebug_fd_cb;
737 uloop_fd_add(&ctx->fd, ULOOP_READ);
738 }
739
740 void udebug_free(struct udebug *ctx)
741 {
742 struct udebug_remote_buf *rb, *tmp;
743 struct udebug_buf *buf;
744
745 free(ctx->socket_path);
746 ctx->socket_path = NULL;
747
748 __udebug_disconnect(ctx, false);
749 uloop_timeout_cancel(&ctx->reconnect);
750
751 while (!list_empty(&ctx->local_rings)) {
752 buf = list_first_entry(&ctx->local_rings, struct udebug_buf, list);
753 udebug_buf_free(buf);
754 }
755
756 avl_for_each_element_safe(&ctx->remote_rings, rb, node, tmp)
757 udebug_remote_buf_unmap(ctx, rb);
758 }