Debian Codegraph @

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

raw
1 /* sysprof-capture-writer.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 #ifdef __APPLE__
61 # include <machine/endian.h>
62 #else
63 # include <endian.h>
64 #endif
65 #include <errno.h>
66 #include <fcntl.h>
67 #include <limits.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <sys/stat.h>
71 #include <sys/types.h>
72 #include <unistd.h>
73
74 #include "sysprof-capture-reader.h"
75 #include "sysprof-capture-util-private.h"
76 #include "sysprof-capture-writer.h"
77 #include "sysprof-macros-internal.h"
78 #include "sysprof-macros.h"
79
80 #define DEFAULT_BUFFER_SIZE (_sysprof_getpagesize() * 64L)
81 #define INVALID_ADDRESS (SYSPROF_UINT64_CONSTANT(0))
82 #define MAX_COUNTERS ((1 << 24) - 1)
83 #define MAX_UNWIND_DEPTH 64
84
85 typedef struct
86 {
87 /* A pinter into the string buffer */
88 const char *str;
89
90 /* The unique address for the string */
91 uint64_t addr;
92 } SysprofCaptureJitmapBucket;
93
94 struct _SysprofCaptureWriter
95 {
96 /*
97 * This is our buffer location for incoming strings. This is used
98 * similarly to GStringChunk except there is only one-page, and after
99 * it fills, we flush to disk.
100 *
101 * This is paired with a closed hash table for deduplication.
102 */
103 char addr_buf[4096*4];
104
105 /* Our hashtable for deduplication. */
106 SysprofCaptureJitmapBucket addr_hash[512];
107
108 /* We keep the large fields above so that our allocation will be page
109 * alinged for the write buffer. This improves the performance of large
110 * writes to the target file-descriptor.
111 */
112 volatile int ref_count;
113
114 /*
115 * Our address sequence counter. The value that comes from
116 * monotonically increasing this is OR'd with JITMAP_MARK to denote
117 * the address name should come from the JIT map.
118 */
119 size_t addr_seq;
120
121 /* Our position in addr_buf. */
122 size_t addr_buf_pos;
123
124 /*
125 * The number of hash table items in @addr_hash. This is an
126 * optimization so that we can avoid calculating the number of strings
127 * when flushing out the jitmap.
128 */
129 unsigned int addr_hash_size;
130
131 /* Capture file handle */
132 int fd;
133
134 /* Our write buffer for fd */
135 uint8_t *buf;
136 size_t pos;
137 size_t len;
138
139 /* counter id sequence */
140 int next_counter_id;
141
142 /* Statistics while recording */
143 SysprofCaptureStat stat;
144 };
145
146 static inline void
147 sysprof_capture_writer_frame_init (SysprofCaptureFrame *frame_,
148 int len,
149 int cpu,
150 int32_t pid,
151 int64_t time_,
152 SysprofCaptureFrameType type)
153 {
154 assert (frame_ != NULL);
155
156 frame_->len = len;
157 frame_->cpu = cpu;
158 frame_->pid = pid;
159 frame_->time = time_;
160 frame_->type = type;
161 frame_->padding1 = 0;
162 frame_->padding2 = 0;
163 }
164
165 static void
166 sysprof_capture_writer_finalize (SysprofCaptureWriter *self)
167 {
168 if (self != NULL)
169 {
170 sysprof_capture_writer_flush (self);
171
172 if (self->fd != -1)
173 {
174 close (self->fd);
175 self->fd = -1;
176 }
177
178 free (self->buf);
179 free (self);
180 }
181 }
182
183 SysprofCaptureWriter *
184 sysprof_capture_writer_ref (SysprofCaptureWriter *self)
185 {
186 assert (self != NULL);
187 assert (self->ref_count > 0);
188
189 __atomic_fetch_add (&self->ref_count, 1, __ATOMIC_SEQ_CST);
190
191 return self;
192 }
193
194 void
195 sysprof_capture_writer_unref (SysprofCaptureWriter *self)
196 {
197 assert (self != NULL);
198 assert (self->ref_count > 0);
199
200 if (__atomic_fetch_sub (&self->ref_count, 1, __ATOMIC_SEQ_CST) == 1)
201 sysprof_capture_writer_finalize (self);
202 }
203
204 static bool
205 sysprof_capture_writer_flush_data (SysprofCaptureWriter *self)
206 {
207 const uint8_t *buf;
208 ssize_t written;
209 size_t to_write;
210
211 assert (self != NULL);
212 assert (self->pos <= self->len);
213 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
214
215 if (self->pos == 0)
216 return true;
217
218 buf = self->buf;
219 to_write = self->pos;
220
221 while (to_write > 0)
222 {
223 written = _sysprof_write (self->fd, buf, to_write);
224 if (written < 0)
225 return false;
226
227 if (written == 0 && errno != EAGAIN)
228 return false;
229
230 assert (written <= (ssize_t)to_write);
231
232 buf += written;
233 to_write -= written;
234 }
235
236 self->pos = 0;
237
238 return true;
239 }
240
241 static inline void
242 sysprof_capture_writer_realign (size_t *pos)
243 {
244 *pos = (*pos + SYSPROF_CAPTURE_ALIGN - 1) & ~(SYSPROF_CAPTURE_ALIGN - 1);
245 }
246
247 static inline bool
248 sysprof_capture_writer_ensure_space_for (SysprofCaptureWriter *self,
249 size_t len)
250 {
251 /* Check for max frame size */
252 if (len > USHRT_MAX)
253 return false;
254
255 if ((self->len - self->pos) < len)
256 {
257 if (!sysprof_capture_writer_flush_data (self))
258 return false;
259 }
260
261 return true;
262 }
263
264 static inline void *
265 sysprof_capture_writer_allocate (SysprofCaptureWriter *self,
266 size_t *len)
267 {
268 void *p;
269
270 assert (self != NULL);
271 assert (len != NULL);
272 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
273
274 sysprof_capture_writer_realign (len);
275
276 if (!sysprof_capture_writer_ensure_space_for (self, *len))
277 return NULL;
278
279 p = (void *)&self->buf[self->pos];
280
281 self->pos += *len;
282
283 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
284
285 return p;
286 }
287
288 static bool
289 sysprof_capture_writer_flush_jitmap (SysprofCaptureWriter *self)
290 {
291 SysprofCaptureJitmap jitmap;
292 ssize_t r;
293 size_t len;
294
295 assert (self != NULL);
296
297 if (self->addr_hash_size == 0)
298 return true;
299
300 assert (self->addr_buf_pos > 0);
301
302 len = sizeof jitmap + self->addr_buf_pos;
303
304 sysprof_capture_writer_realign (&len);
305
306 sysprof_capture_writer_frame_init (&jitmap.frame,
307 len,
308 -1,
309 _sysprof_getpid (),
310 SYSPROF_CAPTURE_CURRENT_TIME,
311 SYSPROF_CAPTURE_FRAME_JITMAP);
312 jitmap.n_jitmaps = self->addr_hash_size;
313
314 if (sizeof jitmap != _sysprof_write (self->fd, &jitmap, sizeof jitmap))
315 return false;
316
317 r = _sysprof_write (self->fd, self->addr_buf, len - sizeof jitmap);
318 if (r < 0 || (size_t)r != (len - sizeof jitmap))
319 return false;
320
321 self->addr_buf_pos = 0;
322 self->addr_hash_size = 0;
323 memset (self->addr_hash, 0, sizeof self->addr_hash);
324
325 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_JITMAP]++;
326
327 return true;
328 }
329
330 /* djb hash */
331 static unsigned int
332 str_hash (const char *str)
333 {
334 const uint8_t *p;
335 uint32_t h = 5381;
336
337 for (p = (const uint8_t *) str; *p != '\0'; p++)
338 h = (h << 5) + h + *p;
339
340 return h;
341 }
342
343 static bool
344 sysprof_capture_writer_lookup_jitmap (SysprofCaptureWriter *self,
345 const char *name,
346 SysprofCaptureAddress *addr)
347 {
348 unsigned int hash;
349 unsigned int i;
350
351 assert (self != NULL);
352 assert (name != NULL);
353 assert (addr != NULL);
354
355 hash = str_hash (name) % SYSPROF_N_ELEMENTS (self->addr_hash);
356
357 for (i = hash; i < SYSPROF_N_ELEMENTS (self->addr_hash); i++)
358 {
359 SysprofCaptureJitmapBucket *bucket = &self->addr_hash[i];
360
361 if (bucket->str == NULL)
362 return false;
363
364 if (strcmp (bucket->str, name) == 0)
365 {
366 *addr = bucket->addr;
367 return true;
368 }
369 }
370
371 for (i = 0; i < hash; i++)
372 {
373 SysprofCaptureJitmapBucket *bucket = &self->addr_hash[i];
374
375 if (bucket->str == NULL)
376 return false;
377
378 if (strcmp (bucket->str, name) == 0)
379 {
380 *addr = bucket->addr;
381 return true;
382 }
383 }
384
385 return false;
386 }
387
388 static SysprofCaptureAddress
389 sysprof_capture_writer_insert_jitmap (SysprofCaptureWriter *self,
390 const char *str)
391 {
392 SysprofCaptureAddress addr;
393 char *dst;
394 size_t len;
395 unsigned int hash;
396 unsigned int i;
397
398 assert (self != NULL);
399 assert (str != NULL);
400 assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
401
402 len = sizeof addr + strlen (str) + 1;
403
404 if ((self->addr_hash_size == SYSPROF_N_ELEMENTS (self->addr_hash)) ||
405 ((sizeof self->addr_buf - self->addr_buf_pos) < len))
406 {
407 if (!sysprof_capture_writer_flush_jitmap (self))
408 return INVALID_ADDRESS;
409
410 assert (self->addr_hash_size == 0);
411 assert (self->addr_buf_pos == 0);
412 }
413
414 assert (self->addr_hash_size < SYSPROF_N_ELEMENTS (self->addr_hash));
415 assert (len > sizeof addr);
416
417 /* Allocate the next unique address */
418 addr = SYSPROF_CAPTURE_JITMAP_MARK | ++self->addr_seq;
419
420 /* Copy the address into the buffer */
421 dst = (char *)&self->addr_buf[self->addr_buf_pos];
422 memcpy (dst, &addr, sizeof addr);
423
424 /*
425 * Copy the string into the buffer, keeping dst around for
426 * when we insert into the hashtable.
427 */
428 dst += sizeof addr;
429 memcpy (dst, str, len - sizeof addr);
430
431 /* Advance our string cache position */
432 self->addr_buf_pos += len;
433 assert (self->addr_buf_pos <= sizeof self->addr_buf);
434
435 /* Now place the address into the hashtable */
436 hash = str_hash (str) % SYSPROF_N_ELEMENTS (self->addr_hash);
437
438 /* Start from the current hash bucket and go forward */
439 for (i = hash; i < SYSPROF_N_ELEMENTS (self->addr_hash); i++)
440 {
441 SysprofCaptureJitmapBucket *bucket = &self->addr_hash[i];
442
443 if (SYSPROF_LIKELY (bucket->str == NULL))
444 {
445 bucket->str = dst;
446 bucket->addr = addr;
447 self->addr_hash_size++;
448 return addr;
449 }
450 }
451
452 /* Wrap around to the beginning */
453 for (i = 0; i < hash; i++)
454 {
455 SysprofCaptureJitmapBucket *bucket = &self->addr_hash[i];
456
457 if (SYSPROF_LIKELY (bucket->str == NULL))
458 {
459 bucket->str = dst;
460 bucket->addr = addr;
461 self->addr_hash_size++;
462 return addr;
463 }
464 }
465
466 sysprof_assert_not_reached ();
467
468 return INVALID_ADDRESS;
469 }
470
471 SysprofCaptureWriter *
472 sysprof_capture_writer_new_from_fd (int fd,
473 size_t buffer_size)
474 {
475 time_t now;
476 char now_str[sizeof ("2020-06-30T14:34:00Z")];
477 SysprofCaptureWriter *self;
478 SysprofCaptureFileHeader *header;
479 size_t header_len = sizeof(*header);
480
481 if (fd < 0)
482 return NULL;
483
484 if (buffer_size == 0)
485 buffer_size = DEFAULT_BUFFER_SIZE;
486
487 assert (fd != -1);
488 assert (buffer_size % _sysprof_getpagesize() == 0);
489
490 /* This is only useful on files, memfd, etc */
491 if (ftruncate (fd, 0) != 0) { /* Do Nothing */ }
492
493 self = sysprof_malloc0 (sizeof (SysprofCaptureWriter));
494 if (self == NULL)
495 return NULL;
496
497 self->ref_count = 1;
498 self->fd = fd;
499 self->buf = (uint8_t *) sysprof_malloc0 (buffer_size);
500 if (self->buf == NULL)
501 {
502 free (self);
503 return NULL;
504 }
505 self->len = buffer_size;
506 self->next_counter_id = 1;
507
508 /* Format the time as ISO 8601, in UTC */
509 time (&now);
510 if (strftime (now_str, sizeof (now_str), "%FT%TZ", gmtime (&now)) == 0)
511 {
512 free (self->buf);
513 free (self);
514 return NULL;
515 }
516
517 header = sysprof_capture_writer_allocate (self, &header_len);
518
519 if (header == NULL)
520 {
521 sysprof_capture_writer_finalize (self);
522 return NULL;
523 }
524
525 header->magic = SYSPROF_CAPTURE_MAGIC;
526 header->version = 1;
527 #if __BYTE_ORDER == __LITTLE_ENDIAN
528 header->little_endian = true;
529 #else
530 header->little_endian = false;
531 #endif
532 header->padding = 0;
533 _sysprof_strlcpy (header->capture_time, now_str, sizeof header->capture_time);
534 header->time = SYSPROF_CAPTURE_CURRENT_TIME;
535 header->end_time = 0;
536 memset (header->suffix, 0, sizeof header->suffix);
537
538 if (!sysprof_capture_writer_flush_data (self))
539 {
540 sysprof_capture_writer_finalize (self);
541 return NULL;
542 }
543
544 assert (self->pos == 0);
545 assert (self->len > 0);
546 assert (self->len % _sysprof_getpagesize() == 0);
547 assert (self->buf != NULL);
548 assert (self->addr_hash_size == 0);
549 assert (self->fd != -1);
550
551 return self;
552 }
553
554 SysprofCaptureWriter *
555 sysprof_capture_writer_new (const char *filename,
556 size_t buffer_size)
557 {
558 SysprofCaptureWriter *self;
559 int fd;
560
561 assert (filename != NULL);
562 assert (buffer_size % _sysprof_getpagesize() == 0);
563
564 if ((-1 == (fd = open (filename, O_CREAT | O_RDWR, 0640))) ||
565 (-1 == ftruncate (fd, 0L)))
566 return NULL;
567
568 self = sysprof_capture_writer_new_from_fd (fd, buffer_size);
569
570 if (self == NULL)
571 close (fd);
572
573 return self;
574 }
575
576 bool
577 sysprof_capture_writer_add_map (SysprofCaptureWriter *self,
578 int64_t time,
579 int cpu,
580 int32_t pid,
581 uint64_t start,
582 uint64_t end,
583 uint64_t offset,
584 uint64_t inode,
585 const char *filename)
586 {
587 SysprofCaptureMap *ev;
588 size_t len;
589
590 if (filename == NULL)
591 filename = "";
592
593 assert (self != NULL);
594 assert (filename != NULL);
595
596 len = sizeof *ev + strlen (filename) + 1;
597
598 ev = (SysprofCaptureMap *)sysprof_capture_writer_allocate (self, &len);
599 if (!ev)
600 return false;
601
602 sysprof_capture_writer_frame_init (&ev->frame,
603 len,
604 cpu,
605 pid,
606 time,
607 SYSPROF_CAPTURE_FRAME_MAP);
608 ev->start = start;
609 ev->end = end;
610 ev->offset = offset;
611 ev->inode = inode;
612
613 _sysprof_strlcpy (ev->filename, filename, len - sizeof *ev);
614 ev->filename[len - sizeof *ev - 1] = '\0';
615
616 ((char*)ev)[len-1] = 0;
617
618 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_MAP]++;
619
620 return true;
621 }
622
623 bool
624 sysprof_capture_writer_add_map_with_build_id (SysprofCaptureWriter *self,
625 int64_t time,
626 int cpu,
627 int32_t pid,
628 uint64_t start,
629 uint64_t end,
630 uint64_t offset,
631 uint64_t inode,
632 const char *filename,
633 const char *build_id)
634 {
635 SysprofCaptureMap *ev;
636 size_t len;
637 size_t filename_len;
638 size_t build_id_len;
639
640 if (filename == NULL)
641 filename = "";
642
643 if (build_id == NULL)
644 build_id = "";
645
646 assert (self != NULL);
647 assert (filename != NULL);
648 assert (build_id != NULL);
649
650 filename_len = strlen (filename) + 1;
651 build_id_len = strlen (build_id) + 1;
652
653 len = sizeof *ev + filename_len + strlen("@") + build_id_len;
654
655 ev = (SysprofCaptureMap *)sysprof_capture_writer_allocate (self, &len);
656 if (!ev)
657 return false;
658
659 sysprof_capture_writer_frame_init (&ev->frame,
660 len,
661 cpu,
662 pid,
663 time,
664 SYSPROF_CAPTURE_FRAME_MAP);
665 ev->start = start;
666 ev->end = end;
667 ev->offset = offset;
668 ev->inode = inode;
669
670 _sysprof_strlcpy (ev->filename, filename, filename_len);
671 ev->filename[filename_len] = '@';
672 _sysprof_strlcpy (&ev->filename[filename_len+1], build_id, build_id_len);
673
674 ((char*)ev)[len-1] = 0;
675
676 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_MAP]++;
677
678 return true;
679 }
680
681 bool
682 sysprof_capture_writer_add_mark (SysprofCaptureWriter *self,
683 int64_t time,
684 int cpu,
685 int32_t pid,
686 uint64_t duration,
687 const char *group,
688 const char *name,
689 const char *message)
690 {
691 SysprofCaptureMark *ev;
692 size_t message_len;
693 size_t len;
694
695 assert (self != NULL);
696 assert (name != NULL);
697 assert (group != NULL);
698
699 if (message == NULL)
700 message = "";
701 message_len = strlen (message) + 1;
702
703 len = sizeof *ev + message_len;
704 ev = (SysprofCaptureMark *)sysprof_capture_writer_allocate (self, &len);
705 if (!ev)
706 return false;
707
708 sysprof_capture_writer_frame_init (&ev->frame,
709 len,
710 cpu,
711 pid,
712 time,
713 SYSPROF_CAPTURE_FRAME_MARK);
714
715 ev->duration = duration;
716 _sysprof_strlcpy (ev->group, group, sizeof ev->group);
717 _sysprof_strlcpy (ev->name, name, sizeof ev->name);
718 memcpy (ev->message, message, message_len);
719
720 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_MARK]++;
721
722 return true;
723 }
724
725 bool
726 sysprof_capture_writer_add_metadata (SysprofCaptureWriter *self,
727 int64_t time,
728 int cpu,
729 int32_t pid,
730 const char *id,
731 const char *metadata,
732 ssize_t metadata_len)
733 {
734 SysprofCaptureMetadata *ev;
735 size_t len;
736
737 assert (self != NULL);
738 assert (id != NULL);
739
740 if (metadata == NULL)
741 {
742 metadata = "";
743 len = 0;
744 }
745
746 if (metadata_len < 0)
747 metadata_len = strlen (metadata);
748
749 len = sizeof *ev + metadata_len + 1;
750 ev = (SysprofCaptureMetadata *)sysprof_capture_writer_allocate (self, &len);
751 if (!ev)
752 return false;
753
754 sysprof_capture_writer_frame_init (&ev->frame,
755 len,
756 cpu,
757 pid,
758 time,
759 SYSPROF_CAPTURE_FRAME_METADATA);
760
761 _sysprof_strlcpy (ev->id, id, sizeof ev->id);
762 memcpy (ev->metadata, metadata, metadata_len);
763 ev->metadata[metadata_len] = 0;
764
765 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_METADATA]++;
766
767 return true;
768 }
769
770 bool
771 sysprof_capture_writer_add_dbus_message (SysprofCaptureWriter *self,
772 int64_t time,
773 int cpu,
774 int32_t pid,
775 uint16_t bus_type,
776 uint16_t flags,
777 const uint8_t *message_data,
778 size_t message_len)
779 {
780 SysprofCaptureDBusMessage *ev;
781 size_t len;
782
783 assert (self != NULL);
784 assert (message_data != NULL || message_len == 0);
785
786 if (message_len > (1<<16) - sizeof *ev - 16)
787 {
788 flags |= SYSPROF_CAPTURE_DBUS_FLAGS_MESSAGE_TOO_LARGE;
789 message_data = NULL;
790 message_len = 0;
791 }
792
793 len = sizeof *ev + message_len;
794
795 ev = (SysprofCaptureDBusMessage *)sysprof_capture_writer_allocate (self, &len);
796 if (!ev)
797 return false;
798
799 sysprof_capture_writer_frame_init (&ev->frame,
800 len,
801 cpu,
802 pid,
803 time,
804 SYSPROF_CAPTURE_FRAME_DBUS_MESSAGE);
805
806 ev->bus_type = bus_type;
807 ev->flags = flags;
808 ev->message_len = message_len;
809 memcpy (ev->message, message_data, message_len);
810
811 return true;
812 }
813
814 SysprofCaptureAddress
815 sysprof_capture_writer_add_jitmap (SysprofCaptureWriter *self,
816 const char *name)
817 {
818 SysprofCaptureAddress addr = INVALID_ADDRESS;
819
820 if (name == NULL)
821 name = "";
822
823 assert (self != NULL);
824 assert (name != NULL);
825
826 if (!sysprof_capture_writer_lookup_jitmap (self, name, &addr))
827 addr = sysprof_capture_writer_insert_jitmap (self, name);
828
829 return addr;
830 }
831
832 bool
833 sysprof_capture_writer_add_process (SysprofCaptureWriter *self,
834 int64_t time,
835 int cpu,
836 int32_t pid,
837 const char *cmdline)
838 {
839 SysprofCaptureProcess *ev;
840 size_t len;
841
842 if (cmdline == NULL)
843 cmdline = "";
844
845 assert (self != NULL);
846 assert (cmdline != NULL);
847
848 len = sizeof *ev + strlen (cmdline) + 1;
849
850 ev = (SysprofCaptureProcess *)sysprof_capture_writer_allocate (self, &len);
851 if (!ev)
852 return false;
853
854 sysprof_capture_writer_frame_init (&ev->frame,
855 len,
856 cpu,
857 pid,
858 time,
859 SYSPROF_CAPTURE_FRAME_PROCESS);
860
861 _sysprof_strlcpy (ev->cmdline, cmdline, len - sizeof *ev);
862 ev->cmdline[len - sizeof *ev - 1] = '\0';
863
864 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_PROCESS]++;
865
866 return true;
867 }
868
869 bool
870 sysprof_capture_writer_add_sample (SysprofCaptureWriter *self,
871 int64_t time,
872 int cpu,
873 int32_t pid,
874 int32_t tid,
875 const SysprofCaptureAddress *addrs,
876 unsigned int n_addrs)
877 {
878 SysprofCaptureSample *ev;
879 size_t len;
880
881 assert (self != NULL);
882
883 len = sizeof *ev + (n_addrs * sizeof (SysprofCaptureAddress));
884
885 ev = (SysprofCaptureSample *)sysprof_capture_writer_allocate (self, &len);
886 if (!ev)
887 return false;
888
889 sysprof_capture_writer_frame_init (&ev->frame,
890 len,
891 cpu,
892 pid,
893 time,
894 SYSPROF_CAPTURE_FRAME_SAMPLE);
895 ev->n_addrs = n_addrs;
896 ev->tid = tid;
897
898 memcpy (ev->addrs, addrs, (n_addrs * sizeof (SysprofCaptureAddress)));
899
900 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_SAMPLE]++;
901
902 return true;
903 }
904
905 bool
906 sysprof_capture_writer_add_trace (SysprofCaptureWriter *self,
907 int64_t time,
908 int cpu,
909 int32_t pid,
910 int32_t tid,
911 const SysprofCaptureAddress *addrs,
912 unsigned int n_addrs,
913 bool entering)
914 {
915 SysprofCaptureTrace *ev;
916 size_t len;
917
918 assert (self != NULL);
919
920 len = sizeof *ev + (n_addrs * sizeof (SysprofCaptureAddress));
921
922 ev = (SysprofCaptureTrace *)sysprof_capture_writer_allocate (self, &len);
923 if (!ev)
924 return false;
925
926 sysprof_capture_writer_frame_init (&ev->frame,
927 len,
928 cpu,
929 pid,
930 time,
931 SYSPROF_CAPTURE_FRAME_SAMPLE);
932 ev->n_addrs = n_addrs;
933 ev->tid = tid;
934 ev->entering = !!entering;
935
936 memcpy (ev->addrs, addrs, (n_addrs * sizeof (SysprofCaptureAddress)));
937
938 return true;
939 }
940
941 bool
942 sysprof_capture_writer_add_fork (SysprofCaptureWriter *self,
943 int64_t time,
944 int cpu,
945 int32_t pid,
946 int32_t child_pid)
947 {
948 SysprofCaptureFork *ev;
949 size_t len = sizeof *ev;
950
951 assert (self != NULL);
952
953 ev = (SysprofCaptureFork *)sysprof_capture_writer_allocate (self, &len);
954 if (!ev)
955 return false;
956
957 sysprof_capture_writer_frame_init (&ev->frame,
958 len,
959 cpu,
960 pid,
961 time,
962 SYSPROF_CAPTURE_FRAME_FORK);
963 ev->child_pid = child_pid;
964
965 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_FORK]++;
966
967 return true;
968 }
969
970 bool
971 sysprof_capture_writer_add_overlay (SysprofCaptureWriter *self,
972 int64_t time,
973 int cpu,
974 int32_t pid,
975 uint32_t layer,
976 const char *src,
977 const char *dst)
978 {
979 SysprofCaptureOverlay *ev;
980 size_t srclen = strlen (src);
981 size_t dstlen = strlen (dst);
982 size_t len = sizeof *ev + srclen + 1 + dstlen + 1;
983
984 assert (self != NULL);
985 assert (src != NULL);
986 assert (dst != NULL);
987
988 if (srclen > INT16_MAX || dstlen > INT16_MAX)
989 return false;
990
991 ev = (SysprofCaptureOverlay *)sysprof_capture_writer_allocate (self, &len);
992 if (!ev)
993 return false;
994
995 sysprof_capture_writer_frame_init (&ev->frame,
996 len,
997 cpu,
998 pid,
999 time,
1000 SYSPROF_CAPTURE_FRAME_OVERLAY);
1001
1002 ev->layer = layer;
1003 ev->src_len = srclen;
1004 ev->dst_len = dstlen;
1005
1006 memcpy (&ev->data[0], src, srclen);
1007 memcpy (&ev->data[srclen+1], dst, dstlen);
1008
1009 ev->data[srclen] = 0;
1010 ev->data[srclen+1+dstlen] = 0;
1011
1012 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_OVERLAY]++;
1013
1014 return true;
1015 }
1016
1017 bool
1018 sysprof_capture_writer_add_exit (SysprofCaptureWriter *self,
1019 int64_t time,
1020 int cpu,
1021 int32_t pid)
1022 {
1023 SysprofCaptureExit *ev;
1024 size_t len = sizeof *ev;
1025
1026 assert (self != NULL);
1027
1028 ev = (SysprofCaptureExit *)sysprof_capture_writer_allocate (self, &len);
1029 if (!ev)
1030 return false;
1031
1032 sysprof_capture_writer_frame_init (&ev->frame,
1033 len,
1034 cpu,
1035 pid,
1036 time,
1037 SYSPROF_CAPTURE_FRAME_EXIT);
1038
1039 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_EXIT]++;
1040
1041 return true;
1042 }
1043
1044 bool
1045 sysprof_capture_writer_add_timestamp (SysprofCaptureWriter *self,
1046 int64_t time,
1047 int cpu,
1048 int32_t pid)
1049 {
1050 SysprofCaptureTimestamp *ev;
1051 size_t len = sizeof *ev;
1052
1053 assert (self != NULL);
1054
1055 ev = (SysprofCaptureTimestamp *)sysprof_capture_writer_allocate (self, &len);
1056 if (!ev)
1057 return false;
1058
1059 sysprof_capture_writer_frame_init (&ev->frame,
1060 len,
1061 cpu,
1062 pid,
1063 time,
1064 SYSPROF_CAPTURE_FRAME_TIMESTAMP);
1065
1066 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_TIMESTAMP]++;
1067
1068 return true;
1069 }
1070
1071 static bool
1072 sysprof_capture_writer_flush_end_time (SysprofCaptureWriter *self)
1073 {
1074 int64_t end_time = SYSPROF_CAPTURE_CURRENT_TIME;
1075 ssize_t ret;
1076
1077 assert (self != NULL);
1078
1079 /* This field is opportunistic, so a failure is okay. */
1080
1081 again:
1082 ret = _sysprof_pwrite (self->fd,
1083 &end_time,
1084 sizeof (end_time),
1085 offsetof (SysprofCaptureFileHeader, end_time));
1086
1087 if (ret < 0 && errno == EAGAIN)
1088 goto again;
1089
1090 return true;
1091 }
1092
1093 bool
1094 sysprof_capture_writer_flush (SysprofCaptureWriter *self)
1095 {
1096 assert (self != NULL);
1097
1098 return sysprof_capture_writer_flush_jitmap (self) &&
1099 sysprof_capture_writer_flush_data (self) &&
1100 sysprof_capture_writer_flush_end_time (self);
1101 }
1102
1103 /**
1104 * sysprof_capture_writer_save_as:
1105 * @self: A #SysprofCaptureWriter
1106 * @filename: the file to save the capture as
1107 *
1108 * Saves the captured data as the file @filename.
1109 *
1110 * This is primarily useful if the writer was created with a memory-backed
1111 * file-descriptor such as a memfd or tmpfs file on Linux.
1112 *
1113 * `errno` is set on error, to any of the errors returned by `open()`,
1114 * sysprof_capture_writer_flush(), `lseek()` or `sendfile()`.
1115 *
1116 * Returns: %TRUE if successful, otherwise %FALSE and `errno` is set.
1117 */
1118 bool
1119 sysprof_capture_writer_save_as (SysprofCaptureWriter *self,
1120 const char *filename)
1121 {
1122 size_t to_write;
1123 off_t in_off;
1124 off_t pos;
1125 int fd = -1;
1126 int errsv;
1127
1128 assert (self != NULL);
1129 assert (self->fd != -1);
1130 assert (filename != NULL);
1131
1132 if (-1 == (fd = open (filename, O_CREAT | O_RDWR, 0640)))
1133 goto handle_errno;
1134
1135 if (!sysprof_capture_writer_flush (self))
1136 goto handle_errno;
1137
1138 if (-1 == (pos = lseek (self->fd, 0L, SEEK_CUR)))
1139 goto handle_errno;
1140
1141 to_write = pos;
1142 in_off = 0;
1143
1144 while (to_write > 0)
1145 {
1146 ssize_t written;
1147
1148 written = _sysprof_sendfile (fd, self->fd, &in_off, pos);
1149
1150 if (written < 0)
1151 goto handle_errno;
1152
1153 if (written == 0 && errno != EAGAIN)
1154 goto handle_errno;
1155
1156 assert (written <= (ssize_t)to_write);
1157
1158 to_write -= written;
1159 }
1160
1161 close (fd);
1162
1163 return true;
1164
1165 handle_errno:
1166 errsv = errno;
1167
1168 if (fd != -1)
1169 {
1170 close (fd);
1171 unlink (filename);
1172 }
1173
1174 errno = errsv;
1175
1176 return false;
1177 }
1178
1179 /**
1180 * _sysprof_capture_writer_splice_from_fd:
1181 * @self: An #SysprofCaptureWriter
1182 * @fd: the fd to read from.
1183 *
1184 * This is internal API for SysprofCaptureWriter and SysprofCaptureReader to
1185 * communicate when splicing a reader into a writer.
1186 *
1187 * This should not be used outside of #SysprofCaptureReader or
1188 * #SysprofCaptureWriter.
1189 *
1190 * This will not advance the position of @fd.
1191 *
1192 * `errno` is set on error, to any of the errors returned by `fstat()` or
1193 * `sendfile()`, or `EBADMSG` if the file is corrupt.
1194 *
1195 * Returns: %TRUE if successful; otherwise %FALSE and `errno` is set.
1196 */
1197 bool
1198 _sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter *self,
1199 int fd)
1200 {
1201 struct stat stbuf;
1202 off_t in_off;
1203 size_t to_write;
1204
1205 assert (self != NULL);
1206 assert (self->fd != -1);
1207
1208 if (-1 == fstat (fd, &stbuf))
1209 goto handle_errno;
1210
1211 if (stbuf.st_size < 256)
1212 {
1213 errno = EBADMSG;
1214 return false;
1215 }
1216
1217 in_off = 256;
1218 to_write = stbuf.st_size - in_off;
1219
1220 while (to_write > 0)
1221 {
1222 ssize_t written;
1223
1224 written = _sysprof_sendfile (self->fd, fd, &in_off, to_write);
1225
1226 if (written < 0)
1227 goto handle_errno;
1228
1229 if (written == 0 && errno != EAGAIN)
1230 goto handle_errno;
1231
1232 assert (written <= (ssize_t)to_write);
1233
1234 to_write -= written;
1235 }
1236
1237 return true;
1238
1239 handle_errno:
1240 /* errno is propagated */
1241 return false;
1242 }
1243
1244 /**
1245 * sysprof_capture_writer_splice:
1246 * @self: An #SysprofCaptureWriter
1247 * @dest: An #SysprofCaptureWriter
1248 *
1249 * This function will copy the capture @self into the capture @dest. This
1250 * tries to be semi-efficient by using sendfile() to copy the contents between
1251 * the captures. @self and @dest will be flushed before the contents are copied
1252 * into the @dest file-descriptor.
1253 *
1254 * `errno` is set on error, to any of the errors returned by
1255 * sysprof_capture_writer_flush(), `lseek()` or
1256 * _sysprof_capture_writer_splice_from_fd().
1257 *
1258 * Returns: %TRUE if successful, otherwise %FALSE and and `errno` is set.
1259 */
1260 bool
1261 sysprof_capture_writer_splice (SysprofCaptureWriter *self,
1262 SysprofCaptureWriter *dest)
1263 {
1264 bool ret;
1265 off_t pos;
1266 int errsv;
1267
1268 assert (self != NULL);
1269 assert (self->fd != -1);
1270 assert (dest != NULL);
1271 assert (dest->fd != -1);
1272
1273 /* Flush before writing anything to ensure consistency */
1274 if (!sysprof_capture_writer_flush (self) || !sysprof_capture_writer_flush (dest))
1275 goto handle_errno;
1276
1277 /* Track our current position so we can reset */
1278 if ((off_t)-1 == (pos = lseek (self->fd, 0L, SEEK_CUR)))
1279 goto handle_errno;
1280
1281 /* Perform the splice */
1282 ret = _sysprof_capture_writer_splice_from_fd (dest, self->fd);
1283 errsv = errno;
1284
1285 /* Now reset or file-descriptor position (it should be the same */
1286 if (pos != lseek (self->fd, pos, SEEK_SET))
1287 goto handle_errno;
1288
1289 if (!ret)
1290 errno = errsv;
1291 return ret;
1292
1293 handle_errno:
1294 /* errno is propagated */
1295 return false;
1296 }
1297
1298 /**
1299 * sysprof_capture_writer_create_reader:
1300 * @self: A #SysprofCaptureWriter
1301 *
1302 * Creates a new reader for the writer.
1303 *
1304 * Since readers use positioned reads, this uses the same file-descriptor for
1305 * the #SysprofCaptureReader. Therefore, if you are writing to the capture while
1306 * also consuming from the reader, you could get transient failures unless you
1307 * synchronize the operations.
1308 *
1309 * `errno` is set on error, to any of the errors returned by
1310 * sysprof_capture_writer_flush(), `dup()` or
1311 * sysprof_capture_reader_new_from_fd().
1312 *
1313 * Returns: (transfer full): A #SysprofCaptureReader.
1314 */
1315 SysprofCaptureReader *
1316 sysprof_capture_writer_create_reader (SysprofCaptureWriter *self)
1317 {
1318 SysprofCaptureReader *ret;
1319 int copy;
1320
1321 assert (self != NULL);
1322 assert (self->fd != -1);
1323
1324 if (!sysprof_capture_writer_flush (self))
1325 {
1326 /* errno is propagated */
1327 return NULL;
1328 }
1329
1330 /*
1331 * We don't care about the write position, since the reader
1332 * uses positioned reads.
1333 */
1334 if (-1 == (copy = dup (self->fd)))
1335 {
1336 /* errno is propagated */
1337 return NULL;
1338 }
1339
1340 if (!(ret = sysprof_capture_reader_new_from_fd (copy)))
1341 {
1342 /* errno is propagated */
1343 return NULL;
1344 }
1345
1346 sysprof_capture_reader_set_stat (ret, &self->stat);
1347
1348 return sysprof_steal_pointer (&ret);
1349 }
1350
1351 /**
1352 * sysprof_capture_writer_stat:
1353 * @self: A #SysprofCaptureWriter
1354 * @stat: (out): A location for an #SysprofCaptureStat
1355 *
1356 * This function will fill @stat with statistics generated while capturing
1357 * the profiler session.
1358 */
1359 void
1360 sysprof_capture_writer_stat (SysprofCaptureWriter *self,
1361 SysprofCaptureStat *stat)
1362 {
1363 assert (self != NULL);
1364 assert (stat != NULL);
1365
1366 *stat = self->stat;
1367 }
1368
1369 bool
1370 sysprof_capture_writer_define_counters (SysprofCaptureWriter *self,
1371 int64_t time,
1372 int cpu,
1373 int32_t pid,
1374 const SysprofCaptureCounter *counters,
1375 unsigned int n_counters)
1376 {
1377 SysprofCaptureCounterDefine *def;
1378 size_t len;
1379 unsigned int i;
1380
1381 assert (self != NULL);
1382 assert (counters != NULL);
1383
1384 if (n_counters == 0)
1385 return true;
1386
1387 len = sizeof *def + (sizeof *counters * n_counters);
1388
1389 def = (SysprofCaptureCounterDefine *)sysprof_capture_writer_allocate (self, &len);
1390 if (!def)
1391 return false;
1392
1393 sysprof_capture_writer_frame_init (&def->frame,
1394 len,
1395 cpu,
1396 pid,
1397 time,
1398 SYSPROF_CAPTURE_FRAME_CTRDEF);
1399 def->padding1 = 0;
1400 def->padding2 = 0;
1401 def->n_counters = n_counters;
1402
1403 for (i = 0; i < n_counters; i++)
1404 {
1405 /* Has the counter been registered? */
1406 assert (counters[i].id < self->next_counter_id);
1407 def->counters[i] = counters[i];
1408 }
1409
1410 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_CTRDEF]++;
1411
1412 return true;
1413 }
1414
1415 bool
1416 sysprof_capture_writer_set_counters (SysprofCaptureWriter *self,
1417 int64_t time,
1418 int cpu,
1419 int32_t pid,
1420 const unsigned int *counters_ids,
1421 const SysprofCaptureCounterValue *values,
1422 unsigned int n_counters)
1423 {
1424 SysprofCaptureCounterSet *set;
1425 size_t len;
1426 unsigned int n_groups;
1427 unsigned int group;
1428 unsigned int field;
1429 unsigned int i;
1430
1431 assert (self != NULL);
1432 assert (counters_ids != NULL || n_counters == 0);
1433 assert (values != NULL || !n_counters);
1434
1435 if (n_counters == 0)
1436 return true;
1437
1438 /* Determine how many value groups we need */
1439 n_groups = n_counters / SYSPROF_N_ELEMENTS (set->values[0].values);
1440 if ((n_groups * SYSPROF_N_ELEMENTS (set->values[0].values)) != n_counters)
1441 n_groups++;
1442
1443 len = sizeof *set + (n_groups * sizeof (SysprofCaptureCounterValues));
1444
1445 set = (SysprofCaptureCounterSet *)sysprof_capture_writer_allocate (self, &len);
1446 if (!set)
1447 return false;
1448
1449 memset (set, 0, len);
1450
1451 sysprof_capture_writer_frame_init (&set->frame,
1452 len,
1453 cpu,
1454 pid,
1455 time,
1456 SYSPROF_CAPTURE_FRAME_CTRSET);
1457 set->padding1 = 0;
1458 set->padding2 = 0;
1459 set->n_values = n_groups;
1460
1461 for (i = 0, group = 0, field = 0; i < n_counters; i++)
1462 {
1463 set->values[group].ids[field] = counters_ids[i];
1464 set->values[group].values[field] = values[i];
1465
1466 field++;
1467
1468 if (field == SYSPROF_N_ELEMENTS (set->values[0].values))
1469 {
1470 field = 0;
1471 group++;
1472 }
1473 }
1474
1475 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_CTRSET]++;
1476
1477 return true;
1478 }
1479
1480 /**
1481 * sysprof_capture_writer_request_counter:
1482 *
1483 * This requests a series of counter identifiers for the capture.
1484 *
1485 * The returning number is always greater than zero. The resulting counter
1486 * values are monotonic starting from the resulting value.
1487 *
1488 * For example, if you are returned 5, and requested 3 counters, the counter
1489 * ids you should use are 5, 6, and 7.
1490 *
1491 * Returns: The next series of counter values or zero on failure.
1492 */
1493 unsigned int
1494 sysprof_capture_writer_request_counter (SysprofCaptureWriter *self,
1495 unsigned int n_counters)
1496 {
1497 int ret;
1498
1499 assert (self != NULL);
1500
1501 if (MAX_COUNTERS - n_counters < self->next_counter_id)
1502 return 0;
1503
1504 ret = self->next_counter_id;
1505 self->next_counter_id += n_counters;
1506
1507 return ret;
1508 }
1509
1510 bool
1511 _sysprof_capture_writer_set_time_range (SysprofCaptureWriter *self,
1512 int64_t start_time,
1513 int64_t end_time)
1514 {
1515 ssize_t ret;
1516
1517 assert (self != NULL);
1518
1519 do_start:
1520 ret = _sysprof_pwrite (self->fd,
1521 &start_time,
1522 sizeof (start_time),
1523 offsetof (SysprofCaptureFileHeader, time));
1524
1525 if (ret < 0 && errno == EAGAIN)
1526 goto do_start;
1527
1528 do_end:
1529 ret = _sysprof_pwrite (self->fd,
1530 &end_time,
1531 sizeof (end_time),
1532 offsetof (SysprofCaptureFileHeader, end_time));
1533
1534 if (ret < 0 && errno == EAGAIN)
1535 goto do_end;
1536
1537 return true;
1538 }
1539
1540 SysprofCaptureWriter *
1541 sysprof_capture_writer_new_from_env (size_t buffer_size)
1542 {
1543 const char *fdstr;
1544 int fd;
1545
1546 if (!(fdstr = getenv ("SYSPROF_TRACE_FD")))
1547 return NULL;
1548
1549 /* Make sure the clock is initialized */
1550 sysprof_clock_init ();
1551
1552 if (!(fd = atoi (fdstr)))
1553 return NULL;
1554
1555 /* ignore stdin/stdout/stderr */
1556 if (fd < 2)
1557 return NULL;
1558
1559 return sysprof_capture_writer_new_from_fd (dup (fd), buffer_size);
1560 }
1561
1562 size_t
1563 sysprof_capture_writer_get_buffer_size (SysprofCaptureWriter *self)
1564 {
1565 assert (self != NULL);
1566
1567 return self->len;
1568 }
1569
1570 bool
1571 sysprof_capture_writer_add_log (SysprofCaptureWriter *self,
1572 int64_t time,
1573 int cpu,
1574 int32_t pid,
1575 int severity,
1576 const char *domain,
1577 const char *message)
1578 {
1579 SysprofCaptureLog *ev;
1580 size_t message_len;
1581 size_t len;
1582
1583 assert (self != NULL);
1584
1585 if (domain == NULL)
1586 domain = "";
1587
1588 if (message == NULL)
1589 message = "";
1590 message_len = strlen (message) + 1;
1591
1592 len = sizeof *ev + message_len;
1593 ev = (SysprofCaptureLog *)sysprof_capture_writer_allocate (self, &len);
1594 if (!ev)
1595 return false;
1596
1597 sysprof_capture_writer_frame_init (&ev->frame,
1598 len,
1599 cpu,
1600 pid,
1601 time,
1602 SYSPROF_CAPTURE_FRAME_LOG);
1603
1604 ev->severity = severity & 0xFFFF;
1605 ev->padding1 = 0;
1606 ev->padding2 = 0;
1607 _sysprof_strlcpy (ev->domain, domain, sizeof ev->domain);
1608 memcpy (ev->message, message, message_len);
1609
1610 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_LOG]++;
1611
1612 return true;
1613 }
1614
1615 bool
1616 sysprof_capture_writer_add_file (SysprofCaptureWriter *self,
1617 int64_t time,
1618 int cpu,
1619 int32_t pid,
1620 const char *path,
1621 bool is_last,
1622 const uint8_t *data,
1623 size_t data_len)
1624 {
1625 SysprofCaptureFileChunk *ev;
1626 size_t len;
1627
1628 assert (self != NULL);
1629
1630 len = sizeof *ev + data_len;
1631 ev = (SysprofCaptureFileChunk *)sysprof_capture_writer_allocate (self, &len);
1632 if (!ev)
1633 return false;
1634
1635 sysprof_capture_writer_frame_init (&ev->frame,
1636 len,
1637 cpu,
1638 pid,
1639 time,
1640 SYSPROF_CAPTURE_FRAME_FILE_CHUNK);
1641
1642 ev->padding1 = 0;
1643 ev->is_last = !!is_last;
1644 ev->len = data_len;
1645 _sysprof_strlcpy (ev->path, path, sizeof ev->path);
1646 memcpy (ev->data, data, data_len);
1647
1648 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_FILE_CHUNK]++;
1649
1650 return true;
1651 }
1652
1653 bool
1654 sysprof_capture_writer_add_file_fd (SysprofCaptureWriter *self,
1655 int64_t time,
1656 int cpu,
1657 int32_t pid,
1658 const char *path,
1659 int fd)
1660 {
1661 uint8_t data[(4096*4L) - sizeof(SysprofCaptureFileChunk)];
1662
1663 assert (self != NULL);
1664
1665 for (;;)
1666 {
1667 bool is_last;
1668 ssize_t n_read;
1669
1670 again:
1671 n_read = read (fd, data, sizeof data);
1672 if (n_read < 0 && errno == EAGAIN)
1673 goto again;
1674
1675 is_last = n_read == 0;
1676
1677 if (!sysprof_capture_writer_add_file (self, time, cpu, pid, path, is_last, data, n_read))
1678 return false;
1679
1680 if (is_last)
1681 break;
1682 }
1683
1684 return true;
1685 }
1686
1687 bool
1688 sysprof_capture_writer_add_allocation (SysprofCaptureWriter *self,
1689 int64_t time,
1690 int cpu,
1691 int32_t pid,
1692 int32_t tid,
1693 SysprofCaptureAddress alloc_addr,
1694 int64_t alloc_size,
1695 SysprofBacktraceFunc backtrace_func,
1696 void *backtrace_data)
1697 {
1698 SysprofCaptureAllocation *ev;
1699 size_t len;
1700 int n_addrs;
1701
1702 assert (self != NULL);
1703 assert (backtrace_func != NULL);
1704
1705 len = sizeof *ev + (MAX_UNWIND_DEPTH * sizeof (SysprofCaptureAddress));
1706 ev = (SysprofCaptureAllocation *)sysprof_capture_writer_allocate (self, &len);
1707 if (!ev)
1708 return false;
1709
1710 if ((n_addrs = backtrace_func (ev->addrs, MAX_UNWIND_DEPTH, backtrace_data)) < 0)
1711 n_addrs = 0;
1712
1713 sysprof_capture_writer_frame_init (&ev->frame,
1714 len,
1715 cpu,
1716 pid,
1717 time,
1718 SYSPROF_CAPTURE_FRAME_ALLOCATION);
1719
1720 ev->alloc_size = alloc_size;
1721 ev->alloc_addr = alloc_addr;
1722 ev->padding1 = 0;
1723 ev->tid = tid;
1724 ev->n_addrs = 0;
1725
1726 if (n_addrs <= MAX_UNWIND_DEPTH)
1727 ev->n_addrs = n_addrs;
1728
1729 if (ev->n_addrs < MAX_UNWIND_DEPTH)
1730 {
1731 size_t diff = (sizeof (SysprofCaptureAddress) * (MAX_UNWIND_DEPTH - ev->n_addrs));
1732
1733 ev->frame.len -= diff;
1734 self->pos -= diff;
1735 }
1736
1737 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_ALLOCATION]++;
1738
1739 return true;
1740 }
1741
1742 bool
1743 sysprof_capture_writer_add_allocation_copy (SysprofCaptureWriter *self,
1744 int64_t time,
1745 int cpu,
1746 int32_t pid,
1747 int32_t tid,
1748 SysprofCaptureAddress alloc_addr,
1749 int64_t alloc_size,
1750 const SysprofCaptureAddress *addrs,
1751 unsigned int n_addrs)
1752 {
1753 SysprofCaptureAllocation *ev;
1754 size_t len;
1755
1756 assert (self != NULL);
1757
1758 if (n_addrs > 0xFFF)
1759 n_addrs = 0xFFF;
1760
1761 len = sizeof *ev + (n_addrs * sizeof (SysprofCaptureAddress));
1762 ev = (SysprofCaptureAllocation *)sysprof_capture_writer_allocate (self, &len);
1763 if (!ev)
1764 return false;
1765
1766 sysprof_capture_writer_frame_init (&ev->frame,
1767 len,
1768 cpu,
1769 pid,
1770 time,
1771 SYSPROF_CAPTURE_FRAME_ALLOCATION);
1772
1773 ev->alloc_size = alloc_size;
1774 ev->alloc_addr = alloc_addr;
1775 ev->padding1 = 0;
1776 ev->tid = tid;
1777 ev->n_addrs = n_addrs;
1778
1779 memcpy (ev->addrs, addrs, sizeof (SysprofCaptureAddress) * n_addrs);
1780
1781 self->stat.frame_count[SYSPROF_CAPTURE_FRAME_ALLOCATION]++;
1782
1783 return true;
1784 }
1785
1786 bool
1787 _sysprof_capture_writer_add_raw (SysprofCaptureWriter *self,
1788 const SysprofCaptureFrame *fr)
1789 {
1790 void *begin;
1791 size_t len;
1792
1793 assert (self != NULL);
1794 assert ((fr->len & 0x7) == 0);
1795 assert (fr->type < SYSPROF_CAPTURE_FRAME_LAST);
1796
1797 len = fr->len;
1798
1799 if (!(begin = sysprof_capture_writer_allocate (self, &len)))
1800 return false;
1801
1802 assert (fr->len == len);
1803 assert (fr->type < SYSPROF_CAPTURE_FRAME_LAST);
1804
1805 memcpy (begin, fr, fr->len);
1806
1807 if (fr->type < SYSPROF_N_ELEMENTS (self->stat.frame_count))
1808 self->stat.frame_count[fr->type]++;
1809
1810 return true;
1811 }
1812
1813 int
1814 _sysprof_capture_writer_dup_fd (SysprofCaptureWriter *self)
1815 {
1816 assert (self != NULL);
1817
1818 if (self->fd == -1)
1819 return -1;
1820
1821 return dup (self->fd);
1822 }