udebug: add functions for manipulating entry length
[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 uint16_t udebug_entry_trim(struct udebug_buf *buf, uint16_t len)
471 {
472 struct udebug_hdr *hdr = buf->hdr;
473 struct udebug_ptr *ptr = udebug_ring_ptr(hdr, hdr->head);
474
475 if (len)
476 ptr->len -= len;
477
478 return ptr->len;
479 }
480
481 void udebug_entry_set_length(struct udebug_buf *buf, uint16_t len)
482 {
483 struct udebug_hdr *hdr = buf->hdr;
484 struct udebug_ptr *ptr = udebug_ring_ptr(hdr, hdr->head);
485
486 ptr->len = len;
487 }
488
489 int udebug_entry_printf(struct udebug_buf *buf, const char *fmt, ...)
490 {
491 va_list ap;
492 size_t ret;
493
494 va_start(ap, fmt);
495 ret = udebug_entry_vprintf(buf, fmt, ap);
496 va_end(ap);
497
498 return ret;
499 }
500
501 int udebug_entry_vprintf(struct udebug_buf *buf, const char *fmt, va_list ap)
502 {
503 struct udebug_hdr *hdr = buf->hdr;
504 struct udebug_ptr *ptr;
505 uint32_t ofs;
506 uint32_t len;
507 char *str;
508
509 if (!hdr)
510 return -1;
511
512 ptr = udebug_ring_ptr(hdr, hdr->head);
513 ofs = ptr->start + ptr->len;
514 if (ptr->len > buf->data_size / 2)
515 return -1;
516
517 str = udebug_buf_alloc(buf, ofs, UDEBUG_MIN_ALLOC_LEN);
518 len = vsnprintf(str, UDEBUG_MIN_ALLOC_LEN, fmt, ap);
519 if (len <= UDEBUG_MIN_ALLOC_LEN)
520 goto out;
521
522 if (ptr->len + len > buf->data_size / 2)
523 return -1;
524
525 udebug_buf_alloc(buf, ofs, len + 1);
526 len = vsnprintf(str, len, fmt, ap);
527
528 out:
529 ptr->len += len;
530 return 0;
531 }
532
533 void udebug_entry_add(struct udebug_buf *buf)
534 {
535 struct udebug_hdr *hdr = buf->hdr;
536 struct udebug_ptr *ptr = udebug_ring_ptr(hdr, hdr->head);
537 uint32_t notify;
538 uint8_t *data;
539
540 /* ensure strings are always 0-terminated */
541 data = udebug_buf_ptr(buf, ptr->start + ptr->len);
542 *data = 0;
543 hdr->data_head = ptr->start + ptr->len + 1;
544
545 /* ensure that all data changes are visible before advancing head */
546 __sync_synchronize();
547
548 u32_set(&hdr->head, u32_get(&hdr->head) + 1);
549 if (!u32_get(&hdr->head))
550 u32_set(&hdr->head_hi, u32_get(&hdr->head_hi) + 1);
551
552 /* ensure that head change is visible */
553 __sync_synchronize();
554
555 notify = __atomic_exchange_n(&hdr->notify, 0, __ATOMIC_RELAXED);
556 if (notify) {
557 struct udebug_client_msg msg = {
558 .type = CL_MSG_RING_NOTIFY,
559 .id = buf->id,
560 .notify_mask = notify,
561 };
562 blob_buf_init(&b, 0);
563
564 udebug_send_msg(buf->ctx, &msg, b.head, -1);
565 }
566 }
567 void udebug_buf_free(struct udebug_buf *buf)
568 {
569 struct udebug *ctx = buf->ctx;
570
571 if (!list_empty(&buf->list) && buf->list.prev)
572 list_del(&buf->list);
573
574 if (ctx && ctx->fd.fd >= 0)
575 udebug_buf_msg(buf, CL_MSG_RING_REMOVE);
576
577 munmap(buf->hdr, buf->head_size + 2 * buf->data_size);
578 close(buf->fd);
579 memset(buf, 0, sizeof(*buf));
580 }
581
582 static void
583 __udebug_buf_add(struct udebug *ctx, struct udebug_buf *buf)
584 {
585 struct udebug_client_msg msg = {
586 .type = CL_MSG_RING_ADD,
587 .id = buf->id,
588 .ring_size = buf->hdr->ring_size,
589 .data_size = buf->hdr->data_size,
590 };
591 const struct udebug_buf_meta *meta = buf->meta;
592 void *c;
593
594 blob_buf_init(&b, 0);
595 blobmsg_add_string(&b, "name", meta->name);
596 c = blobmsg_open_array(&b, "flags");
597 for (size_t i = 0; i < meta->n_flags; i++) {
598 const struct udebug_buf_flag *flag = &meta->flags[i];
599 void *e = blobmsg_open_array(&b, NULL);
600 blobmsg_add_string(&b, NULL, flag->name);
601 blobmsg_add_u64(&b, NULL, flag->mask);
602 blobmsg_close_array(&b, e);
603 }
604 blobmsg_close_array(&b, c);
605
606 udebug_send_msg(ctx, &msg, b.head, buf->fd);
607 }
608
609 int udebug_buf_add(struct udebug *ctx, struct udebug_buf *buf,
610 const struct udebug_buf_meta *meta)
611 {
612 list_add_tail(&buf->list, &ctx->local_rings);
613 buf->ctx = ctx;
614 buf->meta = meta;
615 buf->id = ctx->next_id++;
616 buf->hdr->format = meta->format;
617 buf->hdr->sub_format = meta->sub_format;
618
619 if (ctx->fd.fd >= 0)
620 __udebug_buf_add(ctx, buf);
621
622 return 0;
623 }
624
625 void udebug_init(struct udebug *ctx)
626 {
627 INIT_LIST_HEAD(&ctx->local_rings);
628 avl_init(&ctx->remote_rings, udebug_id_cmp, true, NULL);
629 ctx->fd.fd = -1;
630 ctx->poll_handle = -1;
631 }
632
633 static void udebug_reconnect_cb(struct uloop_timeout *t)
634 {
635 struct udebug *ctx = container_of(t, struct udebug, reconnect);
636
637 if (udebug_connect(ctx, ctx->socket_path) < 0) {
638 uloop_timeout_set(&ctx->reconnect, 1000);
639 return;
640 }
641
642 udebug_add_uloop(ctx);
643 }
644
645 void udebug_auto_connect(struct udebug *ctx, const char *path)
646 {
647 free(ctx->socket_path);
648 ctx->reconnect.cb = udebug_reconnect_cb;
649 ctx->socket_path = path ? strdup(path) : NULL;
650 if (ctx->fd.fd >= 0)
651 return;
652
653 udebug_reconnect_cb(&ctx->reconnect);
654 }
655
656 int udebug_connect(struct udebug *ctx, const char *path)
657 {
658 struct udebug_remote_buf *rb;
659 struct udebug_buf *buf;
660
661 if (ctx->fd.fd >= 0)
662 close(ctx->fd.fd);
663 ctx->fd.fd = -1;
664
665 if (!path)
666 path = UDEBUG_SOCK_NAME;
667
668 ctx->fd.fd = usock(USOCK_UNIX, path, NULL);
669 if (ctx->fd.fd < 0)
670 return -1;
671
672 list_for_each_entry(buf, &ctx->local_rings, list)
673 __udebug_buf_add(ctx, buf);
674
675 avl_for_each_element(&ctx->remote_rings, rb, node) {
676 if (!rb->poll)
677 continue;
678
679 rb->poll = false;
680 udebug_remote_buf_set_poll(ctx, rb, true);
681 }
682
683 return 0;
684 }
685
686 static bool
687 udebug_recv_msg(struct udebug *ctx, struct udebug_client_msg *msg, int *fd,
688 bool wait)
689 {
690 struct iovec iov = {
691 .iov_base = msg,
692 .iov_len = sizeof(*msg)
693 };
694 int ret;
695
696 ret = recv_retry(ctx->fd.fd, &iov, wait, fd);
697 if (ret == -2)
698 __udebug_disconnect(ctx, true);
699
700 return ret == sizeof(*msg);
701 }
702
703 struct udebug_client_msg *__udebug_poll(struct udebug *ctx, int *fd, bool wait)
704 {
705 static struct udebug_client_msg msg = {};
706
707 while (udebug_recv_msg(ctx, &msg, fd, wait)) {
708 struct udebug_remote_buf *rb;
709 void *key;
710
711 if (msg.type != CL_MSG_RING_NOTIFY)
712 return &msg;
713
714 if (fd && *fd >= 0)
715 close(*fd);
716
717 if (!ctx->notify_cb)
718 continue;
719
720 key = (void *)(uintptr_t)msg.id;
721 rb = avl_find_element(&ctx->remote_rings, key, rb, node);
722 if (!rb || !rb->poll)
723 continue;
724
725 if (ctx->poll_handle >= 0)
726 __atomic_fetch_or(&rb->buf.hdr->notify,
727 1UL << ctx->poll_handle,
728 __ATOMIC_RELAXED);
729 ctx->notify_cb(ctx, rb);
730 }
731
732 return NULL;
733 }
734
735 void udebug_poll(struct udebug *ctx)
736 {
737 while (__udebug_poll(ctx, NULL, false));
738 }
739
740 static void udebug_fd_cb(struct uloop_fd *fd, unsigned int events)
741 {
742 struct udebug *ctx = container_of(fd, struct udebug, fd);
743
744 if (fd->eof)
745 __udebug_disconnect(ctx, true);
746
747 udebug_poll(ctx);
748 }
749
750 void udebug_add_uloop(struct udebug *ctx)
751 {
752 if (ctx->fd.registered)
753 return;
754
755 ctx->fd.cb = udebug_fd_cb;
756 uloop_fd_add(&ctx->fd, ULOOP_READ);
757 }
758
759 void udebug_free(struct udebug *ctx)
760 {
761 struct udebug_remote_buf *rb, *tmp;
762 struct udebug_buf *buf;
763
764 free(ctx->socket_path);
765 ctx->socket_path = NULL;
766
767 __udebug_disconnect(ctx, false);
768 uloop_timeout_cancel(&ctx->reconnect);
769
770 while (!list_empty(&ctx->local_rings)) {
771 buf = list_first_entry(&ctx->local_rings, struct udebug_buf, list);
772 udebug_buf_free(buf);
773 }
774
775 avl_for_each_element_safe(&ctx->remote_rings, rb, node, tmp)
776 udebug_remote_buf_unmap(ctx, rb);
777 }