Debian Codegraph @

src/libsysprof-capture/sysprof-capture-reader.c

raw
1 /* sysprof-capture-reader.c
2 *
3 * Copyright 2016-2019 Christian Hergert <[email protected]>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Subject to the terms and conditions of this license, each copyright holder
17 * and contributor hereby grants to those receiving rights under this license
18 * a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
19 * irrevocable (except for failure to satisfy the conditions of this license)
20 * patent license to make, have made, use, offer to sell, sell, import, and
21 * otherwise transfer this software, where such license applies only to those
22 * patent claims, already acquired or hereafter acquired, licensable by such
23 * copyright holder or contributor that are necessarily infringed by:
24 *
25 * (a) their Contribution(s) (the licensed copyrights of copyright holders
26 * and non-copyrightable additions of contributors, in source or binary
27 * form) alone; or
28 *
29 * (b) combination of their Contribution(s) with the work of authorship to
30 * which such Contribution(s) was added by such copyright holder or
31 * contributor, if, at the time the Contribution is added, such addition
32 * causes such combination to be necessarily infringed. The patent license
33 * shall not apply to any other combinations which include the
34 * Contribution.
35 *
36 * Except as expressly stated above, no rights or licenses from any copyright
37 * holder or contributor is granted under this license, whether expressly, by
38 * implication, estoppel or otherwise.
39 *
40 * DISCLAIMER
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
43 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
44 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
46 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
47 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
48 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
49 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
50 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
51 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
52 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 *
54 * SPDX-License-Identifier: BSD-2-Clause-Patent
55 */
56
57 #include "config.h"
58
59 #include <assert.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <limits.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <sys/stat.h>
66 #include <sys/types.h>
67 #include <unistd.h>
68
69 #include "sysprof-capture-reader.h"
70 #include "sysprof-capture-util-private.h"
71 #include "sysprof-capture-writer.h"
72 #include "sysprof-macros-internal.h"
73
74 struct _SysprofCaptureReader
75 {
76 volatile int ref_count;
77 char *filename;
78 uint8_t *buf;
79 size_t bufsz;
80 size_t len;
81 size_t pos;
82 size_t fd_off;
83 int fd;
84 int endian;
85 SysprofCaptureFileHeader header;
86 int64_t end_time;
87 SysprofCaptureStat st_buf;
88 unsigned int st_buf_set : 1;
89 char **list_files;
90 size_t n_list_files;
91 };
92
93 /* Sets @errno on failure. Sets @errno to EBADMSG if the file magic doesn’t
94 * match, and otherwise can return any error as for read(). */
95 static bool
96 sysprof_capture_reader_read_file_header (SysprofCaptureReader *self,
97 SysprofCaptureFileHeader *header)
98 {
99 assert (self != NULL);
100 assert (header != NULL);
101
102 if (sizeof *header != _sysprof_pread (self->fd, header, sizeof *header, 0L))
103 {
104 /* errno is propagated */
105 return false;
106 }
107
108 if (header->magic != SYSPROF_CAPTURE_MAGIC)
109 {
110 errno = EBADMSG;
111 return false;
112 }
113
114 header->capture_time[sizeof header->capture_time - 1] = '\0';
115
116 return true;
117 }
118
119 static void
120 sysprof_capture_reader_finalize (SysprofCaptureReader *self)
121 {
122 if (self != NULL)
123 {
124 for (size_t i = 0; i < self->n_list_files; i++)
125 free (self->list_files[i]);
126 free (self->list_files);
127
128 close (self->fd);
129 free (self->buf);
130 free (self->filename);
131 free (self);
132 }
133 }
134
135 const char *
136 sysprof_capture_reader_get_time (SysprofCaptureReader *self)
137 {
138 assert (self != NULL);
139
140 return self->header.capture_time;
141 }
142
143 const char *
144 sysprof_capture_reader_get_filename (SysprofCaptureReader *self)
145 {
146 assert (self != NULL);
147
148 return self->filename;
149 }
150
151 static void
152 sysprof_capture_reader_discover_end_time (SysprofCaptureReader *self)
153 {
154 SysprofCaptureFrame frame;
155
156 assert (self != NULL);
157
158 while (sysprof_capture_reader_peek_frame (self, &frame))
159 {
160 int64_t end_time = frame.time;
161
162 switch (frame.type)
163 {
164 case SYSPROF_CAPTURE_FRAME_MARK: {
165 const SysprofCaptureMark *mark = NULL;
166
167 if ((mark = sysprof_capture_reader_read_mark (self)))
168 end_time = frame.time + ((mark->duration > 0) ? mark->duration : 0);
169 }
170 break;
171
172 case SYSPROF_CAPTURE_FRAME_ALLOCATION:
173 case SYSPROF_CAPTURE_FRAME_CTRSET:
174 case SYSPROF_CAPTURE_FRAME_EXIT:
175 case SYSPROF_CAPTURE_FRAME_FORK:
176 case SYSPROF_CAPTURE_FRAME_LOG:
177 case SYSPROF_CAPTURE_FRAME_PROCESS:
178 case SYSPROF_CAPTURE_FRAME_SAMPLE:
179 case SYSPROF_CAPTURE_FRAME_TIMESTAMP:
180 if (end_time > self->end_time)
181 self->end_time = end_time;
182 break;
183
184 case SYSPROF_CAPTURE_FRAME_MAP:
185 case SYSPROF_CAPTURE_FRAME_JITMAP:
186 case SYSPROF_CAPTURE_FRAME_CTRDEF:
187 case SYSPROF_CAPTURE_FRAME_METADATA:
188 case SYSPROF_CAPTURE_FRAME_FILE_CHUNK:
189 default:
190 break;
191 }
192
193 if (!sysprof_capture_reader_skip (self))
194 break;
195 }
196
197 sysprof_capture_reader_reset (self);
198 }
199
200 /**
201 * sysprof_capture_reader_new_from_fd:
202 * @fd: an fd to take ownership from
203 *
204 * Creates a new reader using the file-descriptor.
205 *
206 * This is useful if you don't necessarily have access to the filename itself.
207 *
208 * If this function fails, `errno` is set.
209 *
210 * Returns: (transfer full): an #SysprofCaptureReader or %NULL upon failure.
211 */
212 SysprofCaptureReader *
213 sysprof_capture_reader_new_from_fd (int fd)
214 {
215 SysprofCaptureReader *self;
216
217 assert (fd > -1);
218
219 self = sysprof_malloc0 (sizeof (SysprofCaptureReader));
220 if (self == NULL)
221 {
222 errno = ENOMEM;
223 return NULL;
224 }
225
226 self->ref_count = 1;
227 self->bufsz = USHRT_MAX * 2;
228 self->buf = sysprof_malloc0 (self->bufsz);
229 if (self->buf == NULL)
230 {
231 free (self);
232 errno = ENOMEM;
233 return NULL;
234 }
235
236 self->len = 0;
237 self->pos = 0;
238 self->fd = fd;
239 self->fd_off = sizeof (SysprofCaptureFileHeader);
240
241 if (!sysprof_capture_reader_read_file_header (self, &self->header))
242 {
243 int errsv = errno;
244 sysprof_capture_reader_finalize (self);
245 errno = errsv;
246 return NULL;
247 }
248
249 if (self->header.little_endian)
250 self->endian = __LITTLE_ENDIAN;
251 else
252 self->endian = __BIG_ENDIAN;
253
254 /* If we detect a capture file that did not get an end time, or an erroneous
255 * end time, then we need to take a performance hit here and scan the file
256 * and discover the end time with frame timings.
257 */
258 if (self->header.end_time < self->header.time)
259 sysprof_capture_reader_discover_end_time (self);
260
261 return self;
262 }
263
264 SysprofCaptureReader *
265 sysprof_capture_reader_new (const char *filename)
266 {
267 SysprofCaptureReader *self;
268 int fd;
269
270 assert (filename != NULL);
271
272 if (-1 == (fd = open (filename, O_RDONLY, 0)))
273 {
274 /* errno is propagated */
275 return NULL;
276 }
277
278 if (NULL == (self = sysprof_capture_reader_new_from_fd (fd)))
279 {
280 int errsv = errno;
281 close (fd);
282 errno = errsv;
283 return NULL;
284 }
285
286 self->filename = sysprof_strdup (filename);
287
288 return self;
289 }
290
291 static inline void
292 sysprof_capture_reader_bswap_frame (SysprofCaptureReader *self,
293 SysprofCaptureFrame *frame)
294 {
295 assert (self != NULL);
296 assert (frame!= NULL);
297
298 if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER))
299 {
300 frame->len = bswap_16 (frame->len);
301 frame->cpu = bswap_16 (frame->cpu);
302 frame->pid = bswap_32 (frame->pid);
303 frame->time = bswap_64 (frame->time);
304 }
305 }
306
307 static inline void
308 sysprof_capture_reader_bswap_file_chunk (SysprofCaptureReader *self,
309 SysprofCaptureFileChunk *file_chunk)
310 {
311 assert (self != NULL);
312 assert (file_chunk != NULL);
313
314 if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER))
315 file_chunk->len = bswap_16 (file_chunk->len);
316 }
317
318 static inline void
319 sysprof_capture_reader_bswap_log (SysprofCaptureReader *self,
320 SysprofCaptureLog *log)
321 {
322 assert (self != NULL);
323 assert (log != NULL);
324
325 if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER))
326 log->severity = bswap_16 (log->severity);
327 }
328
329 static inline void
330 sysprof_capture_reader_bswap_map (SysprofCaptureReader *self,
331 SysprofCaptureMap *map)
332 {
333 assert (self != NULL);
334 assert (map != NULL);
335
336 if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER))
337 {
338 map->start = bswap_64 (map->start);
339 map->end = bswap_64 (map->end);
340 map->offset = bswap_64 (map->offset);
341 map->inode = bswap_64 (map->inode);
342 }
343 }
344
345 static inline void
346 sysprof_capture_reader_bswap_mark (SysprofCaptureReader *self,
347 SysprofCaptureMark *mark)
348 {
349 assert (self != NULL);
350 assert (mark != NULL);
351
352 if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER))
353 mark->duration = bswap_64 (mark->duration);
354 }
355
356 static inline void
357 sysprof_capture_reader_bswap_overlay (SysprofCaptureReader *self,
358 SysprofCaptureOverlay *pr)
359 {
360 assert (self != NULL);
361 assert (pr != NULL);
362
363 if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER))
364 {
365 pr->layer = bswap_32 (pr->layer);
366 pr->src_len = bswap_32 (pr->src_len);
367 pr->dst_len = bswap_32 (pr->dst_len);
368 }
369 }
370
371 static inline void
372 sysprof_capture_reader_bswap_jitmap (SysprofCaptureReader *self,
373 SysprofCaptureJitmap *jitmap)
374 {
375 assert (self != NULL);
376 assert (jitmap != NULL);
377
378 if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER))
379 jitmap->n_jitmaps = bswap_64 (jitmap->n_jitmaps);
380 }
381
382 static bool
383 sysprof_capture_reader_ensure_space_for (SysprofCaptureReader *self,
384 size_t len)
385 {
386 assert (self != NULL);
387 assert (self->pos <= self->len);
388 assert (len > 0);
389
390 /* Ensure alignment of length to read */
391 len = (len + SYSPROF_CAPTURE_ALIGN - 1) & ~(SYSPROF_CAPTURE_ALIGN - 1);
392
393 if ((self->len - self->pos) < len)
394 {
395 ssize_t r;
396
397 if (self->len > self->pos)
398 memmove (self->buf, &self->buf[self->pos], self->len - self->pos);
399 self->len -= self->pos;
400 self->pos = 0;
401
402 while (self->len < len)
403 {
404 assert ((self->pos + self->len) < self->bufsz);
405 assert (self->len < self->bufsz);
406
407 /* Read into our buffer after our current read position */
408 r = _sysprof_pread (self->fd,
409 &self->buf[self->len],
410 self->bufsz - self->len,
411 self->fd_off);
412
413 if (r <= 0)
414 break;
415
416 self->fd_off += r;
417 self->len += r;
418 }
419 }
420
421 return (self->len - self->pos) >= len;
422 }
423
424 bool
425 sysprof_capture_reader_skip (SysprofCaptureReader *self)
426 {
427 SysprofCaptureFrame *frame;
428
429 assert (self != NULL);
430 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
431
432 if (!sysprof_capture_reader_ensure_space_for (self, sizeof (SysprofCaptureFrame)))
433 return false;
434
435 frame = (SysprofCaptureFrame *)(void *)&self->buf[self->pos];
436 sysprof_capture_reader_bswap_frame (self, frame);
437
438 if (frame->len < sizeof (SysprofCaptureFrame))
439 return false;
440
441 if (!sysprof_capture_reader_ensure_space_for (self, frame->len))
442 return false;
443
444 frame = (SysprofCaptureFrame *)(void *)&self->buf[self->pos];
445
446 self->pos += frame->len;
447
448 if ((self->pos % SYSPROF_CAPTURE_ALIGN) != 0)
449 return false;
450
451 return true;
452 }
453
454 bool
455 sysprof_capture_reader_peek_frame (SysprofCaptureReader *self,
456 SysprofCaptureFrame *frame)
457 {
458 SysprofCaptureFrame *real_frame;
459
460 assert (self != NULL);
461 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
462 assert (self->pos <= self->len);
463 assert (self->pos <= self->bufsz);
464
465 if (!sysprof_capture_reader_ensure_space_for (self, sizeof *real_frame))
466 return false;
467
468 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
469
470 real_frame = (SysprofCaptureFrame *)(void *)&self->buf[self->pos];
471
472 *frame = *real_frame;
473
474 sysprof_capture_reader_bswap_frame (self, frame);
475
476 /* In case the capture did not update the end_time during normal usage,
477 * we can update our cached known end_time based on the greatest frame
478 * we come across.
479 */
480 if (frame->time > self->end_time)
481 self->end_time = frame->time;
482
483 return frame->type > 0 && frame->type < SYSPROF_CAPTURE_FRAME_LAST;
484 }
485
486 bool
487 sysprof_capture_reader_peek_type (SysprofCaptureReader *self,
488 SysprofCaptureFrameType *type)
489 {
490 SysprofCaptureFrame frame;
491
492 assert (self != NULL);
493 assert (type != NULL);
494
495 if (!sysprof_capture_reader_peek_frame (self, &frame))
496 return false;
497
498 *type = frame.type;
499
500 return frame.type > 0 && frame.type < SYSPROF_CAPTURE_FRAME_LAST;
501 }
502
503 static const SysprofCaptureFrame *
504 sysprof_capture_reader_read_basic (SysprofCaptureReader *self,
505 SysprofCaptureFrameType type,
506 size_t extra)
507 {
508 SysprofCaptureFrame *frame;
509 size_t len = sizeof *frame + extra;
510
511 assert (self != NULL);
512 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
513 assert (self->pos <= self->bufsz);
514
515 if (!sysprof_capture_reader_ensure_space_for (self, len))
516 return NULL;
517
518 frame = (SysprofCaptureFrame *)(void *)&self->buf[self->pos];
519
520 sysprof_capture_reader_bswap_frame (self, frame);
521
522 if (frame->len < len)
523 return NULL;
524
525 if (frame->type != type)
526 return NULL;
527
528 if (frame->len > (self->len - self->pos))
529 return NULL;
530
531 self->pos += frame->len;
532
533 return frame;
534 }
535
536 const SysprofCaptureTimestamp *
537 sysprof_capture_reader_read_timestamp (SysprofCaptureReader *self)
538 {
539 return (SysprofCaptureTimestamp *)
540 sysprof_capture_reader_read_basic (self, SYSPROF_CAPTURE_FRAME_TIMESTAMP, 0);
541 }
542
543 const SysprofCaptureExit *
544 sysprof_capture_reader_read_exit (SysprofCaptureReader *self)
545 {
546 return (SysprofCaptureExit *)
547 sysprof_capture_reader_read_basic (self, SYSPROF_CAPTURE_FRAME_EXIT, 0);
548 }
549
550 const SysprofCaptureFork *
551 sysprof_capture_reader_read_fork (SysprofCaptureReader *self)
552 {
553 SysprofCaptureFork *fk;
554
555 assert (self != NULL);
556
557 fk = (SysprofCaptureFork *)
558 sysprof_capture_reader_read_basic (self, SYSPROF_CAPTURE_FRAME_FORK, sizeof (uint32_t));
559
560 if (fk != NULL)
561 {
562 if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER))
563 fk->child_pid = bswap_32 (fk->child_pid);
564 }
565
566 return fk;
567 }
568
569 const SysprofCaptureMap *
570 sysprof_capture_reader_read_map (SysprofCaptureReader *self)
571 {
572 SysprofCaptureMap *map;
573
574 assert (self != NULL);
575 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
576 assert (self->pos <= self->bufsz);
577
578 if (!sysprof_capture_reader_ensure_space_for (self, sizeof *map))
579 return NULL;
580
581 map = (SysprofCaptureMap *)(void *)&self->buf[self->pos];
582
583 sysprof_capture_reader_bswap_frame (self, &map->frame);
584
585 if (map->frame.type != SYSPROF_CAPTURE_FRAME_MAP)
586 return NULL;
587
588 if (map->frame.len < (sizeof *map + 1))
589 return NULL;
590
591 if (!sysprof_capture_reader_ensure_space_for (self, map->frame.len))
592 return NULL;
593
594 map = (SysprofCaptureMap *)(void *)&self->buf[self->pos];
595
596 if (self->buf[self->pos + map->frame.len - 1] != '\0')
597 return NULL;
598
599 sysprof_capture_reader_bswap_map (self, map);
600
601 self->pos += map->frame.len;
602
603 if ((self->pos % SYSPROF_CAPTURE_ALIGN) != 0)
604 return NULL;
605
606 return map;
607 }
608
609 const SysprofCaptureLog *
610 sysprof_capture_reader_read_log (SysprofCaptureReader *self)
611 {
612 SysprofCaptureLog *log;
613
614 assert (self != NULL);
615 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
616 assert (self->pos <= self->bufsz);
617
618 if (!sysprof_capture_reader_ensure_space_for (self, sizeof *log))
619 return NULL;
620
621 log = (SysprofCaptureLog *)(void *)&self->buf[self->pos];
622
623 sysprof_capture_reader_bswap_frame (self, &log->frame);
624
625 if (log->frame.type != SYSPROF_CAPTURE_FRAME_LOG)
626 return NULL;
627
628 if (log->frame.len < (sizeof *log + 1))
629 return NULL;
630
631 if (!sysprof_capture_reader_ensure_space_for (self, log->frame.len))
632 return NULL;
633
634 log = (SysprofCaptureLog *)(void *)&self->buf[self->pos];
635
636 sysprof_capture_reader_bswap_log (self, log);
637
638 self->pos += log->frame.len;
639
640 if ((self->pos % SYSPROF_CAPTURE_ALIGN) != 0)
641 return NULL;
642
643 /* Ensure trailing \0 in domain and message */
644 log->domain[sizeof log->domain - 1] = 0;
645 if (log->frame.len > sizeof *log)
646 ((char *)log)[log->frame.len - 1] = 0;
647
648 return log;
649 }
650
651 const SysprofCaptureMark *
652 sysprof_capture_reader_read_mark (SysprofCaptureReader *self)
653 {
654 SysprofCaptureMark *mark;
655
656 assert (self != NULL);
657 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
658 assert (self->pos <= self->bufsz);
659
660 if (!sysprof_capture_reader_ensure_space_for (self, sizeof *mark))
661 return NULL;
662
663 mark = (SysprofCaptureMark *)(void *)&self->buf[self->pos];
664
665 sysprof_capture_reader_bswap_frame (self, &mark->frame);
666
667 if (mark->frame.type != SYSPROF_CAPTURE_FRAME_MARK)
668 return NULL;
669
670 if (mark->frame.len < (sizeof *mark + 1))
671 return NULL;
672
673 if (!sysprof_capture_reader_ensure_space_for (self, mark->frame.len))
674 return NULL;
675
676 mark = (SysprofCaptureMark *)(void *)&self->buf[self->pos];
677
678 sysprof_capture_reader_bswap_mark (self, mark);
679
680 self->pos += mark->frame.len;
681
682 if ((self->pos % SYSPROF_CAPTURE_ALIGN) != 0)
683 return NULL;
684
685 /* Ensure trailing \0 in name and message */
686 mark->name[sizeof mark->name - 1] = 0;
687 if (mark->frame.len > sizeof *mark)
688 ((char *)mark)[mark->frame.len - 1] = 0;
689
690 /* Maybe update end-time */
691 if SYSPROF_UNLIKELY ((mark->frame.time + mark->duration) > self->end_time)
692 self->end_time = mark->frame.time + mark->duration;
693
694 return mark;
695 }
696
697 const SysprofCaptureOverlay *
698 sysprof_capture_reader_read_overlay (SysprofCaptureReader *self)
699 {
700 SysprofCaptureOverlay *pr;
701
702 assert (self != NULL);
703 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
704 assert (self->pos <= self->bufsz);
705
706 if (!sysprof_capture_reader_ensure_space_for (self, sizeof *pr + 1))
707 return NULL;
708
709 pr = (SysprofCaptureOverlay *)(void *)&self->buf[self->pos];
710
711 sysprof_capture_reader_bswap_frame (self, &pr->frame);
712
713 if (pr->frame.type != SYSPROF_CAPTURE_FRAME_OVERLAY)
714 return NULL;
715
716 if (pr->frame.len < (sizeof *pr + 2))
717 return NULL;
718
719 if (!sysprof_capture_reader_ensure_space_for (self, pr->frame.len))
720 return NULL;
721
722 pr = (SysprofCaptureOverlay *)(void *)&self->buf[self->pos];
723
724 sysprof_capture_reader_bswap_overlay (self, pr);
725
726 /* Make sure there is enough space for paths and trailing \0 */
727 if (((size_t)pr->src_len + (size_t)pr->dst_len) > (pr->frame.len - sizeof *pr - 2))
728 return NULL;
729
730 /* Enforce trailing \0 */
731 pr->data[pr->src_len] = 0;
732 pr->data[pr->src_len+1+pr->dst_len] = 0;
733
734 self->pos += pr->frame.len;
735
736 if ((self->pos % SYSPROF_CAPTURE_ALIGN) != 0)
737 return NULL;
738
739 /* Ensure trailing \0 in name and message */
740 ((char *)pr)[pr->frame.len-1] = 0;
741
742 return pr;
743 }
744
745 const SysprofCaptureMetadata *
746 sysprof_capture_reader_read_metadata (SysprofCaptureReader *self)
747 {
748 SysprofCaptureMetadata *metadata;
749
750 assert (self != NULL);
751 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
752 assert (self->pos <= self->bufsz);
753
754 if (!sysprof_capture_reader_ensure_space_for (self, sizeof *metadata))
755 return NULL;
756
757 metadata = (SysprofCaptureMetadata *)(void *)&self->buf[self->pos];
758
759 sysprof_capture_reader_bswap_frame (self, &metadata->frame);
760
761 if (metadata->frame.type != SYSPROF_CAPTURE_FRAME_METADATA)
762 return NULL;
763
764 if (metadata->frame.len < (sizeof *metadata + 1))
765 return NULL;
766
767 if (!sysprof_capture_reader_ensure_space_for (self, metadata->frame.len))
768 return NULL;
769
770 metadata = (SysprofCaptureMetadata *)(void *)&self->buf[self->pos];
771
772 self->pos += metadata->frame.len;
773
774 if ((self->pos % SYSPROF_CAPTURE_ALIGN) != 0)
775 return NULL;
776
777 /* Ensure trailing \0 in .id and .metadata */
778 metadata->id[sizeof metadata->id - 1] = 0;
779 if (metadata->frame.len > sizeof *metadata)
780 ((char *)metadata)[metadata->frame.len - 1] = 0;
781
782 return metadata;
783 }
784
785 static inline void
786 sysprof_capture_reader_bswap_dbus_message (SysprofCaptureReader *self,
787 SysprofCaptureDBusMessage *dbus_message)
788 {
789 assert (self != NULL);
790 assert (dbus_message != NULL);
791
792 /* This only swaps the frame, not the endianness of the message data
793 * which is automatically handled by dbus libraries such as GDBus.
794 */
795
796 if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER))
797 {
798 dbus_message->flags = bswap_16 (dbus_message->flags);
799 dbus_message->message_len = bswap_16 (dbus_message->message_len);
800 }
801 }
802
803 const SysprofCaptureDBusMessage *
804 sysprof_capture_reader_read_dbus_message (SysprofCaptureReader *self)
805 {
806 SysprofCaptureDBusMessage *dbus_message;
807
808 assert (self != NULL);
809 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
810 assert (self->pos <= self->bufsz);
811
812 if (!sysprof_capture_reader_ensure_space_for (self, sizeof *dbus_message))
813 return NULL;
814
815 dbus_message = (SysprofCaptureDBusMessage *)(void *)&self->buf[self->pos];
816
817 sysprof_capture_reader_bswap_frame (self, &dbus_message->frame);
818
819 if (dbus_message->frame.type != SYSPROF_CAPTURE_FRAME_DBUS_MESSAGE)
820 return NULL;
821
822 sysprof_capture_reader_bswap_dbus_message (self, dbus_message);
823
824 if (dbus_message->frame.len < (sizeof *dbus_message + dbus_message->message_len))
825 return NULL;
826
827 if (!sysprof_capture_reader_ensure_space_for (self, dbus_message->frame.len))
828 return NULL;
829
830 dbus_message = (SysprofCaptureDBusMessage *)(void *)&self->buf[self->pos];
831
832 self->pos += dbus_message->frame.len;
833
834 if ((self->pos % SYSPROF_CAPTURE_ALIGN) != 0)
835 return NULL;
836
837 return dbus_message;
838 }
839
840 const SysprofCaptureProcess *
841 sysprof_capture_reader_read_process (SysprofCaptureReader *self)
842 {
843 SysprofCaptureProcess *process;
844
845 assert (self != NULL);
846 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
847 assert (self->pos <= self->bufsz);
848
849 if (!sysprof_capture_reader_ensure_space_for (self, sizeof *process))
850 return NULL;
851
852 process = (SysprofCaptureProcess *)(void *)&self->buf[self->pos];
853
854 sysprof_capture_reader_bswap_frame (self, &process->frame);
855
856 if (process->frame.type != SYSPROF_CAPTURE_FRAME_PROCESS)
857 return NULL;
858
859 if (process->frame.len < (sizeof *process + 1))
860 return NULL;
861
862 if (!sysprof_capture_reader_ensure_space_for (self, process->frame.len))
863 return NULL;
864
865 process = (SysprofCaptureProcess *)(void *)&self->buf[self->pos];
866
867 if (self->buf[self->pos + process->frame.len - 1] != '\0')
868 return NULL;
869
870 self->pos += process->frame.len;
871
872 if ((self->pos % SYSPROF_CAPTURE_ALIGN) != 0)
873 return NULL;
874
875 return process;
876 }
877
878 const SysprofCaptureJitmap *
879 sysprof_capture_reader_read_jitmap (SysprofCaptureReader *self)
880 {
881 SysprofCaptureJitmap *jitmap;
882 uint8_t *buf;
883 uint8_t *endptr;
884 unsigned int i;
885
886 assert (self != NULL);
887 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
888 assert (self->pos <= self->bufsz);
889
890 if (!sysprof_capture_reader_ensure_space_for (self, sizeof *jitmap))
891 return NULL;
892
893 jitmap = (SysprofCaptureJitmap *)(void *)&self->buf[self->pos];
894
895 sysprof_capture_reader_bswap_frame (self, &jitmap->frame);
896
897 if (jitmap->frame.type != SYSPROF_CAPTURE_FRAME_JITMAP)
898 return NULL;
899
900 if (jitmap->frame.len < sizeof *jitmap)
901 return NULL;
902
903 if (!sysprof_capture_reader_ensure_space_for (self, jitmap->frame.len))
904 return NULL;
905
906 jitmap = (SysprofCaptureJitmap *)(void *)&self->buf[self->pos];
907
908 buf = jitmap->data;
909 endptr = &self->buf[self->pos + jitmap->frame.len];
910
911 /* Check the strings are all nul-terminated. */
912 for (i = 0; i < jitmap->n_jitmaps; i++)
913 {
914 SysprofCaptureAddress addr;
915
916 if (buf + sizeof addr >= endptr)
917 return NULL;
918
919 memcpy (&addr, buf, sizeof addr);
920 buf += sizeof addr;
921
922 buf = memchr (buf, '\0', (endptr - buf));
923
924 if (buf == NULL)
925 return NULL;
926
927 buf++;
928 }
929
930 sysprof_capture_reader_bswap_jitmap (self, jitmap);
931
932 self->pos += jitmap->frame.len;
933
934 return jitmap;
935 }
936
937 const SysprofCaptureSample *
938 sysprof_capture_reader_read_sample (SysprofCaptureReader *self)
939 {
940 SysprofCaptureSample *sample;
941
942 assert (self != NULL);
943 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
944 assert (self->pos <= self->bufsz);
945
946 if (!sysprof_capture_reader_ensure_space_for (self, sizeof *sample))
947 return NULL;
948
949 sample = (SysprofCaptureSample *)(void *)&self->buf[self->pos];
950
951 sysprof_capture_reader_bswap_frame (self, &sample->frame);
952
953 if (sample->frame.type != SYSPROF_CAPTURE_FRAME_SAMPLE)
954 return NULL;
955
956 if (sample->frame.len < sizeof *sample)
957 return NULL;
958
959 if (self->endian != __BYTE_ORDER)
960 sample->n_addrs = bswap_16 (sample->n_addrs);
961
962 if (sample->frame.len < (sizeof *sample + (sizeof(SysprofCaptureAddress) * sample->n_addrs)))
963 return NULL;
964
965 if (!sysprof_capture_reader_ensure_space_for (self, sample->frame.len))
966 return NULL;
967
968 sample = (SysprofCaptureSample *)(void *)&self->buf[self->pos];
969
970 if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER))
971 {
972 unsigned int i;
973
974 for (i = 0; i < sample->n_addrs; i++)
975 sample->addrs[i] = bswap_64 (sample->addrs[i]);
976 }
977
978 self->pos += sample->frame.len;
979
980 return sample;
981 }
982
983 const SysprofCaptureTrace *
984 sysprof_capture_reader_read_trace (SysprofCaptureReader *self)
985 {
986 SysprofCaptureTrace *trace;
987
988 assert (self != NULL);
989 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
990 assert (self->pos <= self->bufsz);
991
992 if (!sysprof_capture_reader_ensure_space_for (self, sizeof *trace))
993 return NULL;
994
995 trace = (SysprofCaptureTrace *)(void *)&self->buf[self->pos];
996
997 sysprof_capture_reader_bswap_frame (self, &trace->frame);
998
999 if (trace->frame.type != SYSPROF_CAPTURE_FRAME_TRACE)
1000 return NULL;
1001
1002 if (trace->frame.len < sizeof *trace)
1003 return NULL;
1004
1005 if (self->endian != __BYTE_ORDER)
1006 trace->n_addrs = bswap_16 (trace->n_addrs);
1007
1008 if (trace->frame.len < (sizeof *trace + (sizeof(SysprofCaptureAddress) * trace->n_addrs)))
1009 return NULL;
1010
1011 if (!sysprof_capture_reader_ensure_space_for (self, trace->frame.len))
1012 return NULL;
1013
1014 trace = (SysprofCaptureTrace *)(void *)&self->buf[self->pos];
1015
1016 if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER))
1017 {
1018 unsigned int i;
1019
1020 for (i = 0; i < trace->n_addrs; i++)
1021 trace->addrs[i] = bswap_64 (trace->addrs[i]);
1022 }
1023
1024 self->pos += trace->frame.len;
1025
1026 return trace;
1027 }
1028
1029 const SysprofCaptureCounterDefine *
1030 sysprof_capture_reader_read_counter_define (SysprofCaptureReader *self)
1031 {
1032 SysprofCaptureCounterDefine *def;
1033
1034 assert (self != NULL);
1035 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
1036 assert (self->pos <= self->bufsz);
1037
1038 if (!sysprof_capture_reader_ensure_space_for (self, sizeof *def))
1039 return NULL;
1040
1041 def = (SysprofCaptureCounterDefine *)(void *)&self->buf[self->pos];
1042
1043 if (def->frame.type != SYSPROF_CAPTURE_FRAME_CTRDEF)
1044 return NULL;
1045
1046 if (def->frame.len < sizeof *def)
1047 return NULL;
1048
1049 if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER))
1050 def->n_counters = bswap_16 (def->n_counters);
1051
1052 if (def->frame.len < (sizeof *def + (sizeof (SysprofCaptureCounterDefine) * def->n_counters)))
1053 return NULL;
1054
1055 if (!sysprof_capture_reader_ensure_space_for (self, def->frame.len))
1056 return NULL;
1057
1058 def = (SysprofCaptureCounterDefine *)(void *)&self->buf[self->pos];
1059
1060 if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER))
1061 {
1062 unsigned int i;
1063
1064 for (i = 0; i < def->n_counters; i++)
1065 {
1066 def->counters[i].id = bswap_32 (def->counters[i].id);
1067 def->counters[i].value.v64 = bswap_64 (def->counters[i].value.v64);
1068 }
1069 }
1070
1071 self->pos += def->frame.len;
1072
1073 return def;
1074 }
1075
1076 const SysprofCaptureCounterSet *
1077 sysprof_capture_reader_read_counter_set (SysprofCaptureReader *self)
1078 {
1079 SysprofCaptureCounterSet *set;
1080
1081 assert (self != NULL);
1082 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
1083 assert (self->pos <= self->bufsz);
1084
1085 if (!sysprof_capture_reader_ensure_space_for (self, sizeof *set))
1086 return NULL;
1087
1088 set = (SysprofCaptureCounterSet *)(void *)&self->buf[self->pos];
1089
1090 if (set->frame.type != SYSPROF_CAPTURE_FRAME_CTRSET)
1091 return NULL;
1092
1093 if (set->frame.len < sizeof *set)
1094 return NULL;
1095
1096 if (self->endian != __BYTE_ORDER)
1097 set->n_values = bswap_16 (set->n_values);
1098
1099 if (set->frame.len < (sizeof *set + (sizeof (SysprofCaptureCounterValues) * set->n_values)))
1100 return NULL;
1101
1102 if (!sysprof_capture_reader_ensure_space_for (self, set->frame.len))
1103 return NULL;
1104
1105 set = (SysprofCaptureCounterSet *)(void *)&self->buf[self->pos];
1106
1107 if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER))
1108 {
1109 unsigned int i;
1110
1111 for (i = 0; i < set->n_values; i++)
1112 {
1113 unsigned int j;
1114
1115 for (j = 0; j < SYSPROF_N_ELEMENTS (set->values[0].values); j++)
1116 {
1117 set->values[i].ids[j] = bswap_32 (set->values[i].ids[j]);
1118 set->values[i].values[j].v64 = bswap_64 (set->values[i].values[j].v64);
1119 }
1120 }
1121 }
1122
1123 self->pos += set->frame.len;
1124
1125 return set;
1126 }
1127
1128 bool
1129 sysprof_capture_reader_reset (SysprofCaptureReader *self)
1130 {
1131 assert (self != NULL);
1132
1133 self->fd_off = sizeof (SysprofCaptureFileHeader);
1134 self->pos = 0;
1135 self->len = 0;
1136
1137 return true;
1138 }
1139
1140 SysprofCaptureReader *
1141 sysprof_capture_reader_ref (SysprofCaptureReader *self)
1142 {
1143 assert (self != NULL);
1144 assert (self->ref_count > 0);
1145
1146 __atomic_fetch_add (&self->ref_count, 1, __ATOMIC_SEQ_CST);
1147
1148 return self;
1149 }
1150
1151 void
1152 sysprof_capture_reader_unref (SysprofCaptureReader *self)
1153 {
1154 assert (self != NULL);
1155 assert (self->ref_count > 0);
1156
1157 if (__atomic_fetch_sub (&self->ref_count, 1, __ATOMIC_SEQ_CST) == 1)
1158 sysprof_capture_reader_finalize (self);
1159 }
1160
1161 bool
1162 sysprof_capture_reader_splice (SysprofCaptureReader *self,
1163 SysprofCaptureWriter *dest)
1164 {
1165 assert (self != NULL);
1166 assert (self->fd != -1);
1167 assert (dest != NULL);
1168
1169 /* Flush before writing anything to ensure consistency */
1170 if (!sysprof_capture_writer_flush (dest))
1171 {
1172 /* errno is propagated */
1173 return false;
1174 }
1175
1176 /*
1177 * We don't need to track position because writer will
1178 * track the current position to avoid reseting it.
1179 */
1180
1181 /* Perform the splice; errno is propagated on failure */
1182 return _sysprof_capture_writer_splice_from_fd (dest, self->fd);
1183 }
1184
1185 /**
1186 * sysprof_capture_reader_save_as:
1187 * @self: An #SysprofCaptureReader
1188 * @filename: the file to save the capture as
1189 *
1190 * This is a convenience function for copying a capture file for which
1191 * you may have already discarded the writer for.
1192 *
1193 * `errno` is set on failure. It may be any of the errors returned by
1194 * `open()`, `fstat()`, `ftruncate()`, `lseek()` or `sendfile()`.
1195 *
1196 * Returns: %TRUE on success; otherwise %FALSE.
1197 */
1198 bool
1199 sysprof_capture_reader_save_as (SysprofCaptureReader *self,
1200 const char *filename)
1201 {
1202 struct stat stbuf;
1203 off_t in_off;
1204 size_t to_write;
1205 int fd = -1;
1206 int errsv;
1207
1208 assert (self != NULL);
1209 assert (filename != NULL);
1210
1211 if (-1 == (fd = open (filename, O_CREAT | O_WRONLY, 0640)))
1212 goto handle_errno;
1213
1214 if (-1 == fstat (self->fd, &stbuf))
1215 goto handle_errno;
1216
1217 if (-1 == ftruncate (fd, stbuf.st_size))
1218 goto handle_errno;
1219
1220 if ((off_t)-1 == lseek (fd, 0L, SEEK_SET))
1221 goto handle_errno;
1222
1223 in_off = 0;
1224 to_write = stbuf.st_size;
1225
1226 while (to_write > 0)
1227 {
1228 ssize_t written;
1229
1230 written = _sysprof_sendfile (fd, self->fd, &in_off, to_write);
1231
1232 if (written < 0)
1233 goto handle_errno;
1234
1235 if (written == 0 && errno != EAGAIN)
1236 goto handle_errno;
1237
1238 assert (written <= (ssize_t)to_write);
1239
1240 to_write -= written;
1241 }
1242
1243 if (self->filename == NULL)
1244 self->filename = sysprof_strdup (filename);
1245
1246 close (fd);
1247
1248 return true;
1249
1250 handle_errno:
1251 errsv = errno;
1252
1253 if (fd != -1)
1254 close (fd);
1255
1256 errno = errsv;
1257
1258 return false;
1259 }
1260
1261 int64_t
1262 sysprof_capture_reader_get_start_time (SysprofCaptureReader *self)
1263 {
1264 assert (self != NULL);
1265
1266 if (self->endian != __BYTE_ORDER)
1267 return bswap_64 (self->header.time);
1268
1269 return self->header.time;
1270 }
1271
1272 /**
1273 * sysprof_capture_reader_get_end_time:
1274 *
1275 * This function will return the end time for the capture, which is the
1276 * same as the last event added to the capture.
1277 *
1278 * If the end time has been stored in the capture header, that will be used.
1279 * Otherwise, the time is discovered from the last capture frame and therefore
1280 * the caller must have read all frames to ensure this value is accurate.
1281 *
1282 * Captures created by sysprof versions containing this API will have the
1283 * end time set if the output capture file-descriptor supports seeking.
1284 *
1285 * Since: 3.22.1
1286 */
1287 int64_t
1288 sysprof_capture_reader_get_end_time (SysprofCaptureReader *self)
1289 {
1290 int64_t end_time = 0;
1291
1292 assert (self != NULL);
1293
1294 if (self->header.end_time != 0)
1295 {
1296 if (self->endian != __BYTE_ORDER)
1297 end_time = bswap_64 (self->header.end_time);
1298 else
1299 end_time = self->header.end_time;
1300 }
1301
1302 return (self->end_time > end_time) ? self->end_time : end_time;
1303 }
1304
1305 /**
1306 * sysprof_capture_reader_copy:
1307 *
1308 * This function makes a copy of the reader. Since readers use
1309 * positioned reads with pread(), this allows you to have multiple
1310 * readers with the shared file descriptor. This uses dup() to create
1311 * another file descriptor.
1312 *
1313 * Returns: (transfer full): A copy of @self with a new file-descriptor.
1314 */
1315 SysprofCaptureReader *
1316 sysprof_capture_reader_copy (SysprofCaptureReader *self)
1317 {
1318 SysprofCaptureReader *copy;
1319 int fd;
1320
1321 assert (self != NULL);
1322
1323 if (-1 == (fd = dup (self->fd)))
1324 return NULL;
1325
1326 copy = sysprof_malloc0 (sizeof (SysprofCaptureReader));
1327 if (copy == NULL)
1328 {
1329 close (fd);
1330 return NULL;
1331 }
1332
1333 *copy = *self;
1334
1335 copy->ref_count = 1;
1336 copy->filename = sysprof_strdup (self->filename);
1337 copy->fd = fd;
1338 copy->end_time = self->end_time;
1339 copy->st_buf = self->st_buf;
1340 copy->st_buf_set = self->st_buf_set;
1341
1342 copy->buf = malloc (self->bufsz);
1343 if (copy->buf == NULL)
1344 {
1345 close (fd);
1346 free (copy->filename);
1347 free (copy);
1348 return NULL;
1349 }
1350
1351 memcpy (copy->buf, self->buf, self->bufsz);
1352
1353 return copy;
1354 }
1355
1356 void
1357 sysprof_capture_reader_set_stat (SysprofCaptureReader *self,
1358 const SysprofCaptureStat *st_buf)
1359 {
1360 assert (self != NULL);
1361
1362 if (st_buf != NULL)
1363 {
1364 self->st_buf = *st_buf;
1365 self->st_buf_set = true;
1366 }
1367 else
1368 {
1369 memset (&self->st_buf, 0, sizeof (self->st_buf));
1370 self->st_buf_set = false;
1371 }
1372 }
1373
1374 bool
1375 sysprof_capture_reader_get_stat (SysprofCaptureReader *self,
1376 SysprofCaptureStat *st_buf)
1377 {
1378 assert (self != NULL);
1379
1380 if (st_buf != NULL)
1381 *st_buf = self->st_buf;
1382
1383 return self->st_buf_set;
1384 }
1385
1386 const SysprofCaptureFileChunk *
1387 sysprof_capture_reader_read_file (SysprofCaptureReader *self)
1388 {
1389 SysprofCaptureFileChunk *file_chunk;
1390
1391 assert (self != NULL);
1392 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
1393 assert (self->pos <= self->bufsz);
1394
1395 if (!sysprof_capture_reader_ensure_space_for (self, sizeof *file_chunk))
1396 return NULL;
1397
1398 file_chunk = (SysprofCaptureFileChunk *)(void *)&self->buf[self->pos];
1399
1400 sysprof_capture_reader_bswap_frame (self, &file_chunk->frame);
1401
1402 if (file_chunk->frame.type != SYSPROF_CAPTURE_FRAME_FILE_CHUNK)
1403 return NULL;
1404
1405 if (file_chunk->frame.len < sizeof *file_chunk)
1406 return NULL;
1407
1408 if (!sysprof_capture_reader_ensure_space_for (self, file_chunk->frame.len))
1409 return NULL;
1410
1411 file_chunk = (SysprofCaptureFileChunk *)(void *)&self->buf[self->pos];
1412
1413 sysprof_capture_reader_bswap_file_chunk (self, file_chunk);
1414
1415 self->pos += file_chunk->frame.len;
1416
1417 if ((self->pos % SYSPROF_CAPTURE_ALIGN) != 0)
1418 return NULL;
1419
1420 /* Make sure len is < the extra frame data */
1421 if (file_chunk->len > (file_chunk->frame.len - sizeof *file_chunk))
1422 return NULL;
1423
1424 /* Ensure trailing \0 in .path */
1425 file_chunk->path[sizeof file_chunk->path - 1] = 0;
1426
1427 return file_chunk;
1428 }
1429
1430 static bool
1431 array_append (const char ***files,
1432 size_t *n_files,
1433 size_t *n_files_allocated,
1434 const char *new_element)
1435 {
1436 if (*n_files == *n_files_allocated)
1437 {
1438 const char **new_files;
1439
1440 *n_files_allocated = (*n_files_allocated > 0) ? 2 * *n_files_allocated : 4;
1441 new_files = _sysprof_reallocarray (*files, *n_files_allocated, sizeof (**files));
1442 if (new_files == NULL)
1443 return false;
1444 *files = new_files;
1445 }
1446
1447 (*files)[*n_files] = new_element ? sysprof_strdup (new_element) : NULL;
1448 *n_files = *n_files + 1;
1449 assert (*n_files <= *n_files_allocated);
1450
1451 return true;
1452 }
1453
1454 static void
1455 array_deduplicate (const char **files,
1456 size_t *n_files)
1457 {
1458 size_t last_written, next_to_read;
1459
1460 if (*n_files == 0)
1461 return;
1462
1463 for (last_written = 0, next_to_read = 1; last_written <= next_to_read && next_to_read < *n_files;)
1464 {
1465 if (strcmp (files[next_to_read], files[last_written]) == 0)
1466 {
1467 free ((char *)files[next_to_read]);
1468 next_to_read++;
1469 }
1470 else
1471 files[++last_written] = files[next_to_read++];
1472 }
1473
1474 assert (last_written + 1 <= *n_files);
1475 *n_files = last_written + 1;
1476 }
1477
1478 static int
1479 compare_strings (const void *a,
1480 const void *b)
1481 {
1482 const char * const *astr = a;
1483 const char * const *bstr = b;
1484
1485 return strcmp (*astr, *bstr);
1486 }
1487
1488 const char **
1489 sysprof_capture_reader_list_files (SysprofCaptureReader *self)
1490 {
1491 const char **files = NULL;
1492 size_t n_files = 0, n_files_allocated = 0;
1493 SysprofCaptureFrameType type;
1494
1495 assert (self != NULL);
1496
1497 /* Only generate the list of files once */
1498 if (self->list_files == NULL)
1499 {
1500 while (sysprof_capture_reader_peek_type (self, &type))
1501 {
1502 const SysprofCaptureFileChunk *file;
1503
1504 if (type != SYSPROF_CAPTURE_FRAME_FILE_CHUNK)
1505 {
1506 sysprof_capture_reader_skip (self);
1507 continue;
1508 }
1509
1510 if (!(file = sysprof_capture_reader_read_file (self)))
1511 break;
1512
1513 if (!array_append (&files, &n_files, &n_files_allocated, file->path))
1514 {
1515 free (files);
1516 errno = ENOMEM;
1517 return NULL;
1518 }
1519 }
1520
1521 /* Sort and deduplicate the files array. */
1522 qsort (files, n_files, sizeof (*files), compare_strings);
1523 array_deduplicate (files, &n_files);
1524
1525 /* Add a null terminator */
1526 if (!array_append (&files, &n_files, &n_files_allocated, NULL))
1527 {
1528 free (files);
1529 errno = ENOMEM;
1530 return NULL;
1531 }
1532
1533 self->list_files = (char **)sysprof_steal_pointer (&files);
1534 self->n_list_files = n_files; /* including NULL */
1535 }
1536
1537 /* Now copy the list but not the strings */
1538 files = malloc (sizeof (char *) * self->n_list_files);
1539 memcpy (files, self->list_files, sizeof (char *) * self->n_list_files);
1540
1541 return sysprof_steal_pointer (&files);
1542 }
1543
1544 bool
1545 sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self,
1546 const char *path,
1547 int fd)
1548 {
1549 assert (self != NULL);
1550 assert (path != NULL);
1551 assert (fd > -1);
1552
1553 for (;;)
1554 {
1555 SysprofCaptureFrameType type;
1556 const SysprofCaptureFileChunk *file;
1557 const uint8_t *buf;
1558 size_t to_write;
1559
1560 if (!sysprof_capture_reader_peek_type (self, &type))
1561 return false;
1562
1563 if (type != SYSPROF_CAPTURE_FRAME_FILE_CHUNK)
1564 goto skip;
1565
1566 if (!(file = sysprof_capture_reader_read_file (self)))
1567 return false;
1568
1569 if (strcmp (path, file->path) != 0)
1570 goto skip;
1571
1572 buf = file->data;
1573 to_write = file->len;
1574
1575 while (to_write > 0)
1576 {
1577 ssize_t written;
1578
1579 written = _sysprof_write (fd, buf, to_write);
1580 if (written < 0)
1581 return false;
1582
1583 if (written == 0 && errno != EAGAIN)
1584 return false;
1585
1586 assert (written <= (ssize_t)to_write);
1587
1588 buf += written;
1589 to_write -= written;
1590 }
1591
1592 if (!file->is_last)
1593 continue;
1594
1595 return true;
1596
1597 skip:
1598 if (!sysprof_capture_reader_skip (self))
1599 return false;
1600 }
1601
1602 sysprof_assert_not_reached ();
1603 }
1604
1605 int
1606 sysprof_capture_reader_get_byte_order (SysprofCaptureReader *self)
1607 {
1608 assert (self != NULL);
1609
1610 return self->endian;
1611 }
1612
1613 const SysprofCaptureFileChunk *
1614 sysprof_capture_reader_find_file (SysprofCaptureReader *self,
1615 const char *path)
1616 {
1617 SysprofCaptureFrameType type;
1618
1619 assert (self != NULL);
1620 assert (path != NULL);
1621
1622 while (sysprof_capture_reader_peek_type (self, &type))
1623 {
1624 if (type == SYSPROF_CAPTURE_FRAME_FILE_CHUNK)
1625 {
1626 const SysprofCaptureFileChunk *fc;
1627
1628 if (!(fc = sysprof_capture_reader_read_file (self)))
1629 break;
1630
1631 if (strcmp (path, fc->path) == 0)
1632 return fc;
1633
1634 continue;
1635 }
1636
1637 if (!sysprof_capture_reader_skip (self))
1638 break;
1639 }
1640
1641 return NULL;
1642 }
1643
1644 const SysprofCaptureAllocation *
1645 sysprof_capture_reader_read_allocation (SysprofCaptureReader *self)
1646 {
1647 SysprofCaptureAllocation *ma;
1648
1649 assert (self != NULL);
1650 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
1651 assert (self->pos <= self->bufsz);
1652
1653 if (!sysprof_capture_reader_ensure_space_for (self, sizeof *ma))
1654 return NULL;
1655
1656 ma = (SysprofCaptureAllocation *)(void *)&self->buf[self->pos];
1657
1658 sysprof_capture_reader_bswap_frame (self, &ma->frame);
1659
1660 if (ma->frame.type != SYSPROF_CAPTURE_FRAME_ALLOCATION)
1661 return NULL;
1662
1663 if (ma->frame.len < sizeof *ma)
1664 return NULL;
1665
1666 if (self->endian != __BYTE_ORDER)
1667 {
1668 ma->n_addrs = bswap_16 (ma->n_addrs);
1669 ma->alloc_size = bswap_64 (ma->alloc_size);
1670 ma->alloc_addr = bswap_64 (ma->alloc_addr);
1671 ma->tid = bswap_32 (ma->tid);
1672 }
1673
1674 if (ma->frame.len < (sizeof *ma + (sizeof(SysprofCaptureAddress) * ma->n_addrs)))
1675 return NULL;
1676
1677 if (!sysprof_capture_reader_ensure_space_for (self, ma->frame.len))
1678 return NULL;
1679
1680 ma = (SysprofCaptureAllocation *)(void *)&self->buf[self->pos];
1681
1682 if (SYSPROF_UNLIKELY (self->endian != __BYTE_ORDER))
1683 {
1684 for (unsigned int i = 0; i < ma->n_addrs; i++)
1685 ma->addrs[i] = bswap_64 (ma->addrs[i]);
1686 }
1687
1688 self->pos += ma->frame.len;
1689
1690 return ma;
1691 }
1692
1693 typedef struct {
1694 const SysprofCaptureJitmap *jitmap;
1695 const uint8_t *buf;
1696 unsigned int i;
1697
1698 void *padding1;
1699 void *padding2;
1700 } RealSysprofCaptureJitmapIter;
1701
1702 void
1703 sysprof_capture_jitmap_iter_init (SysprofCaptureJitmapIter *iter,
1704 const SysprofCaptureJitmap *jitmap)
1705 {
1706 RealSysprofCaptureJitmapIter *real_iter = (RealSysprofCaptureJitmapIter *) iter;
1707
1708 assert (iter != NULL);
1709 assert (jitmap != NULL);
1710
1711 real_iter->jitmap = jitmap;
1712 real_iter->buf = jitmap->data;
1713 real_iter->i = 0;
1714 }
1715
1716 bool
1717 sysprof_capture_jitmap_iter_next (SysprofCaptureJitmapIter *iter,
1718 SysprofCaptureAddress *addr,
1719 const char **name)
1720 {
1721 RealSysprofCaptureJitmapIter *real_iter = (RealSysprofCaptureJitmapIter *) iter;
1722 const char *_name;
1723
1724 assert (iter != NULL);
1725
1726 if (real_iter->i >= real_iter->jitmap->n_jitmaps)
1727 return false;
1728
1729 if (addr != NULL)
1730 memcpy (addr, real_iter->buf, sizeof (*addr));
1731 real_iter->buf += sizeof (*addr);
1732
1733 _name = (const char *) real_iter->buf;
1734 if (name != NULL)
1735 *name = _name;
1736 real_iter->buf += strlen (_name) + 1;
1737
1738 real_iter->i++;
1739
1740 return true;
1741 }