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 #include "udebug-priv.h" 19 20 static int 21 udebug_remote_get_handle(struct udebug *ctx) 22 { 23 struct udebug_client_msg *msg; 24 struct udebug_client_msg send_msg = { 25 .type = CL_MSG_GET_HANDLE, 26 }; 27 28 if (ctx->poll_handle >= 0 || !udebug_is_connected(ctx)) 29 return 0; 30 31 msg = udebug_send_and_wait(ctx, &send_msg, NULL); 32 if (!msg) 33 return -1; 34 35 ctx->poll_handle = msg->id; 36 return 0; 37 } 38 39 struct udebug_remote_buf *udebug_remote_buf_get(struct udebug *ctx, uint32_t id) 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 } 46 47 int udebug_remote_buf_map(struct udebug *ctx, struct udebug_remote_buf *rb, uint32_t id) 48 { 49 void *key = (void *)(uintptr_t)id; 50 struct udebug_client_msg *msg; 51 struct udebug_client_msg send_msg = { 52 .type = CL_MSG_RING_GET, 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 close(fd); 66 return -1; 67 } 68 69 rb->pcap_iface = ~0U; 70 rb->node.key = key; 71 avl_insert(&ctx->remote_rings, &rb->node); 72 73 return 0; 74 } 75 76 void udebug_remote_buf_unmap(struct udebug *ctx, struct udebug_remote_buf *rb) 77 { 78 if (!rb->buf.data) 79 return; 80 81 avl_delete(&ctx->remote_rings, &rb->node); 82 udebug_buf_free(&rb->buf); 83 rb->poll = 0; 84 rb->node.key = NULL; 85 rb->pcap_iface = ~0U; 86 } 87 88 int udebug_remote_buf_set_poll(struct udebug *ctx, struct udebug_remote_buf *rb, bool val) 89 { 90 int handle; 91 92 if (!rb->buf.data) 93 return -1; 94 95 if (rb->poll == val) 96 return 0; 97 98 rb->poll = val; 99 if (!val) 100 return 0; 101 102 handle = udebug_remote_get_handle(ctx); 103 if (handle < 0) 104 return -1; 105 106 __atomic_fetch_or(&rb->buf.hdr->notify, 1UL << handle, __ATOMIC_RELAXED); 107 return 0; 108 } 109 110 static void 111 rbuf_advance_read_head(struct udebug_remote_buf *rb, uint32_t head, 112 uint32_t *data_start) 113 { 114 struct udebug_hdr *hdr = rb->buf.hdr; 115 uint32_t min_head = head + 1 - rb->buf.ring_size; 116 uint32_t min_data = u32_get(&hdr->data_used) - rb->buf.data_size; 117 struct udebug_ptr *last_ptr = udebug_ring_ptr(hdr, head - 1); 118 119 if (!u32_get(&hdr->head_hi) && u32_sub(0, min_head) > 0) 120 min_head = 0; 121 122 /* advance head to skip over any entries that are guaranteed 123 * to be overwritten now. final check will be performed after 124 * data copying */ 125 126 if (u32_sub(rb->head, min_head) < 0) 127 rb->head = min_head; 128 129 for (size_t i = 0; i < rb->buf.ring_size; i++) { 130 struct udebug_ptr *ptr = udebug_ring_ptr(hdr, rb->head); 131 132 if (data_start) { 133 *data_start = u32_get(&ptr->start); 134 __sync_synchronize(); 135 } 136 137 if (rb->head == head) 138 break; 139 140 if (ptr->timestamp > last_ptr->timestamp) 141 continue; 142 143 if (u32_sub(ptr->start, min_data) > 0) 144 break; 145 146 rb->head++; 147 } 148 } 149 150 void udebug_remote_buf_set_start_time(struct udebug_remote_buf *rb, uint64_t ts) 151 { 152 struct udebug_hdr *hdr = rb->buf.hdr; 153 uint32_t head, start, end; 154 uint32_t diff; 155 156 if (!hdr) 157 return; 158 159 head = u32_get(&hdr->head); 160 start = rb->head; 161 end = head; 162 163 rbuf_advance_read_head(rb, head, NULL); 164 while ((diff = u32_sub(end, start)) > 0) { 165 uint32_t cur = start + diff / 2; 166 struct udebug_ptr *ptr; 167 168 ptr = udebug_ring_ptr(hdr, cur); 169 if (ptr->timestamp > ts) 170 end = cur - 1; 171 else 172 start = cur + 1; 173 } 174 175 rb->head = start; 176 } 177 178 void udebug_remote_buf_set_start_offset(struct udebug_remote_buf *rb, uint32_t idx) 179 { 180 if (!rb->buf.hdr) 181 return; 182 183 rb->head = rb->buf.hdr->head - idx; 184 } 185 186 void udebug_remote_buf_set_flags(struct udebug_remote_buf *rb, uint64_t mask, uint64_t set) 187 { 188 struct udebug_hdr *hdr = rb->buf.hdr; 189 190 if (!hdr) 191 return; 192 193 if ((uintptr_t)mask) 194 __atomic_and_fetch(&hdr->flags[0], (uintptr_t)~mask, __ATOMIC_RELAXED); 195 if ((uintptr_t)set) 196 __atomic_or_fetch(&hdr->flags[0], (uintptr_t)set, __ATOMIC_RELAXED); 197 198 if (sizeof(mask) == sizeof(unsigned long)) 199 return; 200 201 mask >>= 32; 202 if ((uintptr_t)mask) 203 __atomic_and_fetch(&hdr->flags[1], (uintptr_t)~mask, __ATOMIC_RELAXED); 204 if ((uintptr_t)set) 205 __atomic_or_fetch(&hdr->flags[1], (uintptr_t)set, __ATOMIC_RELAXED); 206 } 207 208 struct udebug_snapshot * 209 udebug_remote_buf_snapshot(struct udebug_remote_buf *rb) 210 { 211 struct udebug_hdr *hdr = rb->buf.hdr; 212 struct udebug_ptr *last_ptr; 213 uint32_t data_start, data_end, data_used; 214 struct udebug_snapshot *s = NULL; 215 struct udebug_ptr *ptr_buf, *first_ptr; 216 uint32_t data_size, ptr_size; 217 uint32_t head, first_idx; 218 uint32_t prev_read_head = rb->head; 219 void *data_buf; 220 221 if (!hdr) 222 return NULL; 223 224 head = u32_get(&hdr->head); 225 rbuf_advance_read_head(rb, head, &data_start); 226 if (rb->head == head) 227 return NULL; 228 229 first_idx = rb->head; 230 first_ptr = udebug_ring_ptr(hdr, first_idx); 231 last_ptr = udebug_ring_ptr(hdr, head - 1); 232 data_end = last_ptr->start + last_ptr->len; 233 234 data_size = data_end - data_start; 235 ptr_size = head - rb->head; 236 if (data_size > rb->buf.data_size || ptr_size > rb->buf.ring_size) { 237 fprintf(stderr, "Invalid data size: %x > %x, %x > %x\n", data_size, (int)rb->buf.data_size, ptr_size, (int)rb->buf.ring_size); 238 goto out; 239 } 240 241 s = calloc_a(sizeof(*s), 242 &ptr_buf, ptr_size * sizeof(*ptr_buf), 243 &data_buf, (size_t)data_size); 244 245 s->data = memcpy(data_buf, udebug_buf_ptr(&rb->buf, data_start), data_size); 246 s->data_size = data_size; 247 s->entries = ptr_buf; 248 s->dropped = rb->head - prev_read_head; 249 250 if (first_ptr > last_ptr) { 251 struct udebug_ptr *start_ptr = udebug_ring_ptr(hdr, 0); 252 struct udebug_ptr *end_ptr = udebug_ring_ptr(hdr, rb->buf.ring_size - 1) + 1; 253 uint32_t size = end_ptr - first_ptr; 254 memcpy(s->entries, first_ptr, size * sizeof(*s->entries)); 255 memcpy(s->entries + size, start_ptr, (last_ptr + 1 - start_ptr) * sizeof(*s->entries)); 256 } else { 257 memcpy(s->entries, first_ptr, (last_ptr + 1 - first_ptr) * sizeof(*s->entries)); 258 } 259 260 /* get a snapshot of the counter that indicates how much data has been 261 * clobbered by newly added entries */ 262 __sync_synchronize(); 263 data_used = u32_get(&hdr->data_used) - rb->buf.data_size; 264 265 s->n_entries = head - first_idx; 266 267 rbuf_advance_read_head(rb, head, NULL); 268 if (s->n_entries < rb->head - first_idx) { 269 free(s); 270 s = NULL; 271 goto out; 272 } 273 274 s->entries += rb->head - first_idx; 275 s->n_entries -= rb->head - first_idx; 276 while (s->n_entries > 0 && 277 u32_sub(s->entries[0].start, data_used) < 0) { 278 s->entries++; 279 s->n_entries--; 280 s->dropped++; 281 } 282 283 for (size_t i = 0; i < s->n_entries; i++) 284 s->entries[i].start -= data_start; 285 286 s->format = hdr->format; 287 s->sub_format = hdr->sub_format; 288 s->rbuf_idx = (uint32_t)(uintptr_t)rb->node.key; 289 290 out: 291 rb->head = head; 292 return s; 293 } 294 295 bool udebug_snapshot_get_entry(struct udebug_snapshot *s, struct udebug_iter *it, unsigned int entry) 296 { 297 struct udebug_ptr *ptr; 298 299 it->len = 0; 300 if (entry >= s->n_entries) 301 goto error; 302 303 ptr = &s->entries[entry]; 304 if (ptr->start > s->data_size || ptr->len > s->data_size || 305 ptr->start + ptr->len > s->data_size) 306 goto error; 307 308 it->s = s; 309 it->data = s->data + ptr->start; 310 it->len = ptr->len; 311 it->timestamp = ptr->timestamp; 312 return true; 313 314 error: 315 it->data = NULL; 316 return false; 317 } 318 319 void udebug_iter_start(struct udebug_iter *it, struct udebug_snapshot **s, size_t n) 320 { 321 memset(it, 0, sizeof(*it)); 322 323 it->list = s; 324 it->n = n; 325 326 for (size_t i = 0; i < it->n; i++) 327 it->list[i]->iter_idx = 0; 328 } 329 330 bool udebug_iter_next(struct udebug_iter *it) 331 { 332 while (1) { 333 struct udebug_snapshot *s; 334 uint64_t cur_ts; 335 int cur = -1; 336 337 for (size_t i = 0; i < it->n; i++) { 338 struct udebug_ptr *ptr; 339 340 s = it->list[i]; 341 if (s->iter_idx >= s->n_entries) 342 continue; 343 344 ptr = &s->entries[s->iter_idx]; 345 if (cur >= 0 && ptr->timestamp > cur_ts) 346 continue; 347 348 cur = i; 349 cur_ts = ptr->timestamp; 350 } 351 352 if (cur < 0) 353 return false; 354 355 s = it->list[cur]; 356 it->s_idx = cur; 357 if (!udebug_snapshot_get_entry(s, it, s->iter_idx++)) 358 continue; 359 360 return true; 361 } 362 } 363
This page was automatically generated by LXR 0.3.1. • OpenWrt