udebug.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. #define ALIGN(i, sz) (((i) + (sz) - 1) & ~((sz) - 1))
  32. #ifndef MAP_ANONYMOUS
  33. #define MAP_ANONYMOUS MAP_ANON
  34. #endif
  35. #define UDEBUG_MIN_ALLOC_LEN 128
  36. static struct blob_buf b;
  37. static unsigned int page_size;
  38. static void __randname(char *template)
  39. {
  40. int i;
  41. struct timespec ts;
  42. unsigned long r;
  43. clock_gettime(CLOCK_REALTIME, &ts);
  44. r = ts.tv_sec + ts.tv_nsec;
  45. for (i=0; i<6; i++, r>>=5)
  46. template[i] = 'A'+(r&15)+(r&16)*2;
  47. }
  48. int udebug_id_cmp(const void *k1, const void *k2, void *ptr)
  49. {
  50. uint32_t id1 = (uint32_t)(uintptr_t)k1, id2 = (uint32_t)(uintptr_t)k2;
  51. if (id1 < id2)
  52. return -1;
  53. else if (id1 > id2)
  54. return 1;
  55. return 0;
  56. }
  57. static inline int
  58. shm_open_anon(char *name)
  59. {
  60. char *template = name + strlen(name) - 6;
  61. int fd;
  62. if (template < name || memcmp(template, "XXXXXX", 6) != 0)
  63. return -1;
  64. for (int i = 0; i < 100; i++) {
  65. __randname(template);
  66. fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
  67. if (fd >= 0) {
  68. if (shm_unlink(name) < 0) {
  69. close(fd);
  70. continue;
  71. }
  72. return fd;
  73. }
  74. if (fd < 0 && errno != EEXIST)
  75. return -1;
  76. }
  77. return -1;
  78. }
  79. static void __udebug_disconnect(struct udebug *ctx, bool reconnect)
  80. {
  81. uloop_fd_delete(&ctx->fd);
  82. close(ctx->fd.fd);
  83. ctx->fd.fd = -1;
  84. ctx->poll_handle = -1;
  85. if (ctx->reconnect.cb && reconnect)
  86. uloop_timeout_set(&ctx->reconnect, 1);
  87. }
  88. uint64_t udebug_timestamp(void)
  89. {
  90. struct timespec ts;
  91. uint64_t val;
  92. clock_gettime(CLOCK_REALTIME, &ts);
  93. val = ts.tv_sec;
  94. val *= UDEBUG_TS_SEC;
  95. val += ts.tv_nsec / 1000;
  96. return val;
  97. }
  98. static int
  99. __udebug_buf_map(struct udebug_buf *buf, int fd)
  100. {
  101. unsigned int pad = 0;
  102. void *ptr, *ptr2;
  103. #ifdef mips
  104. pad = page_size;
  105. #endif
  106. ptr = mmap(NULL, buf->head_size + 2 * buf->data_size + pad, PROT_NONE,
  107. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  108. if (ptr == MAP_FAILED)
  109. return -1;
  110. #ifdef mips
  111. ptr = (void *)ALIGN((unsigned long)ptr, page_size);
  112. #endif
  113. ptr2 = mmap(ptr, buf->head_size + buf->data_size,
  114. PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, fd, 0);
  115. if (ptr2 != ptr)
  116. goto err_unmap;
  117. ptr2 = mmap(ptr + buf->head_size + buf->data_size, buf->data_size,
  118. PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, fd,
  119. buf->head_size);
  120. if (ptr2 != ptr + buf->head_size + buf->data_size)
  121. goto err_unmap;
  122. buf->hdr = ptr;
  123. buf->data = ptr + buf->head_size;
  124. return 0;
  125. err_unmap:
  126. munmap(ptr, buf->head_size + 2 * buf->data_size);
  127. return -1;
  128. }
  129. static int
  130. writev_retry(int fd, struct iovec *iov, int iov_len, int sock_fd)
  131. {
  132. uint8_t fd_buf[CMSG_SPACE(sizeof(int))] = { 0 };
  133. struct msghdr msghdr = { 0 };
  134. struct cmsghdr *cmsg;
  135. int len = 0;
  136. int *pfd;
  137. msghdr.msg_iov = iov,
  138. msghdr.msg_iovlen = iov_len,
  139. msghdr.msg_control = fd_buf;
  140. msghdr.msg_controllen = sizeof(fd_buf);
  141. cmsg = CMSG_FIRSTHDR(&msghdr);
  142. cmsg->cmsg_type = SCM_RIGHTS;
  143. cmsg->cmsg_level = SOL_SOCKET;
  144. cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  145. pfd = (int *) CMSG_DATA(cmsg);
  146. msghdr.msg_controllen = cmsg->cmsg_len;
  147. do {
  148. ssize_t cur_len;
  149. if (sock_fd < 0) {
  150. msghdr.msg_control = NULL;
  151. msghdr.msg_controllen = 0;
  152. } else {
  153. *pfd = sock_fd;
  154. }
  155. cur_len = sendmsg(fd, &msghdr, 0);
  156. if (cur_len < 0) {
  157. struct pollfd pfd = {
  158. .fd = fd,
  159. .events = POLLOUT
  160. };
  161. switch(errno) {
  162. case EAGAIN:
  163. poll(&pfd, 1, -1);
  164. break;
  165. case EINTR:
  166. break;
  167. default:
  168. return -1;
  169. }
  170. continue;
  171. }
  172. if (len > 0)
  173. sock_fd = -1;
  174. len += cur_len;
  175. while (cur_len >= (ssize_t) iov->iov_len) {
  176. cur_len -= iov->iov_len;
  177. iov_len--;
  178. iov++;
  179. if (!iov_len)
  180. return len;
  181. }
  182. iov->iov_base += cur_len;
  183. iov->iov_len -= cur_len;
  184. msghdr.msg_iov = iov;
  185. msghdr.msg_iovlen = iov_len;
  186. } while (1);
  187. /* Should never reach here */
  188. return -1;
  189. }
  190. static int
  191. recv_retry(int fd, struct iovec *iov, bool wait, int *recv_fd)
  192. {
  193. uint8_t fd_buf[CMSG_SPACE(sizeof(int))] = { 0 };
  194. struct msghdr msghdr = { 0 };
  195. struct cmsghdr *cmsg;
  196. int total = 0;
  197. int bytes;
  198. int *pfd;
  199. msghdr.msg_iov = iov,
  200. msghdr.msg_iovlen = 1,
  201. msghdr.msg_control = fd_buf;
  202. msghdr.msg_controllen = sizeof(fd_buf);
  203. cmsg = CMSG_FIRSTHDR(&msghdr);
  204. cmsg->cmsg_type = SCM_RIGHTS;
  205. cmsg->cmsg_level = SOL_SOCKET;
  206. cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  207. pfd = (int *) CMSG_DATA(cmsg);
  208. while (iov->iov_len > 0) {
  209. if (recv_fd) {
  210. msghdr.msg_control = fd_buf;
  211. msghdr.msg_controllen = cmsg->cmsg_len;
  212. } else {
  213. msghdr.msg_control = NULL;
  214. msghdr.msg_controllen = 0;
  215. }
  216. *pfd = -1;
  217. bytes = recvmsg(fd, &msghdr, 0);
  218. if (!bytes)
  219. return -2;
  220. if (bytes < 0) {
  221. bytes = 0;
  222. if (errno == EINTR)
  223. continue;
  224. if (errno != EAGAIN)
  225. return -2;
  226. }
  227. if (!wait && !bytes)
  228. return 0;
  229. if (recv_fd)
  230. *recv_fd = *pfd;
  231. else if (*pfd >= 0)
  232. close(*pfd);
  233. if (bytes > 0)
  234. recv_fd = NULL;
  235. wait = true;
  236. iov->iov_len -= bytes;
  237. iov->iov_base += bytes;
  238. total += bytes;
  239. if (iov->iov_len > 0) {
  240. struct pollfd pfd = {
  241. .fd = fd,
  242. .events = POLLIN
  243. };
  244. int ret;
  245. do {
  246. ret = poll(&pfd, 1, UDEBUG_TIMEOUT);
  247. } while (ret < 0 && errno == EINTR);
  248. if (!(pfd.revents & POLLIN))
  249. return -1;
  250. }
  251. }
  252. return total;
  253. }
  254. static void
  255. udebug_send_msg(struct udebug *ctx, struct udebug_client_msg *msg,
  256. struct blob_attr *meta, int fd)
  257. {
  258. struct iovec iov[2] = {
  259. { .iov_base = msg, .iov_len = sizeof(*msg) },
  260. {}
  261. };
  262. if (!meta) {
  263. blob_buf_init(&b, 0);
  264. meta = b.head;
  265. }
  266. iov[1].iov_base = meta;
  267. iov[1].iov_len = blob_pad_len(meta);
  268. writev_retry(ctx->fd.fd, iov, ARRAY_SIZE(iov), fd);
  269. }
  270. static bool
  271. udebug_recv_msg(struct udebug *ctx, struct udebug_client_msg *msg, int *fd,
  272. bool wait)
  273. {
  274. struct iovec iov = {
  275. .iov_base = msg,
  276. .iov_len = sizeof(*msg)
  277. };
  278. int ret;
  279. ret = recv_retry(ctx->fd.fd, &iov, wait, fd);
  280. if (ret == -2)
  281. __udebug_disconnect(ctx, true);
  282. return ret == sizeof(*msg);
  283. }
  284. static struct udebug_client_msg *
  285. __udebug_poll(struct udebug *ctx, int *fd, bool wait)
  286. {
  287. static struct udebug_client_msg msg = {};
  288. while (udebug_recv_msg(ctx, &msg, fd, wait)) {
  289. struct udebug_remote_buf *rb;
  290. void *key;
  291. if (msg.type != CL_MSG_RING_NOTIFY)
  292. return &msg;
  293. if (fd && *fd >= 0)
  294. close(*fd);
  295. if (!ctx->notify_cb)
  296. continue;
  297. key = (void *)(uintptr_t)msg.id;
  298. rb = avl_find_element(&ctx->remote_rings, key, rb, node);
  299. if (!rb || !rb->poll)
  300. continue;
  301. if (ctx->poll_handle >= 0)
  302. __atomic_fetch_or(&rb->buf.hdr->notify,
  303. 1UL << ctx->poll_handle,
  304. __ATOMIC_RELAXED);
  305. ctx->notify_cb(ctx, rb);
  306. }
  307. return NULL;
  308. }
  309. static struct udebug_client_msg *
  310. udebug_wait_for_response(struct udebug *ctx, struct udebug_client_msg *msg, int *rfd)
  311. {
  312. int type = msg->type;
  313. int fd = -1;
  314. do {
  315. if (fd >= 0)
  316. close(fd);
  317. fd = -1;
  318. msg = __udebug_poll(ctx, &fd, true);
  319. } while (msg && msg->type != type);
  320. if (!msg)
  321. return NULL;
  322. if (rfd)
  323. *rfd = fd;
  324. else if (fd >= 0)
  325. close(fd);
  326. return msg;
  327. }
  328. static void
  329. udebug_buf_msg(struct udebug_buf *buf, enum udebug_client_msg_type type)
  330. {
  331. struct udebug_client_msg msg = {
  332. .type = type,
  333. .id = buf->id,
  334. };
  335. udebug_send_msg(buf->ctx, &msg, NULL, -1);
  336. udebug_wait_for_response(buf->ctx, &msg, NULL);
  337. }
  338. static size_t __udebug_headsize(unsigned int ring_size)
  339. {
  340. ring_size *= sizeof(struct udebug_ptr);
  341. return ALIGN(sizeof(struct udebug_hdr) + ring_size, page_size);
  342. }
  343. static void udebug_init_page_size(void)
  344. {
  345. if (page_size)
  346. return;
  347. page_size = sysconf(_SC_PAGESIZE);
  348. #ifdef mips
  349. /* leave extra alignment room to account for data cache aliases */
  350. if (page_size < 32 * 1024)
  351. page_size = 32 * 1024;
  352. #endif
  353. }
  354. int udebug_buf_open(struct udebug_buf *buf, int fd, uint32_t ring_size, uint32_t data_size)
  355. {
  356. udebug_init_page_size();
  357. INIT_LIST_HEAD(&buf->list);
  358. buf->ring_size = ring_size;
  359. buf->head_size = __udebug_headsize(ring_size);
  360. buf->data_size = data_size;
  361. if (buf->ring_size > (1U << 24) || buf->data_size > (1U << 29))
  362. return -1;
  363. if (__udebug_buf_map(buf, fd))
  364. return -1;
  365. if (buf->ring_size != buf->hdr->ring_size ||
  366. buf->data_size != buf->hdr->data_size) {
  367. munmap(buf->hdr, buf->head_size + 2 * buf->data_size);
  368. buf->hdr = NULL;
  369. return -1;
  370. }
  371. buf->fd = fd;
  372. return 0;
  373. }
  374. int udebug_buf_init(struct udebug_buf *buf, size_t entries, size_t size)
  375. {
  376. char filename[] = "/udebug.XXXXXX";
  377. unsigned int order = 12;
  378. uint8_t ring_order = 5;
  379. size_t head_size;
  380. int fd;
  381. udebug_init_page_size();
  382. INIT_LIST_HEAD(&buf->list);
  383. if (size < page_size)
  384. size = page_size;
  385. while(size > 1U << order)
  386. order++;
  387. size = 1 << order;
  388. while (entries > 1U << ring_order)
  389. ring_order++;
  390. entries = 1 << ring_order;
  391. if (size > (1U << 29) || entries > (1U << 24))
  392. return -1;
  393. head_size = __udebug_headsize(entries);
  394. while (ALIGN(sizeof(*buf->hdr) + (entries * 2) * sizeof(struct udebug_ptr), page_size) == head_size)
  395. entries *= 2;
  396. fd = shm_open_anon(filename);
  397. if (fd < 0)
  398. return -1;
  399. if (ftruncate(fd, head_size + size) < 0)
  400. goto err_close;
  401. buf->head_size = head_size;
  402. buf->data_size = size;
  403. buf->ring_size = entries;
  404. if (__udebug_buf_map(buf, fd))
  405. goto err_close;
  406. buf->fd = fd;
  407. buf->hdr->ring_size = entries;
  408. buf->hdr->data_size = size;
  409. /* ensure hdr changes are visible */
  410. __sync_synchronize();
  411. return 0;
  412. err_close:
  413. close(fd);
  414. return -1;
  415. }
  416. static void *udebug_buf_alloc(struct udebug_buf *buf, uint32_t ofs, uint32_t len)
  417. {
  418. struct udebug_hdr *hdr = buf->hdr;
  419. hdr->data_used = u32_max(hdr->data_used, ofs + len + 1);
  420. /* ensure that data_used update is visible before clobbering data */
  421. __sync_synchronize();
  422. return udebug_buf_ptr(buf, ofs);
  423. }
  424. uint64_t udebug_buf_flags(struct udebug_buf *buf)
  425. {
  426. struct udebug_hdr *hdr = buf->hdr;
  427. uint64_t flags;
  428. if (!hdr)
  429. return 0;
  430. flags = hdr->flags[0];
  431. if (sizeof(flags) != sizeof(uintptr_t))
  432. flags |= ((uint64_t)hdr->flags[1]) << 32;
  433. return flags;
  434. }
  435. void udebug_entry_init_ts(struct udebug_buf *buf, uint64_t timestamp)
  436. {
  437. struct udebug_hdr *hdr = buf->hdr;
  438. struct udebug_ptr *ptr;
  439. if (!hdr)
  440. return;
  441. ptr = udebug_ring_ptr(hdr, hdr->head);
  442. ptr->start = hdr->data_head;
  443. ptr->len = 0;
  444. ptr->timestamp = timestamp;
  445. }
  446. void *udebug_entry_append(struct udebug_buf *buf, const void *data, uint32_t len)
  447. {
  448. struct udebug_hdr *hdr = buf->hdr;
  449. struct udebug_ptr *ptr;
  450. uint32_t ofs;
  451. void *ret;
  452. if (!hdr)
  453. return NULL;
  454. ptr = udebug_ring_ptr(hdr, hdr->head);
  455. ofs = ptr->start + ptr->len;
  456. if (ptr->len + len > buf->data_size / 2)
  457. return NULL;
  458. ret = udebug_buf_alloc(buf, ofs, len);
  459. if (data)
  460. memcpy(ret, data, len);
  461. ptr->len += len;
  462. return ret;
  463. }
  464. uint16_t udebug_entry_trim(struct udebug_buf *buf, uint16_t len)
  465. {
  466. struct udebug_hdr *hdr = buf->hdr;
  467. struct udebug_ptr *ptr;
  468. if (!hdr)
  469. return 0;
  470. ptr = udebug_ring_ptr(hdr, hdr->head);
  471. if (len)
  472. ptr->len -= len;
  473. return ptr->len;
  474. }
  475. void udebug_entry_set_length(struct udebug_buf *buf, uint16_t len)
  476. {
  477. struct udebug_hdr *hdr = buf->hdr;
  478. struct udebug_ptr *ptr;
  479. if (!hdr)
  480. return;
  481. ptr = udebug_ring_ptr(hdr, hdr->head);
  482. ptr->len = len;
  483. }
  484. int udebug_entry_printf(struct udebug_buf *buf, const char *fmt, ...)
  485. {
  486. va_list ap;
  487. size_t ret;
  488. va_start(ap, fmt);
  489. ret = udebug_entry_vprintf(buf, fmt, ap);
  490. va_end(ap);
  491. return ret;
  492. }
  493. int udebug_entry_vprintf(struct udebug_buf *buf, const char *fmt, va_list ap)
  494. {
  495. struct udebug_hdr *hdr = buf->hdr;
  496. struct udebug_ptr *ptr;
  497. uint32_t ofs;
  498. uint32_t len;
  499. va_list ap2;
  500. char *str;
  501. if (!hdr)
  502. return -1;
  503. ptr = udebug_ring_ptr(hdr, hdr->head);
  504. ofs = ptr->start + ptr->len;
  505. if (ptr->len > buf->data_size / 2)
  506. return -1;
  507. str = udebug_buf_alloc(buf, ofs, UDEBUG_MIN_ALLOC_LEN);
  508. va_copy(ap2, ap);
  509. len = vsnprintf(str, UDEBUG_MIN_ALLOC_LEN, fmt, ap2);
  510. va_end(ap2);
  511. if (len <= UDEBUG_MIN_ALLOC_LEN)
  512. goto out;
  513. if (ptr->len + len > buf->data_size / 2)
  514. return -1;
  515. udebug_buf_alloc(buf, ofs, len + 1);
  516. len = vsnprintf(str, len, fmt, ap);
  517. out:
  518. ptr->len += len;
  519. return 0;
  520. }
  521. void udebug_entry_add(struct udebug_buf *buf)
  522. {
  523. struct udebug_hdr *hdr = buf->hdr;
  524. struct udebug_ptr *ptr;
  525. uint32_t notify;
  526. uint8_t *data;
  527. if (!hdr)
  528. return;
  529. ptr = udebug_ring_ptr(hdr, hdr->head);
  530. /* ensure strings are always 0-terminated */
  531. data = udebug_buf_ptr(buf, ptr->start + ptr->len);
  532. *data = 0;
  533. hdr->data_head = ptr->start + ptr->len + 1;
  534. /* ensure that all data changes are visible before advancing head */
  535. __sync_synchronize();
  536. u32_set(&hdr->head, u32_get(&hdr->head) + 1);
  537. if (!u32_get(&hdr->head))
  538. u32_set(&hdr->head_hi, u32_get(&hdr->head_hi) + 1);
  539. /* ensure that head change is visible */
  540. __sync_synchronize();
  541. notify = __atomic_exchange_n(&hdr->notify, 0, __ATOMIC_RELAXED);
  542. if (notify) {
  543. struct udebug_client_msg msg = {
  544. .type = CL_MSG_RING_NOTIFY,
  545. .id = buf->id,
  546. .notify_mask = notify,
  547. };
  548. blob_buf_init(&b, 0);
  549. udebug_send_msg(buf->ctx, &msg, b.head, -1);
  550. }
  551. }
  552. void udebug_buf_free(struct udebug_buf *buf)
  553. {
  554. struct udebug *ctx = buf->ctx;
  555. if (!list_empty(&buf->list) && buf->list.prev)
  556. list_del(&buf->list);
  557. if (ctx && ctx->fd.fd >= 0)
  558. udebug_buf_msg(buf, CL_MSG_RING_REMOVE);
  559. munmap(buf->hdr, buf->head_size + 2 * buf->data_size);
  560. close(buf->fd);
  561. memset(buf, 0, sizeof(*buf));
  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. blob_buf_init(&b, 0);
  575. blobmsg_add_string(&b, "name", meta->name);
  576. c = blobmsg_open_array(&b, "flags");
  577. for (size_t i = 0; i < meta->n_flags; i++) {
  578. const struct udebug_buf_flag *flag = &meta->flags[i];
  579. void *e = blobmsg_open_array(&b, NULL);
  580. blobmsg_add_string(&b, NULL, flag->name);
  581. blobmsg_add_u64(&b, NULL, flag->mask);
  582. blobmsg_close_array(&b, e);
  583. }
  584. blobmsg_close_array(&b, c);
  585. udebug_send_msg(ctx, &msg, b.head, buf->fd);
  586. udebug_wait_for_response(ctx, &msg, NULL);
  587. }
  588. int udebug_buf_add(struct udebug *ctx, struct udebug_buf *buf,
  589. const struct udebug_buf_meta *meta)
  590. {
  591. if (!buf->hdr)
  592. return -1;
  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. if (ctx->fd.fd >= 0)
  600. __udebug_buf_add(ctx, buf);
  601. return 0;
  602. }
  603. void udebug_init(struct udebug *ctx)
  604. {
  605. INIT_LIST_HEAD(&ctx->local_rings);
  606. avl_init(&ctx->remote_rings, udebug_id_cmp, true, NULL);
  607. ctx->fd.fd = -1;
  608. ctx->poll_handle = -1;
  609. }
  610. static void udebug_reconnect_cb(struct uloop_timeout *t)
  611. {
  612. struct udebug *ctx = container_of(t, struct udebug, reconnect);
  613. if (udebug_connect(ctx, ctx->socket_path) < 0) {
  614. uloop_timeout_set(&ctx->reconnect, 1000);
  615. return;
  616. }
  617. udebug_add_uloop(ctx);
  618. }
  619. void udebug_auto_connect(struct udebug *ctx, const char *path)
  620. {
  621. free(ctx->socket_path);
  622. ctx->reconnect.cb = udebug_reconnect_cb;
  623. ctx->socket_path = path ? strdup(path) : NULL;
  624. if (ctx->fd.fd >= 0)
  625. return;
  626. udebug_reconnect_cb(&ctx->reconnect);
  627. }
  628. int udebug_connect(struct udebug *ctx, const char *path)
  629. {
  630. struct udebug_remote_buf *rb;
  631. struct udebug_buf *buf;
  632. if (ctx->fd.fd >= 0)
  633. close(ctx->fd.fd);
  634. ctx->fd.fd = -1;
  635. if (!path)
  636. path = UDEBUG_SOCK_NAME;
  637. ctx->fd.fd = usock(USOCK_UNIX, path, NULL);
  638. if (ctx->fd.fd < 0)
  639. return -1;
  640. list_for_each_entry(buf, &ctx->local_rings, list)
  641. __udebug_buf_add(ctx, buf);
  642. avl_for_each_element(&ctx->remote_rings, rb, node) {
  643. if (!rb->poll)
  644. continue;
  645. rb->poll = false;
  646. udebug_remote_buf_set_poll(ctx, rb, true);
  647. }
  648. return 0;
  649. }
  650. void udebug_poll(struct udebug *ctx)
  651. {
  652. while (__udebug_poll(ctx, NULL, false));
  653. }
  654. struct udebug_client_msg *
  655. udebug_send_and_wait(struct udebug *ctx, struct udebug_client_msg *msg, int *rfd)
  656. {
  657. udebug_send_msg(ctx, msg, NULL, -1);
  658. return udebug_wait_for_response(ctx, msg, rfd);
  659. }
  660. static void udebug_fd_cb(struct uloop_fd *fd, unsigned int events)
  661. {
  662. struct udebug *ctx = container_of(fd, struct udebug, fd);
  663. if (fd->eof)
  664. __udebug_disconnect(ctx, true);
  665. udebug_poll(ctx);
  666. }
  667. void udebug_add_uloop(struct udebug *ctx)
  668. {
  669. if (ctx->fd.registered)
  670. return;
  671. ctx->fd.cb = udebug_fd_cb;
  672. uloop_fd_add(&ctx->fd, ULOOP_READ);
  673. }
  674. void udebug_free(struct udebug *ctx)
  675. {
  676. struct udebug_remote_buf *rb, *tmp;
  677. struct udebug_buf *buf;
  678. free(ctx->socket_path);
  679. ctx->socket_path = NULL;
  680. __udebug_disconnect(ctx, false);
  681. uloop_timeout_cancel(&ctx->reconnect);
  682. while (!list_empty(&ctx->local_rings)) {
  683. buf = list_first_entry(&ctx->local_rings, struct udebug_buf, list);
  684. udebug_buf_free(buf);
  685. }
  686. avl_for_each_element_safe(&ctx->remote_rings, rb, node, tmp)
  687. udebug_remote_buf_unmap(ctx, rb);
  688. }