src/tracefile.cpp

filter: Statementstmt 85.0% 680 / 800 Branchbran 57.1% 422 / 738 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 71.6% 1102 / 1538 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/snappy.cpp ↑ Summary src/runloop.cpp »
1 #include "tracefile.h"
2  
3 #if SNAPPY
4 #include "snappy.h"
5 #endif
6  
7 #include <ctime>
8 #include <vector>
9  
10 using namespace devel::statprofiler;
11 using namespace std;
12  
13 #define FILE_MAGIC "=statprofiler"
14 #define FORMAT_VERSION 1
15  
16 #if PERL_VERSION < 16
17 # ifndef GvNAMEUTF8
18 # define GvNAMEUTF8(foo) 0
19 # endif
20 #endif
21  
22 enum {
23 TAG_SAMPLE_START = 1,
24 TAG_SAMPLE_END = 2,
25 TAG_SUB_FRAME = 3,
26 TAG_EVAL_FRAME = 4,
27 TAG_XSUB_FRAME = 5,
28 TAG_MAIN_FRAME = 6,
29 TAG_EVAL_STRING = 7, // unused
30 TAG_EVAL_SEQ = 8,
31 TAG_FILE_END = 196,
32 TAG_STREAM_END = 197,
33 TAG_SECTION_START = 198,
34 TAG_SECTION_END = 199,
35 TAG_CUSTOM_META = 200,
36 TAG_META_PERL_VERSION = 201,
37 TAG_META_TICK_DURATION = 202,
38 TAG_META_STACK_SAMPLE_DEPTH = 203,
39 TAG_META_LIBRARY_VERSION = 204, // TODO implement
40 TAG_META_GENEALOGY = 205,
41 TAG_HEADER_SEPARATOR = 254,
42 TAG_TAG_CONTINUATION = 255 // just reserved for now
43 };
44  
45  
46 5605 InputBuffer::InputBuffer() :
47 5605 fh(NULL), input_position(input_buffer), input_end(input_buffer)
48 {
49 #if SNAPPY
50 5605 ▶ snappy = new SnappyInput(OUTPUT_BUFFER_SIZE);
Branch: 2 branches
BranchTrueFalseTotal
snappy = new SnappyInput(OUTPUT_BUFFER_SIZE); 5605 0 5605
snappy = new SnappyInput(OUTPUT_BUFFER_SIZE); 0 0 0
51 #endif
52 5605 }
53  
54 5604 InputBuffer::~InputBuffer()
55 {
56 5604 close();
57 #if SNAPPY
58 5604 ▶ delete snappy;
Branch: 1 branch
BranchTrueFalseTotal
delete snappy; 5604 0 5604
59 #endif
60 5604 }
61  
62 10189 void InputBuffer::fill_buffer()
63 {
64 size_t size;
65  
66 10189 ▶ if (input_position != input_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (input_position != input_end) { 0 10189 10189
67 0 memcpy(input_buffer, input_position, input_end - input_position);
68 0 input_end = input_buffer + (input_end - input_position);
69 0 input_position = input_buffer;
70 0 size = OUTPUT_BUFFER_SIZE - (input_end - input_position);
71 } else {
72 10189 input_position = input_end = input_buffer;
73 10189 size = OUTPUT_BUFFER_SIZE;
74 }
75  
76 #if SNAPPY
77 10189 int bytes = snappy->read(fh, input_end, OUTPUT_BUFFER_SIZE - (input_end - input_buffer));
78 #else
79 int bytes = fread(input_end, 1, size, fh);
80 #endif
81  
82 10189 input_end += bytes;
83 10189 }
84  
85 34211 void InputBuffer::skip_bytes(size_t size)
86 {
87 34211 ▶ if (!size)
Branch: 1 branch
BranchTrueFalseTotal
if (!size) 0 34211 34211
88 34211 return;
89  
90 for (;;) {
91 0 ▶ if (input_end - input_position >= size) {
Branch: 1 branch
BranchTrueFalseTotal
if (input_end - input_position >= size) { 0 0 0
92 0 input_position += size;
93  
94 0 return;
95 } else {
96 0 size -= input_end - input_position;
97 0 input_position = input_end;
98 }
99  
100 0 fill_buffer();
101  
102 0 ▶ if (input_end == input_position)
Branch: 1 branch
BranchTrueFalseTotal
if (input_end == input_position) 0 0 0
103 0 croak("Unexpected end-of-file while skipping over data");
104 }
105 }
106  
107 2644577 int InputBuffer::read_byte()
108 {
109 2644577 ▶ if (input_end - input_position >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (input_end - input_position >= 1) 2634770 9807 2644577
110 2634770 return (unsigned char) *input_position++;
111  
112 9807 fill_buffer();
113  
114 9807 ▶ if (input_end == input_position)
Branch: 1 branch
BranchTrueFalseTotal
if (input_end == input_position) 2650 7157 9807
115 2650 return EOF;
116  
117 7157 return (unsigned char) *input_position++;
118 }
119  
120 642578 void InputBuffer::read_bytes(void *buffer, size_t size)
121 {
122 642578 char *curr = (char *) buffer;
123  
124 for (;;) {
125 642960 ▶ if (input_end - input_position >= size) {
Branch: 1 branch
BranchTrueFalseTotal
if (input_end - input_position >= size) { 642578 382 642960
126 642578 memcpy(curr, input_position, size);
127 642578 input_position += size;
128  
129 642578 return;
130 } else {
131 382 memcpy(curr, input_position, input_end - input_position);
132 382 curr += input_end - input_position;
133 382 size -= input_end - input_position;
134 382 input_position = input_end;
135 }
136  
137 382 fill_buffer();
138  
139 382 ▶ if (input_end == input_position)
Branch: 1 branch
BranchTrueFalseTotal
if (input_end == input_position) 0 382 382
140 0 croak("Unexpected end-of-file while reading bytes");
141 }
142 }
143  
144 5605 void InputBuffer::open(std::FILE *_fh)
145 {
146 5605 fh = _fh;
147 5605 input_position = input_end = input_buffer;
148 5605 }
149  
150 16813 void InputBuffer::close()
151 {
152 16813 ▶ if (fh)
Branch: 1 branch
BranchTrueFalseTotal
if (fh) 5604 11209 16813
153 5604 fclose(fh);
154 16813 fh = NULL;
155 16813 }
156  
157 5604 int InputBuffer::read_raw_byte()
158 {
159 5604 return fgetc(fh);
160 }
161  
162 5604 bool InputBuffer::read_raw_bytes(void *buffer, size_t size)
163 {
164 5604 return fread(buffer, 1, size, fh) == size;
165 }
166  
167  
168 58 OutputBuffer::OutputBuffer() :
169 58 fh(NULL), output_position(output_buffer)
170 {
171 #if SNAPPY
172 58 ▶ snappy = new SnappyOutput(OUTPUT_BUFFER_SIZE);
Branch: 2 branches
BranchTrueFalseTotal
snappy = new SnappyOutput(OUTPUT_BUFFER_SIZE); 58 0 58
snappy = new SnappyOutput(OUTPUT_BUFFER_SIZE); 0 0 0
173 #endif
174 58 }
175  
176 58 OutputBuffer::~OutputBuffer()
177 {
178 58 close();
179 #if SNAPPY
180 58 ▶ delete snappy;
Branch: 1 branch
BranchTrueFalseTotal
delete snappy; 58 0 58
181 #endif
182 58 }
183  
184 3156 int OutputBuffer::flush_buffer()
185 {
186 3156 ▶ if (output_position == output_buffer)
Branch: 1 branch
BranchTrueFalseTotal
if (output_position == output_buffer) 924 2232 3156
187 924 return 1;
188  
189 2232 const char *pos = output_position;
190  
191 2232 output_position = output_buffer;
192  
193 #if SNAPPY
194 2232 return snappy->write(fh, output_buffer, pos - output_buffer);
195 #else
196 return fwrite(output_buffer, 1, pos - output_buffer, fh) == pos - output_buffer;
197 #endif
198 }
199  
200 1855 int OutputBuffer::flush()
201 {
202 1855 ▶ return flush_buffer() && fflush(fh) == 0;
Branch: 2 branches
BranchTrueFalseTotal
return flush_buffer() && fflush(fh) == 0; 1855 0 1855
return flush_buffer() && fflush(fh) == 0; 1855 0 1855
203 }
204  
205 1643885 int OutputBuffer::write_bytes(const void *bytes, size_t size)
206 {
207 1643885 ▶ if (output_position + size > output_buffer + OUTPUT_BUFFER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (output_position + size > output_buffer + OUTPUT_BUFFER_SIZE) 353 1643532 1643885
208 353 ▶ if (!flush_buffer())
Branch: 1 branch
BranchTrueFalseTotal
if (!flush_buffer()) 0 353 353
209 0 return 0;
210  
211 1643885 ▶ if (size > OUTPUT_BUFFER_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (size > OUTPUT_BUFFER_SIZE) { 1 1643884 1643885
212 #if SNAPPY
213 23 ▶ for (size_t pos = 0; pos < size; pos += OUTPUT_BUFFER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
for (size_t pos = 0; pos < size; pos += OUTPUT_BUFFER_SIZE) 22 1 23
214 22 ▶ if (!write_bytes(((const char *) bytes) + pos, min(size - pos, (size_t) OUTPUT_BUFFER_SIZE)))
Branch: 2 branches
BranchTrueFalseTotal
if (!write_bytes(((const char *) bytes) + pos, min(size - pos, (size_t) OUTPUT_BUFFER_SIZE))) 22 0 22
if (!write_bytes(((const char *) bytes) + pos, min(size - pos, (size_t) OUTPUT_BUFFER_SIZE))) 0 22 22
215 0 return 0;
216  
217 1 return 1;
218 #else
219 return fwrite(bytes, 1, size, fh) == size;
220 #endif
221 }
222  
223 1643884 memcpy(output_position, bytes, size);
224 1643884 output_position += size;
225  
226 1643884 return 1;
227 }
228  
229 757351 int OutputBuffer::write_byte(const char byte)
230 {
231 757351 ▶ if (output_position + 1 > output_buffer + OUTPUT_BUFFER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (output_position + 1 > output_buffer + OUTPUT_BUFFER_SIZE) 29 757322 757351
232 29 ▶ if (!flush_buffer())
Branch: 1 branch
BranchTrueFalseTotal
if (!flush_buffer()) 0 29 29
233 0 return 0;
234  
235 757351 *output_position++ = byte;
236  
237 757351 return 1;
238 }
239  
240 24243 int OutputBuffer::position() const
241 {
242 24243 return ftell(fh) + (output_position - output_buffer);
243 }
244  
245 919 void OutputBuffer::open(std::FILE *_fh)
246 {
247 919 fh = _fh;
248 919 output_position = output_buffer;
249 919 }
250  
251 977 void OutputBuffer::close()
252 {
253 977 ▶ if (fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (fh) { 919 58 977
254 919 flush_buffer();
255 919 fclose(fh);
256 }
257 977 fh = NULL;
258 977 }
259  
260 919 bool OutputBuffer::write_raw_byte(int c)
261 {
262 919 return fputc(c, fh) != EOF;
263 }
264  
265 919 bool OutputBuffer::write_raw_bytes(const void *buffer, size_t size)
266 {
267 919 return fwrite(buffer, 1, size, fh) == size;
268 }
269  
270  
271 namespace {
272 6146 void append_hex(string &str, uint32_t value)
273 {
274 static const char digits[] = "0123456789abcdef";
275  
276 55314 ▶ for (int i = 0; i < 8; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < 8; ++i) { 49168 6146 55314
277 49168 str += digits[value >> 28];
278 49168 value <<= 4;
279 }
280 6146 }
281  
282 67248 void append_hex(char *buffer, uint32_t value)
283 {
284 static const char digits[] = "0123456789abcdef";
285  
286 605232 ▶ for (int i = 0; i < 8; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < 8; ++i) { 537984 67248 605232
287 537984 buffer[i] = digits[value >> 28];
288 537984 value <<= 4;
289 }
290 67248 }
291  
292 882724 size_t varint_size(unsigned int value)
293 {
294 882724 ▶ return value < (1 << 7) ? 1 :
Branch: 3 branches
BranchTrueFalseTotal
return value < (1 << 7) ? 1 : 253975 628749 882724
return value < (1 << 7) ? 1 : 1 253974 253975
return value < (1 << 7) ? 1 : 1 0 1
295 value < (1 << 14) ? 2 :
296 value < (1 << 21) ? 3 :
297 882724 4;
298 }
299  
300 524919 size_t string_size(int length)
301 {
302 524919 return varint_size(length) + length;
303 }
304  
305 24246 size_t string_size(const char *value)
306 {
307 24246 ▶ return string_size(value ? strlen(value) : 0);
Branch: 1 branch
BranchTrueFalseTotal
return string_size(value ? strlen(value) : 0); 24226 20 24246
308 }
309  
310 113 size_t string_size(pTHX_ SV *value)
311 {
312 113 return string_size(SvCUR(value));
313 }
314  
315 1390597 unsigned int read_varint(InputBuffer &in)
316 {
317 1390597 int res = 0;
318 int v;
319  
320 for (;;) {
321 1696259 v = in.read_byte();
322 1696259 ▶ if (v == EOF)
Branch: 1 branch
BranchTrueFalseTotal
if (v == EOF) 0 1696259 1696259
323 0 croak("Unexpected end-of-file while reading a varint");
324 1696259 res = (res << 7) | (v & 0x7f);
325 1696259 ▶ if (!(v & 0x80))
Branch: 1 branch
BranchTrueFalseTotal
if (!(v & 0x80)) 1390597 305662 1696259
326 1390597 break;
327 }
328  
329 1390597 return res;
330 }
331  
332 631397 SV *read_string(pTHX_ InputBuffer &in)
333 {
334 631397 int flags = in.read_byte();
335 631397 unsigned int size = read_varint(in);
336  
337 631397 ▶ if (flags == EOF)
Branch: 1 branch
BranchTrueFalseTotal
if (flags == EOF) 0 631397 631397
338 0 croak("Unexpected end-of-file while reading a string");
339  
340 // don't return undef when length is 0
341 631397 ▶ if (size == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (size == 0) 27 631370 631397
342 27 return sv_2mortal(newSVpvn("", 0));
343  
344 631370 SV *sv = sv_2mortal(newSV(size));
345  
346 631370 SvPOK_on(sv);
347 631370 SvCUR_set(sv, size);
348 631370 ▶ if (flags & 1)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & 1) 0 631370 631370
349 0 SvUTF8_on(sv);
350  
351 631370 in.read_bytes(SvPVX(sv), size);
352  
353 631370 return sv;
354 }
355  
356 1116994 int write_varint(OutputBuffer &out, unsigned int value)
357 {
358 1116994 char buffer[10], *curr = &buffer[sizeof(buffer) - 1];
359  
360 do {
361 1372775 *curr-- = (value & 0x7f) | 0x80;
362 1372775 value >>= 7;
363 1372775 ▶ } while (value);
Branch: 1 branch
BranchTrueFalseTotal
} while (value); 255781 1116994 1372775
364  
365 1116994 buffer[sizeof(buffer) - 1] &= 0x7f;
366 2233988 ▶ return out.write_bytes(curr + 1, (buffer + sizeof(buffer)) - (curr + 1));
Branch: 1 branch
BranchTrueFalseTotal
return out.write_bytes(curr + 1, (buffer + sizeof(buffer)) - (curr + 1)); 1116994 0 1116994
367 }
368  
369 525031 int write_string(OutputBuffer &out, const char *value, size_t length, bool utf8)
370 {
371 525031 int status = 0;
372 525031 ▶ status += out.write_byte(utf8 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
status += out.write_byte(utf8 ? 1 : 0); 0 525031 525031
373 525031 status += write_varint(out, length);
374 525031 status += out.write_bytes(value, length);
375 525031 return status;
376 }
377  
378 24246 int write_string(OutputBuffer &out, const char *value, bool utf8)
379 {
380 24246 ▶ return write_string(out, value, value ? strlen(value) : 0, utf8);
Branch: 1 branch
BranchTrueFalseTotal
return write_string(out, value, value ? strlen(value) : 0, utf8); 24226 20 24246
381 }
382  
383 0 int write_string(OutputBuffer &out, const std::string &value, bool utf8)
384 {
385 0 return write_string(out, value.c_str(), value.length(), utf8);
386 }
387  
388 225 int write_string(pTHX_ OutputBuffer &out, SV *value)
389 {
390 225 U32 utf8 = SvUTF8(value);
391 STRLEN len;
392 225 char *str = SvPV(value, len);
393 450 ▶ return write_string(out, str, len, utf8);
Branch: 1 branch
BranchTrueFalseTotal
return write_string(out, str, len, utf8); 225 0 225
394 }
395  
396 192404 SV *make_fullname(pTHX_ SV *package, SV *name) {
397 192404 SV *fullname = newSV(SvCUR(package) + 2 + SvCUR(name));
398  
399 192404 SvPOK_on(fullname);
400 192404 sv_catsv(fullname, package);
401 192404 sv_catpvn(fullname, "::", 2);
402 192404 sv_catsv(fullname, name);
403  
404 192404 return fullname;
405 }
406  
407 188290 SV *make_fullfile(pTHX_ const Genealogy_t &genealogy, SV *file) {
408 188290 STRLEN len = SvCUR(file);
409  
410 188290 ▶ if (len >= 8) { // sizeof("(eval 1)") - 1
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 8) { // sizeof("(eval 1)") - 1 188290 0 188290
411 188290 char *s = SvPVX(file);
412  
413 188290 ▶ if (s[len - 1] == ')' && strncmp(s, "(eval ", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (s[len - 1] == ')' && strncmp(s, "(eval ", 6) == 0) { 0 188290 188290
if (s[len - 1] == ')' && strncmp(s, "(eval ", 6) == 0) { 0 0 0
414 0 return newSVpvf("qeval:%s/%" SVf, genealogy.id, file);
415 }
416 }
417  
418 188290 return SvREFCNT_inc(file);
419 }
420  
421 192404 bool is_begin(pTHX_ SV *name) {
422 STRLEN len;
423 192404 const char *buf = SvPV(name, len);
424  
425 384808 ▶ return len == 5 && buf[0] == 'B' && memcmp(buf, "BEGIN", 5) == 0;
Branch: 3 branches
BranchTrueFalseTotal
return len == 5 && buf[0] == 'B' && memcmp(buf, "BEGIN", 5) == 0; 57018 135386 192404
return len == 5 && buf[0] == 'B' && memcmp(buf, "BEGIN", 5) == 0; 973 56045 57018
return len == 5 && buf[0] == 'B' && memcmp(buf, "BEGIN", 5) == 0; 973 0 973
426 }
427 }
428  
429  
430 5605 TraceFileReader::TraceFileReader(pTHX_ SV *_mapper)
431 5605 : file_format_version(0), sections(NULL),
432 5605 sections_changed(false), metadata_changed(false),
433 5605 stream_ended(false), file_ended(false), sub_prefix_rx(NULL),
434 5605 map_evals(false)
435 {
436 SET_THX_MEMBER
437 5605 source_perl_version.revision = 0;
438 5605 source_perl_version.version = 0;
439 5605 source_perl_version.subversion = 0;
440 5605 ▶ custom_metadata = newHV();
Branch: 1 branch
BranchTrueFalseTotal
custom_metadata = newHV(); 5605 0 5605
441 5605 ▶ source_code = newHV();
Branch: 1 branch
BranchTrueFalseTotal
source_code = newHV(); 5605 0 5605
442 5605 ▶ st_stash = gv_stashpv("Devel::StatProfiler::StackTrace", 0);
Branch: 1 branch
BranchTrueFalseTotal
st_stash = gv_stashpv("Devel::StatProfiler::StackTrace", 0); 5605 0 5605
443 5605 ▶ sf_stash = gv_stashpv("Devel::StatProfiler::StackFrame", 0);
Branch: 1 branch
BranchTrueFalseTotal
sf_stash = gv_stashpv("Devel::StatProfiler::StackFrame", 0); 5605 0 5605
444 5605 ▶ msf_stash = gv_stashpv("Devel::StatProfiler::MainStackFrame", 0);
Branch: 1 branch
BranchTrueFalseTotal
msf_stash = gv_stashpv("Devel::StatProfiler::MainStackFrame", 0); 5605 0 5605
445 5605 ▶ esf_stash = gv_stashpv("Devel::StatProfiler::EvalStackFrame", 0);
Branch: 1 branch
BranchTrueFalseTotal
esf_stash = gv_stashpv("Devel::StatProfiler::EvalStackFrame", 0); 5605 0 5605
446 5605 ▶ mapper = _mapper && SvOK(_mapper) ? SvREFCNT_inc(_mapper) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
mapper = _mapper && SvOK(_mapper) ? SvREFCNT_inc(_mapper) : NULL; 5296 309 5605
mapper = _mapper && SvOK(_mapper) ? SvREFCNT_inc(_mapper) : NULL; 35 5261 5296
447  
448 5605 ▶ if (mapper) {
Branch: 1 branch
BranchTrueFalseTotal
if (mapper) { 35 5570 5605
449 35 ▶ SV **_rx = hv_fetchs((HV *) SvRV(mapper), "rx", 0);
Branch: 1 branch
BranchTrueFalseTotal
SV **_rx = hv_fetchs((HV *) SvRV(mapper), "rx", 0); 35 0 35
450  
451 35 ▶ sub_prefix_rx = _rx ? (HV *) SvRV(*_rx) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
sub_prefix_rx = _rx ? (HV *) SvRV(*_rx) : NULL; 1 34 35
452 35 ▶ map_evals = can_map_evals();
Branch: 1 branch
BranchTrueFalseTotal
map_evals = can_map_evals(); 35 0 35
453 }
454 5605 }
455  
456 5604 TraceFileReader::~TraceFileReader()
457 {
458 5604 SvREFCNT_dec(source_code);
459 5604 SvREFCNT_dec(custom_metadata);
460 5604 SvREFCNT_dec(sections);
461 5604 SvREFCNT_dec(mapper);
462 5604 close();
463 5604 }
464  
465 5605 void TraceFileReader::open(const std::string &path)
466 {
467 5605 SvREFCNT_dec(sections);
468 5605 sections = newHV();
469 5605 close();
470 5605 in.open(fopen(path.c_str(), "rb"));
471 5605 ▶ if (!in.is_valid())
Branch: 1 branch
BranchTrueFalseTotal
if (!in.is_valid()) 1 5604 5605
472 1 croak("Failed to open file '%s'", path.c_str());
473 5604 read_header();
474 5604 }
475  
476 5604 void TraceFileReader::read_header()
477 {
478 char magic[sizeof(FILE_MAGIC) - 1];
479  
480 5604 ▶ if (!in.read_raw_bytes(magic, sizeof(magic)))
Branch: 2 branches
BranchTrueFalseTotal
if (!in.read_raw_bytes(magic, sizeof(magic))) 5604 0 5604
if (!in.read_raw_bytes(magic, sizeof(magic))) 0 5604 5604
481 0 croak("Unexpected end-of-file while reading file magic");
482 5604 ▶ if (strncmp(magic, FILE_MAGIC, sizeof(magic)))
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(magic, FILE_MAGIC, sizeof(magic))) 0 5604 5604
483 0 croak("Invalid file magic");
484  
485 // In future, will check that the version is at least not newer
486 // than this library's file format version. That's necessary even
487 // if there's a backcompat layer.
488 5604 ▶ unsigned int version_from_file = in.read_raw_byte();
Branch: 1 branch
BranchTrueFalseTotal
unsigned int version_from_file = in.read_raw_byte(); 5604 0 5604
489 5604 ▶ if (version_from_file == EOF)
Branch: 1 branch
BranchTrueFalseTotal
if (version_from_file == EOF) 0 5604 5604
490 0 croak("Unexpected end-of-file while reading file version");
491 5604 ▶ if (version_from_file < 1 || version_from_file > FORMAT_VERSION)
Branch: 2 branches
BranchTrueFalseTotal
if (version_from_file < 1 || version_from_file > FORMAT_VERSION) 5604 0 5604
if (version_from_file < 1 || version_from_file > FORMAT_VERSION) 0 5604 5604
492 0 croak("Incompatible file format version %i", version_from_file);
493  
494 5604 file_format_version = (unsigned int)version_from_file;
495  
496 // TODO this becomes a loop reading header records
497 5604 bool cont = 1;
498 33624 ▶ while (cont) {
Branch: 1 branch
BranchTrueFalseTotal
while (cont) { 28020 5604 33624
499 28020 ▶ const int tag = in.read_byte();
Branch: 1 branch
BranchTrueFalseTotal
const int tag = in.read_byte(); 28020 0 28020
500  
501 28020 switch (tag) {
502 0 case EOF:
503 0 croak("Invalid input file: File ends before end of file header");
504 5604 case TAG_HEADER_SEPARATOR:
505 5604 cont = 0;
506 5604 break;
507  
508 5604 case TAG_META_PERL_VERSION: {
509 5604 ▶ source_perl_version.revision = read_varint(in);
Branch: 1 branch
BranchTrueFalseTotal
source_perl_version.revision = read_varint(in); 5604 0 5604
510 5604 ▶ source_perl_version.version = read_varint(in);
Branch: 1 branch
BranchTrueFalseTotal
source_perl_version.version = read_varint(in); 5604 0 5604
511 5604 ▶ source_perl_version.subversion = read_varint(in);
Branch: 1 branch
BranchTrueFalseTotal
source_perl_version.subversion = read_varint(in); 5604 0 5604
512 5604 break;
513 }
514 5604 case TAG_META_TICK_DURATION: {
515 5604 ▶ source_tick_duration = read_varint(in);
Branch: 1 branch
BranchTrueFalseTotal
source_tick_duration = read_varint(in); 5604 0 5604
516 5604 break;
517 }
518 5604 case TAG_META_STACK_SAMPLE_DEPTH: {
519 5604 ▶ source_stack_sample_depth = read_varint(in);
Branch: 1 branch
BranchTrueFalseTotal
source_stack_sample_depth = read_varint(in); 5604 0 5604
520 5604 break;
521 }
522 5604 case TAG_META_GENEALOGY: {
523 uint32_t temp[ID_SIZE];
524  
525 5604 ▶ genealogy_info.ordinal = read_varint(in);
Branch: 1 branch
BranchTrueFalseTotal
genealogy_info.ordinal = read_varint(in); 5604 0 5604
526 5604 ▶ genealogy_info.parent_ordinal = read_varint(in);
Branch: 1 branch
BranchTrueFalseTotal
genealogy_info.parent_ordinal = read_varint(in); 5604 0 5604
527  
528 5604 ▶ in.read_bytes(temp, sizeof(temp));
Branch: 1 branch
BranchTrueFalseTotal
in.read_bytes(temp, sizeof(temp)); 5604 0 5604
529 39228 ▶ for (int i = 0; i < ID_SIZE; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < ID_SIZE; ++i) 33624 5604 39228
530 33624 append_hex(genealogy_info.id + i * 8, temp[i]);
531  
532 5604 ▶ in.read_bytes(temp, sizeof(temp));
Branch: 1 branch
BranchTrueFalseTotal
in.read_bytes(temp, sizeof(temp)); 5604 0 5604
533 39228 ▶ for (int i = 0; i < ID_SIZE; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < ID_SIZE; ++i) 33624 5604 39228
534 33624 append_hex(genealogy_info.parent_id + i * 8, temp[i]);
535  
536 5604 genealogy_info.id[ID_SIZE * 4 * 2] = genealogy_info.parent_id[ID_SIZE * 4 * 2] = 0;
537 5604 break;
538 }
539 0 case TAG_CUSTOM_META:
540 0 ▶ read_custom_meta_record(read_varint(in));
Branch: 2 branches
BranchTrueFalseTotal
read_custom_meta_record(read_varint(in)); 0 0 0
read_custom_meta_record(read_varint(in)); 0 0 0
541 0 break;
542  
543 0 default:
544 0 croak("Invalid input file: Invalid header record tag (%i)", tag);
545 }
546 }
547 5604 }
548  
549 264 void TraceFileReader::read_custom_meta_record(const int size, HV *extra_output_hash)
550 {
551 264 SV *key = read_string(aTHX_ in);
552 264 SV *value = read_string(aTHX_ in);
553 264 SvREFCNT_inc(value);
554 264 hv_store_ent(custom_metadata, key, value, 0);
555  
556 264 ▶ if (extra_output_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (extra_output_hash) { 264 0 264
557 264 SvREFCNT_inc(value);
558 264 hv_store_ent(extra_output_hash, key, value, 0);
559 }
560 264 }
561  
562 11209 void TraceFileReader::close()
563 {
564 11209 in.close();
565 11209 }
566  
567 35 bool TraceFileReader::can_map_evals() {
568 35 dSP;
569  
570 35 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 35 35
571 35 ▶ XPUSHs(mapper);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(mapper); 0 35 35
572 35 PUTBACK;
573  
574 35 call_method("can_map_eval", G_SCALAR);
575  
576 35 SPAGAIN;
577 35 SV *res = POPs;
578 35 bool result = SvTRUE(res);
579 35 PUTBACK;
580  
581 35 return result;
582 }
583  
584 3346 SV *TraceFileReader::map_name(SV *package, SV *name) {
585 // duplicates logic in the mapper, to avoid unnecessary method calls
586 3346 HE *e = hv_fetch_ent(sub_prefix_rx, package, 0, 0);
587  
588 3346 ▶ if (!e)
Branch: 1 branch
BranchTrueFalseTotal
if (!e) 3342 4 3346
589 3342 return name;
590  
591 4 dSP;
592  
593 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
594 4 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 4 4
595 4 PUSHs(mapper);
596 4 PUSHs(package);
597 4 PUSHs(name);
598 4 PUTBACK;
599  
600 4 call_method("map_sub", G_SCALAR);
601  
602 4 SPAGAIN;
603 4 SV *newname = POPs;
604 4 PUTBACK;
605  
606 4 return newname;
607 }
608  
609 0 SV *TraceFileReader::map_eval(SV *eval_name) {
610 0 dSP;
611  
612 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
613 0 ▶ EXTEND(SP, 4);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 4); 0 0 0
614 0 PUSHs(mapper);
615 0 PUSHs(newSVpvn_flags((const char *) genealogy_info.id, sizeof(genealogy_info.id) - 1, SVs_TEMP));
616 0 mPUSHs(newSVuv(genealogy_info.ordinal));
617 0 PUSHs(eval_name);
618 0 PUTBACK;
619  
620 0 call_method("map_eval_name", G_SCALAR);
621  
622 0 SPAGAIN;
623 0 SV *newname = POPs;
624 0 PUTBACK;
625  
626 0 return newname;
627 }
628  
629 0 static SV *sha1_hex(pTHX_ SV *source) {
630 0 dSP;
631  
632 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
633 0 ▶ XPUSHs(source);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(source); 0 0 0
634 0 PUTBACK;
635  
636 0 call_pv("Digest::SHA::sha1_hex", G_SCALAR);
637  
638 0 SPAGAIN;
639 0 SV *res = POPs;
640 0 PUTBACK;
641  
642 0 return res;
643 }
644  
645 6 SV *TraceFileReader::maybe_map_eval(SV *file) {
646 6 STRLEN len = SvCUR(file);
647 6 char *s = SvPVX(file);
648  
649 6 ▶ if (s[len - 1] == ')' && strncmp(s, "(eval ", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (s[len - 1] == ')' && strncmp(s, "(eval ", 6) == 0) { 0 6 6
if (s[len - 1] == ')' && strncmp(s, "(eval ", 6) == 0) { 0 0 0
650 0 HE *source = hv_fetch_ent(source_code, file, 0, 0);
651  
652 0 ▶ if (source) {
Branch: 1 branch
BranchTrueFalseTotal
if (source) { 0 0 0
653 0 SV *digest = sha1_hex(aTHX_ HeVAL(source));
654 0 SV *res = sv_2mortal(newSV(5 + len));
655 0 char *buffer = SvPVX(res);
656  
657 0 memcpy(buffer, "eval:", 5);
658 0 SvPOK_on(res);
659 0 SvCUR_set(res, 5);
660 0 sv_catsv(res, digest);
661  
662 0 return res;
663 }
664  
665 0 return map_eval(file);
666 }
667  
668 6 return file;
669 }
670  
671 // works around the source code explosion caused by Sub::Quote, and
672 // possibly other modules
673 455 static void normalize_references(pTHX_ SV *source) {
674 455 char *ptr = SvPVX(source);
675  
676 461 ▶ for (char *curr = strstr(ptr + 1, "(0x"); curr; curr = strstr(curr + 1, "(0x")) {
Branch: 1 branch
BranchTrueFalseTotal
for (char *curr = strstr(ptr + 1, "(0x"); curr; curr = strstr(curr + 1, "(0x")) { 6 455 461
677 // look back for CODE, SCALAR, ARRAY, ...
678 const char *check_for;
679 int check_len;
680 6 switch (curr[-1]) {
681 0 case 'B': // GLOB
682 0 check_for = "GLOB";
683 0 check_len = 4;
684 0 break;
685 4 case 'E': // CODE
686 4 check_for = "CODE";
687 4 check_len = 4;
688 4 break;
689 2 case 'F': // REF
690 2 check_for = "REF";
691 2 check_len = 3;
692 2 break;
693 0 case 'H': // HASH
694 0 check_for = "HASH";
695 0 check_len = 4;
696 0 break;
697 0 case 'O': // IO
698 0 check_for = "IO";
699 0 check_len = 2;
700 0 break;
701 0 case 'R': // SCALAR
702 0 check_for = "SCALAR";
703 0 check_len = 6;
704 0 break;
705 0 case 'Y': // ARRAY
706 0 check_for = "ARRAY";
707 0 check_len = 5;
708 0 break;
709 0 default:
710 0 continue;
711 }
712 6 ▶ if (strncmp(curr - check_len, check_for, check_len) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(curr - check_len, check_for, check_len) != 0) 0 6 6
713 0 continue;
714  
715 // scan forward for hex digits terminated by a )
716 6 char *end = curr + 3, c;
717 84 ▶ while ((c = *end)) {
Branch: 1 branch
BranchTrueFalseTotal
while ((c = *end)) { 84 0 84
718 84 ▶ if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')))
Branch: 4 branches
BranchTrueFalseTotal
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) 78 6 84
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) 30 48 78
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) 30 6 36
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) 30 0 30
719 break;
720 78 ++end;
721 }
722 6 ▶ if (*end != ')')
Branch: 1 branch
BranchTrueFalseTotal
if (*end != ')') 0 6 6
723 0 continue;
724  
725 84 ▶ for (char *i = curr + 3; i < end; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (char *i = curr + 3; i < end; ++i) 78 6 84
726 78 *i = 'f';
727 6 curr = end;
728 }
729 455 }
730  
731 37372 SV *TraceFileReader::read_trace()
732 {
733 37372 HV *sample = NULL;
734 AV *frames;
735 37372 vector<SV *> try_eval_remap;
736 37372 bool eval_remap = false;
737  
738 // in newer Perls, there is always an eval/main stack frame as the
739 // caller of a BEGIN block, while older Perls don't have it
740 37372 bool last_was_begin = false;
741  
742 // As we read more meta data, we'll build up this hash (which is
743 // created lazily below). If there's any, that hash will be returned
744 // with the trace as a hash element of the trace ("metadata").
745 // We also insert any metadata found into the TraceFileReader global
746 // metadata hash.
747 37372 HV *new_metadata = NULL;
748  
749 for (;;) {
750 288901 ▶ int type = in.read_byte();
Branch: 1 branch
BranchTrueFalseTotal
int type = in.read_byte(); 288901 0 288901
751  
752 288901 ▶ if (type == EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (type == EOF) { 2650 286251 288901
753 // treat truncated files as the end of a stream
754 2650 ▶ if (!file_ended)
Branch: 1 branch
BranchTrueFalseTotal
if (!file_ended) 0 2650 2650
755 0 file_ended = stream_ended = true;
756 2650 ▶ return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
return newSV(0); 2650 0 2650
757 }
758  
759 286251 ▶ int size = read_varint(in);
Branch: 1 branch
BranchTrueFalseTotal
int size = read_varint(in); 286251 0 286251
760 286251 switch (type) {
761 34211 case TAG_SAMPLE_START: {
762 34211 ▶ unsigned int weight = read_varint(in);
Branch: 1 branch
BranchTrueFalseTotal
unsigned int weight = read_varint(in); 34211 0 34211
763 34211 ▶ SV *op_name = read_string(aTHX_ in);
Branch: 1 branch
BranchTrueFalseTotal
SV *op_name = read_string(aTHX_ in); 34211 0 34211
764  
765 34211 ▶ sample = (HV *) sv_2mortal((SV *) newHV());
Branch: 2 branches
BranchTrueFalseTotal
sample = (HV *) sv_2mortal((SV *) newHV()); 34211 0 34211
sample = (HV *) sv_2mortal((SV *) newHV()); 34211 0 34211
766 34211 ▶ frames = newAV();
Branch: 1 branch
BranchTrueFalseTotal
frames = newAV(); 34211 0 34211
767  
768 34211 ▶ hv_stores(sample, "frames", newRV_noinc((SV *) frames));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(sample, "frames", newRV_noinc((SV *) frames)); 34211 0 34211
hv_stores(sample, "frames", newRV_noinc((SV *) frames)); 34211 0 34211
769 34211 ▶ hv_stores(sample, "weight", newSViv(weight));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(sample, "weight", newSViv(weight)); 34211 0 34211
hv_stores(sample, "weight", newSViv(weight)); 34211 0 34211
770 34211 ▶ hv_stores(sample, "op_name", SvREFCNT_inc(op_name));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(sample, "op_name", SvREFCNT_inc(op_name)); 34211 0 34211
771  
772 34211 last_was_begin = false;
773  
774 34211 break;
775 }
776 0 default:
777 0 ▶ warn("Unknown record type in trace file. Attempting to skip this record");
Branch: 1 branch
BranchTrueFalseTotal
warn("Unknown record type in trace file. Attempting to skip this record"); 0 0 0
778 0 ▶ in.skip_bytes(size);
Branch: 1 branch
BranchTrueFalseTotal
in.skip_bytes(size); 0 0 0
779 0 break;
780 188171 case TAG_SUB_FRAME: {
781 188171 ▶ if (!sample)
Branch: 1 branch
BranchTrueFalseTotal
if (!sample) 0 188171 188171
782 0 croak("Invalid input file: Found stray sub-frame tag without sample-start tag");
783 188171 ▶ SV *package = read_string(aTHX_ in);
Branch: 1 branch
BranchTrueFalseTotal
SV *package = read_string(aTHX_ in); 188171 0 188171
784 188171 ▶ SV *name = read_string(aTHX_ in);
Branch: 1 branch
BranchTrueFalseTotal
SV *name = read_string(aTHX_ in); 188171 0 188171
785 188171 ▶ SV *file = read_string(aTHX_ in);
Branch: 1 branch
BranchTrueFalseTotal
SV *file = read_string(aTHX_ in); 188171 0 188171
786 188171 ▶ int line = read_varint(in);
Branch: 1 branch
BranchTrueFalseTotal
int line = read_varint(in); 188171 0 188171
787 188171 ▶ int first_line = read_varint(in);
Branch: 1 branch
BranchTrueFalseTotal
int first_line = read_varint(in); 188171 0 188171
788 188171 ▶ HV *frame = newHV();
Branch: 1 branch
BranchTrueFalseTotal
HV *frame = newHV(); 188171 0 188171
789 188171 ▶ SV *full_file = make_fullfile(aTHX_ genealogy_info, file);
Branch: 1 branch
BranchTrueFalseTotal
SV *full_file = make_fullfile(aTHX_ genealogy_info, file); 188171 0 188171
790 188171 ▶ bool maybe_eval = map_evals && SvCUR(file) >= 8 && SvPVX(file)[0] == '(';
Branch: 3 branches
BranchTrueFalseTotal
bool maybe_eval = map_evals && SvCUR(file) >= 8 && SvPVX(file)[0] == '('; 6670 181501 188171
bool maybe_eval = map_evals && SvCUR(file) >= 8 && SvPVX(file)[0] == '('; 6670 0 6670
bool maybe_eval = map_evals && SvCUR(file) >= 8 && SvPVX(file)[0] == '('; 2 6668 6670
791  
792 188171 ▶ if (sub_prefix_rx)
Branch: 1 branch
BranchTrueFalseTotal
if (sub_prefix_rx) 3342 184829 188171
793 3342 ▶ name = map_name(package, name);
Branch: 1 branch
BranchTrueFalseTotal
name = map_name(package, name); 3342 0 3342
794 188171 ▶ if (map_evals) {
Branch: 1 branch
BranchTrueFalseTotal
if (map_evals) { 6670 181501 188171
795 6670 ▶ eval_remap = eval_remap || maybe_eval;
Branch: 2 branches
BranchTrueFalseTotal
eval_remap = eval_remap || maybe_eval; 6661 9 6670
eval_remap = eval_remap || maybe_eval; 2 6659 6661
796 6670 ▶ try_eval_remap.push_back(maybe_eval ? SvREFCNT_inc(file) : NULL);
Branch: 2 branches
BranchTrueFalseTotal
try_eval_remap.push_back(maybe_eval ? SvREFCNT_inc(file) : NULL); 2 6668 6670
try_eval_remap.push_back(maybe_eval ? SvREFCNT_inc(file) : NULL); 6670 0 6670
797 }
798  
799 188171 ▶ hv_stores(frame, "fq_sub_name", make_fullname(aTHX_ package, name));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(frame, "fq_sub_name", make_fullname(aTHX_ package, name)); 188171 0 188171
hv_stores(frame, "fq_sub_name", make_fullname(aTHX_ package, name)); 188171 0 188171
800 188171 ▶ hv_stores(frame, "package", SvREFCNT_inc(package));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(frame, "package", SvREFCNT_inc(package)); 188171 0 188171
801 188171 ▶ hv_stores(frame, "sub_name", SvREFCNT_inc(name));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(frame, "sub_name", SvREFCNT_inc(name)); 188171 0 188171
802 188171 ▶ hv_stores(frame, "file", full_file);
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(frame, "file", full_file); 188171 0 188171
803 188171 ▶ hv_stores(frame, "file_pretty", SvREFCNT_inc(full_file));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(frame, "file_pretty", SvREFCNT_inc(full_file)); 188171 0 188171
804 188171 ▶ hv_stores(frame, "line", newSViv(line));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(frame, "line", newSViv(line)); 188171 0 188171
hv_stores(frame, "line", newSViv(line)); 188171 0 188171
805 188171 ▶ hv_stores(frame, "first_line", newSViv(first_line));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(frame, "first_line", newSViv(first_line)); 188171 0 188171
hv_stores(frame, "first_line", newSViv(first_line)); 188171 0 188171
806 188171 ▶ av_push(frames, sv_bless(newRV_noinc((SV *) frame), sf_stash));
Branch: 3 branches
BranchTrueFalseTotal
av_push(frames, sv_bless(newRV_noinc((SV *) frame), sf_stash)); 188171 0 188171
av_push(frames, sv_bless(newRV_noinc((SV *) frame), sf_stash)); 188171 0 188171
av_push(frames, sv_bless(newRV_noinc((SV *) frame), sf_stash)); 188171 0 188171
807  
808 188171 ▶ last_was_begin = is_begin(aTHX_ name);
Branch: 1 branch
BranchTrueFalseTotal
last_was_begin = is_begin(aTHX_ name); 188171 0 188171
809  
810 188171 break;
811 }
812 4233 case TAG_XSUB_FRAME: {
813 4233 ▶ if (!sample)
Branch: 1 branch
BranchTrueFalseTotal
if (!sample) 0 4233 4233
814 0 croak("Invalid input file: Found stray sub-frame tag without sample-start tag");
815 4233 ▶ SV *package = read_string(aTHX_ in);
Branch: 1 branch
BranchTrueFalseTotal
SV *package = read_string(aTHX_ in); 4233 0 4233
816 4233 ▶ SV *name = read_string(aTHX_ in);
Branch: 1 branch
BranchTrueFalseTotal
SV *name = read_string(aTHX_ in); 4233 0 4233
817 4233 ▶ HV *frame = newHV();
Branch: 1 branch
BranchTrueFalseTotal
HV *frame = newHV(); 4233 0 4233
818  
819 4233 ▶ if (sub_prefix_rx)
Branch: 1 branch
BranchTrueFalseTotal
if (sub_prefix_rx) 4 4229 4233
820 4 ▶ name = map_name(package, name);
Branch: 1 branch
BranchTrueFalseTotal
name = map_name(package, name); 4 0 4
821 4233 ▶ if (map_evals)
Branch: 1 branch
BranchTrueFalseTotal
if (map_evals) 5 4228 4233
822 5 ▶ try_eval_remap.push_back(NULL);
Branch: 1 branch
BranchTrueFalseTotal
try_eval_remap.push_back(NULL); 5 0 5
823  
824 4233 ▶ hv_stores(frame, "fq_sub_name", make_fullname(aTHX_ package, name));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(frame, "fq_sub_name", make_fullname(aTHX_ package, name)); 4233 0 4233
hv_stores(frame, "fq_sub_name", make_fullname(aTHX_ package, name)); 4233 0 4233
825 4233 ▶ hv_stores(frame, "package", SvREFCNT_inc(package));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(frame, "package", SvREFCNT_inc(package)); 4233 0 4233
826 4233 ▶ hv_stores(frame, "sub_name", SvREFCNT_inc(name));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(frame, "sub_name", SvREFCNT_inc(name)); 4233 0 4233
827 4233 ▶ hv_stores(frame, "file", newSVpvn("", 0));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(frame, "file", newSVpvn("", 0)); 4233 0 4233
hv_stores(frame, "file", newSVpvn("", 0)); 4233 0 4233
828 4233 ▶ hv_stores(frame, "line", newSViv(-1));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(frame, "line", newSViv(-1)); 4233 0 4233
hv_stores(frame, "line", newSViv(-1)); 4233 0 4233
829 4233 ▶ hv_stores(frame, "first_line", newSViv(-1));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(frame, "first_line", newSViv(-1)); 4233 0 4233
hv_stores(frame, "first_line", newSViv(-1)); 4233 0 4233
830 4233 ▶ av_push(frames, sv_bless(newRV_noinc((SV *) frame), sf_stash));
Branch: 3 branches
BranchTrueFalseTotal
av_push(frames, sv_bless(newRV_noinc((SV *) frame), sf_stash)); 4233 0 4233
av_push(frames, sv_bless(newRV_noinc((SV *) frame), sf_stash)); 4233 0 4233
av_push(frames, sv_bless(newRV_noinc((SV *) frame), sf_stash)); 4233 0 4233
831  
832 4233 ▶ last_was_begin = is_begin(aTHX_ name);
Branch: 1 branch
BranchTrueFalseTotal
last_was_begin = is_begin(aTHX_ name); 4233 0 4233
833  
834 4233 break;
835 }
836 113 case TAG_EVAL_FRAME: {
837 113 ▶ if (!sample)
Branch: 1 branch
BranchTrueFalseTotal
if (!sample) 0 113 113
838 0 croak("Invalid input file: Found stray sub-frame tag without sample-start tag");
839 113 ▶ SV *file = read_string(aTHX_ in);
Branch: 1 branch
BranchTrueFalseTotal
SV *file = read_string(aTHX_ in); 113 0 113
840 113 ▶ int line = read_varint(in);
Branch: 1 branch
BranchTrueFalseTotal
int line = read_varint(in); 113 0 113
841 113 ▶ HV *frame = newHV();
Branch: 1 branch
BranchTrueFalseTotal
HV *frame = newHV(); 113 0 113
842 113 ▶ SV *full_file = make_fullfile(aTHX_ genealogy_info, file);
Branch: 1 branch
BranchTrueFalseTotal
SV *full_file = make_fullfile(aTHX_ genealogy_info, file); 113 0 113
843 113 ▶ bool maybe_eval = map_evals && SvCUR(file) >= 8 && SvPVX(file)[0] == '(';
Branch: 3 branches
BranchTrueFalseTotal
bool maybe_eval = map_evals && SvCUR(file) >= 8 && SvPVX(file)[0] == '('; 4 109 113
bool maybe_eval = map_evals && SvCUR(file) >= 8 && SvPVX(file)[0] == '('; 4 0 4
bool maybe_eval = map_evals && SvCUR(file) >= 8 && SvPVX(file)[0] == '('; 4 0 4
844  
845 113 ▶ if (map_evals) {
Branch: 1 branch
BranchTrueFalseTotal
if (map_evals) { 4 109 113
846 4 ▶ eval_remap = eval_remap || maybe_eval;
Branch: 2 branches
BranchTrueFalseTotal
eval_remap = eval_remap || maybe_eval; 2 2 4
eval_remap = eval_remap || maybe_eval; 2 0 2
847 4 ▶ try_eval_remap.push_back(maybe_eval ? SvREFCNT_inc(file) : NULL);
Branch: 2 branches
BranchTrueFalseTotal
try_eval_remap.push_back(maybe_eval ? SvREFCNT_inc(file) : NULL); 4 0 4
try_eval_remap.push_back(maybe_eval ? SvREFCNT_inc(file) : NULL); 4 0 4
848 }
849  
850 113 ▶ hv_stores(frame, "file", full_file);
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(frame, "file", full_file); 113 0 113
851 113 ▶ hv_stores(frame, "file_pretty", SvREFCNT_inc(full_file));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(frame, "file_pretty", SvREFCNT_inc(full_file)); 113 0 113
852 113 ▶ hv_stores(frame, "line", newSViv(line));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(frame, "line", newSViv(line)); 113 0 113
hv_stores(frame, "line", newSViv(line)); 113 0 113
853  
854 // see comment where last_was_begin is defined
855 113 ▶ if (PERL_VERSION < 36 || !last_was_begin) {
Branch: 1 branch
BranchTrueFalseTotal
if (PERL_VERSION < 36 || !last_was_begin) { 109 4 113
856 109 ▶ av_push(frames, sv_bless(newRV_noinc((SV *) frame), esf_stash));
Branch: 3 branches
BranchTrueFalseTotal
av_push(frames, sv_bless(newRV_noinc((SV *) frame), esf_stash)); 109 0 109
av_push(frames, sv_bless(newRV_noinc((SV *) frame), esf_stash)); 109 0 109
av_push(frames, sv_bless(newRV_noinc((SV *) frame), esf_stash)); 109 0 109
857 } else {
858 4 ▶ SvREFCNT_dec(frame);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec(frame); 4 0 4
859 }
860  
861 113 last_was_begin = false;
862  
863 113 break;
864 }
865 455 case TAG_EVAL_SEQ: {
866 455 ▶ SV *text = read_string(aTHX_ in);
Branch: 1 branch
BranchTrueFalseTotal
SV *text = read_string(aTHX_ in); 455 0 455
867 455 ▶ unsigned long eval_seq = read_varint(in);
Branch: 1 branch
BranchTrueFalseTotal
unsigned long eval_seq = read_varint(in); 455 0 455
868 char file[28]; // 20 for 64-bit int, 7 for (eval ), 1 for null
869 int file_size;
870  
871 455 normalize_references(aTHX_ text);
872 455 file_size = sprintf(file, "(eval %lu)", eval_seq);
873 455 ▶ hv_store(source_code, file, file_size, SvREFCNT_inc(text), 0);
Branch: 1 branch
BranchTrueFalseTotal
hv_store(source_code, file, file_size, SvREFCNT_inc(text), 0); 455 0 455
874 455 break;
875 }
876 22600 case TAG_MAIN_FRAME: {
877 22600 ▶ if (!sample)
Branch: 1 branch
BranchTrueFalseTotal
if (!sample) 0 22600 22600
878 0 croak("Invalid input file: Found stray sub-frame tag without sample-start tag");
879 22600 ▶ SV *file = read_string(aTHX_ in);
Branch: 1 branch
BranchTrueFalseTotal
SV *file = read_string(aTHX_ in); 22600 0 22600
880 22600 ▶ int line = read_varint(in);
Branch: 1 branch
BranchTrueFalseTotal
int line = read_varint(in); 22600 0 22600
881 22600 ▶ HV *frame = newHV();
Branch: 1 branch
BranchTrueFalseTotal
HV *frame = newHV(); 22600 0 22600
882 22600 ▶ bool maybe_eval = map_evals && SvCUR(file) >= 8 && SvPVX(file)[0] == '(';
Branch: 3 branches
BranchTrueFalseTotal
bool maybe_eval = map_evals && SvCUR(file) >= 8 && SvPVX(file)[0] == '('; 52 22548 22600
bool maybe_eval = map_evals && SvCUR(file) >= 8 && SvPVX(file)[0] == '('; 52 0 52
bool maybe_eval = map_evals && SvCUR(file) >= 8 && SvPVX(file)[0] == '('; 0 52 52
883  
884 22600 ▶ if (map_evals) {
Branch: 1 branch
BranchTrueFalseTotal
if (map_evals) { 52 22548 22600
885 52 ▶ eval_remap = eval_remap || maybe_eval;
Branch: 2 branches
BranchTrueFalseTotal
eval_remap = eval_remap || maybe_eval; 48 4 52
eval_remap = eval_remap || maybe_eval; 0 48 48
886 52 ▶ try_eval_remap.push_back(maybe_eval ? SvREFCNT_inc(file) : NULL);
Branch: 2 branches
BranchTrueFalseTotal
try_eval_remap.push_back(maybe_eval ? SvREFCNT_inc(file) : NULL); 0 52 52
try_eval_remap.push_back(maybe_eval ? SvREFCNT_inc(file) : NULL); 52 0 52
887 }
888  
889 22600 ▶ hv_stores(frame, "file", SvREFCNT_inc(file));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(frame, "file", SvREFCNT_inc(file)); 22600 0 22600
890 22600 ▶ hv_stores(frame, "file_pretty", SvREFCNT_inc(file));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(frame, "file_pretty", SvREFCNT_inc(file)); 22600 0 22600
891 22600 ▶ hv_stores(frame, "line", newSViv(line));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(frame, "line", newSViv(line)); 22600 0 22600
hv_stores(frame, "line", newSViv(line)); 22600 0 22600
892  
893 // see comment where last_was_begin is defined
894 22600 ▶ if (PERL_VERSION < 36 || !last_was_begin) {
Branch: 1 branch
BranchTrueFalseTotal
if (PERL_VERSION < 36 || !last_was_begin) { 21638 962 22600
895 21638 ▶ av_push(frames, sv_bless(newRV_noinc((SV *) frame), msf_stash));
Branch: 3 branches
BranchTrueFalseTotal
av_push(frames, sv_bless(newRV_noinc((SV *) frame), msf_stash)); 21638 0 21638
av_push(frames, sv_bless(newRV_noinc((SV *) frame), msf_stash)); 21638 0 21638
av_push(frames, sv_bless(newRV_noinc((SV *) frame), msf_stash)); 21638 0 21638
896 } else {
897 962 ▶ SvREFCNT_dec(frame);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec(frame); 962 0 962
898 }
899  
900 22600 last_was_begin = false;
901  
902 22600 break;
903 }
904 34211 case TAG_SAMPLE_END:
905 34211 ▶ if (!sample)
Branch: 1 branch
BranchTrueFalseTotal
if (!sample) 0 34211 34211
906 0 croak("Invalid input file: Found stray sample-end tag without sample-start tag");
907 34211 ▶ in.skip_bytes(size);
Branch: 1 branch
BranchTrueFalseTotal
in.skip_bytes(size); 34211 0 34211
908  
909 34211 ▶ if (eval_remap) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval_remap) { 4 34207 34211
910 // the source code for an eval might be emitted after
911 // the first stack frame referring to the eval, so we
912 // need to perform the remapping at the end of the
913 // stack frame
914 26 ▶ for (size_t i = 0, n = try_eval_remap.size(); i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0, n = try_eval_remap.size(); i < n; ++i) { 22 4 26
915 22 SV *file = try_eval_remap[i];
916 22 ▶ if (!file)
Branch: 1 branch
BranchTrueFalseTotal
if (!file) 16 6 22
917 16 continue;
918  
919 6 ▶ SV *mapped = maybe_map_eval(file);
Branch: 1 branch
BranchTrueFalseTotal
SV *mapped = maybe_map_eval(file); 6 0 6
920 6 ▶ SV *frame = *av_fetch(frames, i, 0);
Branch: 1 branch
BranchTrueFalseTotal
SV *frame = *av_fetch(frames, i, 0); 6 0 6
921  
922 6 ▶ hv_stores((HV *) SvRV(frame), "file", make_fullfile(aTHX_ genealogy_info, mapped));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores((HV *) SvRV(frame), "file", make_fullfile(aTHX_ genealogy_info, mapped)); 6 0 6
hv_stores((HV *) SvRV(frame), "file", make_fullfile(aTHX_ genealogy_info, mapped)); 6 0 6
923 6 try_eval_remap[i] = NULL;
924 6 ▶ SvREFCNT_dec(file);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec(file); 6 0 6
925 }
926 }
927 34211 ▶ if (map_evals) {
Branch: 1 branch
BranchTrueFalseTotal
if (map_evals) { 504 33707 34211
928 504 eval_remap = false;
929 504 try_eval_remap.clear();
930 }
931  
932 34211 ▶ if (new_metadata)
Branch: 1 branch
BranchTrueFalseTotal
if (new_metadata) 3 34208 34211
933 3 ▶ hv_stores(sample, "metadata", newRV_inc((SV *)new_metadata));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(sample, "metadata", newRV_inc((SV *)new_metadata)); 3 0 3
hv_stores(sample, "metadata", newRV_inc((SV *)new_metadata)); 3 0 3
934 34211 ▶ hv_stores(sample, "sections_changed", sections_changed ? &PL_sv_yes : &PL_sv_no);
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(sample, "sections_changed", sections_changed ? &PL_sv_yes : &PL_sv_no); 0 34211 34211
hv_stores(sample, "sections_changed", sections_changed ? &PL_sv_yes : &PL_sv_no); 34211 0 34211
935 34211 ▶ hv_stores(sample, "metadata_changed", metadata_changed ? &PL_sv_yes : &PL_sv_no);;
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(sample, "metadata_changed", metadata_changed ? &PL_sv_yes : &PL_sv_no);; 3 34208 34211
hv_stores(sample, "metadata_changed", metadata_changed ? &PL_sv_yes : &PL_sv_no);; 34211 0 34211
936 34211 sections_changed = metadata_changed = false;
937 34211 ▶ return sv_bless(newRV_inc((SV *) sample), st_stash);
Branch: 2 branches
BranchTrueFalseTotal
return sv_bless(newRV_inc((SV *) sample), st_stash); 34211 0 34211
return sv_bless(newRV_inc((SV *) sample), st_stash); 34211 0 34211
938 264 case TAG_CUSTOM_META:
939 264 ▶ if (!new_metadata)
Branch: 1 branch
BranchTrueFalseTotal
if (!new_metadata) 259 5 264
940 259 ▶ new_metadata = (HV *)sv_2mortal((SV *)newHV());
Branch: 2 branches
BranchTrueFalseTotal
new_metadata = (HV *)sv_2mortal((SV *)newHV()); 259 0 259
new_metadata = (HV *)sv_2mortal((SV *)newHV()); 259 0 259
941 264 ▶ read_custom_meta_record(size, new_metadata);
Branch: 1 branch
BranchTrueFalseTotal
read_custom_meta_record(size, new_metadata); 264 0 264
942 264 metadata_changed = true;
943 264 break;
944 256 case TAG_SECTION_START: {
945 256 ▶ SV *section_name = read_string(aTHX_ in);
Branch: 1 branch
BranchTrueFalseTotal
SV *section_name = read_string(aTHX_ in); 256 0 256
946 256 ▶ HE *depth = hv_fetch_ent(sections, section_name, 1, 0);
Branch: 1 branch
BranchTrueFalseTotal
HE *depth = hv_fetch_ent(sections, section_name, 1, 0); 256 0 256
947 256 ▶ if (!SvOK(HeVAL(depth)))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(HeVAL(depth))) 256 0 256
948 256 ▶ sv_setuv(HeVAL(depth), 1);
Branch: 1 branch
BranchTrueFalseTotal
sv_setuv(HeVAL(depth), 1); 256 0 256
949 else
950 0 ▶ sv_setuv(HeVAL(depth), 1 + SvUV(HeVAL(depth)));
Branch: 2 branches
BranchTrueFalseTotal
sv_setuv(HeVAL(depth), 1 + SvUV(HeVAL(depth))); 0 0 0
sv_setuv(HeVAL(depth), 1 + SvUV(HeVAL(depth))); 0 0 0
951 256 sections_changed = true;
952  
953 if (1) {
954 // horrible temporary hack to avoid the sample after a
955 // section close to be counted as part of the section;
956 // to be fixed by adding a saner API for dealing with
957 // samples inside sections
958 256 ▶ sample = (HV *) sv_2mortal((SV *) newHV());
Branch: 2 branches
BranchTrueFalseTotal
sample = (HV *) sv_2mortal((SV *) newHV()); 256 0 256
sample = (HV *) sv_2mortal((SV *) newHV()); 256 0 256
959 256 ▶ frames = newAV();
Branch: 1 branch
BranchTrueFalseTotal
frames = newAV(); 256 0 256
960  
961 256 ▶ hv_stores(sample, "frames", newRV_noinc((SV *) frames));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(sample, "frames", newRV_noinc((SV *) frames)); 256 0 256
hv_stores(sample, "frames", newRV_noinc((SV *) frames)); 256 0 256
962 256 ▶ hv_stores(sample, "weight", newSViv(0));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(sample, "weight", newSViv(0)); 256 0 256
hv_stores(sample, "weight", newSViv(0)); 256 0 256
963 256 ▶ hv_stores(sample, "op_name", &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(sample, "op_name", &PL_sv_no); 256 0 256
964  
965 256 ▶ if (new_metadata)
Branch: 1 branch
BranchTrueFalseTotal
if (new_metadata) 6 250 256
966 6 ▶ hv_stores(sample, "metadata", newRV_inc((SV *)new_metadata));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(sample, "metadata", newRV_inc((SV *)new_metadata)); 6 0 6
hv_stores(sample, "metadata", newRV_inc((SV *)new_metadata)); 6 0 6
967 256 ▶ hv_stores(sample, "sections_changed", sections_changed ? &PL_sv_yes : &PL_sv_no);
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(sample, "sections_changed", sections_changed ? &PL_sv_yes : &PL_sv_no); 256 0 256
hv_stores(sample, "sections_changed", sections_changed ? &PL_sv_yes : &PL_sv_no); 256 0 256
968 256 ▶ hv_stores(sample, "metadata_changed", metadata_changed ? &PL_sv_yes : &PL_sv_no);;
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(sample, "metadata_changed", metadata_changed ? &PL_sv_yes : &PL_sv_no);; 6 250 256
hv_stores(sample, "metadata_changed", metadata_changed ? &PL_sv_yes : &PL_sv_no);; 256 0 256
969 256 sections_changed = metadata_changed = false;
970 256 ▶ return sv_bless(newRV_inc((SV *) sample), st_stash);
Branch: 2 branches
BranchTrueFalseTotal
return sv_bless(newRV_inc((SV *) sample), st_stash); 256 0 256
return sv_bless(newRV_inc((SV *) sample), st_stash); 256 0 256
971 }
972  
973 break;
974 }
975 255 case TAG_SECTION_END: {
976 255 ▶ SV *section_name = read_string(aTHX_ in);
Branch: 1 branch
BranchTrueFalseTotal
SV *section_name = read_string(aTHX_ in); 255 0 255
977 255 ▶ HE *depth= hv_fetch_ent(sections, section_name, 0, 0);
Branch: 1 branch
BranchTrueFalseTotal
HE *depth= hv_fetch_ent(sections, section_name, 0, 0); 255 0 255
978 255 ▶ if (!depth || !SvOK(HeVAL(depth)) || SvUV(HeVAL(depth)) == 0) {
Branch: 5 branches
BranchTrueFalseTotal
if (!depth || !SvOK(HeVAL(depth)) || SvUV(HeVAL(depth)) == 0) { 255 0 255
if (!depth || !SvOK(HeVAL(depth)) || SvUV(HeVAL(depth)) == 0) { 255 0 255
if (!depth || !SvOK(HeVAL(depth)) || SvUV(HeVAL(depth)) == 0) { 255 0 255
if (!depth || !SvOK(HeVAL(depth)) || SvUV(HeVAL(depth)) == 0) { 0 255 255
if (!depth || !SvOK(HeVAL(depth)) || SvUV(HeVAL(depth)) == 0) { 0 255 255
979 STRLEN len;
980 0 char *str = SvPV(section_name, len);
981 0 croak("Invalid input file: Unmatched section end for '%.*s'", (int) len, str);
982 }
983  
984 255 ▶ const UV depth_num = SvUV(HeVAL(depth));
Branch: 1 branch
BranchTrueFalseTotal
const UV depth_num = SvUV(HeVAL(depth)); 255 0 255
985 255 ▶ if (depth_num == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (depth_num == 1) 255 0 255
986 255 ▶ hv_delete_ent(sections, section_name, G_DISCARD, 0);
Branch: 1 branch
BranchTrueFalseTotal
hv_delete_ent(sections, section_name, G_DISCARD, 0); 255 0 255
987 else
988 0 ▶ sv_setuv(HeVAL(depth), depth_num - 1);
Branch: 1 branch
BranchTrueFalseTotal
sv_setuv(HeVAL(depth), depth_num - 1); 0 0 0
989 255 sections_changed = true;
990  
991 if (1) {
992 // horrible temporary hack to avoid the sample after a
993 // section close to be counted as part of the section;
994 // to be fixed by adding a saner API for dealing with
995 // samples inside sections
996 255 ▶ sample = (HV *) sv_2mortal((SV *) newHV());
Branch: 2 branches
BranchTrueFalseTotal
sample = (HV *) sv_2mortal((SV *) newHV()); 255 0 255
sample = (HV *) sv_2mortal((SV *) newHV()); 255 0 255
997 255 ▶ frames = newAV();
Branch: 1 branch
BranchTrueFalseTotal
frames = newAV(); 255 0 255
998  
999 255 ▶ hv_stores(sample, "frames", newRV_noinc((SV *) frames));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(sample, "frames", newRV_noinc((SV *) frames)); 255 0 255
hv_stores(sample, "frames", newRV_noinc((SV *) frames)); 255 0 255
1000 255 ▶ hv_stores(sample, "weight", newSViv(0));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(sample, "weight", newSViv(0)); 255 0 255
hv_stores(sample, "weight", newSViv(0)); 255 0 255
1001 255 ▶ hv_stores(sample, "op_name", &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(sample, "op_name", &PL_sv_no); 255 0 255
1002  
1003 255 ▶ if (new_metadata)
Branch: 1 branch
BranchTrueFalseTotal
if (new_metadata) 250 5 255
1004 250 ▶ hv_stores(sample, "metadata", newRV_inc((SV *)new_metadata));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(sample, "metadata", newRV_inc((SV *)new_metadata)); 250 0 250
hv_stores(sample, "metadata", newRV_inc((SV *)new_metadata)); 250 0 250
1005 255 ▶ hv_stores(sample, "sections_changed", sections_changed ? &PL_sv_yes : &PL_sv_no);
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(sample, "sections_changed", sections_changed ? &PL_sv_yes : &PL_sv_no); 255 0 255
hv_stores(sample, "sections_changed", sections_changed ? &PL_sv_yes : &PL_sv_no); 255 0 255
1006 255 ▶ hv_stores(sample, "metadata_changed", metadata_changed ? &PL_sv_yes : &PL_sv_no);;
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(sample, "metadata_changed", metadata_changed ? &PL_sv_yes : &PL_sv_no);; 250 5 255
hv_stores(sample, "metadata_changed", metadata_changed ? &PL_sv_yes : &PL_sv_no);; 255 0 255
1007 255 sections_changed = metadata_changed = false;
1008 255 ▶ return sv_bless(newRV_inc((SV *) sample), st_stash);
Branch: 2 branches
BranchTrueFalseTotal
return sv_bless(newRV_inc((SV *) sample), st_stash); 255 0 255
return sv_bless(newRV_inc((SV *) sample), st_stash); 255 0 255
1009 }
1010  
1011 break;
1012 }
1013 61 case TAG_STREAM_END:
1014 61 stream_ended = true;
1015 61 file_ended = true;
1016 61 break;
1017 1421 case TAG_FILE_END:
1018 1421 file_ended = true;
1019 1421 break;
1020 } // end switch
1021 251529 }
1022 37372 }
1023  
1024 50098 HV *TraceFileReader::get_custom_metadata()
1025 {
1026 50098 return custom_metadata;
1027 }
1028  
1029 9009 void TraceFileReader::clear_custom_metadata()
1030 {
1031 9009 hv_clear(custom_metadata);
1032 9009 }
1033  
1034 708 void TraceFileReader::delete_custom_metadata(AV *keys)
1035 {
1036 1416 ▶ for (int i = 0, max = AvFILL(keys); i <= max; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0, max = AvFILL(keys); i <= max; ++i) { 708 708 1416
1037 708 SV **key = av_fetch(keys, i, 0);
1038  
1039 708 ▶ if (key)
Branch: 1 branch
BranchTrueFalseTotal
if (key) 708 0 708
1040 708 hv_delete_ent(custom_metadata, *key, G_DISCARD, 0);
1041 }
1042 708 }
1043  
1044 16002 HV *TraceFileReader::get_source_code()
1045 {
1046 16002 return source_code;
1047 }
1048  
1049 32846 HV *TraceFileReader::get_active_sections()
1050 {
1051 32846 return sections;
1052 }
1053  
1054  
1055 58 TraceFileWriter::TraceFileWriter(pTHX) :
1056 58 force_empty_frame(false)
1057 {
1058 SET_THX_MEMBER
1059 58 }
1060  
1061 58 TraceFileWriter::~TraceFileWriter()
1062 {
1063 58 close();
1064 58 }
1065  
1066 24243 long TraceFileWriter::position() const
1067 {
1068 24243 return out.position();
1069 }
1070  
1071 919 int TraceFileWriter::open(const std::string &path, bool is_template, uint32_t id[ID_SIZE], unsigned int ordinal)
1072 {
1073 919 ▶ close();
Branch: 1 branch
BranchTrueFalseTotal
close(); 919 0 919
1074 919 ▶ output_file = path;
Branch: 1 branch
BranchTrueFalseTotal
output_file = path; 919 0 919
1075  
1076 919 ▶ if (is_template) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_template) { 878 41 919
1077 878 ▶ output_file += '.';
Branch: 1 branch
BranchTrueFalseTotal
output_file += '.'; 878 0 878
1078 6146 ▶ for (int i = 0; i < ID_SIZE; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < ID_SIZE; ++i) 5268 878 6146
1079 5268 ▶ append_hex(output_file, id[i]);
Branch: 1 branch
BranchTrueFalseTotal
append_hex(output_file, id[i]); 5268 0 5268
1080  
1081 878 ▶ output_file += '.';
Branch: 1 branch
BranchTrueFalseTotal
output_file += '.'; 878 0 878
1082 878 ▶ append_hex(output_file, ordinal);
Branch: 1 branch
BranchTrueFalseTotal
append_hex(output_file, ordinal); 878 0 878
1083 }
1084  
1085 919 ▶ string temp = output_file + "_";
Branch: 1 branch
BranchTrueFalseTotal
string temp = output_file + "_"; 919 0 919
1086  
1087 919 ▶ out.open(fopen(temp.c_str(), "wb"));
Branch: 2 branches
BranchTrueFalseTotal
out.open(fopen(temp.c_str(), "wb")); 919 0 919
out.open(fopen(temp.c_str(), "wb")); 919 0 919
1088 919 ▶ if (!out.is_valid())
Branch: 1 branch
BranchTrueFalseTotal
if (!out.is_valid()) 0 919 919
1089 0 return 1;
1090  
1091 919 return 0;
1092 919 }
1093  
1094 919 int TraceFileWriter::write_perl_version()
1095 {
1096 919 int status = 0;
1097 919 status += out.write_byte(TAG_META_PERL_VERSION);
1098 919 status += write_varint(out, PERL_REVISION);
1099 919 status += write_varint(out, PERL_VERSION);
1100 919 status += write_varint(out, PERL_SUBVERSION);
1101 919 return status;
1102 }
1103  
1104 919 int TraceFileWriter::write_header(unsigned int sampling_interval,
1105 unsigned int stack_collect_depth,
1106 uint32_t id[ID_SIZE], unsigned int ordinal,
1107 uint32_t parent_id[ID_SIZE], unsigned int parent_ordinal,
1108 HV *global_metadata)
1109 {
1110 919 int status = 0;
1111 919 status += out.write_raw_bytes(FILE_MAGIC, sizeof(FILE_MAGIC) - 1);
1112 919 status += out.write_raw_byte(FORMAT_VERSION);
1113  
1114 // Write meta data: Perl version, tick duration, stack sample depth
1115 919 status += write_perl_version();
1116  
1117 919 status += out.write_byte(TAG_META_TICK_DURATION);
1118 919 status += write_varint(out, sampling_interval);
1119  
1120 919 status += out.write_byte(TAG_META_STACK_SAMPLE_DEPTH);
1121 919 status += write_varint(out, stack_collect_depth);
1122  
1123 919 status += out.write_byte(TAG_META_GENEALOGY);
1124 919 status += write_varint(out, ordinal);
1125 919 status += write_varint(out, parent_ordinal);
1126 919 status += out.write_bytes(id, sizeof(id[0]) * ID_SIZE);
1127 919 status += out.write_bytes(parent_id, sizeof(parent_id[0]) * ID_SIZE);
1128  
1129 919 ▶ if (global_metadata) {
Branch: 1 branch
BranchTrueFalseTotal
if (global_metadata) { 0 919 919
1130 char *key;
1131 I32 length;
1132  
1133 0 ▶ hv_iterinit(global_metadata);
Branch: 1 branch
BranchTrueFalseTotal
hv_iterinit(global_metadata); 0 0 0
1134 0 ▶ while (SV *value = hv_iternextsv(global_metadata, &key, &length)) {
Branch: 2 branches
BranchTrueFalseTotal
while (SV *value = hv_iternextsv(global_metadata, &key, &length)) { 0 0 0
while (SV *value = hv_iternextsv(global_metadata, &key, &length)) { 0 0 0
1135 0 ▶ write_custom_metadata(key, length, value);
Branch: 1 branch
BranchTrueFalseTotal
write_custom_metadata(key, length, value); 0 0 0
1136 0 }
1137 }
1138  
1139 919 status += out.write_byte(TAG_HEADER_SEPARATOR);
1140  
1141 919 flush();
1142  
1143 919 return status;
1144 }
1145  
1146 1950 void TraceFileWriter::close(bool write_end_tag)
1147 {
1148 1950 ▶ if (!out.is_valid())
Branch: 1 branch
BranchTrueFalseTotal
if (!out.is_valid()) 1042 908 1950
1149 1042 return;
1150  
1151 908 ▶ string temp = output_file + "_";
Branch: 1 branch
BranchTrueFalseTotal
string temp = output_file + "_"; 908 0 908
1152  
1153 908 ▶ out.write_byte(write_end_tag ? TAG_STREAM_END : TAG_FILE_END);
Branch: 2 branches
BranchTrueFalseTotal
out.write_byte(write_end_tag ? TAG_STREAM_END : TAG_FILE_END); 59 849 908
out.write_byte(write_end_tag ? TAG_STREAM_END : TAG_FILE_END); 908 0 908
1154 908 ▶ write_varint(out, 0);
Branch: 1 branch
BranchTrueFalseTotal
write_varint(out, 0); 908 0 908
1155  
1156 908 ▶ flush();
Branch: 1 branch
BranchTrueFalseTotal
flush(); 908 0 908
1157  
1158 908 ▶ out.close();
Branch: 1 branch
BranchTrueFalseTotal
out.close(); 908 0 908
1159 908 ▶ if (!rename(temp.c_str(), output_file.c_str()))
Branch: 1 branch
BranchTrueFalseTotal
if (!rename(temp.c_str(), output_file.c_str())) 908 0 908
1160 908 unlink(temp.c_str());
1161 908 }
1162  
1163 11 void TraceFileWriter::shut()
1164 {
1165 11 out.close();
1166  
1167 11 force_empty_frame = false;
1168 11 }
1169  
1170 1855 void TraceFileWriter::flush()
1171 {
1172 1855 ▶ if (!out.is_valid())
Branch: 1 branch
BranchTrueFalseTotal
if (!out.is_valid()) 0 1855 1855
1173 0 return;
1174  
1175 1855 ▶ if (force_empty_frame) {
Branch: 1 branch
BranchTrueFalseTotal
if (force_empty_frame) { 3 1852 1855
1176 3 start_sample(0, NULL);
1177 3 end_sample();
1178 }
1179  
1180 1855 out.flush();
1181 }
1182  
1183 24246 int TraceFileWriter::start_sample(unsigned int weight, const char *op_name)
1184 {
1185 // TODO maybe track whether we're already in a sample so we can barf if we
1186 // generate nested samples in error? This would also allow for forbidding
1187 // adding section info within a sample, which makes no sense.
1188 24246 int status = 0;
1189  
1190 24246 status += out.write_byte(TAG_SAMPLE_START);
1191 24246 status += write_varint(out, varint_size(weight) + string_size(op_name));
1192 24246 status += write_varint(out, weight);
1193 24246 status += write_string(out, op_name, false);
1194  
1195 24246 return status;
1196 }
1197  
1198 58 int TraceFileWriter::start_section(SV *section_name)
1199 {
1200 // TODO possibly track sections fully to forbid generating invalid sections
1201 58 int status = 0;
1202  
1203 58 status += out.write_byte(TAG_SECTION_START);
1204 58 status += write_varint(out, string_size(aTHX_ section_name));
1205 58 status += write_string(aTHX_ out, section_name);
1206 58 force_empty_frame = false;
1207  
1208 58 return status;
1209 }
1210  
1211 55 int TraceFileWriter::end_section(SV *section_name)
1212 {
1213 // TODO possibly track sections fully to forbid generating invalid sections
1214 55 int status = 0;
1215  
1216 55 status += out.write_byte(TAG_SECTION_END);
1217 55 status += write_varint(out, string_size(aTHX_ section_name));
1218 55 status += write_string(aTHX_ out, section_name);
1219 55 force_empty_frame = true;
1220  
1221 55 return status;
1222 }
1223  
1224 421 int TraceFileWriter::add_eval_source(SV *eval_text, U32 eval_seq)
1225 {
1226 421 int status = 0;
1227 421 int eval_size = SvCUR(eval_text);
1228 421 const char *eval = SvPVX(eval_text) ;
1229  
1230 // the code in toke.c:Perl_lex_start appends "\n;" to the string
1231 // if it does not end in ";" already; this is an heuristic to try
1232 // to get the original source code
1233 421 ▶ if (eval_size >= 2 && eval[eval_size - 1] == ';' && eval[eval_size - 2] == '\n')
Branch: 3 branches
BranchTrueFalseTotal
if (eval_size >= 2 && eval[eval_size - 1] == ';' && eval[eval_size - 2] == '\n') 421 0 421
if (eval_size >= 2 && eval[eval_size - 1] == ';' && eval[eval_size - 2] == '\n') 421 0 421
if (eval_size >= 2 && eval[eval_size - 1] == ';' && eval[eval_size - 2] == '\n') 421 0 421
1234 421 eval_size -= 2;
1235  
1236 421 status += out.write_byte(TAG_EVAL_SEQ);
1237 842 status += write_varint(out, string_size(eval_size) +
1238 421 varint_size(eval_seq));
1239 421 status += write_string(out, eval, eval_size, SvUTF8(eval_text));
1240 421 status += write_varint(out, eval_seq);
1241  
1242 421 return status;
1243 }
1244  
1245 177735 int TraceFileWriter::add_frame(FrameType frame_type, CV *sub, GV *sub_name, COP *line)
1246 {
1247 const char *file;
1248 size_t file_size;
1249 177735 int lineno, status = 0;
1250  
1251 177735 ▶ if (frame_type != FRAME_XSUB) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame_type != FRAME_XSUB) { 173869 3866 177735
1252 173869 ▶ file = OutCopFILE(line);
Branch: 1 branch
BranchTrueFalseTotal
file = OutCopFILE(line); 173869 0 173869
1253 173869 ▶ file_size = file ? strlen(file) : 0;
Branch: 1 branch
BranchTrueFalseTotal
file_size = file ? strlen(file) : 0; 173869 0 173869
1254 173869 lineno = CopLINE(line);
1255 }
1256  
1257 // require: cx->blk_eval.old_namesv
1258 // mPUSHs(newSVsv(cx->blk_eval.old_namesv));
1259  
1260 177735 ▶ if (frame_type == FRAME_SUB || frame_type == FRAME_XSUB) {
Branch: 2 branches
BranchTrueFalseTotal
if (frame_type == FRAME_SUB || frame_type == FRAME_XSUB) { 18466 159269 177735
if (frame_type == FRAME_SUB || frame_type == FRAME_XSUB) { 3866 14600 18466
1261 163135 const char *package = "__ANON__", *name = "(unknown)";
1262 163135 bool package_utf8 = false, name_utf8 = false;
1263 163135 size_t package_size = 8, name_size = 9;
1264  
1265 // from Perl_pp_caller
1266 163135 ▶ GV * const cvgv = sub_name ? sub_name : CvGV(sub);
Branch: 1 branch
BranchTrueFalseTotal
GV * const cvgv = sub_name ? sub_name : CvGV(sub); 159269 3866 163135
1267 163135 ▶ if (cvgv && isGV(cvgv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cvgv && isGV(cvgv)) { 163135 0 163135
if (cvgv && isGV(cvgv)) { 163135 0 163135
1268 163135 ▶ GV *egv = GvEGVx(cvgv) ? GvEGVx(cvgv) : cvgv;
Branch: 7 branches
BranchTrueFalseTotal
GV *egv = GvEGVx(cvgv) ? GvEGVx(cvgv) : cvgv; 163135 0 163135
GV *egv = GvEGVx(cvgv) ? GvEGVx(cvgv) : cvgv; 0 163135 163135
GV *egv = GvEGVx(cvgv) ? GvEGVx(cvgv) : cvgv; 0 0 0
GV *egv = GvEGVx(cvgv) ? GvEGVx(cvgv) : cvgv; 163135 0 163135
GV *egv = GvEGVx(cvgv) ? GvEGVx(cvgv) : cvgv; 163135 0 163135
GV *egv = GvEGVx(cvgv) ? GvEGVx(cvgv) : cvgv; 0 163135 163135
GV *egv = GvEGVx(cvgv) ? GvEGVx(cvgv) : cvgv; 0 0 0
1269 163135 HV *stash = GvSTASH(egv);
1270  
1271 163135 ▶ if (stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (stash) { 163135 0 163135
1272 163135 ▶ package = HvNAME(stash);
Branch: 5 branches
BranchTrueFalseTotal
package = HvNAME(stash); 163135 0 163135
package = HvNAME(stash); 163135 0 163135
package = HvNAME(stash); 0 163135 163135
package = HvNAME(stash); 163135 0 163135
package = HvNAME(stash); 0 163135 163135
1273 #if PERL_VERSION >= 16
1274 163135 ▶ package_utf8 = HvNAMEUTF8(stash);
Branch: 6 branches
BranchTrueFalseTotal
package_utf8 = HvNAMEUTF8(stash); 163135 0 163135
package_utf8 = HvNAMEUTF8(stash); 163135 0 163135
package_utf8 = HvNAMEUTF8(stash); 0 163135 163135
package_utf8 = HvNAMEUTF8(stash); 163135 0 163135
package_utf8 = HvNAMEUTF8(stash); 0 163135 163135
package_utf8 = HvNAMEUTF8(stash); 0 163135 163135
1275 163135 ▶ package_size = HvNAMELEN(stash);
Branch: 5 branches
BranchTrueFalseTotal
package_size = HvNAMELEN(stash); 163135 0 163135
package_size = HvNAMELEN(stash); 163135 0 163135
package_size = HvNAMELEN(stash); 0 163135 163135
package_size = HvNAMELEN(stash); 163135 0 163135
package_size = HvNAMELEN(stash); 0 163135 163135
1276 #else
1277 package_utf8 = 0;
1278 package_size = strlen(package);
1279 #endif
1280 }
1281 163135 name = GvNAME(egv);
1282 163135 name_utf8 = GvNAMEUTF8(egv);
1283 163135 name_size = GvNAMELEN(egv);
1284 }
1285  
1286 163135 ▶ if (frame_type == FRAME_SUB) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame_type == FRAME_SUB) { 159269 3866 163135
1287 159269 OP *first = CvSTART(sub);
1288 159269 COP *first_line = NULL;
1289  
1290 159269 ▶ if (first->op_type == OP_NEXTSTATE) {
Branch: 1 branch
BranchTrueFalseTotal
if (first->op_type == OP_NEXTSTATE) { 159267 2 159269
1291 159267 first_line = (COP *) first;
1292 }
1293 #if PERL_VERSION >= 18
1294 2 ▶ else if (first->op_type == OP_INTROCV) {
Branch: 1 branch
BranchTrueFalseTotal
else if (first->op_type == OP_INTROCV) { 2 0 2
1295 2 OP *lineseq1 = ((UNOP *) CvROOT(sub))->op_first;
1296 2 OP *lineseq2 = ((UNOP *) lineseq1)->op_first;
1297 2 OP *nextstate = lineseq2->op_next;
1298  
1299 2 ▶ first_line = nextstate && nextstate->op_type == OP_NEXTSTATE ? (COP *) nextstate : NULL;
Branch: 2 branches
BranchTrueFalseTotal
first_line = nextstate && nextstate->op_type == OP_NEXTSTATE ? (COP *) nextstate : NULL; 2 0 2
first_line = nextstate && nextstate->op_type == OP_NEXTSTATE ? (COP *) nextstate : NULL; 2 0 2
1300 }
1301 #endif
1302  
1303 159269 ▶ int first_lineno = first_line ? CopLINE(first_line) : -1;
Branch: 1 branch
BranchTrueFalseTotal
int first_lineno = first_line ? CopLINE(first_line) : -1; 159269 0 159269
1304  
1305 159269 status += out.write_byte(TAG_SUB_FRAME);
1306 318538 status += write_varint(out, string_size(package_size) +
1307 159269 string_size(name_size) +
1308 159269 string_size(file_size) +
1309 159269 varint_size(lineno) +
1310 159269 varint_size(first_lineno));
1311 159269 status += write_string(out, package, package_size, package_utf8);
1312 159269 status += write_string(out, name, name_size, name_utf8);
1313 159269 status += write_string(out, file, file_size, false);
1314 159269 status += write_varint(out, lineno);
1315 159269 status += write_varint(out, first_lineno);
1316 } else {
1317 3866 status += out.write_byte(TAG_XSUB_FRAME);
1318 7732 status += write_varint(out, string_size(package_size) +
1319 3866 string_size(name_size));
1320 3866 status += write_string(out, package, package_size, package_utf8);
1321 3866 status += write_string(out, name, name_size, name_utf8);
1322 }
1323 177735 ▶ } else if (frame_type == FRAME_EVAL) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (frame_type == FRAME_EVAL) { 131 14469 14600
1324 131 status += out.write_byte(TAG_EVAL_FRAME);
1325 262 status += write_varint(out, string_size(file_size) +
1326 131 varint_size(lineno));
1327 131 status += write_string(out, file, file_size, false);
1328 131 status += write_varint(out, lineno);
1329 } else {
1330 14469 status += out.write_byte(TAG_MAIN_FRAME);
1331 28938 status += write_varint(out, string_size(file_size) +
1332 14469 varint_size(lineno));
1333 14469 status += write_string(out, file, file_size, false);
1334 14469 status += write_varint(out, lineno);
1335 }
1336  
1337 177735 return status;
1338 }
1339  
1340 24246 int TraceFileWriter::end_sample()
1341 {
1342 24246 int status = 0;
1343 24246 status += out.write_byte(TAG_SAMPLE_END);
1344 24246 status += write_varint(out, 0);
1345 24246 force_empty_frame = false;
1346 24246 return status;
1347 }
1348  
1349 0 int TraceFileWriter::write_custom_metadata(const char *key, I32 klen, SV *value)
1350 {
1351 0 int status = 0;
1352 0 status += out.write_byte(TAG_CUSTOM_META);
1353 0 status += write_varint(out, abs(klen) + SvCUR(value));
1354 0 status += write_string(out, key, abs(klen), klen < 0);
1355 0 status += write_string(aTHX_ out, value);
1356 0 force_empty_frame = true;
1357 0 return status;
1358 }
1359  
1360 56 int TraceFileWriter::write_custom_metadata(SV *key, SV *value)
1361 {
1362 56 int status = 0;
1363 56 status += out.write_byte(TAG_CUSTOM_META);
1364 56 status += write_varint(out, SvCUR(key) + SvCUR(value));
1365 56 status += write_string(aTHX_ out, key);
1366 56 status += write_string(aTHX_ out, value);
1367 56 force_empty_frame = true;
1368 56 return status;
1369 }
1370  
« src/snappy.cpp ↑ Summary src/runloop.cpp »