NYTProf.xs

filter: Statementstmt 77.9% 1678 / 2152 Branchbran 56.0% 902 / 1608 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 68.6% 2580 / 3760 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« FileHandle.xs ↑ Summary t/test40pmc.pm »
1 /* vim: ts=8 sw=4 expandtab:
2 * ************************************************************************
3 * This file is part of the Devel::NYTProf package.
4 * Copyright 2008 Adam J. Kaplan, The New York Times Company.
5 * Copyright 2009-2010 Tim Bunce, Ireland.
6 * Released under the same terms as Perl 5.8
7 * See http://metacpan.org/release/Devel-NYTProf/
8 *
9 * Contributors:
10 * Tim Bunce, http://blog.timbunce.org
11 * Nicholas Clark,
12 * Adam Kaplan, akaplan at nytimes.com
13 * Steve Peters, steve at fisharerojo.org
14 *
15 * ************************************************************************
16 */
17 #define PERL_NO_GET_CONTEXT /* we want efficiency */
18  
19 #include "EXTERN.h"
20 #include "perl.h"
21 #include "XSUB.h"
22  
23 #include "FileHandle.h"
24 #include "NYTProf.h"
25  
26 #ifndef NO_PPPORT_H
27 #define NEED_my_snprintf_GLOBAL
28 #define NEED_newRV_noinc_GLOBAL
29 #define NEED_sv_2pv_flags
30 #define NEED_newSVpvn_flags
31 #define NEED_my_strlcat
32 # include "ppport.h"
33 #endif
34  
35 /* Until ppport.h gets this: */
36 #ifndef memEQs
37 # define memEQs(s1, l, s2) \
38 (sizeof(s2)-1 == l && memEQ(s1, ("" s2 ""), (sizeof(s2)-1)))
39 #endif
40  
41 #ifdef USE_HARD_ASSERT
42 #undef NDEBUG
43 #include <assert.h>
44 #endif
45  
46 #if !defined(OutCopFILE)
47 # define OutCopFILE CopFILE
48 #endif
49  
50 #ifndef gv_fetchfile_flags /* added in perl 5.009005 */
51 /* we know our uses don't contain embedded nulls, so we just need to copy to a
52 * buffer so we can add a trailing null byte */
53 #define gv_fetchfile_flags(a,b,c) gv_fetchfile_flags(a,b,c)
54 static GV *
55 gv_fetchfile_flags(pTHX_ const char *const name, const STRLEN namelen, const U32 flags) {
56 char buf[2000];
57 if (namelen >= sizeof(buf)-1)
58 croak("panic: gv_fetchfile_flags overflow");
59 memcpy(buf, name, namelen);
60 buf[namelen] = '\0'; /* null-terminate */
61 return gv_fetchfile(buf);
62 }
63 #endif
64  
65 #ifndef OP_SETSTATE
66 #define OP_SETSTATE OP_NEXTSTATE
67 #endif
68 #ifndef PERLDBf_SAVESRC
69 #define PERLDBf_SAVESRC PERLDBf_SUBLINE
70 #endif
71 #ifndef PERLDBf_SAVESRC_NOSUBS
72 #define PERLDBf_SAVESRC_NOSUBS 0
73 #endif
74 #ifndef CvISXSUB
75 #define CvISXSUB CvXSUB
76 #endif
77  
78 #if (PERL_VERSION < 8) || ((PERL_VERSION == 8) && (PERL_SUBVERSION < 8))
79 /* If we're using DB::DB() instead of opcode redirection with an old perl
80 * then PL_curcop in DB() will refer to the DB() wrapper in Devel/NYTProf.pm
81 * so we'd have to crawl the stack to find the right cop. However, for some
82 * reason that I don't pretend to understand the following expression works:
83 */
84 #define PL_curcop_nytprof (opt_use_db_sub ? ((cxstack + cxstack_ix)->blk_oldcop) : PL_curcop)
85 #else
86 #define PL_curcop_nytprof PL_curcop
87 #endif
88  
89 #define OP_NAME_safe(op) ((op) ? OP_NAME(op) : "NULL")
90  
91 #ifdef I_SYS_TIME
92 #include <sys/time.h>
93 #endif
94 #include <stdio.h>
95  
96 #ifdef HAS_ZLIB
97 #include <zlib.h>
98 #define default_compression_level 6
99 #else
100 #define default_compression_level 0
101 #endif
102 #ifndef ZLIB_VERSION
103 #define ZLIB_VERSION "0"
104 #endif
105  
106 #ifndef NYTP_MAX_SUB_NAME_LEN
107 #define NYTP_MAX_SUB_NAME_LEN 500
108 #endif
109  
110 #define NYTP_FILE_MAJOR_VERSION 5
111 #define NYTP_FILE_MINOR_VERSION 0
112  
113 #define NYTP_START_NO 0
114 #define NYTP_START_BEGIN 1
115 #define NYTP_START_CHECK_unused 2 /* not used */
116 #define NYTP_START_INIT 3
117 #define NYTP_START_END 4
118  
119 #define NYTP_OPTf_ADDPID 0x0001 /* append .pid to output filename */
120 #define NYTP_OPTf_OPTIMIZE 0x0002 /* affect $^P & 0x04 */
121 #define NYTP_OPTf_SAVESRC 0x0004 /* copy source code lines into profile data */
122 #define NYTP_OPTf_ADDTIMESTAMP 0x0008 /* append timestamp to output filename */
123  
124 #define NYTP_FIDf_IS_PMC 0x0001 /* .pm probably really loaded as .pmc */
125 #define NYTP_FIDf_VIA_STMT 0x0002 /* fid first seen by stmt profiler */
126 #define NYTP_FIDf_VIA_SUB 0x0004 /* fid first seen by sub profiler */
127 #define NYTP_FIDf_IS_AUTOSPLIT 0x0008 /* fid is an autosplit (see AutoLoader) */
128 #define NYTP_FIDf_HAS_SRC 0x0010 /* src is available to profiler */
129 #define NYTP_FIDf_SAVE_SRC 0x0020 /* src will be saved by profiler, if NYTP_FIDf_HAS_SRC also set */
130 #define NYTP_FIDf_IS_ALIAS 0x0040 /* fid is clone of the 'parent' fid it was autosplit from */
131 #define NYTP_FIDf_IS_FAKE 0x0080 /* eg dummy caller of a string eval that doesn't have a filename */
132 #define NYTP_FIDf_IS_EVAL 0x0100 /* is an eval */
133  
134 /* indices to elements of the file info array */
135 #define NYTP_FIDi_FILENAME 0
136 #define NYTP_FIDi_EVAL_FID 1
137 #define NYTP_FIDi_EVAL_LINE 2
138 #define NYTP_FIDi_FID 3
139 #define NYTP_FIDi_FLAGS 4
140 #define NYTP_FIDi_FILESIZE 5
141 #define NYTP_FIDi_FILEMTIME 6
142 #define NYTP_FIDi_PROFILE 7
143 #define NYTP_FIDi_EVAL_FI 8
144 #define NYTP_FIDi_HAS_EVALS 9
145 #define NYTP_FIDi_SUBS_DEFINED 10
146 #define NYTP_FIDi_SUBS_CALLED 11
147 #define NYTP_FIDi_elements 12 /* highest index, plus 1 */
148  
149 /* indices to elements of the sub info array (report-side only) */
150 #define NYTP_SIi_FID 0 /* fid of file sub was defined in */
151 #define NYTP_SIi_FIRST_LINE 1 /* line number of first line of sub */
152 #define NYTP_SIi_LAST_LINE 2 /* line number of last line of sub */
153 #define NYTP_SIi_CALL_COUNT 3 /* number of times sub was called */
154 #define NYTP_SIi_INCL_RTIME 4 /* incl real time in sub */
155 #define NYTP_SIi_EXCL_RTIME 5 /* excl real time in sub */
156 #define NYTP_SIi_SUB_NAME 6 /* sub name */
157 #define NYTP_SIi_PROFILE 7 /* ref to profile object */
158 #define NYTP_SIi_REC_DEPTH 8 /* max recursion call depth */
159 #define NYTP_SIi_RECI_RTIME 9 /* recursive incl real time in sub */
160 #define NYTP_SIi_CALLED_BY 10 /* { fid => { line => [...] } } */
161 #define NYTP_SIi_elements 11 /* highest index, plus 1 */
162  
163 /* indices to elements of the sub call info array */
164 /* XXX currently ticks are accumulated into NYTP_SCi_*_TICKS during profiling
165 * and then NYTP_SCi_*_RTIME are calculated and output. This avoids float noise
166 * during profiling but we should really output ticks so the reporting side
167 * can also be more accurate when merging subs, for example.
168 * That'll probably need a file format bump and thus also a major version bump.
169 * Will need corresponding changes to NYTP_SIi_* as well.
170 */
171 #define NYTP_SCi_CALL_COUNT 0 /* count of calls to sub */
172 #define NYTP_SCi_INCL_RTIME 1 /* inclusive real time in sub (set from NYTP_SCi_INCL_TICKS) */
173 #define NYTP_SCi_EXCL_RTIME 2 /* exclusive real time in sub (set from NYTP_SCi_EXCL_TICKS) */
174 #define NYTP_SCi_INCL_TICKS 3 /* inclusive ticks in sub */
175 #define NYTP_SCi_EXCL_TICKS 4 /* exclusive ticks in sub */
176 #define NYTP_SCi_RECI_RTIME 5 /* recursive incl real time in sub */
177 #define NYTP_SCi_REC_DEPTH 6 /* max recursion call depth */
178 #define NYTP_SCi_CALLING_SUB 7 /* name of calling sub */
179 #define NYTP_SCi_elements 8 /* highest index, plus 1 */
180  
181  
182 /* we're not thread-safe (or even multiplicity safe) yet, so detect and bail */
183 #ifdef MULTIPLICITY
184 static PerlInterpreter *orig_my_perl;
185 #endif
186  
187  
188 #define MAX_HASH_SIZE 512
189  
190 typedef struct hash_entry Hash_entry;
191  
192 struct hash_entry {
193 unsigned int id;
194 char* key;
195 int key_len;
196 Hash_entry* next_entry;
197 Hash_entry* next_inserted; /* linked list in insertion order */
198 };
199  
200 typedef struct hash_table {
201 Hash_entry** table;
202 char *name;
203 unsigned int size;
204 unsigned int entry_struct_size;
205 Hash_entry* first_inserted;
206 Hash_entry* prior_inserted; /* = last_inserted before the last insertion */
207 Hash_entry* last_inserted;
208 unsigned int next_id; /* starts at 1, 0 is reserved */
209 } Hash_table;
210  
211 typedef struct {
212 Hash_entry he;
213 unsigned int eval_fid;
214 unsigned int eval_line_num;
215 unsigned int file_size;
216 unsigned int file_mtime;
217 unsigned int fid_flags;
218 char *key_abs;
219 /* update autosplit logic in get_file_id if fields are added or changed */
220 } fid_hash_entry;
221  
222 static Hash_table fidhash = { NULL, "fid", MAX_HASH_SIZE, sizeof(fid_hash_entry), NULL, NULL, NULL, 1 };
223  
224 typedef struct {
225 Hash_entry he;
226 } str_hash_entry;
227 static Hash_table strhash = { NULL, "str", MAX_HASH_SIZE, sizeof(str_hash_entry), NULL, NULL, NULL, 1 };
228 /* END Hash table definitions */
229  
230  
231 /* defaults */
232 static NYTP_file out;
233  
234 /* options and overrides */
235 static char PROF_output_file[MAXPATHLEN+1] = "nytprof.out";
236 static unsigned int profile_opts = NYTP_OPTf_OPTIMIZE | NYTP_OPTf_SAVESRC;
237 static int profile_start = NYTP_START_BEGIN; /* when to start profiling */
238  
239 static char const *nytp_panic_overflow_msg_fmt = "panic: buffer overflow of %s on '%s' (see TROUBLESHOOTING section of the NYTProf documentation)";
240  
241 struct NYTP_options_t {
242 const char *option_name;
243 IV option_iv;
244 char *option_pv; /* strdup'd */
245 };
246  
247 /* XXX boolean options should be moved into profile_opts */
248 static struct NYTP_options_t options[] = {
249 #define profile_usecputime options[0].option_iv
250 { "usecputime", 0, NULL },
251 #define profile_subs options[1].option_iv
252 { "subs", 1, NULL }, /* subroutine times */
253 #define profile_blocks options[2].option_iv
254 { "blocks", 0, NULL }, /* block and sub *exclusive* times */
255 #define profile_leave options[3].option_iv
256 { "leave", 1, NULL }, /* correct block end timing */
257 #define embed_fid_line options[4].option_iv
258 { "expand", 0, NULL },
259 #define trace_level options[5].option_iv
260 { "trace", 0, NULL },
261 #define opt_use_db_sub options[6].option_iv
262 { "use_db_sub", 0, NULL },
263 #define compression_level options[7].option_iv
264 { "compress", default_compression_level, NULL },
265 #define profile_clock options[8].option_iv
266 { "clock", -1, NULL },
267 #define profile_stmts options[9].option_iv
268 { "stmts", 1, NULL }, /* statement exclusive times */
269 #define profile_slowops options[10].option_iv
270 { "slowops", 2, NULL }, /* slow opcodes, typically system calls */
271 #define profile_findcaller options[11].option_iv
272 { "findcaller", 0, NULL }, /* find sub caller instead of trusting outer */
273 #define profile_forkdepth options[12].option_iv
274 { "forkdepth", -1, NULL }, /* how many generations of kids to profile */
275 #define opt_perldb options[13].option_iv
276 { "perldb", 0, NULL }, /* force certain PL_perldb value */
277 #define opt_nameevals options[14].option_iv
278 { "nameevals", 1, NULL }, /* change $^P 0x100 bit */
279 #define opt_nameanonsubs options[15].option_iv
280 { "nameanonsubs", 1, NULL }, /* change $^P 0x200 bit */
281 #define opt_calls options[16].option_iv
282 { "calls", 1, NULL }, /* output call/return event stream */
283 #define opt_evals options[17].option_iv
284 { "evals", 0, NULL } /* handling of string evals - TBD XXX */
285 };
286 /* XXX TODO: add these to options:
287 if (strEQ(option, "file")) {
288 strncpy(PROF_output_file, value, MAXPATHLEN);
289 else if (strEQ(option, "log")) {
290 else if (strEQ(option, "start")) {
291 else if (strEQ(option, "addpid")) {
292 else if (strEQ(option, "optimize") || strEQ(option, "optimise")) {
293 else if (strEQ(option, "savesrc")) {
294 else if (strEQ(option, "endatexit")) {
295 else if (strEQ(option, "libcexit")) {
296 and write the options to the stream when profiling starts.
297 */
298  
299  
300 /* time tracking */
301 #ifdef WIN32
302 /* win32_gettimeofday has ~15 ms resolution on Win32, so use
303 * QueryPerformanceCounter which has us or ns resolution depending on
304 * motherboard and OS. Comment this out to use the old clock.
305 */
306 # define HAS_QPC
307 #endif /* WIN32 */
308  
309 #ifdef HAS_CLOCK_GETTIME
310  
311 /* http://www.freebsd.org/cgi/man.cgi?query=clock_gettime
312 * http://webnews.giga.net.tw/article//mailing.freebsd.performance/710
313 * http://sean.chittenden.org/news/2008/06/01/
314 * Explanation of why gettimeofday() (and presumably CLOCK_REALTIME) may go backwards:
315 * https://groups.google.com/forum/#!topic/comp.os.linux.development.apps/3CkHHyQX918
316 */
317 typedef struct timespec time_of_day_t;
318 # define CLOCK_GETTIME(ts) clock_gettime(profile_clock, ts)
319 # define TICKS_PER_SEC 10000000 /* 10 million - 100ns */
320 # define get_time_of_day(into) CLOCK_GETTIME(&into)
321 # define get_ticks_between(typ, s, e, ticks, overflow) STMT_START { \
322 overflow = 0; \
323 ticks = ((e.tv_sec - s.tv_sec) * TICKS_PER_SEC + (e.tv_nsec / (typ)100) - (s.tv_nsec / (typ)100)); \
324 } STMT_END
325  
326 #else /* !HAS_CLOCK_GETTIME */
327  
328 #ifdef HAS_MACH_TIME
329  
330 #include <mach/mach.h>
331 #include <mach/mach_time.h>
332 mach_timebase_info_data_t our_timebase;
333 typedef uint64_t time_of_day_t;
334 # define TICKS_PER_SEC 10000000 /* 10 million - 100ns */
335 # define get_time_of_day(into) into = mach_absolute_time()
336 # define get_ticks_between(typ, s, e, ticks, overflow) STMT_START { \
337 overflow = 0; \
338 if( our_timebase.denom == 0 ) mach_timebase_info(&our_timebase); \
339 ticks = (e-s) * our_timebase.numer / our_timebase.denom / (typ)100; \
340 } STMT_END
341  
342 #else /* !HAS_MACH_TIME */
343  
344 #ifdef HAS_QPC
345  
346 # ifndef U64_CONST
347 # ifdef _MSC_VER
348 # define U64_CONST(x) x##UI64
349 # else
350 # define U64_CONST(x) x##ULL
351 # endif
352 # endif
353  
354 unsigned __int64 time_frequency = U64_CONST(0);
355 typedef unsigned __int64 time_of_day_t;
356 # define TICKS_PER_SEC time_frequency
357 # define get_time_of_day(into) QueryPerformanceCounter((LARGE_INTEGER*)&into)
358 # define get_ticks_between(typ, s, e, ticks, overflow) STMT_START { \
359 overflow = 0; /* XXX whats this? */ \
360 ticks = (typ)(e-s); \
361 } STMT_END
362  
363 /* workaround for "error C2520: conversion from unsigned __int64 to double not
364 implemented, use signed __int64" on VC 6 */
365 # if defined(_MSC_VER) && _MSC_VER < 1300 /* < VC 7/2003*/
366 # define NYTPIuint642NV(x) \
367 ((NV)(__int64)((x) & U64_CONST(0x7FFFFFFFFFFFFFFF)) \
368 + -(NV)(__int64)((x) & U64_CONST(0x8000000000000000)))
369 # define get_NV_ticks_between(s, e, ticks, overflow) STMT_START { \
370 overflow = 0; /* XXX whats this? */ \
371 ticks = NYTPIuint642NV(e-s); \
372 } STMT_END
373  
374 # endif
375  
376 #elif defined(HAS_GETTIMEOFDAY)
377 /* on Win32 gettimeofday is always implemented in Perl, not the MS C lib, so
378 either we use PerlProc_gettimeofday or win32_gettimeofday, depending on the
379 Perl defines about NO_XSLOCKS and PERL_IMPLICIT_SYS, to simplify logic,
380 we don't check the defines, just the macro symbol to see if it forwards to
381 presumably the iperlsys.h vtable call or not.
382 See https://github.com/timbunce/devel-nytprof/pull/27#issuecomment-46102026
383 for more details.
384 */
385 #if defined(WIN32) && !defined(gettimeofday)
386 # define gettimeofday win32_gettimeofday
387 #endif
388  
389 typedef struct timeval time_of_day_t;
390 # define TICKS_PER_SEC 1000000 /* 1 million */
391 # define get_time_of_day(into) gettimeofday(&into, NULL)
392 # define get_ticks_between(typ, s, e, ticks, overflow) STMT_START { \
393 overflow = 0; \
394 ticks = ((e.tv_sec - s.tv_sec) * TICKS_PER_SEC + e.tv_usec - s.tv_usec); \
395 } STMT_END
396  
397 #else /* !HAS_GETTIMEOFDAY */
398  
399 /* worst-case fallback - use Time::HiRes which is expensive to call */
400 #define WANT_TIME_HIRES
401 typedef UV time_of_day_t[2];
402 # define TICKS_PER_SEC 1000000 /* 1 million */
403 # define get_time_of_day(into) (*time_hires_u2time_hook)(aTHX_ into)
404 # define get_ticks_between(typ, s, e, ticks, overflow) STMT_START { \
405 overflow = 0; \
406 ticks = ((e[0] - s[0]) * (typ)TICKS_PER_SEC + e[1] - s[1]); \
407 } STMT_END
408  
409 static int (*time_hires_u2time_hook)(pTHX_ UV *) = 0;
410  
411 #endif /* HAS_GETTIMEOFDAY else */
412 #endif /* HAS_MACH_TIME else */
413 #endif /* HAS_CLOCK_GETTIME else */
414  
415 #ifndef get_NV_ticks_between
416 # define get_NV_ticks_between(s, e, ticks, overflow) get_ticks_between(NV, s, e, ticks, overflow)
417 #endif
418  
419 #ifndef NYTPIuint642NV
420 # define NYTPIuint642NV(x) ((NV)(x))
421 #endif
422  
423 static time_of_day_t start_time;
424 static time_of_day_t end_time;
425  
426 static unsigned int last_executed_line;
427 static unsigned int last_executed_fid;
428 static char *last_executed_fileptr;
429 static unsigned int last_block_line;
430 static unsigned int last_sub_line;
431 static unsigned int is_profiling; /* disable_profile() & enable_profile() */
432 static Pid_t last_pid = 0;
433 static NV cumulative_overhead_ticks = 0.0;
434 static NV cumulative_subr_ticks = 0.0;
435 static UV cumulative_subr_seqn = 0;
436 static int main_runtime_used = 0;
437 static SV *DB_CHECK_cv;
438 static SV *DB_INIT_cv;
439 static SV *DB_END_cv;
440 static SV *DB_fin_cv;
441 static const char *class_mop_evaltag = " defined at ";
442 static int class_mop_evaltag_len = 12;
443  
444 static unsigned int ticks_per_sec = 0; /* 0 forces error if not set */
445  
446 static AV *slowop_name_cache;
447  
448 /* prototypes */
449 static void output_header(pTHX);
450 static SV *read_str(pTHX_ NYTP_file ifile, SV *sv);
451 static unsigned int get_file_id(pTHX_ char*, STRLEN, int created_via);
452 static void DB_stmt(pTHX_ COP *cop, OP *op);
453 static void set_option(pTHX_ const char*, const char*);
454 static int enable_profile(pTHX_ char *file);
455 static int disable_profile(pTHX);
456 static void finish_profile(pTHX);
457 static void finish_profile_nocontext(void);
458 static void open_output_file(pTHX_ char *);
459 static int reinit_if_forked(pTHX);
460 static int parse_DBsub_value(pTHX_ SV *sv, STRLEN *filename_len_p, UV *first_line_p, UV *last_line_p, char *sub_name);
461 static void write_cached_fids(void);
462 static void write_src_of_files(pTHX);
463 static void write_sub_line_ranges(pTHX);
464 static void write_sub_callers(pTHX);
465 static AV *store_profile_line_entry(pTHX_ SV *rvav, unsigned int line_num,
466 NV time, int count, unsigned int fid);
467  
468 /* copy of original contents of PL_ppaddr */
469 typedef OP * (CPERLscope(*orig_ppaddr_t))(pTHX);
470 orig_ppaddr_t *PL_ppaddr_orig;
471 #define run_original_op(type) CALL_FPTR(PL_ppaddr_orig[type])(aTHX)
472 static OP *pp_entersub_profiler(pTHX);
473 static OP *pp_subcall_profiler(pTHX_ int type);
474 static OP *pp_leave_profiler(pTHX);
475 static HV *sub_callers_hv;
476 static HV *pkg_fids_hv; /* currently just package names */
477  
478 /* PL_sawampersand is disabled in 5.17.7+ 1a904fc */
479 #if (PERL_VERSION < 17) || ((PERL_VERSION == 17) && (PERL_SUBVERSION < 7)) || defined(PERL_SAWAMPERSAND)
480 static U8 last_sawampersand;
481 #define CHECK_SAWAMPERSAND(fid,line) STMT_START { \
482 if (PL_sawampersand != last_sawampersand) { \
483 if (trace_level >= 1) \
484 logwarn("Slow regex match variable seen (0x%x->0x%x at %u:%u)\n", PL_sawampersand, last_sawampersand, fid, line); \
485 /* XXX this is a hack used by test14 to avoid different behaviour \
486 * pre/post perl 5.17.7 since it's not relevant to the test, which is really \
487 * about AutoSplit */ \
488 if (!getenv("DISABLE_NYTPROF_SAWAMPERSAND")) \
489 NYTP_write_sawampersand(out, fid, line); \
490 last_sawampersand = (U8)PL_sawampersand; \
491 } \
492 } STMT_END
493 #else
494 #define CHECK_SAWAMPERSAND(fid,line) (void)0
495 #endif
496  
497 /* macros for outputting profile data */
498 #ifndef HAS_GETPPID
499 #define getppid() 0
500 #endif
501  
502 static FILE *logfh;
503  
504 /* predeclare to set attribute */
505 static void logwarn(const char *pat, ...) __attribute__format__(__printf__,1,2);
506 static void
507 0 logwarn(const char *pat, ...)
508 {
509 /* we avoid using any perl mechanisms here */
510 va_list args;
511 NYTP_IO_dTHX;
512 0 va_start(args, pat);
513 0 ▶ if (!logfh)
Branch: 1 branch
BranchTrueFalseTotal
if (!logfh) 0 0 0
514 0 logfh = stderr;
515 0 vfprintf(logfh, pat, args);
516 /* Flush to ensure the log message gets pushed out to the kernel.
517 * This flush will be expensive but is needed to ensure the log has recent info
518 * if there's a core dump. Could add an option to disable flushing if needed.
519 */
520 0 fflush(logfh);
521 0 va_end(args);
522 0 }
523  
524  
525 /***********************************
526 * Devel::NYTProf Functions *
527 ***********************************/
528  
529 static NV
530 1320 gettimeofday_nv(void)
531 {
532 #ifdef HAS_GETTIMEOFDAY
533  
534 NYTP_IO_dTHX;
535 struct timeval when;
536 1320 gettimeofday(&when, (struct timezone *) 0);
537 1320 return when.tv_sec + (when.tv_usec / 1000000.0);
538  
539 #else
540 #ifdef WANT_TIME_HIRES
541  
542 NYTP_IO_dTHX;
543 UV time_of_day[2];
544 (*time_hires_u2time_hook)(aTHX_ &time_of_day);
545 return time_of_day[0] + (time_of_day[1] / 1000000.0);
546  
547 #else
548  
549 return (NV)time(); /* practically useless */
550  
551 #endif /* WANT_TIME_HIRES else */
552 #endif /* HAS_GETTIMEOFDAY else */
553 }
554  
555  
556 /**
557 * output file header
558 */
559 static void
560 675 output_header(pTHX)
561 {
562 /* $0 - application name */
563 675 SV *const sv = get_sv("0",GV_ADDWARN);
564 675 time_t basetime = PL_basetime;
565 /* This comes back with a terminating \n, and we don't want that. */
566 675 const char *const basetime_str = ctime(&basetime);
567 675 const STRLEN basetime_str_len = strlen(basetime_str);
568 675 const char version[] = STRINGIFY(PERL_REVISION) "."
569 STRINGIFY(PERL_VERSION) "." STRINGIFY(PERL_SUBVERSION);
570 STRLEN len;
571 675 const char *argv0 = SvPV(sv, len);
572  
573 675 ▶ assert(out != NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(out != NULL); 0 675 675
574 /* File header with "magic" string, with file major and minor version */
575 675 NYTP_write_header(out, NYTP_FILE_MAJOR_VERSION, NYTP_FILE_MINOR_VERSION);
576 /* Human readable comments and attributes follow
577 * comments start with '#', end with '\n', and are discarded
578 * attributes start with ':', a word, '=', then the value, then '\n'
579 */
580 675 NYTP_write_comment(out, "Perl profile database. Generated by Devel::NYTProf on %.*s",
581 675 (int)basetime_str_len - 1, basetime_str);
582  
583 /* XXX add options, $0, etc, but beware of embedded newlines */
584 /* XXX would be good to adopt a proper charset & escaping for these */
585 675 NYTP_write_attribute_unsigned(out, STR_WITH_LEN("basetime"), (unsigned long)PL_basetime); /* $^T */
586 675 NYTP_write_attribute_string(out, STR_WITH_LEN("application"), argv0, len);
587 /* perl constants: */
588 675 NYTP_write_attribute_string(out, STR_WITH_LEN("perl_version"), version, sizeof(version) - 1);
589 675 NYTP_write_attribute_unsigned(out, STR_WITH_LEN("nv_size"), sizeof(NV));
590 /* sanity checks: */
591 675 NYTP_write_attribute_string(out, STR_WITH_LEN("xs_version"), STR_WITH_LEN(XS_VERSION));
592 675 NYTP_write_attribute_unsigned(out, STR_WITH_LEN("PL_perldb"), PL_perldb);
593 /* these are really options: */
594 675 NYTP_write_attribute_signed(out, STR_WITH_LEN("clock_id"), profile_clock);
595 675 NYTP_write_attribute_unsigned(out, STR_WITH_LEN("ticks_per_sec"), ticks_per_sec);
596  
597 if (1) {
598 675 struct NYTP_options_t *opt_p = options;
599 675 const struct NYTP_options_t *const opt_end
600 = options + sizeof(options) / sizeof (struct NYTP_options_t);
601 do {
602 12150 NYTP_write_option_iv(out, opt_p->option_name, opt_p->option_iv);
603 12150 ▶ } while (++opt_p < opt_end);
Branch: 1 branch
BranchTrueFalseTotal
} while (++opt_p < opt_end); 11475 675 12150
604 }
605  
606  
607 #ifdef HAS_ZLIB
608 675 ▶ if (compression_level) {
Branch: 1 branch
BranchTrueFalseTotal
if (compression_level) { 370 305 675
609 370 NYTP_start_deflate_write_tag_comment(out, compression_level);
610 }
611 #endif
612  
613 675 NYTP_write_process_start(out, getpid(), getppid(), gettimeofday_nv());
614  
615 675 write_cached_fids(); /* empty initially, non-empty after fork */
616  
617 675 NYTP_flush(out);
618 675 }
619  
620 static SV *
621 217124029 read_str(pTHX_ NYTP_file ifile, SV *sv) {
622 STRLEN len;
623 char *buf;
624 unsigned char tag;
625  
626 217124029 NYTP_read(ifile, &tag, sizeof(tag), "string prefix");
627  
628 217124029 ▶ if (NYTP_TAG_STRING != tag && NYTP_TAG_STRING_UTF8 != tag)
Branch: 2 branches
BranchTrueFalseTotal
if (NYTP_TAG_STRING != tag && NYTP_TAG_STRING_UTF8 != tag) 0 217124029 217124029
if (NYTP_TAG_STRING != tag && NYTP_TAG_STRING_UTF8 != tag) 0 0 0
629 0 croak("Profile format error at offset %ld%s, expected string tag but found %d ('%c') (see TROUBLESHOOTING in NYTProf docs)",
630 NYTP_tell(ifile)-1, NYTP_type_of_offset(ifile), tag, tag);
631  
632 217124029 len = read_u32(ifile);
633 217124029 ▶ if (sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv) { 209731960 7392069 217124029
634 209731960 ▶ SvGROW(sv, len+1); /* forces SVt_PV */
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(sv, len+1); /* forces SVt_PV */ 209731960 0 209731960
SvGROW(sv, len+1); /* forces SVt_PV */ 8628 209723332 209731960
635 }
636 else {
637 7392069 sv = newSV(len+1); /* +1 to force SVt_PV even for 0 length string */
638 }
639 217124029 SvPOK_on(sv);
640  
641 217124029 buf = SvPV_nolen(sv);
642 217124029 NYTP_read(ifile, buf, len, "string");
643 217124029 ▶ SvCUR_set(sv, len);
Branch: 6 branches
BranchTrueFalseTotal
SvCUR_set(sv, len); 0 217124029 217124029
SvCUR_set(sv, len); 0 217124029 217124029
SvCUR_set(sv, len); 0 0 0
SvCUR_set(sv, len); 0 0 0
SvCUR_set(sv, len); 0 217124029 217124029
SvCUR_set(sv, len); 0 0 0
644 217124029 *SvEND(sv) = '\0';
645  
646 217124029 ▶ if (NYTP_TAG_STRING_UTF8 == tag)
Branch: 1 branch
BranchTrueFalseTotal
if (NYTP_TAG_STRING_UTF8 == tag) 0 217124029 217124029
647 0 SvUTF8_on(sv);
648  
649 217124029 ▶ if (trace_level >= 19) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 19) { 0 217124029 217124029
650 0 STRLEN len2 = len;
651 0 const char *newline = "";
652 0 ▶ if (buf[len2-1] == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (buf[len2-1] == '\n') { 0 0 0
653 0 --len2;
654 0 newline = "\\n";
655 }
656 0 logwarn(" read string '%.*s%s'%s\n", (int)len2, SvPV_nolen(sv),
657 0 ▶ newline, (SvUTF8(sv)) ? " (utf8)" : "");
Branch: 1 branch
BranchTrueFalseTotal
newline, (SvUTF8(sv)) ? " (utf8)" : ""); 0 0 0
658 }
659  
660 217124029 return sv;
661 }
662  
663  
664 /**
665 * An implementation of the djb2 hash function by Dan Bernstein.
666 */
667 static unsigned long
668 18022557 hash (char* _str, unsigned int len)
669 {
670 18022557 char* str = _str;
671 18022557 unsigned long hash = 5381;
672  
673 1058821607 ▶ while (len--) {
Branch: 1 branch
BranchTrueFalseTotal
while (len--) { 1040799050 18022557 1058821607
674 /* hash * 33 + c */
675 1040799050 hash = ((hash << 5) + hash) + *str++;
676 }
677 18022557 return hash;
678 }
679  
680 /**
681 * Returns a pointer to the ')' after the digits in the (?:re_)?eval prefix.
682 * As the prefix length is known, this gives the length of the digits.
683 */
684 static const char *
685 17 eval_prefix(const char *filename, const char *prefix, STRLEN prefix_len) {
686 17 ▶ if (memEQ(filename, prefix, prefix_len)
Branch: 1 branch
BranchTrueFalseTotal
if (memEQ(filename, prefix, prefix_len) 17 0 17
687 17 ▶ && isdigit((int)filename[prefix_len])) {
Branch: 1 branch
BranchTrueFalseTotal
&& isdigit((int)filename[prefix_len])) { 17 0 17
688 17 const char *s = filename + prefix_len + 1;
689  
690 50 ▶ while (isdigit((int)*s))
Branch: 1 branch
BranchTrueFalseTotal
while (isdigit((int)*s)) 33 17 50
691 33 ++s;
692 17 ▶ if (s[0] == ')')
Branch: 1 branch
BranchTrueFalseTotal
if (s[0] == ')') 17 0 17
693 17 return s;
694 }
695 0 return NULL;
696 }
697  
698 /**
699 * Return true if filename looks like an eval
700 */
701 static int
702 7076 filename_is_eval(const char *filename, STRLEN filename_len)
703 {
704 7076 ▶ if (filename_len < 6)
Branch: 1 branch
BranchTrueFalseTotal
if (filename_len < 6) 0 7076 7076
705 0 return 0;
706 /* typically "(eval N)[...]" sometimes just "(eval N)" */
707 7076 ▶ if (filename[filename_len - 1] != ']' && filename[filename_len - 1] != ')')
Branch: 2 branches
BranchTrueFalseTotal
if (filename[filename_len - 1] != ']' && filename[filename_len - 1] != ')') 7075 1 7076
if (filename[filename_len - 1] != ']' && filename[filename_len - 1] != ')') 7059 16 7075
708 7059 return 0;
709 17 ▶ if (eval_prefix(filename, "(eval ", 6))
Branch: 1 branch
BranchTrueFalseTotal
if (eval_prefix(filename, "(eval ", 6)) 17 0 17
710 17 return 1;
711 0 ▶ if (eval_prefix(filename, "(re_eval ", 9))
Branch: 1 branch
BranchTrueFalseTotal
if (eval_prefix(filename, "(re_eval ", 9)) 0 0 0
712 0 return 1;
713 0 return 0;
714 }
715  
716  
717 /**
718 * Fetch/Store on hash table. entry must always be defined.
719 * hash_op will find hash_entry in the hash table.
720 * hash_entry not in table, insert is false: returns NULL
721 * hash_entry not in table, insert is true: inserts hash_entry and returns hash_entry
722 * hash_entry in table, insert IGNORED: returns pointer to the actual hash entry
723 */
724 static char
725 18022557 hash_op(Hash_table *hashtable, char *key, int key_len, Hash_entry** retval, bool insert)
726 {
727 18022557 unsigned long h = hash(key, key_len) % hashtable->size;
728  
729 18022557 Hash_entry* found = hashtable->table[h];
730 18046003 ▶ while(NULL != found) {
Branch: 1 branch
BranchTrueFalseTotal
while(NULL != found) { 17896213 149790 18046003
731  
732 17896213 ▶ if (found->key_len == key_len
Branch: 1 branch
BranchTrueFalseTotal
if (found->key_len == key_len 17871920 24293 17896213
733 17871920 ▶ && memEQ(found->key, key, key_len)
Branch: 1 branch
BranchTrueFalseTotal
&& memEQ(found->key, key, key_len) 17871920 0 17871920
734 ) {
735 17871920 *retval = found;
736 17871920 return 0;
737 }
738  
739 24293 ▶ if (NULL == found->next_entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == found->next_entry) { 847 23446 24293
740 847 ▶ if (insert) {
Branch: 1 branch
BranchTrueFalseTotal
if (insert) { 847 0 847
741  
742 Hash_entry* e;
743 847 Newc(0, e, hashtable->entry_struct_size, char, Hash_entry);
744 847 memzero(e, hashtable->entry_struct_size);
745 847 e->id = hashtable->next_id++;
746 847 e->next_entry = NULL;
747 847 e->key_len = key_len;
748 847 e->key = (char*)safemalloc(sizeof(char) * key_len + 1);
749 847 e->key[key_len] = '\0';
750 847 memcpy(e->key, key, key_len);
751 847 found->next_entry = e;
752 847 *retval = found->next_entry;
753 847 hashtable->prior_inserted = hashtable->last_inserted;
754 847 hashtable->last_inserted = e;
755 847 return 1;
756 }
757 else {
758 0 *retval = NULL;
759 0 return -1;
760 }
761 }
762 23446 found = found->next_entry;
763 }
764  
765 149790 ▶ if (insert) {
Branch: 1 branch
BranchTrueFalseTotal
if (insert) { 20264 129526 149790
766 Hash_entry* e;
767 20264 Newc(0, e, hashtable->entry_struct_size, char, Hash_entry);
768 20264 memzero(e, hashtable->entry_struct_size);
769 20264 e->id = hashtable->next_id++;
770 20264 e->next_entry = NULL;
771 20264 e->key_len = key_len;
772 20264 e->key = (char*)safemalloc(sizeof(char) * e->key_len + 1);
773 20264 e->key[e->key_len] = '\0';
774 20264 memcpy(e->key, key, key_len);
775  
776 20264 *retval = hashtable->table[h] = e;
777  
778 20264 ▶ if (!hashtable->first_inserted)
Branch: 1 branch
BranchTrueFalseTotal
if (!hashtable->first_inserted) 600 19664 20264
779 600 hashtable->first_inserted = e;
780 20264 hashtable->prior_inserted = hashtable->last_inserted;
781 20264 hashtable->last_inserted = e;
782  
783 20264 return 1;
784 }
785  
786 129526 *retval = NULL;
787 129526 return -1;
788 }
789  
790 static void
791 0 hash_stats(Hash_table *hashtable, int verbosity)
792 {
793 0 int idx = 0;
794 0 int max_chain_len = 0;
795 0 int buckets = 0;
796 0 int items = 0;
797  
798 0 ▶ if (verbosity)
Branch: 1 branch
BranchTrueFalseTotal
if (verbosity) 0 0 0
799 0 warn("%s hash: size %d\n", hashtable->name, hashtable->size);
800 0 ▶ if (!hashtable->table)
Branch: 1 branch
BranchTrueFalseTotal
if (!hashtable->table) 0 0 0
801 0 return;
802  
803 0 ▶ for (idx=0; idx < hashtable->size; ++idx) {
Branch: 1 branch
BranchTrueFalseTotal
for (idx=0; idx < hashtable->size; ++idx) { 0 0 0
804 0 int chain_len = 0;
805  
806 0 Hash_entry *found = hashtable->table[idx];
807 0 ▶ if (!found)
Branch: 1 branch
BranchTrueFalseTotal
if (!found) 0 0 0
808 0 continue;
809  
810 0 ++buckets;
811 0 ▶ while (NULL != found) {
Branch: 1 branch
BranchTrueFalseTotal
while (NULL != found) { 0 0 0
812 0 ++chain_len;
813 0 ++items;
814 0 found = found->next_entry;
815 }
816 0 ▶ if (verbosity)
Branch: 1 branch
BranchTrueFalseTotal
if (verbosity) 0 0 0
817 0 warn("%s hash[%3d]: %d items\n", hashtable->name, idx, chain_len);
818 0 ▶ if (chain_len > max_chain_len)
Branch: 1 branch
BranchTrueFalseTotal
if (chain_len > max_chain_len) 0 0 0
819 0 max_chain_len = chain_len;
820 }
821 /* XXX would be nice to show a histogram of chain lengths */
822 0 warn("%s hash: %d of %d buckets used, %d items, max chain %d\n",
823 hashtable->name, buckets, hashtable->size, items, max_chain_len);
824 }
825  
826  
827 static void
828 22736 emit_fid (fid_hash_entry *fid_info)
829 {
830 22736 char *file_name = fid_info->he.key;
831 22736 STRLEN file_name_len = fid_info->he.key_len;
832 22736 char *file_name_copy = NULL;
833  
834 22736 ▶ if (fid_info->key_abs) {
Branch: 1 branch
BranchTrueFalseTotal
if (fid_info->key_abs) { 580 22156 22736
835 580 file_name = fid_info->key_abs;
836 580 file_name_len = strlen(file_name);
837 }
838  
839 #ifdef WIN32
840 /* Make sure we only use forward slashes in filenames */
841 if (memchr(file_name, '\\', file_name_len)) {
842 STRLEN i;
843 file_name_copy = (char*)safemalloc(file_name_len);
844 for (i=0; i<file_name_len; ++i) {
845 char ch = file_name[i];
846 file_name_copy[i] = ch == '\\' ? '/' : ch;
847 }
848 file_name = file_name_copy;
849 }
850 #endif
851  
852 22736 NYTP_write_new_fid(out, fid_info->he.id, fid_info->eval_fid,
853 fid_info->eval_line_num, fid_info->fid_flags,
854 fid_info->file_size, fid_info->file_mtime,
855 file_name, (I32)file_name_len);
856  
857 22736 ▶ if (file_name_copy)
Branch: 1 branch
BranchTrueFalseTotal
if (file_name_copy) 0 22736 22736
858 0 Safefree(file_name_copy);
859 22736 }
860  
861  
862 /* return true if file is a .pm that was actually loaded as a .pmc */
863 static int
864 21079 fid_is_pmc(pTHX_ fid_hash_entry *fid_info)
865 {
866 21079 int is_pmc = 0;
867 21079 char *file_name = fid_info->he.key;
868 21079 STRLEN len = fid_info->he.key_len;
869 21079 ▶ if (fid_info->key_abs) {
Branch: 1 branch
BranchTrueFalseTotal
if (fid_info->key_abs) { 516 20563 21079
870 516 file_name = fid_info->key_abs;
871 516 len = strlen(file_name);
872 }
873  
874 21079 ▶ if (len > 3 && memEQs(file_name + len - 3, 3, ".pm")) {
Branch: 2 branches
BranchTrueFalseTotal
if (len > 3 && memEQs(file_name + len - 3, 3, ".pm")) { 20977 102 21079
if (len > 3 && memEQs(file_name + len - 3, 3, ".pm")) { 14464 6513 20977
875 /* ends in .pm, ok, does a newer .pmc exist? */
876 /* based on doopen_pm() in perl's pp_ctl.c */
877 14464 SV *const pmcsv = newSV(len + 2);
878 14464 char *const pmc = SvPVX(pmcsv);
879 Stat_t pmstat;
880 Stat_t pmcstat;
881  
882 14464 memcpy(pmc, file_name, len);
883 14464 pmc[len] = 'c';
884 14464 pmc[len + 1] = '\0';
885  
886 14464 ▶ if (PerlLIO_lstat(pmc, &pmcstat) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (PerlLIO_lstat(pmc, &pmcstat) == 0) { 16 14448 14464
887 /* .pmc exists, is it newer than the .pm (if that exists) */
888  
889 /* Keys in the fid_info are explicitly written with a terminating
890 '\0', so it is safe to pass file_name to a system call. */
891 16 ▶ if (PerlLIO_lstat(file_name, &pmstat) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if (PerlLIO_lstat(file_name, &pmstat) < 0 || 16 0 16
892 16 ▶ pmstat.st_mtime < pmcstat.st_mtime) {
Branch: 1 branch
BranchTrueFalseTotal
pmstat.st_mtime < pmcstat.st_mtime) { 16 0 16
893 16 is_pmc = 1; /* hey, maybe it's Larry working on the perl6 compiler */
894 }
895 }
896 14464 SvREFCNT_dec(pmcsv);
897 }
898  
899 21079 return is_pmc;
900 }
901  
902  
903 static char *
904 0 fmt_fid_flags(pTHX_ int fid_flags, char *buf, Size_t len) {
905 0 *buf = '\0';
906 0 ▶ if (fid_flags & NYTP_FIDf_IS_EVAL) my_strlcat(buf, "eval,", len);
Branch: 1 branch
BranchTrueFalseTotal
if (fid_flags & NYTP_FIDf_IS_EVAL) my_strlcat(buf, "eval,", len); 0 0 0
907 0 ▶ if (fid_flags & NYTP_FIDf_IS_FAKE) my_strlcat(buf, "fake,", len);
Branch: 1 branch
BranchTrueFalseTotal
if (fid_flags & NYTP_FIDf_IS_FAKE) my_strlcat(buf, "fake,", len); 0 0 0
908 0 ▶ if (fid_flags & NYTP_FIDf_IS_AUTOSPLIT) my_strlcat(buf, "autosplit,", len);
Branch: 1 branch
BranchTrueFalseTotal
if (fid_flags & NYTP_FIDf_IS_AUTOSPLIT) my_strlcat(buf, "autosplit,", len); 0 0 0
909 0 ▶ if (fid_flags & NYTP_FIDf_IS_ALIAS) my_strlcat(buf, "alias,", len);
Branch: 1 branch
BranchTrueFalseTotal
if (fid_flags & NYTP_FIDf_IS_ALIAS) my_strlcat(buf, "alias,", len); 0 0 0
910 0 ▶ if (fid_flags & NYTP_FIDf_IS_PMC) my_strlcat(buf, "pmc,", len);
Branch: 1 branch
BranchTrueFalseTotal
if (fid_flags & NYTP_FIDf_IS_PMC) my_strlcat(buf, "pmc,", len); 0 0 0
911 0 ▶ if (fid_flags & NYTP_FIDf_VIA_STMT) my_strlcat(buf, "viastmt,", len);
Branch: 1 branch
BranchTrueFalseTotal
if (fid_flags & NYTP_FIDf_VIA_STMT) my_strlcat(buf, "viastmt,", len); 0 0 0
912 0 ▶ if (fid_flags & NYTP_FIDf_VIA_SUB) my_strlcat(buf, "viasub,", len);
Branch: 1 branch
BranchTrueFalseTotal
if (fid_flags & NYTP_FIDf_VIA_SUB) my_strlcat(buf, "viasub,", len); 0 0 0
913 0 ▶ if (fid_flags & NYTP_FIDf_HAS_SRC) my_strlcat(buf, "hassrc,", len);
Branch: 1 branch
BranchTrueFalseTotal
if (fid_flags & NYTP_FIDf_HAS_SRC) my_strlcat(buf, "hassrc,", len); 0 0 0
914 0 ▶ if (fid_flags & NYTP_FIDf_SAVE_SRC) my_strlcat(buf, "savesrc,", len);
Branch: 1 branch
BranchTrueFalseTotal
if (fid_flags & NYTP_FIDf_SAVE_SRC) my_strlcat(buf, "savesrc,", len); 0 0 0
915 0 ▶ if (*buf) /* trim trailing comma */
Branch: 1 branch
BranchTrueFalseTotal
if (*buf) /* trim trailing comma */ 0 0 0
916 0 buf[ my_strlcat(buf,"",len)-1 ] = '\0';
917 0 return buf;
918 }
919  
920  
921 static void
922 675 write_cached_fids()
923 {
924 675 fid_hash_entry *e = (fid_hash_entry*)fidhash.first_inserted;
925 2332 ▶ while (e) {
Branch: 1 branch
BranchTrueFalseTotal
while (e) { 1657 675 2332
926 1657 ▶ if ( !(e->fid_flags & NYTP_FIDf_IS_ALIAS) )
Branch: 1 branch
BranchTrueFalseTotal
if ( !(e->fid_flags & NYTP_FIDf_IS_ALIAS) ) 1657 0 1657
927 1657 emit_fid(e);
928 1657 e = (fid_hash_entry*)e->he.next_inserted;
929 }
930 675 }
931  
932  
933 static fid_hash_entry *
934 32 find_autosplit_parent(pTHX_ char* file_name)
935 {
936 /* extract basename from file_name, then search for most recent entry
937 * in fidhash that has the same basename
938 */
939 32 fid_hash_entry *e = (fid_hash_entry*)fidhash.first_inserted;
940 32 fid_hash_entry *match = NULL;
941 32 const char *sep = "/";
942 32 char *base_end = strstr(file_name, " (autosplit");
943 32 char *base_start = rninstr(file_name, base_end, sep, sep+1);
944 STRLEN base_len;
945 32 ▶ base_start = (base_start) ? base_start+1 : file_name;
Branch: 1 branch
BranchTrueFalseTotal
base_start = (base_start) ? base_start+1 : file_name; 0 32 32
946 32 base_len = base_end - base_start;
947  
948 32 ▶ if (trace_level >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 3) 0 32 32
949 0 logwarn("find_autosplit_parent of '%.*s' (%s)\n",
950 (int)base_len, base_start, file_name);
951  
952 256 ▶ for ( ; e; e = (fid_hash_entry*)e->he.next_inserted) {
Branch: 1 branch
BranchTrueFalseTotal
for ( ; e; e = (fid_hash_entry*)e->he.next_inserted) { 224 32 256
953 char *e_name;
954  
955 224 ▶ if (e->fid_flags & NYTP_FIDf_IS_AUTOSPLIT)
Branch: 1 branch
BranchTrueFalseTotal
if (e->fid_flags & NYTP_FIDf_IS_AUTOSPLIT) 32 192 224
956 32 continue;
957 192 ▶ if (trace_level >= 4)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 4) 0 192 192
958 0 logwarn("find_autosplit_parent: checking '%.*s'\n", e->he.key_len, e->he.key);
959  
960 /* skip if key is too small to match */
961 192 ▶ if (e->he.key_len < base_len)
Branch: 1 branch
BranchTrueFalseTotal
if (e->he.key_len < base_len) 32 160 192
962 32 continue;
963 /* skip if the last base_len bytes don't match the base name */
964 160 e_name = e->he.key + e->he.key_len - base_len;
965 160 ▶ if (memcmp(e_name, base_start, base_len) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(e_name, base_start, base_len) != 0) 128 32 160
966 128 continue;
967 /* skip if the char before the matched key isn't a separator */
968 32 ▶ if (e->he.key_len > base_len && *(e_name-1) != *sep)
Branch: 2 branches
BranchTrueFalseTotal
if (e->he.key_len > base_len && *(e_name-1) != *sep) 32 0 32
if (e->he.key_len > base_len && *(e_name-1) != *sep) 0 32 32
969 0 continue;
970  
971 32 ▶ if (trace_level >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 3) 0 32 32
972 0 logwarn("matched autosplit '%.*s' to parent fid %d '%.*s' (%c|%c)\n",
973 0 (int)base_len, base_start, e->he.id, e->he.key_len, e->he.key, *(e_name-1),*sep);
974 32 match = e;
975 /* keep looking, so we'll return the most recently profiled match */
976 }
977  
978 32 return match;
979 }
980  
981  
982 #if 0 /* currently unused */
983 static Hash_entry *
984 lookup_file_entry(pTHX_ char* file_name, STRLEN file_name_len) {
985 Hash_entry entry, *found;
986  
987 entry.key = file_name;
988 entry.key_len = (unsigned int)file_name_len;
989 if (hash_op(fidhash, &entry, &found, 0) == 0)
990 return found;
991  
992 return NULL;
993 }
994 #endif
995  
996  
997 /**
998 * Return a unique persistent id number for a file.
999 * If file name has not been seen before
1000 * then, if created_via is false it returns 0 otherwise it
1001 * assigns a new id and outputs the file and id to the stream.
1002 * If the file name is a synthetic name for an eval then
1003 * get_file_id recurses to process the 'embedded' file name first.
1004 * The created_via flag bit is stored in the fid info
1005 * (currently only used as a diagnostic tool)
1006 */
1007 static unsigned int
1008 18022557 get_file_id(pTHX_ char* file_name, STRLEN file_name_len, int created_via)
1009 {
1010  
1011 fid_hash_entry *found, *parent_entry;
1012 18022557 AV *src_av = Nullav;
1013  
1014 18022557 ▶ if (1 != hash_op(&fidhash, file_name, file_name_len, (Hash_entry**)&found, (bool)(created_via ? 1 : 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 != hash_op(&fidhash, file_name, file_name_len, (Hash_entry**)&found, (bool)(created_via ? 1 : 0))) { 18001446 21111 18022557
1015 /* found existing entry or else didn't but didn't create new one either */
1016 18001446 ▶ if (trace_level >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 7) { 0 18001446 18001446
1017 0 ▶ if (found)
Branch: 1 branch
BranchTrueFalseTotal
if (found) 0 0 0
1018 0 logwarn("fid %d: %.*s\n", found->he.id, found->he.key_len, found->he.key);
1019 0 else logwarn("fid -: %.*s not profiled\n", (int)file_name_len, file_name);
1020 }
1021 18001446 ▶ return (found) ? found->he.id : 0;
Branch: 1 branch
BranchTrueFalseTotal
return (found) ? found->he.id : 0; 17871920 129526 18001446
1022 }
1023 /* inserted new entry */
1024 21111 ▶ if (fidhash.prior_inserted)
Branch: 1 branch
BranchTrueFalseTotal
if (fidhash.prior_inserted) 20511 600 21111
1025 20511 fidhash.prior_inserted->next_inserted = fidhash.last_inserted;
1026  
1027 /* if this is a synthetic filename for a string eval
1028 * ie "(eval 42)[/some/filename.pl:line]"
1029 * then ensure we've already generated a fid for the underlying
1030 * filename, and associate that fid with this eval fid
1031 */
1032 21111 ▶ if ('(' == file_name[0]) { /* first char is '(' */
Branch: 1 branch
BranchTrueFalseTotal
if ('(' == file_name[0]) { /* first char is '(' */ 5888 15223 21111
1033 5888 ▶ if (']' == file_name[file_name_len-1]) { /* last char is ']' */
Branch: 1 branch
BranchTrueFalseTotal
if (']' == file_name[file_name_len-1]) { /* last char is ']' */ 5870 18 5888
1034 5870 char *start = strchr(file_name, '[');
1035 5870 const char *colon = ":";
1036 /* can't use strchr here (not nul terminated) so use rninstr */
1037 5870 char *end = rninstr(file_name, file_name+file_name_len-1, colon, colon+1);
1038  
1039 5870 ▶ if (!start || !end || start > end) { /* should never happen */
Branch: 3 branches
BranchTrueFalseTotal
if (!start || !end || start > end) { /* should never happen */ 5870 0 5870
if (!start || !end || start > end) { /* should never happen */ 5870 0 5870
if (!start || !end || start > end) { /* should never happen */ 0 5870 5870
1040 0 logwarn("NYTProf unsupported filename syntax '%s'\n", file_name);
1041 0 return 0;
1042 }
1043 5870 ++start; /* move past [ */
1044 /* recurse */
1045 5870 found->eval_fid = get_file_id(aTHX_ start, end - start,
1046 NYTP_FIDf_IS_EVAL | created_via);
1047 5870 found->eval_line_num = atoi(end+1);
1048 }
1049 18 ▶ else if (filename_is_eval(file_name, file_name_len)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (filename_is_eval(file_name, file_name_len)) { 16 2 18
1050 /* strange eval that doesn't have a filename associated */
1051 /* seen in mod_perl, possibly from eval_sv(sv) api call */
1052 /* also when nameevals=0 option is in effect */
1053 16 char eval_file[] = "/unknown-eval-invoker";
1054 16 found->eval_fid = get_file_id(aTHX_ eval_file, sizeof(eval_file) - 1,
1055 NYTP_FIDf_IS_EVAL | NYTP_FIDf_IS_FAKE | created_via
1056 );
1057 16 found->eval_line_num = 1;
1058 }
1059 }
1060  
1061 /* detect Class::MOP #line evals */
1062 /* See _add_line_directive() in Class::MOP::Method::Generated */
1063 21111 ▶ if (!found->eval_fid) {
Branch: 1 branch
BranchTrueFalseTotal
if (!found->eval_fid) { 15225 5886 21111
1064 15225 ▶ char *tag = ninstr(file_name, file_name+file_name_len, class_mop_evaltag, class_mop_evaltag+class_mop_evaltag_len);
Branch: 2 branches
BranchTrueFalseTotal
char *tag = ninstr(file_name, file_name+file_name_len, class_mop_evaltag, class_mop_evaltag+class_mop_evaltag_len); 0 15225 15225
char *tag = ninstr(file_name, file_name+file_name_len, class_mop_evaltag, class_mop_evaltag+class_mop_evaltag_len); 0 15225 15225
1065 15225 ▶ if (tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag) { 0 15225 15225
1066 0 char *definer = tag + class_mop_evaltag_len;
1067 0 int len = file_name_len - (definer - file_name);
1068 0 found->eval_fid = get_file_id(aTHX_ definer, len, created_via);
1069 0 found->eval_line_num = 1; /* XXX pity Class::MOP doesn't include the line here */
1070 0 ▶ if (trace_level >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) 0 0 0
1071 0 logwarn("Class::MOP eval for '%.*s' (fid %u:%u) from '%.*s'\n",
1072 0 len, definer, found->eval_fid, found->eval_line_num,
1073 (int)file_name_len, file_name);
1074 }
1075 }
1076  
1077 /* is the file is an autosplit, e.g., has a file_name like
1078 * "../../lib/POSIX.pm (autosplit into ../../lib/auto/POSIX/errno.al)"
1079 */
1080 21111 ▶ if ( ')' == file_name[file_name_len-1] && strstr(file_name, " (autosplit ")) {
Branch: 2 branches
BranchTrueFalseTotal
if ( ')' == file_name[file_name_len-1] && strstr(file_name, " (autosplit ")) { 48 21063 21111
if ( ')' == file_name[file_name_len-1] && strstr(file_name, " (autosplit ")) { 32 16 48
1081 32 found->fid_flags |= NYTP_FIDf_IS_AUTOSPLIT;
1082 }
1083  
1084 /* if the file is an autosplit
1085 * then we want it to have the same fid as the file it was split from.
1086 * Thankfully that file will almost certainly be in the fid hash,
1087 * so we can find it and copy the details.
1088 * We do this after the string eval check above in the (untested) hope
1089 * that string evals inside autoloaded subs get treated properly! XXX
1090 */
1091 21111 ▶ if (found->fid_flags & NYTP_FIDf_IS_AUTOSPLIT
Branch: 1 branch
BranchTrueFalseTotal
if (found->fid_flags & NYTP_FIDf_IS_AUTOSPLIT 32 21079 21111
1092 32 ▶ && (parent_entry = find_autosplit_parent(aTHX_ file_name))
Branch: 1 branch
BranchTrueFalseTotal
&& (parent_entry = find_autosplit_parent(aTHX_ file_name)) 32 0 32
1093 ) {
1094 /* copy some details from parent_entry to found */
1095 32 found->he.id = parent_entry->he.id;
1096 32 found->eval_fid = parent_entry->eval_fid;
1097 32 found->eval_line_num = parent_entry->eval_line_num;
1098 32 found->file_size = parent_entry->file_size;
1099 32 found->file_mtime = parent_entry->file_mtime;
1100 32 found->fid_flags = parent_entry->fid_flags;
1101 /* prevent write_cached_fids() from writing this fid */
1102 32 found->fid_flags |= NYTP_FIDf_IS_ALIAS;
1103 /* avoid a gap in the fid sequence */
1104 32 --fidhash.next_id;
1105 /* write a log message if tracing */
1106 32 ▶ if (trace_level >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 2) 0 32 32
1107 0 logwarn("Use fid %2u (after %2u:%-4u) %x e%u:%u %.*s %s\n",
1108 0 found->he.id, last_executed_fid, last_executed_line,
1109 0 found->fid_flags, found->eval_fid, found->eval_line_num,
1110 0 ▶ found->he.key_len, found->he.key, (found->key_abs) ? found->key_abs : "");
Branch: 1 branch
BranchTrueFalseTotal
found->he.key_len, found->he.key, (found->key_abs) ? found->key_abs : ""); 0 0 0
1111 /* bail out without calling emit_fid() */
1112 32 return found->he.id;
1113 }
1114  
1115 /* determine absolute path if file_name is relative */
1116 21079 found->key_abs = NULL;
1117 21079 ▶ if (!found->eval_fid &&
Branch: 1 branch
BranchTrueFalseTotal
if (!found->eval_fid && 15193 5886 21079
1118 15193 ▶ !(file_name[0] == '-'
Branch: 2 branches
BranchTrueFalseTotal
!(file_name[0] == '-' 102 15091 15193
!(file_name[0] == '-' 16 86 102
1119 16 ▶ && (file_name_len==1 || (file_name[1]=='e' && file_name_len==2))) &&
Branch: 2 branches
BranchTrueFalseTotal
&& (file_name_len==1 || (file_name[1]=='e' && file_name_len==2))) && 16 0 16
&& (file_name_len==1 || (file_name[1]=='e' && file_name_len==2))) && 0 16 16
1120 #ifdef WIN32
1121 /* XXX should we check for UNC names too? */
1122 (file_name_len < 3 || !isALPHA(file_name[0]) || file_name[1] != ':' ||
1123 (file_name[2] != '/' && file_name[2] != '\\'))
1124 #else
1125 15091 ▶ *file_name != '/'
Branch: 1 branch
BranchTrueFalseTotal
*file_name != '/' 516 14575 15091
1126 #endif
1127 ) {
1128 char file_name_abs[MAXPATHLEN * 2];
1129 /* Note that the current directory may have changed
1130 * between loading the file and profiling it.
1131 * We don't use realpath() or similar here because we want to
1132 * keep the view of symlinks etc. as the program saw them.
1133 */
1134 516 ▶ if (!getcwd(file_name_abs, sizeof(file_name_abs))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!getcwd(file_name_abs, sizeof(file_name_abs))) { 0 516 516
1135 /* eg permission */
1136 0 logwarn("getcwd: %s\n", strerror(errno));
1137 }
1138 else {
1139 #ifdef WIN32
1140 char *p = file_name_abs;
1141 while (*p) {
1142 if ('\\' == *p)
1143 *p = '/';
1144 ++p;
1145 }
1146 if (p[-1] != '/')
1147 #else
1148 516 ▶ if (strNE(file_name_abs, "/"))
Branch: 1 branch
BranchTrueFalseTotal
if (strNE(file_name_abs, "/")) 516 0 516
1149 #endif
1150 {
1151 516 ▶ if (strnEQ(file_name, "./", 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strnEQ(file_name, "./", 2)) { 0 516 516
1152 0 ++file_name;
1153 } else {
1154 #ifndef VMS
1155 516 strcat(file_name_abs, "/");
1156 #endif
1157 }
1158 }
1159 516 strncat(file_name_abs, file_name, file_name_len);
1160 516 found->key_abs = strdup(file_name_abs);
1161 }
1162 }
1163  
1164 21079 ▶ if (fid_is_pmc(aTHX_ found))
Branch: 1 branch
BranchTrueFalseTotal
if (fid_is_pmc(aTHX_ found)) 16 21063 21079
1165 16 found->fid_flags |= NYTP_FIDf_IS_PMC;
1166 21079 found->fid_flags |= created_via; /* NYTP_FIDf_VIA_STMT or NYTP_FIDf_VIA_SUB */
1167  
1168 /* is source code available? */
1169 /* source only available if PERLDB_LINE or PERLDB_SAVESRC is true */
1170 /* which we set if savesrc option is enabled */
1171 21079 ▶ if ( (src_av = GvAV(gv_fetchfile_flags(found->he.key, found->he.key_len, 0))) )
Branch: 1 branch
BranchTrueFalseTotal
if ( (src_av = GvAV(gv_fetchfile_flags(found->he.key, found->he.key_len, 0))) ) 16239 4840 21079
1172 16239 ▶ if (av_len(src_av) > -1)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(src_av) > -1) 16142 97 16239
1173 16142 found->fid_flags |= NYTP_FIDf_HAS_SRC;
1174  
1175 /* flag "perl -e '...'" and "perl -" as string evals */
1176 21079 ▶ if (found->he.key[0] == '-' && (found->he.key_len == 1 ||
Branch: 2 branches
BranchTrueFalseTotal
if (found->he.key[0] == '-' && (found->he.key_len == 1 || 102 20977 21079
if (found->he.key[0] == '-' && (found->he.key_len == 1 || 16 86 102
1177 16 ▶ (found->he.key[1] == 'e' && found->he.key_len == 2)))
Branch: 2 branches
BranchTrueFalseTotal
(found->he.key[1] == 'e' && found->he.key_len == 2))) 16 0 16
(found->he.key[1] == 'e' && found->he.key_len == 2))) 16 0 16
1178 102 found->fid_flags |= NYTP_FIDf_IS_EVAL;
1179  
1180 /* if it's a string eval or a synthetic filename from CODE ref in @INC,
1181 * then we'd like to save the src (NYTP_FIDf_HAS_SRC) if it's available
1182 */
1183 21079 ▶ if (found->eval_fid
Branch: 1 branch
BranchTrueFalseTotal
if (found->eval_fid 15193 5886 21079
1184 15193 ▶ || (found->fid_flags & NYTP_FIDf_IS_EVAL)
Branch: 1 branch
BranchTrueFalseTotal
|| (found->fid_flags & NYTP_FIDf_IS_EVAL) 15072 121 15193
1185 15072 ▶ || (profile_opts & NYTP_OPTf_SAVESRC)
Branch: 1 branch
BranchTrueFalseTotal
|| (profile_opts & NYTP_OPTf_SAVESRC) 6985 8087 15072
1186 6985 ▶ || (found->he.key_len > 10 && found->he.key[9] == 'x' && strnEQ(found->he.key, "/loader/0x", 10))
Branch: 3 branches
BranchTrueFalseTotal
|| (found->he.key_len > 10 && found->he.key[9] == 'x' && strnEQ(found->he.key, "/loader/0x", 10)) 6881 104 6985
|| (found->he.key_len > 10 && found->he.key[9] == 'x' && strnEQ(found->he.key, "/loader/0x", 10)) 0 6881 6881
|| (found->he.key_len > 10 && found->he.key[9] == 'x' && strnEQ(found->he.key, "/loader/0x", 10)) 0 0 0
1187 ) {
1188 14094 found->fid_flags |= NYTP_FIDf_SAVE_SRC;
1189 }
1190  
1191 21079 emit_fid(found);
1192  
1193 21079 ▶ if (trace_level >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 2) { 0 21079 21079
1194 char buf[80];
1195 /* including last_executed_fid can be handy for tracking down how
1196 * a file got loaded */
1197 0 logwarn("New fid %2u (after %2u:%-4u) 0x%02x e%u:%u %.*s %s %s\n",
1198 0 found->he.id, last_executed_fid, last_executed_line,
1199 0 found->fid_flags, found->eval_fid, found->eval_line_num,
1200 0 ▶ found->he.key_len, found->he.key, (found->key_abs) ? found->key_abs : "",
Branch: 1 branch
BranchTrueFalseTotal
found->he.key_len, found->he.key, (found->key_abs) ? found->key_abs : "", 0 0 0
1201 0 fmt_fid_flags(aTHX_ found->fid_flags, buf, sizeof(buf))
1202 );
1203 }
1204  
1205 21079 return found->he.id;
1206 }
1207  
1208  
1209 static UV
1210 846314 uv_from_av(pTHX_ AV *av, int idx, UV default_uv)
1211 {
1212 846314 SV **svp = av_fetch(av, idx, 0);
1213 846314 ▶ UV uv = (!svp || !SvOK(*svp)) ? default_uv : SvUV(*svp);
Branch: 2 branches
BranchTrueFalseTotal
UV uv = (!svp || !SvOK(*svp)) ? default_uv : SvUV(*svp); 427307 419007 846314
UV uv = (!svp || !SvOK(*svp)) ? default_uv : SvUV(*svp); 427307 0 427307
1214 846314 return uv;
1215 }
1216  
1217 static NV
1218 1269471 nv_from_av(pTHX_ AV *av, int idx, NV default_nv)
1219 {
1220 1269471 SV **svp = av_fetch(av, idx, 0);
1221 1269471 ▶ NV nv = (!svp || !SvOK(*svp)) ? default_nv : SvNV(*svp);
Branch: 2 branches
BranchTrueFalseTotal
NV nv = (!svp || !SvOK(*svp)) ? default_nv : SvNV(*svp); 850464 419007 1269471
NV nv = (!svp || !SvOK(*svp)) ? default_nv : SvNV(*svp); 850464 0 850464
1222 1269471 return nv;
1223 }
1224  
1225  
1226 static const char *
1227 0 cx_block_type(PERL_CONTEXT *cx) {
1228 static char buf[20];
1229 0 switch (CxTYPE(cx)) {
1230 0 case CXt_NULL: return "CXt_NULL";
1231 0 case CXt_SUB: return "CXt_SUB";
1232 0 case CXt_FORMAT: return "CXt_FORMAT";
1233 0 case CXt_EVAL: return "CXt_EVAL";
1234 0 case CXt_SUBST: return "CXt_SUBST";
1235 #ifdef CXt_WHEN
1236 0 case CXt_WHEN: return "CXt_WHEN";
1237 #endif
1238 0 case CXt_BLOCK: return "CXt_BLOCK";
1239 #ifdef CXt_GIVEN
1240 0 case CXt_GIVEN: return "CXt_GIVEN";
1241 #endif
1242 #ifdef CXt_LOOP
1243 case CXt_LOOP: return "CXt_LOOP";
1244 #endif
1245 #ifdef CXt_LOOP_FOR
1246 case CXt_LOOP_FOR: return "CXt_LOOP_FOR";
1247 #endif
1248 #ifdef CXt_LOOP_PLAIN
1249 0 case CXt_LOOP_PLAIN: return "CXt_LOOP_PLAIN";
1250 #endif
1251 #ifdef CXt_LOOP_LAZYSV
1252 0 case CXt_LOOP_LAZYSV: return "CXt_LOOP_LAZYSV";
1253 #endif
1254 #ifdef CXt_LOOP_LAZYIV
1255 0 case CXt_LOOP_LAZYIV: return "CXt_LOOP_LAZYIV";
1256 #endif
1257 #ifdef CXt_LOOP_ARY
1258 0 case CXt_LOOP_ARY: return "CXt_LOOP_ARY";
1259 #endif
1260 #ifdef CXt_LOOP_LIST
1261 0 case CXt_LOOP_LIST: return "CXt_LOOP_LIST";
1262 #endif
1263 }
1264 /* short-lived and not thread safe but we only use this for tracing
1265 * and it should never be reached anyway
1266 */
1267 0 sprintf(buf, "CXt_%ld", (long)CxTYPE(cx));
1268 0 return buf;
1269 }
1270  
1271  
1272 /* based on S_dopoptosub_at() from perl pp_ctl.c */
1273 static int
1274 42460499 dopopcx_at(pTHX_ PERL_CONTEXT *cxstk, I32 startingblock, UV cx_type_mask)
1275 {
1276 I32 i;
1277 PERL_CONTEXT *cx;
1278 42460499 ▶ for (i = startingblock; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = startingblock; i >= 0; i--) { 42451449 9050 42460499
1279 UV type_bit;
1280 42451449 cx = &cxstk[i];
1281 42451449 type_bit = 1 << CxTYPE(cx);
1282 42451449 ▶ if (type_bit & cx_type_mask)
Branch: 1 branch
BranchTrueFalseTotal
if (type_bit & cx_type_mask) 42451449 0 42451449
1283 42451449 return i;
1284 }
1285 9050 return i; /* == -1 */
1286 }
1287  
1288  
1289 static COP *
1290 223094165 start_cop_of_context(pTHX_ PERL_CONTEXT *cx)
1291 {
1292 OP *start_op, *o;
1293 int type;
1294 223094165 int trace = 6;
1295  
1296 223094165 switch (CxTYPE(cx)) {
1297 195968 case CXt_EVAL:
1298 195968 start_op = (OP*)cx->blk_oldcop;
1299 195968 break;
1300 0 case CXt_FORMAT:
1301 0 start_op = CvSTART(cx->blk_sub.cv);
1302 0 break;
1303 188031277 case CXt_SUB:
1304 188031277 start_op = CvSTART(cx->blk_sub.cv);
1305 188031277 break;
1306 #ifdef CXt_LOOP
1307 case CXt_LOOP:
1308 # if (PERL_VERSION < 10) || (PERL_VERSION == 9 && !defined(CX_LOOP_NEXTOP_GET))
1309 start_op = cx->blk_loop.redo_op;
1310 # else
1311 start_op = cx->blk_loop.my_op->op_redoop;
1312 # endif
1313 break;
1314 #else
1315 # if defined (CXt_LOOP_PLAIN) && defined(CXt_LOOP_LAZYIV) && defined (CXt_LOOP_LAZYSV)
1316 /* This is Perl 5.11.0 or later */
1317 13788926 case CXt_LOOP_LAZYIV:
1318 case CXt_LOOP_LAZYSV:
1319 case CXt_LOOP_PLAIN:
1320 # if defined (CXt_LOOP_FOR)
1321 case CXt_LOOP_FOR:
1322 # else
1323 case CXt_LOOP_ARY:
1324 case CXt_LOOP_LIST:
1325 # endif
1326 13788926 start_op = cx->blk_loop.my_op->op_redoop;
1327 13788926 break;
1328 # else
1329 # warning "The perl you are using is missing some essential defines. Your results may not be accurate."
1330 # endif
1331 #endif
1332 21076118 case CXt_BLOCK:
1333 /* this will be NULL for the top-level 'main' block */
1334 21076118 start_op = (OP*)cx->blk_oldcop;
1335 21076118 break;
1336 1876 case CXt_SUBST: /* FALLTHRU */
1337 case CXt_NULL: /* FALLTHRU */
1338 default:
1339 1876 start_op = NULL;
1340 1876 break;
1341 }
1342 223094165 ▶ if (!start_op) {
Branch: 1 branch
BranchTrueFalseTotal
if (!start_op) { 1876 223092289 223094165
1343 1876 ▶ if (trace_level >= trace)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= trace) 0 1876 1876
1344 0 logwarn("\tstart_cop_of_context: can't find start of %s\n",
1345 cx_block_type(cx));
1346 1876 return NULL;
1347 }
1348 /* find next cop from OP */
1349 223092289 o = start_op;
1350 223092289 ▶ while ( o && (type = (o->op_type) ? o->op_type : (int)o->op_targ) ) {
Branch: 3 branches
BranchTrueFalseTotal
while ( o && (type = (o->op_type) ? o->op_type : (int)o->op_targ) ) { 223092289 0 223092289
while ( o && (type = (o->op_type) ? o->op_type : (int)o->op_targ) ) { 222925453 166836 223092289
while ( o && (type = (o->op_type) ? o->op_type : (int)o->op_targ) ) { 223092289 0 223092289
1351 223092289 ▶ if (type == OP_NEXTSTATE ||
Branch: 2 branches
BranchTrueFalseTotal
if (type == OP_NEXTSTATE || 95790235 127302054 223092289
if (type == OP_NEXTSTATE || 95423510 366725 95790235
1352 #if PERL_VERSION < 11
1353 type == OP_SETSTATE ||
1354 #endif
1355 type == OP_DBSTATE)
1356 {
1357 222725564 ▶ if (trace_level >= trace)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= trace) 0 222725564 222725564
1358 0 logwarn("\tstart_cop_of_context %s is %s line %d of %s\n",
1359 0 ▶ cx_block_type(cx), OP_NAME(o), (int)CopLINE((COP*)o),
Branch: 1 branch
BranchTrueFalseTotal
cx_block_type(cx), OP_NAME(o), (int)CopLINE((COP*)o), 0 0 0
1360 0 ▶ OutCopFILE((COP*)o));
Branch: 1 branch
BranchTrueFalseTotal
OutCopFILE((COP*)o)); 0 0 0
1361 222725564 return (COP*)o;
1362 }
1363 366725 ▶ if (trace_level >= trace)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= trace) 0 366725 366725
1364 0 logwarn("\tstart_cop_of_context %s op '%s' isn't a cop, giving up\n",
1365 0 ▶ cx_block_type(cx), OP_NAME(o));
Branch: 1 branch
BranchTrueFalseTotal
cx_block_type(cx), OP_NAME(o)); 0 0 0
1366 366725 return NULL;
1367 #if 0 /* old code that never worked very well anyway */
1368 if (CxTYPE(cx) == CXt_LOOP) /* e.g. "eval $_ for @ary" */
1369 return NULL;
1370 /* should never get here but we do */
1371 if (trace_level >= trace) {
1372 logwarn("\tstart_cop_of_context %s op '%s' isn't a cop\n",
1373 cx_block_type(cx), OP_NAME(o));
1374 if (trace_level > trace)
1375 do_op_dump(1, PerlIO_stderr(), o);
1376 }
1377 o = o->op_next;
1378 #endif
1379 }
1380 0 ▶ if (trace_level >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 3) { 0 0 0
1381 0 logwarn("\tstart_cop_of_context: can't find next cop for %s line %ld\n",
1382 0 cx_block_type(cx), (long)CopLINE(PL_curcop_nytprof));
1383 0 do_op_dump(1, PerlIO_stderr(), start_op);
1384 }
1385 0 return NULL;
1386 }
1387  
1388  
1389 /* Walk up the context stack calling callback
1390 * return first context that callback returns true for
1391 * else return null.
1392 * UV cx_type_mask is a bit flag that specifies what kinds of contexts the
1393 * callback should be called for: (cx_type_mask & (1 << CxTYPE(cx)))
1394 * Use ~0 to stop at all contexts.
1395 * The callback is called with the context pointer and a pointer to
1396 * a copy of the UV cx_type_mask argument (so it can change it on the fly).
1397 */
1398 static PERL_CONTEXT *
1399 188074315 visit_contexts(pTHX_ UV cx_type_mask, int (*callback)(pTHX_ PERL_CONTEXT *cx,
1400 UV *cx_type_mask_ptr))
1401 {
1402 /* modelled on pp_caller() in pp_ctl.c */
1403 188074315 I32 cxix = cxstack_ix;
1404 188074315 PERL_CONTEXT *cx = NULL;
1405 188074315 PERL_CONTEXT *ccstack = cxstack;
1406 188074315 PERL_SI *top_si = PL_curstackinfo;
1407  
1408 188074315 ▶ if (trace_level >= 6)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 6) 0 188074315 188074315
1409 0 logwarn("visit_contexts: \n");
1410  
1411 while (1) {
1412 /* we may be in a higher stacklevel, so dig down deeper */
1413 /* XXX so we'll miss code in sort blocks and signals? */
1414 /* callback should perhaps be moved to dopopcx_at */
1415 230534814 ▶ while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
Branch: 2 branches
BranchTrueFalseTotal
while (cxix < 0 && top_si->si_type != PERLSI_MAIN) { 9050 230525764 230534814
while (cxix < 0 && top_si->si_type != PERLSI_MAIN) { 9050 0 9050
1416 9050 ▶ if (trace_level >= 6)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 6) 0 9050 9050
1417 0 logwarn("Not on main stack (type %d); digging top_si %p->%p, ccstack %p->%p\n",
1418 0 (int)top_si->si_type, (void*)top_si, (void*)top_si->si_prev,
1419 0 (void*)ccstack, (void*)top_si->si_cxstack);
1420 9050 top_si = top_si->si_prev;
1421 9050 ccstack = top_si->si_cxstack;
1422 9050 cxix = dopopcx_at(aTHX_ ccstack, top_si->si_cxix, cx_type_mask);
1423 }
1424 230525764 ▶ if (cxix < 0 || (cxix == 0 && !top_si->si_prev)) {
Branch: 3 branches
BranchTrueFalseTotal
if (cxix < 0 || (cxix == 0 && !top_si->si_prev)) { 230525764 0 230525764
if (cxix < 0 || (cxix == 0 && !top_si->si_prev)) { 13693 230512071 230525764
if (cxix < 0 || (cxix == 0 && !top_si->si_prev)) { 2857 10836 13693
1425 /* cxix==0 && !top_si->si_prev => top-level BLOCK */
1426 2857 ▶ if (trace_level >= 5)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 5) 0 2857 2857
1427 0 logwarn("visit_contexts: reached top of context stack\n");
1428 2857 return NULL;
1429 }
1430 230522907 cx = &ccstack[cxix];
1431 230522907 ▶ if (trace_level >= 5)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 5) 0 230522907 230522907
1432 0 logwarn("visit_context: %s cxix %d (si_prev %p)\n",
1433 0 cx_block_type(cx), (int)cxix, (void*)top_si->si_prev);
1434 230522907 ▶ if (callback(aTHX_ cx, &cx_type_mask))
Branch: 1 branch
BranchTrueFalseTotal
if (callback(aTHX_ cx, &cx_type_mask)) 188071458 42451449 230522907
1435 188071458 return cx;
1436 /* no joy, look further */
1437 42451449 cxix = dopopcx_at(aTHX_ ccstack, cxix - 1, cx_type_mask);
1438 }
1439 return NULL; /* not reached */
1440 }
1441  
1442  
1443 static int
1444 222725564 _cop_in_same_file(COP *a, COP *b)
1445 {
1446 222725564 int same = 0;
1447 222725564 ▶ char *a_file = OutCopFILE(a);
Branch: 1 branch
BranchTrueFalseTotal
char *a_file = OutCopFILE(a); 222725564 0 222725564
1448 222725564 ▶ char *b_file = OutCopFILE(b);
Branch: 1 branch
BranchTrueFalseTotal
char *b_file = OutCopFILE(b); 222725564 0 222725564
1449 222725564 ▶ if (a_file == b_file)
Branch: 1 branch
BranchTrueFalseTotal
if (a_file == b_file) 222065080 660484 222725564
1450 222065080 same = 1;
1451 else
1452 /* fallback to strEQ, surprisingly common (check why) XXX expensive */
1453 660484 ▶ if (strEQ(a_file, b_file))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(a_file, b_file)) 0 660484 660484
1454 0 same = 1;
1455 222725564 return same;
1456 }
1457  
1458  
1459 static int
1460 230522907 _check_context(pTHX_ PERL_CONTEXT *cx, UV *cx_type_mask_ptr)
1461 {
1462 COP *near_cop;
1463 PERL_UNUSED_ARG(cx_type_mask_ptr);
1464  
1465 230522907 ▶ if (CxTYPE(cx) == CXt_SUB) {
Branch: 1 branch
BranchTrueFalseTotal
if (CxTYPE(cx) == CXt_SUB) { 188038725 42484182 230522907
1466 188038725 ▶ if (PL_debstash && CvSTASH(cx->blk_sub.cv) == PL_debstash)
Branch: 2 branches
BranchTrueFalseTotal
if (PL_debstash && CvSTASH(cx->blk_sub.cv) == PL_debstash) 188038725 0 188038725
if (PL_debstash && CvSTASH(cx->blk_sub.cv) == PL_debstash) 7448 188031277 188038725
1467 7448 return 0; /* skip subs in DB package */
1468  
1469 188031277 near_cop = start_cop_of_context(aTHX_ cx);
1470  
1471 /* only use the cop if it's in the same file */
1472 188031277 ▶ if (_cop_in_same_file(near_cop, PL_curcop_nytprof)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_cop_in_same_file(near_cop, PL_curcop_nytprof)) { 187410974 620303 188031277
1473 187410974 last_sub_line = CopLINE(near_cop);
1474 /* treat sub as a block if we've not found a block yet */
1475 187410974 ▶ if (!last_block_line)
Branch: 1 branch
BranchTrueFalseTotal
if (!last_block_line) 153377439 34033535 187410974
1476 153377439 last_block_line = last_sub_line;
1477 }
1478  
1479 188031277 ▶ if (trace_level >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 8) { 0 188031277 188031277
1480 0 GV *sv = CvGV(cx->blk_sub.cv);
1481 0 ▶ logwarn("\tat %d: block %d sub %d for %s %s\n",
Branch: 1 branch
BranchTrueFalseTotal
logwarn("\tat %d: block %d sub %d for %s %s\n", 0 0 0
1482 last_executed_line, last_block_line, last_sub_line,
1483 0 cx_block_type(cx), (sv) ? GvNAME(sv) : "");
1484 0 ▶ if (trace_level >= 99)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 99) 0 0 0
1485 0 sv_dump((SV*)cx->blk_sub.cv);
1486 }
1487  
1488 188031277 return 1; /* stop looking */
1489 }
1490  
1491 /* NULL, EVAL, LOOP, SUBST, BLOCK context */
1492 42484182 ▶ if (trace_level >= 6)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 6) 0 42484182 42484182
1493 0 logwarn("\t%s\n", cx_block_type(cx));
1494  
1495 /* if we've got a block line, skip this context and keep looking for a sub */
1496 42484182 ▶ if (last_block_line)
Branch: 1 branch
BranchTrueFalseTotal
if (last_block_line) 7421294 35062888 42484182
1497 7421294 return 0;
1498  
1499 /* if we can't get a line number for this context, skip it */
1500 35062888 ▶ if ((near_cop = start_cop_of_context(aTHX_ cx)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((near_cop = start_cop_of_context(aTHX_ cx)) == NULL) 368601 34694287 35062888
1501 368601 return 0;
1502  
1503 /* if this context is in a different file... */
1504 34694287 ▶ if (!_cop_in_same_file(near_cop, PL_curcop_nytprof)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!_cop_in_same_file(near_cop, PL_curcop_nytprof)) { 40181 34654106 34694287
1505 /* if we started in a string eval ... */
1506 40181 ▶ if ('(' == *OutCopFILE(PL_curcop_nytprof)) {
Branch: 2 branches
BranchTrueFalseTotal
if ('(' == *OutCopFILE(PL_curcop_nytprof)) { 40181 0 40181
if ('(' == *OutCopFILE(PL_curcop_nytprof)) { 4896 35285 40181
1507 /* give up XXX could do better here */
1508 4896 last_block_line = last_sub_line = last_executed_line;
1509 4896 return 1;
1510 }
1511 /* shouldn't happen! */
1512 35285 ▶ if (trace_level >= 5)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 5) 0 35285 35285
1513 0 logwarn("at %d: %s in different file (%s, %s)\n",
1514 last_executed_line, cx_block_type(cx),
1515 0 ▶ OutCopFILE(near_cop), OutCopFILE(PL_curcop_nytprof));
Branch: 2 branches
BranchTrueFalseTotal
OutCopFILE(near_cop), OutCopFILE(PL_curcop_nytprof)); 0 0 0
OutCopFILE(near_cop), OutCopFILE(PL_curcop_nytprof)); 0 0 0
1516 35285 return 1; /* stop looking */
1517 }
1518  
1519 34654106 last_block_line = CopLINE(near_cop);
1520 34654106 ▶ if (trace_level >= 5)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 5) 0 34654106 34654106
1521 0 logwarn("\tat %d: block %d for %s\n",
1522 last_executed_line, last_block_line, cx_block_type(cx));
1523 34654106 return 0;
1524 }
1525  
1526  
1527 /* copied from perl's S_closest_cop in util.c as used by warn(...) */
1528  
1529 static const COP*
1530 2664 closest_cop(pTHX_ const COP *cop, const OP *o)
1531 {
1532 dVAR;
1533 /* Look for PL_op starting from o. cop is the last COP we've seen. */
1534 2664 ▶ if (!o || o == PL_op)
Branch: 2 branches
BranchTrueFalseTotal
if (!o || o == PL_op) 342 2322 2664
if (!o || o == PL_op) 0 342 342
1535 2322 return cop;
1536 342 ▶ if (o->op_flags & OPf_KIDS) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->op_flags & OPf_KIDS) { 114 228 342
1537 const OP *kid;
1538 342 ▶ for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
Branch: 2 branches
BranchTrueFalseTotal
for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) { 114 114 228
for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) { 228 114 342
1539 const COP *new_cop;
1540 /* If the OP_NEXTSTATE has been optimised away we can still use it
1541 * the get the file and line number. */
1542 228 ▶ if (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE)
Branch: 2 branches
BranchTrueFalseTotal
if (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE) 0 228 228
if (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE) 0 0 0
1543 0 cop = (const COP *)kid;
1544 /* Keep searching, and return when we've found something. */
1545 228 new_cop = closest_cop(aTHX_ cop, kid);
1546 228 ▶ if (new_cop)
Branch: 1 branch
BranchTrueFalseTotal
if (new_cop) 0 228 228
1547 0 return new_cop;
1548 }
1549 }
1550 /* Nothing found. */
1551 342 return NULL;
1552 }
1553  
1554  
1555 /**
1556 * Main statement profiling function. Called before each breakable statement.
1557 */
1558 static void
1559 280486762 DB_stmt(pTHX_ COP *cop, OP *op)
1560 {
1561 int saved_errno;
1562 char *file;
1563 long elapsed, overflow;
1564  
1565 280486762 ▶ if (!is_profiling || !profile_stmts)
Branch: 2 branches
BranchTrueFalseTotal
if (!is_profiling || !profile_stmts) 200963798 79522964 280486762
if (!is_profiling || !profile_stmts) 3097173 197866625 200963798
1566 82620137 return;
1567 #ifdef MULTIPLICITY
1568 if (orig_my_perl && my_perl != orig_my_perl)
1569 return;
1570 #endif
1571  
1572 197866625 saved_errno = errno;
1573  
1574 197866625 get_time_of_day(end_time);
1575 197866625 get_ticks_between(long, start_time, end_time, elapsed, overflow);
1576  
1577 197866625 reinit_if_forked(aTHX);
1578  
1579 /* XXX move down into the (file != last_executed_fileptr) block ? */
1580 CHECK_SAWAMPERSAND(last_executed_fid, last_executed_line);
1581  
1582 197866625 ▶ if (last_executed_fid) {
Branch: 1 branch
BranchTrueFalseTotal
if (last_executed_fid) { 197866018 607 197866625
1583 197866018 ▶ if (profile_blocks)
Branch: 1 branch
BranchTrueFalseTotal
if (profile_blocks) 188083441 9782577 197866018
1584 188083441 NYTP_write_time_block(out, elapsed, overflow, last_executed_fid,
1585 last_executed_line, last_block_line,
1586 last_sub_line);
1587 else
1588 9782577 NYTP_write_time_line(out, elapsed, overflow, last_executed_fid,
1589 last_executed_line);
1590  
1591 197866018 ▶ if (trace_level >= 5) /* previous fid:line and how much time we spent there */
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 5) /* previous fid:line and how much time we spent there */ 0 197866018 197866018
1592 0 logwarn("\t@%d:%-4d %2ld ticks (%u, %u)\n",
1593 last_executed_fid, last_executed_line,
1594 elapsed, last_block_line, last_sub_line);
1595 }
1596  
1597 197866625 ▶ if (!cop)
Branch: 1 branch
BranchTrueFalseTotal
if (!cop) 197866561 64 197866625
1598 197866561 cop = PL_curcop_nytprof;
1599 197866625 ▶ if ( (last_executed_line = CopLINE(cop)) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (last_executed_line = CopLINE(cop)) == 0 ) { 2436 197864189 197866625
1600 /* Might be a cop that has been optimised away. We can try to find such a
1601 * cop by searching through the optree starting from the sibling of PL_curcop.
1602 * See Perl_vmess in perl's util.c for how warn("...") finds the line number.
1603 */
1604 2436 ▶ cop = (COP*)closest_cop(aTHX_ cop, OpSIBLING(cop));
Branch: 1 branch
BranchTrueFalseTotal
cop = (COP*)closest_cop(aTHX_ cop, OpSIBLING(cop)); 114 2322 2436
1605 2436 ▶ if (!cop)
Branch: 1 branch
BranchTrueFalseTotal
if (!cop) 114 2322 2436
1606 114 cop = PL_curcop_nytprof;
1607 2436 last_executed_line = CopLINE(cop);
1608 2436 ▶ if (!last_executed_line) {
Branch: 1 branch
BranchTrueFalseTotal
if (!last_executed_line) { 2436 0 2436
1609 /* perl options, like -n, -p, -Mfoo etc can cause this because perl effectively
1610 * treats those as 'line 0', so we try not to warn in those cases.
1611 */
1612 2436 ▶ char *pkg_name = CopSTASHPV(cop);
Branch: 7 branches
BranchTrueFalseTotal
char *pkg_name = CopSTASHPV(cop); 2436 0 2436
char *pkg_name = CopSTASHPV(cop); 2436 0 2436
char *pkg_name = CopSTASHPV(cop); 2436 0 2436
char *pkg_name = CopSTASHPV(cop); 0 2436 2436
char *pkg_name = CopSTASHPV(cop); 0 0 0
char *pkg_name = CopSTASHPV(cop); 2436 0 2436
char *pkg_name = CopSTASHPV(cop); 0 2436 2436
1613 2436 ▶ int is_preamble = (PL_scopestack_ix <= 7 && strEQ(pkg_name,"main"));
Branch: 2 branches
BranchTrueFalseTotal
int is_preamble = (PL_scopestack_ix <= 7 && strEQ(pkg_name,"main")); 2364 72 2436
int is_preamble = (PL_scopestack_ix <= 7 && strEQ(pkg_name,"main")); 2364 0 2364
1614  
1615 /* op is null when called via finish_profile called by END */
1616 2436 ▶ if (!is_preamble && op) {
Branch: 2 branches
BranchTrueFalseTotal
if (!is_preamble && op) { 72 2364 2436
if (!is_preamble && op) { 0 72 72
1617 /* warn() can't either, in the cases I've encountered */
1618 0 logwarn("Unable to determine line number in %s (ssix%d)\n",
1619 0 ▶ OutCopFILE(cop), (int)PL_scopestack_ix);
Branch: 1 branch
BranchTrueFalseTotal
OutCopFILE(cop), (int)PL_scopestack_ix); 0 0 0
1620 0 ▶ if (trace_level > 5)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level > 5) 0 0 0
1621 0 do_op_dump(1, PerlIO_stderr(), (OP*)cop);
1622 }
1623 2436 last_executed_line = 1; /* don't want zero line numbers in data */
1624 }
1625 }
1626  
1627 197866625 ▶ file = OutCopFILE(cop);
Branch: 1 branch
BranchTrueFalseTotal
file = OutCopFILE(cop); 197866625 0 197866625
1628 197866625 ▶ if (!last_executed_fid) { /* first time */
Branch: 1 branch
BranchTrueFalseTotal
if (!last_executed_fid) { /* first time */ 607 197866018 197866625
1629 607 ▶ if (trace_level >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) { 0 607 607
1630 0 logwarn("~ first statement profiled at line %d of %s, pid %ld\n",
1631 0 ▶ (int)CopLINE(cop), OutCopFILE(cop), (long)getpid());
Branch: 1 branch
BranchTrueFalseTotal
(int)CopLINE(cop), OutCopFILE(cop), (long)getpid()); 0 0 0
1632 }
1633 }
1634 197866625 ▶ if (file != last_executed_fileptr) { /* cache (hit ratio ~50% e.g. for perlcritic) */
Branch: 1 branch
BranchTrueFalseTotal
if (file != last_executed_fileptr) { /* cache (hit ratio ~50% e.g. for perlcritic) */ 6345336 191521289 197866625
1635 6345336 last_executed_fileptr = file;
1636 6345336 last_executed_fid = get_file_id(aTHX_ file, strlen(file), NYTP_FIDf_VIA_STMT);
1637 }
1638  
1639 197866625 ▶ if (trace_level >= 7) /* show the fid:line we're about to execute */
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 7) /* show the fid:line we're about to execute */ 0 197866625 197866625
1640 0 logwarn("\t@%d:%-4d... %s\n", last_executed_fid, last_executed_line,
1641 0 ▶ (profile_blocks) ? "looking for block and sub lines" : "");
Branch: 1 branch
BranchTrueFalseTotal
(profile_blocks) ? "looking for block and sub lines" : ""); 0 0 0
1642  
1643 197866625 ▶ if (profile_blocks) {
Branch: 1 branch
BranchTrueFalseTotal
if (profile_blocks) { 188084020 9782605 197866625
1644 188084020 last_block_line = 0;
1645 188084020 last_sub_line = 0;
1646 188084020 ▶ if (op) {
Branch: 1 branch
BranchTrueFalseTotal
if (op) { 188074315 9705 188084020
1647 188074315 visit_contexts(aTHX_ ~0, &_check_context);
1648 }
1649 /* if we didn't find block or sub scopes then use current line */
1650 188084020 ▶ if (!last_block_line) last_block_line = last_executed_line;
Branch: 1 branch
BranchTrueFalseTotal
if (!last_block_line) last_block_line = last_executed_line; 47633 188036387 188084020
1651 188084020 ▶ if (!last_sub_line) last_sub_line = last_executed_line;
Branch: 1 branch
BranchTrueFalseTotal
if (!last_sub_line) last_sub_line = last_executed_line; 668204 187415816 188084020
1652 }
1653  
1654 197866625 get_time_of_day(start_time);
1655  
1656 /* measure time we've spent measuring so we can discount it */
1657 197866625 get_ticks_between(long, end_time, start_time, elapsed, overflow);
1658 197866625 cumulative_overhead_ticks += elapsed;
1659  
1660 197866625 SETERRNO(saved_errno, 0);
1661 197866625 return;
1662 }
1663  
1664  
1665 static void
1666 21498855 DB_leave(pTHX_ OP *op, OP *prev_op)
1667 {
1668 int saved_errno, is_multicall;
1669 unsigned int prev_last_executed_fid, prev_last_executed_line;
1670  
1671 /* Called _after_ ops that indicate we've completed a statement
1672 * and are returning into the middle of some outer statement.
1673 * Used to ensure that time between now and the _next_ statement
1674 * being entered, is allocated to the outer statement we've
1675 * returned into and not the previous statement.
1676 * PL_curcop has already been updated.
1677 */
1678  
1679 21498855 ▶ if (!is_profiling || !out || !profile_stmts)
Branch: 3 branches
BranchTrueFalseTotal
if (!is_profiling || !out || !profile_stmts) 12217163 9281692 21498855
if (!is_profiling || !out || !profile_stmts) 12217163 0 12217163
if (!is_profiling || !out || !profile_stmts) 0 12217163 12217163
1680 9281692 return;
1681 #ifdef MULTIPLICITY
1682 if (orig_my_perl && my_perl != orig_my_perl)
1683 return;
1684 #endif
1685  
1686 12217163 saved_errno = errno;
1687 12217163 prev_last_executed_fid = last_executed_fid;
1688 12217163 prev_last_executed_line = last_executed_line;
1689  
1690 #if defined(CxMULTICALL) && 0 /* disabled for now */
1691 /* pp_return, pp_leavesub and pp_leavesublv
1692 * return a NULL op when returning from a MULTICALL.
1693 * See Lightweight Callbacks in perlcall.
1694 */
1695 is_multicall = (!op && cxstack_ix >= 0 && CxMULTICALL(&cxstack[cxstack_ix]));
1696 #else
1697 12217163 is_multicall = 0;
1698 #endif
1699  
1700 /* measure and output end time of previous statement
1701 * (earlier than it would have been done)
1702 * and switch back to measuring the 'calling' statement
1703 */
1704 12217163 DB_stmt(aTHX_ NULL, op);
1705  
1706 /* output a 'discount' marker to indicate the next statement time shouldn't
1707 * increment the count (because the time is not for a new statement but simply
1708 * a continuation of a previously counted statement).
1709 */
1710 12217163 NYTP_write_discount(out);
1711  
1712 /* special cases */
1713 12217163 ▶ if (last_executed_line == prev_last_executed_line
Branch: 1 branch
BranchTrueFalseTotal
if (last_executed_line == prev_last_executed_line 2756593 9460570 12217163
1714 2756593 && last_executed_fid == prev_last_executed_fid
1715 ) {
1716 /* XXX OP_UNSTACK needs help */
1717 }
1718  
1719 12217163 ▶ if (trace_level >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 5) { 0 12217163 12217163
1720 0 ▶ logwarn("\tleft %u:%u via %s back to %s at %u:%u (b%u s%u) - discounting next statement%s\n",
Branch: 3 branches
BranchTrueFalseTotal
logwarn("\tleft %u:%u via %s back to %s at %u:%u (b%u s%u) - discounting next statement%s\n", 0 0 0
logwarn("\tleft %u:%u via %s back to %s at %u:%u (b%u s%u) - discounting next statement%s\n", 0 0 0
logwarn("\tleft %u:%u via %s back to %s at %u:%u (b%u s%u) - discounting next statement%s\n", 0 0 0
1721 prev_last_executed_fid, prev_last_executed_line,
1722 0 ▶ OP_NAME_safe(prev_op), OP_NAME_safe(op),
Branch: 2 branches
BranchTrueFalseTotal
OP_NAME_safe(prev_op), OP_NAME_safe(op), 0 0 0
OP_NAME_safe(prev_op), OP_NAME_safe(op), 0 0 0
1723 last_executed_fid, last_executed_line, last_block_line, last_sub_line,
1724 0 ▶ (op || is_multicall) ? "" : ", LEAVING PERL"
Branch: 1 branch
BranchTrueFalseTotal
(op || is_multicall) ? "" : ", LEAVING PERL" 0 0 0
1725 );
1726 }
1727  
1728 12217163 SETERRNO(saved_errno, 0);
1729 }
1730  
1731  
1732 /**
1733 * Sets or toggles the option specified by 'option'.
1734 */
1735 static void
1736 11189 set_option(pTHX_ const char* option, const char* value)
1737 {
1738 11189 ▶ if (!option || !*option)
Branch: 2 branches
BranchTrueFalseTotal
if (!option || !*option) 11189 0 11189
if (!option || !*option) 0 11189 11189
1739 0 croak("%s: invalid option", "NYTProf set_option");
1740 11189 ▶ if (!value || !*value)
Branch: 2 branches
BranchTrueFalseTotal
if (!value || !*value) 11189 0 11189
if (!value || !*value) 0 11189 11189
1741 0 croak("%s: '%s' has no value", "NYTProf set_option", option);
1742  
1743 11189 ▶ if (strEQ(option, "file")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(option, "file")) { 1251 9938 11189
1744 1251 strncpy(PROF_output_file, value, MAXPATHLEN);
1745 }
1746 9938 ▶ else if (strEQ(option, "log")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(option, "log")) { 0 9938 9938
1747 0 FILE *fp = fopen(value, "a");
1748 0 ▶ if (!fp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) { 0 0 0
1749 0 logwarn("Can't open log file '%s' for writing: %s\n",
1750 0 value, strerror(errno));
1751 0 return;
1752 }
1753 0 logfh = fp;
1754 }
1755 9938 ▶ else if (strEQ(option, "start")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(option, "start")) { 1235 8703 9938
1756 1235 ▶ if (strEQ(value,"begin")) profile_start = NYTP_START_BEGIN;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(value,"begin")) profile_start = NYTP_START_BEGIN; 46 1189 1235
1757 1189 ▶ else if (strEQ(value,"init")) profile_start = NYTP_START_INIT;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(value,"init")) profile_start = NYTP_START_INIT; 1189 0 1189
1758 0 ▶ else if (strEQ(value,"end")) profile_start = NYTP_START_END;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(value,"end")) profile_start = NYTP_START_END; 0 0 0
1759 0 ▶ else if (strEQ(value,"no")) profile_start = NYTP_START_NO;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(value,"no")) profile_start = NYTP_START_NO; 0 0 0
1760 0 else croak("NYTProf option 'start' has invalid value '%s'\n", value);
1761 }
1762 8703 ▶ else if (strEQ(option, "addpid")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(option, "addpid")) { 15 8688 8703
1763 15 profile_opts = (atoi(value))
1764 15 ? profile_opts | NYTP_OPTf_ADDPID
1765 15 ▶ : profile_opts & ~NYTP_OPTf_ADDPID;
Branch: 1 branch
BranchTrueFalseTotal
: profile_opts & ~NYTP_OPTf_ADDPID; 15 0 15
1766 }
1767 8688 ▶ else if (strEQ(option, "addtimestamp")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(option, "addtimestamp")) { 0 8688 8688
1768 0 profile_opts = (atoi(value))
1769 0 ? profile_opts | NYTP_OPTf_ADDTIMESTAMP
1770 0 ▶ : profile_opts & ~NYTP_OPTf_ADDTIMESTAMP;
Branch: 1 branch
BranchTrueFalseTotal
: profile_opts & ~NYTP_OPTf_ADDTIMESTAMP; 0 0 0
1771 }
1772 8688 ▶ else if (strEQ(option, "optimize") || strEQ(option, "optimise")) {
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(option, "optimize") || strEQ(option, "optimise")) { 8687 1 8688
else if (strEQ(option, "optimize") || strEQ(option, "optimise")) { 0 8687 8687
1773 1 profile_opts = (atoi(value))
1774 1 ? profile_opts | NYTP_OPTf_OPTIMIZE
1775 1 ▶ : profile_opts & ~NYTP_OPTf_OPTIMIZE;
Branch: 1 branch
BranchTrueFalseTotal
: profile_opts & ~NYTP_OPTf_OPTIMIZE; 1 0 1
1776 }
1777 8687 ▶ else if (strEQ(option, "savesrc")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(option, "savesrc")) { 1234 7453 8687
1778 1234 profile_opts = (atoi(value))
1779 673 ? profile_opts | NYTP_OPTf_SAVESRC
1780 1234 ▶ : profile_opts & ~NYTP_OPTf_SAVESRC;
Branch: 1 branch
BranchTrueFalseTotal
: profile_opts & ~NYTP_OPTf_SAVESRC; 673 561 1234
1781 }
1782 7453 ▶ else if (strEQ(option, "endatexit")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(option, "endatexit")) { 0 7453 7453
1783 0 ▶ if (atoi(value))
Branch: 1 branch
BranchTrueFalseTotal
if (atoi(value)) 0 0 0
1784 0 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1785 }
1786 7453 ▶ else if (strEQ(option, "libcexit")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(option, "libcexit")) { 0 7453 7453
1787 0 ▶ if (atoi(value))
Branch: 1 branch
BranchTrueFalseTotal
if (atoi(value)) 0 0 0
1788 0 atexit(finish_profile_nocontext);
1789 }
1790 else {
1791  
1792 7453 struct NYTP_options_t *opt_p = options;
1793 7453 const struct NYTP_options_t *const opt_end
1794 = options + sizeof(options) / sizeof (struct NYTP_options_t);
1795 7453 bool found = FALSE;
1796 do {
1797 62166 ▶ if (strEQ(option, opt_p->option_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(option, opt_p->option_name)) { 7453 54713 62166
1798 7453 opt_p->option_iv = (IV)strtol(value, NULL, 0);
1799 7453 found = TRUE;
1800 7453 break;
1801 }
1802 54713 ▶ } while (++opt_p < opt_end);
Branch: 1 branch
BranchTrueFalseTotal
} while (++opt_p < opt_end); 54713 0 54713
1803 7453 ▶ if (!found) {
Branch: 1 branch
BranchTrueFalseTotal
if (!found) { 0 7453 7453
1804 0 logwarn("Unknown NYTProf option: '%s'\n", option);
1805 0 return;
1806 }
1807 }
1808 11189 ▶ if (trace_level)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level) 0 11189 11189
1809 0 logwarn("# %s=%s\n", option, value);
1810 }
1811  
1812  
1813 /**
1814 * Open the output file. This is encapsulated because the code can be reused
1815 * without the environment parsing overhead after each fork.
1816 */
1817 static void
1818 675 open_output_file(pTHX_ char *filename)
1819 {
1820 char filename_buf[MAXPATHLEN];
1821 /* 'x' is a GNU C lib extension for O_EXCL which gives us a little
1822 * extra protection, but it isn't POSIX compliant */
1823 675 ▶ const char *mode = (strnEQ(filename, "/dev/", 4) ? "wb" : "wbx");
Branch: 1 branch
BranchTrueFalseTotal
const char *mode = (strnEQ(filename, "/dev/", 4) ? "wb" : "wbx"); 0 675 675
1824 /* most systems that don't support it will silently ignore it
1825 * but for some we need to remove it to avoid an error */
1826 #ifdef WIN32
1827 mode = "wb";
1828 #endif
1829 #ifdef VMS
1830 mode = "wb";
1831 #endif
1832  
1833 675 ▶ if ((profile_opts & (NYTP_OPTf_ADDPID|NYTP_OPTf_ADDTIMESTAMP))
Branch: 1 branch
BranchTrueFalseTotal
if ((profile_opts & (NYTP_OPTf_ADDPID|NYTP_OPTf_ADDTIMESTAMP)) 633 42 675
1834 633 ▶ || out /* already opened so assume we're forking and add the pid */
Branch: 1 branch
BranchTrueFalseTotal
|| out /* already opened so assume we're forking and add the pid */ 0 633 633
1835 ) {
1836 42 ▶ if (strlen(filename) >= MAXPATHLEN-(20+20)) /* buffer overrun protection */
Branch: 1 branch
BranchTrueFalseTotal
if (strlen(filename) >= MAXPATHLEN-(20+20)) /* buffer overrun protection */ 0 42 42
1837 0 croak("Filename '%s' too long", filename);
1838 42 strcpy(filename_buf, filename);
1839 42 ▶ if ((profile_opts & NYTP_OPTf_ADDPID) || out)
Branch: 2 branches
BranchTrueFalseTotal
if ((profile_opts & NYTP_OPTf_ADDPID) || out) 0 42 42
if ((profile_opts & NYTP_OPTf_ADDPID) || out) 0 0 0
1840 42 sprintf(&filename_buf[strlen(filename_buf)], ".%d", getpid());
1841 42 ▶ if ( profile_opts & NYTP_OPTf_ADDTIMESTAMP )
Branch: 1 branch
BranchTrueFalseTotal
if ( profile_opts & NYTP_OPTf_ADDTIMESTAMP ) 0 42 42
1842 0 sprintf(&filename_buf[strlen(filename_buf)], ".%.0" NVff, gettimeofday_nv());
1843 42 filename = filename_buf;
1844 /* caller is expected to have purged/closed old out if appropriate */
1845 }
1846  
1847 /* some protection against multiple processes writing to the same file */
1848 675 unlink(filename); /* throw away any previous file */
1849  
1850 675 out = NYTP_open(filename, mode);
1851 675 ▶ if (!out) {
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { 0 675 675
1852 0 int fopen_errno = errno;
1853 0 const char *hint = "";
1854 0 ▶ if (fopen_errno==EEXIST && !(profile_opts & NYTP_OPTf_ADDPID))
Branch: 2 branches
BranchTrueFalseTotal
if (fopen_errno==EEXIST && !(profile_opts & NYTP_OPTf_ADDPID)) 0 0 0
if (fopen_errno==EEXIST && !(profile_opts & NYTP_OPTf_ADDPID)) 0 0 0
1855 0 hint = " (enable addpid option to protect against concurrent writes)";
1856 0 disable_profile(aTHX);
1857 0 croak("NYTProf failed to open '%s' for writing, error %d: %s%s",
1858 filename, fopen_errno, strerror(fopen_errno), hint);
1859 }
1860 675 ▶ if (trace_level >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) 0 675 675
1861 0 logwarn("~ opened %s at %.6" NVff "\n", filename, gettimeofday_nv());
1862  
1863 675 output_header(aTHX);
1864 675 }
1865  
1866  
1867 static void
1868 681 close_output_file(pTHX) {
1869 int result;
1870 NV timeofday;
1871  
1872 681 ▶ if (!out)
Branch: 1 branch
BranchTrueFalseTotal
if (!out) 36 645 681
1873 36 return;
1874  
1875 645 timeofday = gettimeofday_nv(); /* before write_*() calls */
1876 645 NYTP_write_attribute_nv(out, STR_WITH_LEN("cumulative_overhead_ticks"), cumulative_overhead_ticks);
1877  
1878 645 write_src_of_files(aTHX);
1879 645 write_sub_line_ranges(aTHX);
1880 645 write_sub_callers(aTHX);
1881 /* mark end of profile data for last_pid pid
1882 * which is the pid that this file relates to
1883 */
1884 645 NYTP_write_process_end(out, last_pid, timeofday);
1885  
1886 645 ▶ if ((result = NYTP_close(out, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((result = NYTP_close(out, 0))) 0 645 645
1887 0 logwarn("Error closing profile data file: %s\n", strerror(result));
1888 645 out = NULL;
1889  
1890 645 ▶ if (trace_level >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) 0 645 645
1891 0 logwarn("~ closed file at %.6" NVff "\n", timeofday);
1892 }
1893  
1894  
1895 static int
1896 203636844 reinit_if_forked(pTHX)
1897 {
1898 int open_new_file;
1899  
1900 203636844 ▶ if (getpid() == last_pid)
Branch: 1 branch
BranchTrueFalseTotal
if (getpid() == last_pid) 203636813 31 203636844
1901 203636813 return 0; /* not forked */
1902  
1903 /* we're now the child process */
1904 31 ▶ if (trace_level >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) 0 31 31
1905 0 logwarn("~ new pid %d (was %d) forkdepth %" IVdf "\n", getpid(), last_pid, profile_forkdepth);
1906  
1907 /* reset state */
1908 31 last_pid = getpid();
1909 31 last_executed_fileptr = NULL;
1910 31 last_executed_fid = 0; /* don't count the fork in the child */
1911 31 ▶ if (sub_callers_hv)
Branch: 1 branch
BranchTrueFalseTotal
if (sub_callers_hv) 31 0 31
1912 31 hv_clear(sub_callers_hv);
1913  
1914 31 open_new_file = (out) ? 1 : 0;
1915 31 ▶ if (open_new_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (open_new_file) { 30 1 31
1916 /* data that was unflushed in the parent when it forked
1917 * is now duplicated unflushed in this child,
1918 * so discard it when we close the inherited filehandle.
1919 */
1920 30 int result = NYTP_close(out, 1);
1921 30 ▶ if (result)
Branch: 1 branch
BranchTrueFalseTotal
if (result) 0 30 30
1922 0 logwarn("Error closing profile data file: %s\n", strerror(result));
1923 30 out = NULL;
1924 /* if we fork while profiling then ensure we'll get a distinct filename */
1925 30 profile_opts |= NYTP_OPTf_ADDPID;
1926 }
1927  
1928 31 ▶ if (profile_forkdepth == 0) { /* parent doesn't want children profiled */
Branch: 1 branch
BranchTrueFalseTotal
if (profile_forkdepth == 0) { /* parent doesn't want children profiled */ 4 27 31
1929 4 disable_profile(aTHX);
1930 4 open_new_file = 0;
1931 }
1932 else /* count down another generation */
1933 27 --profile_forkdepth;
1934  
1935 31 ▶ if (open_new_file)
Branch: 1 branch
BranchTrueFalseTotal
if (open_new_file) 27 4 31
1936 27 open_output_file(aTHX_ PROF_output_file);
1937  
1938 31 return 1; /* have forked */
1939 }
1940  
1941  
1942 /******************************************
1943 * Sub caller and inclusive time tracking
1944 ******************************************/
1945  
1946 static AV *
1947 442356 new_sub_call_info_av(pTHX)
1948 {
1949 442356 AV *av = newAV();
1950 442356 av_store(av, NYTP_SCi_CALL_COUNT, newSVuv(1));
1951 442356 av_store(av, NYTP_SCi_INCL_RTIME, newSVnv(0.0));
1952 442356 av_store(av, NYTP_SCi_EXCL_RTIME, newSVnv(0.0));
1953 442356 av_store(av, NYTP_SCi_INCL_TICKS, newSVnv(0.0));
1954 442356 av_store(av, NYTP_SCi_EXCL_TICKS, newSVnv(0.0));
1955 /* others allocated when needed */
1956 442356 return av;
1957 }
1958  
1959 /* subroutine profiler subroutine entry structure. Represents a call
1960 * from one sub to another (the arc between the nodes, if you like)
1961 */
1962 typedef struct subr_entry_st subr_entry_t;
1963 struct subr_entry_st {
1964 unsigned int already_counted;
1965 U32 subr_prof_depth;
1966 long unsigned subr_call_seqn;
1967 SSize_t prev_subr_entry_ix; /* ix to callers subr_entry */
1968  
1969 time_of_day_t initial_call_timeofday;
1970 struct tms initial_call_cputimes;
1971 NV initial_overhead_ticks;
1972 NV initial_subr_ticks;
1973  
1974 unsigned int caller_fid;
1975 int caller_line;
1976 const char *caller_subpkg_pv;
1977 SV *caller_subnam_sv;
1978  
1979 CV *called_cv;
1980 int called_cv_depth;
1981 const char *called_is_xs; /* NULL, "xsub", or "syop" */
1982 const char *called_subpkg_pv;
1983 SV *called_subnam_sv;
1984 /* ensure all items are initialized in first phase of pp_subcall_profiler */
1985 int hide_subr_call_time; /* eg for CORE:accept */
1986 };
1987  
1988 /* save stack index to the current subroutine entry structure */
1989 static SSize_t subr_entry_ix = -1;
1990  
1991 #define subr_entry_ix_ptr(ix) ((ix != -1) ? SSPTR(ix, subr_entry_t *) : NULL)
1992  
1993  
1994 static void
1995 37184 append_linenum_to_begin(pTHX_ subr_entry_t *subr_entry) {
1996 37184 UV line = 0;
1997 SV *fullnamesv;
1998 SV *DBsv;
1999 37184 char *subname = SvPVX(subr_entry->called_subnam_sv);
2000 STRLEN pkg_len;
2001 STRLEN total_len;
2002  
2003 /* If sub is a BEGIN then append the line number to our name
2004 * so multiple BEGINs (either explicit or implicit, e.g., "use")
2005 * in the same file/package can be distinguished.
2006 */
2007 37184 ▶ if (!subname || *subname != 'B' || strNE(subname,"BEGIN"))
Branch: 3 branches
BranchTrueFalseTotal
if (!subname || *subname != 'B' || strNE(subname,"BEGIN")) 37184 0 37184
if (!subname || *subname != 'B' || strNE(subname,"BEGIN")) 37184 0 37184
if (!subname || *subname != 'B' || strNE(subname,"BEGIN")) 32 37152 37184
2008 32 return;
2009  
2010 /* get, and delete, the entry for this sub in the PL_DBsub hash */
2011 37152 pkg_len = strlen(subr_entry->called_subpkg_pv);
2012 37152 total_len = pkg_len + 2 /* :: */ + 5; /* BEGIN */
2013 37152 fullnamesv = newSV(total_len + 1); /* +1 for '\0' */
2014 37152 memcpy(SvPVX(fullnamesv), subr_entry->called_subpkg_pv, pkg_len);
2015 37152 memcpy(SvPVX(fullnamesv) + pkg_len, "::BEGIN", 7 + 1); /* + 1 for '\0' */
2016 37152 ▶ SvCUR_set(fullnamesv, total_len);
Branch: 6 branches
BranchTrueFalseTotal
SvCUR_set(fullnamesv, total_len); 0 37152 37152
SvCUR_set(fullnamesv, total_len); 0 37152 37152
SvCUR_set(fullnamesv, total_len); 0 0 0
SvCUR_set(fullnamesv, total_len); 0 0 0
SvCUR_set(fullnamesv, total_len); 0 37152 37152
SvCUR_set(fullnamesv, total_len); 0 0 0
2017 37152 SvPOK_on(fullnamesv);
2018 37152 DBsv = hv_delete(GvHV(PL_DBsub), SvPVX(fullnamesv), (I32)total_len, 1);
2019  
2020 37152 ▶ if (DBsv && parse_DBsub_value(aTHX_ DBsv, NULL, &line, NULL, SvPVX(fullnamesv))) {
Branch: 2 branches
BranchTrueFalseTotal
if (DBsv && parse_DBsub_value(aTHX_ DBsv, NULL, &line, NULL, SvPVX(fullnamesv))) { 36024 1128 37152
if (DBsv && parse_DBsub_value(aTHX_ DBsv, NULL, &line, NULL, SvPVX(fullnamesv))) { 36024 0 36024
2021 36024 (void)SvREFCNT_inc(DBsv); /* was made mortal by hv_delete */
2022 36024 sv_catpvf(fullnamesv, "@%u", (unsigned int)line);
2023 72048 ▶ if (hv_fetch(GvHV(PL_DBsub), SvPV_nolen(fullnamesv), (I32)SvCUR(fullnamesv), 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_fetch(GvHV(PL_DBsub), SvPV_nolen(fullnamesv), (I32)SvCUR(fullnamesv), 0)) { 811 35213 36024
2024 static unsigned int dup_begin_seqn;
2025 811 sv_catpvf(fullnamesv, ".%u", ++dup_begin_seqn);
2026 }
2027 72048 (void) hv_store(GvHV(PL_DBsub), SvPV_nolen(fullnamesv), (I32)SvCUR(fullnamesv), DBsv, 0);
2028  
2029 /* As we know the length of fullnamesv *before* the concatenation, we
2030 can calculate the length and offset of the formatted addition, and
2031 hence directly string append it, rather than duplicating the call to
2032 a *printf function. */
2033 36024 sv_catpvn(subr_entry->called_subnam_sv, SvPVX(fullnamesv) + total_len,
2034 SvCUR(fullnamesv) - total_len);
2035 }
2036 37152 SvREFCNT_dec(fullnamesv);
2037 }
2038  
2039  
2040 static char *
2041 0 subr_entry_summary(pTHX_ subr_entry_t *subr_entry, int state)
2042 {
2043 static char buf[80]; /* XXX */
2044 0 ▶ sprintf(buf, "(seix %d%s%d, ac%u)",
Branch: 1 branch
BranchTrueFalseTotal
sprintf(buf, "(seix %d%s%d, ac%u)", 0 0 0
2045 0 (int)subr_entry->prev_subr_entry_ix,
2046 (state) ? "<-" : "->",
2047 (int)subr_entry_ix,
2048 subr_entry->already_counted
2049 );
2050 0 return buf;
2051 }
2052  
2053  
2054 static void
2055 263076645 subr_entry_destroy(pTHX_ subr_entry_t *subr_entry)
2056 {
2057 263076645 ▶ if ((trace_level >= 6 || subr_entry->already_counted>1)
Branch: 2 branches
BranchTrueFalseTotal
if ((trace_level >= 6 || subr_entry->already_counted>1) 263076645 0 263076645
if ((trace_level >= 6 || subr_entry->already_counted>1) 0 263076645 263076645
2058 /* ignore the typical second (fallback) destroy */
2059 0 ▶ && !(subr_entry->prev_subr_entry_ix == subr_entry_ix && subr_entry->already_counted==1)
Branch: 2 branches
BranchTrueFalseTotal
&& !(subr_entry->prev_subr_entry_ix == subr_entry_ix && subr_entry->already_counted==1) 0 0 0
&& !(subr_entry->prev_subr_entry_ix == subr_entry_ix && subr_entry->already_counted==1) 0 0 0
2060 ) {
2061 0 logwarn("%2u << %s::%s done %s\n",
2062 0 (unsigned int)subr_entry->subr_prof_depth,
2063 subr_entry->called_subpkg_pv,
2064 0 ▶ (subr_entry->called_subnam_sv && SvOK(subr_entry->called_subnam_sv))
Branch: 2 branches
BranchTrueFalseTotal
(subr_entry->called_subnam_sv && SvOK(subr_entry->called_subnam_sv)) 0 0 0
(subr_entry->called_subnam_sv && SvOK(subr_entry->called_subnam_sv)) 0 0 0
2065 0 ? SvPV_nolen(subr_entry->called_subnam_sv)
2066 : "?",
2067 subr_entry_summary(aTHX_ subr_entry, 1));
2068 }
2069 263076645 ▶ if (subr_entry->caller_subnam_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (subr_entry->caller_subnam_sv) { 131538439 131538206 263076645
2070 131538439 sv_free(subr_entry->caller_subnam_sv);
2071 131538439 subr_entry->caller_subnam_sv = Nullsv;
2072 }
2073 263076645 ▶ if (subr_entry->called_subnam_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (subr_entry->called_subnam_sv) { 131538439 131538206 263076645
2074 131538439 sv_free(subr_entry->called_subnam_sv);
2075 131538439 subr_entry->called_subnam_sv = Nullsv;
2076 }
2077 263076645 ▶ if (subr_entry->prev_subr_entry_ix <= subr_entry_ix)
Branch: 1 branch
BranchTrueFalseTotal
if (subr_entry->prev_subr_entry_ix <= subr_entry_ix) 263076645 0 263076645
2078 263076645 subr_entry_ix = subr_entry->prev_subr_entry_ix;
2079 else
2080 0 logwarn("skipped attempt to raise subr_entry_ix from %d to %d\n",
2081 0 (int)subr_entry_ix, (int)subr_entry->prev_subr_entry_ix);
2082 263076645 }
2083  
2084  
2085 static void
2086 263076645 incr_sub_inclusive_time(pTHX_ subr_entry_t *subr_entry)
2087 {
2088 263076645 int saved_errno = errno;
2089 char called_subname_pv[NYTP_MAX_SUB_NAME_LEN];
2090 263076645 char *called_subname_pv_end = called_subname_pv;
2091 char subr_call_key[NYTP_MAX_SUB_NAME_LEN];
2092 int subr_call_key_len;
2093 NV overhead_ticks, called_sub_ticks;
2094 SV *incl_time_sv, *excl_time_sv;
2095 NV incl_subr_ticks, excl_subr_ticks;
2096 SV *sv_tmp;
2097 AV *subr_call_av;
2098 time_of_day_t sub_end_time;
2099 long ticks, overflow;
2100  
2101 /* an undef SV is a special marker used by subr_entry_setup */
2102 263076645 ▶ if (subr_entry->called_subnam_sv && !SvOK(subr_entry->called_subnam_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (subr_entry->called_subnam_sv && !SvOK(subr_entry->called_subnam_sv)) { 131538439 131538206 263076645
if (subr_entry->called_subnam_sv && !SvOK(subr_entry->called_subnam_sv)) { 4 131538435 131538439
2103 4 ▶ if (trace_level)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level) 0 4 4
2104 0 logwarn("Don't know name of called sub, assuming xsub/builtin exited via an exception (which isn't handled yet)\n");
2105 4 subr_entry->already_counted++;
2106 }
2107  
2108 /* For xsubs we get called both explicitly when the xsub returns, and by
2109 * the destructor. (That way if the xsub leaves via an exception then we'll
2110 * still get called, albeit a little later than we'd like.)
2111 */
2112 263076645 ▶ if (subr_entry->already_counted) {
Branch: 1 branch
BranchTrueFalseTotal
if (subr_entry->already_counted) { 131538210 131538435 263076645
2113 131538210 subr_entry_destroy(aTHX_ subr_entry);
2114 131538210 return;
2115 }
2116 131538435 subr_entry->already_counted++;
2117  
2118 /* statement overheads we've accumulated since we entered the sub */
2119 131538435 overhead_ticks = cumulative_overhead_ticks - subr_entry->initial_overhead_ticks;
2120 /* ticks spent in subroutines called by this subroutine */
2121 131538435 called_sub_ticks = cumulative_subr_ticks - subr_entry->initial_subr_ticks;
2122  
2123 /* calculate ticks since we entered the sub */
2124 131538435 get_time_of_day(sub_end_time);
2125 131538435 get_ticks_between(NV, subr_entry->initial_call_timeofday, sub_end_time, ticks, overflow);
2126  
2127 131538435 incl_subr_ticks = (overflow*ticks_per_sec) + ticks;
2128 /* subtract statement measurement overheads */
2129 131538435 incl_subr_ticks -= overhead_ticks;
2130  
2131 131538435 ▶ if (subr_entry->hide_subr_call_time) {
Branch: 1 branch
BranchTrueFalseTotal
if (subr_entry->hide_subr_call_time) { 0 131538435 131538435
2132 /* account for the time spent in the sub as if it was statement
2133 * profiler overhead. That has the effect of neatly subtracting
2134 * the time from all the sub calls up the call stack.
2135 */
2136 0 cumulative_overhead_ticks += incl_subr_ticks;
2137 0 incl_subr_ticks = 0;
2138 0 called_sub_ticks = 0;
2139 }
2140  
2141 /* exclusive = inclusive - time spent in subroutines called by this subroutine */
2142 131538435 excl_subr_ticks = incl_subr_ticks - called_sub_ticks;
2143  
2144 131538435 subr_call_key_len = my_snprintf(subr_call_key, sizeof(subr_call_key), "%s::%s[%u:%d]",
2145 subr_entry->caller_subpkg_pv,
2146 263076870 ▶ (subr_entry->caller_subnam_sv) ? SvPV_nolen(subr_entry->caller_subnam_sv) : "(null)",
Branch: 1 branch
BranchTrueFalseTotal
(subr_entry->caller_subnam_sv) ? SvPV_nolen(subr_entry->caller_subnam_sv) : "(null)", 131538435 0 131538435
2147 subr_entry->caller_fid, subr_entry->caller_line);
2148 131538435 ▶ if (subr_call_key_len >= sizeof(subr_call_key))
Branch: 1 branch
BranchTrueFalseTotal
if (subr_call_key_len >= sizeof(subr_call_key)) 0 131538435 131538435
2149 0 croak(nytp_panic_overflow_msg_fmt, "subr_call_key", subr_call_key);
2150  
2151 /* compose called_subname_pv as "${pkg}::${sub}" avoiding sprintf */
2152 STMT_START {
2153 STRLEN len;
2154 const char *p;
2155  
2156 131538435 p = subr_entry->called_subpkg_pv;
2157 1317330946 ▶ while (*p)
Branch: 1 branch
BranchTrueFalseTotal
while (*p) 1185792511 131538435 1317330946
2158 1185792511 *called_subname_pv_end++ = *p++;
2159 131538435 *called_subname_pv_end++ = ':';
2160 131538435 *called_subname_pv_end++ = ':';
2161 131538435 ▶ if (subr_entry->called_subnam_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (subr_entry->called_subnam_sv) { 131538435 0 131538435
2162 /* We create this SV, so we know that it is well-formed, and has a
2163 trailing '\0' */
2164 263076870 p = SvPV(subr_entry->called_subnam_sv, len);
2165 }
2166 else {
2167 /* C string constants have a trailing '\0'. */
2168 0 p = "(null)"; len = 6;
2169 }
2170 131538435 memcpy(called_subname_pv_end, p, len + 1);
2171 131538435 called_subname_pv_end += len;
2172 131538435 ▶ if (called_subname_pv_end >= called_subname_pv+sizeof(called_subname_pv))
Branch: 1 branch
BranchTrueFalseTotal
if (called_subname_pv_end >= called_subname_pv+sizeof(called_subname_pv)) 0 131538435 131538435
2173 0 croak(nytp_panic_overflow_msg_fmt, "called_subname_pv", called_subname_pv);
2174 } STMT_END;
2175  
2176 /* { called_subname => { "caller_subname[fid:line]" => [ count, incl_time, ... ] } } */
2177 131538435 sv_tmp = *hv_fetch(sub_callers_hv, called_subname_pv, (I32)(called_subname_pv_end - called_subname_pv), 1);
2178  
2179 131538435 ▶ if (!SvROK(sv_tmp)) { /* autoviv hash ref - is first call of this called subname from anywhere */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv_tmp)) { /* autoviv hash ref - is first call of this called subname from anywhere */ 216717 131321718 131538435
2180 216717 HV *hv = newHV();
2181 216717 sv_setsv(sv_tmp, newRV_noinc((SV *)hv));
2182  
2183 216717 ▶ if (subr_entry->called_is_xs) {
Branch: 1 branch
BranchTrueFalseTotal
if (subr_entry->called_is_xs) { 81185 135532 216717
2184 /* create dummy item with fid=0 & line=0 to act as flag to indicate xs */
2185 81185 AV *av = new_sub_call_info_av(aTHX);
2186 81185 av_store(av, NYTP_SCi_CALL_COUNT, newSVuv(0));
2187 81185 sv_setsv(*hv_fetch(hv, "[0:0]", 5, 1), newRV_noinc((SV *)av));
2188  
2189 81185 ▶ if ( ('s' == *subr_entry->called_is_xs) /* "sop" (slowop) */
Branch: 1 branch
BranchTrueFalseTotal
if ( ('s' == *subr_entry->called_is_xs) /* "sop" (slowop) */ 41166 40019 81185
2190 41166 ▶ || (subr_entry->called_cv && SvTYPE(subr_entry->called_cv) == SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
|| (subr_entry->called_cv && SvTYPE(subr_entry->called_cv) == SVt_PVCV) 41166 0 41166
|| (subr_entry->called_cv && SvTYPE(subr_entry->called_cv) == SVt_PVCV) 41166 0 41166
2191 ) {
2192 /* We just use an empty string as the filename for xsubs
2193 * because CvFILE() isn't reliable on perl 5.8.[78]
2194 * and the name of the .c file isn't very useful anyway.
2195 * The reader can try to associate the xsubs with the
2196 * corresponding .pm file using the package part of the subname.
2197 */
2198 81185 SV *sv = *hv_fetch(GvHV(PL_DBsub), called_subname_pv, (I32)(called_subname_pv_end - called_subname_pv), 1);
2199 81185 ▶ if (!SvOK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) 80647 538 81185
2200 80647 sv_setpvs(sv, ":0-0"); /* empty file name */
2201 81185 ▶ if (trace_level >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 2) 0 81185 81185
2202 0 logwarn("Marking '%s' as %s\n", called_subname_pv, subr_entry->called_is_xs);
2203 }
2204 }
2205 }
2206  
2207 /* drill-down to array of sub call information for this subr_call_key */
2208 131538435 sv_tmp = *hv_fetch((HV*)SvRV(sv_tmp), subr_call_key, subr_call_key_len, 1);
2209 131538435 ▶ if (!SvROK(sv_tmp)) { /* first call from this subname[fid:line] - autoviv array ref */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv_tmp)) { /* first call from this subname[fid:line] - autoviv array ref */ 361171 131177264 131538435
2210 361171 subr_call_av = new_sub_call_info_av(aTHX);
2211  
2212 361171 sv_setsv(sv_tmp, newRV_noinc((SV *)subr_call_av));
2213  
2214 361171 ▶ if (subr_entry->called_subpkg_pv) { /* note that a sub in this package was called */
Branch: 1 branch
BranchTrueFalseTotal
if (subr_entry->called_subpkg_pv) { /* note that a sub in this package was called */ 361171 0 361171
2215 361171 SV *pf_sv = *hv_fetch(pkg_fids_hv, subr_entry->called_subpkg_pv, (I32)strlen(subr_entry->called_subpkg_pv), 1);
2216 361171 ▶ if (SvTYPE(pf_sv) == SVt_NULL) { /* log when first created */
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(pf_sv) == SVt_NULL) { /* log when first created */ 29880 331291 361171
2217 29880 sv_upgrade(pf_sv, SVt_PV);
2218 29880 ▶ if (trace_level >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 3) 0 29880 29880
2219 0 logwarn("Noting that subs in package '%s' were called\n",
2220 subr_entry->called_subpkg_pv);
2221 }
2222 }
2223 }
2224 else {
2225 131177264 subr_call_av = (AV *)SvRV(sv_tmp);
2226 131177264 sv_inc(AvARRAY(subr_call_av)[NYTP_SCi_CALL_COUNT]);
2227 }
2228  
2229 131538435 ▶ if (trace_level >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 5) { 0 131538435 131538435
2230 0 logwarn("%2u <- %s %" NVgf " excl = %" NVgf "t incl - %" NVgf "t (%" NVgf "-%" NVgf "), oh %" NVff "-%" NVff "=%" NVff "t, d%d @%d:%d #%lu %p\n",
2231 0 (unsigned int)subr_entry->subr_prof_depth, called_subname_pv,
2232 excl_subr_ticks, incl_subr_ticks,
2233 called_sub_ticks,
2234 cumulative_subr_ticks, subr_entry->initial_subr_ticks,
2235 cumulative_overhead_ticks, subr_entry->initial_overhead_ticks, overhead_ticks,
2236 (int)subr_entry->called_cv_depth,
2237 subr_entry->caller_fid, subr_entry->caller_line,
2238 subr_entry->subr_call_seqn, (void*)subr_entry);
2239 }
2240  
2241 /* only count inclusive time for the outer-most calls */
2242 131538435 ▶ if (subr_entry->called_cv_depth <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (subr_entry->called_cv_depth <= 1) { 129763738 1774697 131538435
2243 129763738 incl_time_sv = *av_fetch(subr_call_av, NYTP_SCi_INCL_TICKS, 1);
2244 129763738 sv_setnv(incl_time_sv, SvNV(incl_time_sv)+incl_subr_ticks);
2245 }
2246 else { /* recursing into an already entered sub */
2247 /* measure max depth and accumulate incl time separately */
2248 1774697 SV *reci_time_sv = *av_fetch(subr_call_av, NYTP_SCi_RECI_RTIME, 1);
2249 1774697 SV *max_depth_sv = *av_fetch(subr_call_av, NYTP_SCi_REC_DEPTH, 1);
2250 1774697 ▶ sv_setnv(reci_time_sv, (SvOK(reci_time_sv)) ? SvNV(reci_time_sv)+(incl_subr_ticks/ticks_per_sec) : (incl_subr_ticks/ticks_per_sec));
Branch: 1 branch
BranchTrueFalseTotal
sv_setnv(reci_time_sv, (SvOK(reci_time_sv)) ? SvNV(reci_time_sv)+(incl_subr_ticks/ticks_per_sec) : (incl_subr_ticks/ticks_per_sec)); 1770307 4390 1774697
2251 /* we track recursion depth here, which is called_cv_depth-1 */
2252 1774697 ▶ if (!SvOK(max_depth_sv) || subr_entry->called_cv_depth-1 > SvIV(max_depth_sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(max_depth_sv) || subr_entry->called_cv_depth-1 > SvIV(max_depth_sv)) 1770307 4390 1774697
if (!SvOK(max_depth_sv) || subr_entry->called_cv_depth-1 > SvIV(max_depth_sv)) 1834 1768473 1770307
2253 6224 sv_setiv(max_depth_sv, subr_entry->called_cv_depth-1);
2254 }
2255 131538435 excl_time_sv = *av_fetch(subr_call_av, NYTP_SCi_EXCL_TICKS, 1);
2256 131538435 sv_setnv(excl_time_sv, SvNV(excl_time_sv)+excl_subr_ticks);
2257  
2258 131538435 ▶ if (opt_calls && out) {
Branch: 2 branches
BranchTrueFalseTotal
if (opt_calls && out) { 99184939 32353496 131538435
if (opt_calls && out) { 99184900 39 99184939
2259 99184900 NYTP_write_call_return(out, subr_entry->subr_prof_depth, called_subname_pv, incl_subr_ticks, excl_subr_ticks);
2260 }
2261  
2262 131538435 subr_entry_destroy(aTHX_ subr_entry);
2263  
2264 131538435 cumulative_subr_ticks += excl_subr_ticks;
2265 131538435 SETERRNO(saved_errno, 0);
2266 }
2267  
2268 static void /* wrapper called at scope exit due to save_destructor below */
2269 159647651 incr_sub_inclusive_time_ix(pTHX_ void *subr_entry_ix_void)
2270 {
2271 /* recover the SSize_t ix that was stored as a void pointer */
2272 159647651 SSize_t save_ix = (SSize_t)PTR2IV(subr_entry_ix_void);
2273 159647651 ▶ incr_sub_inclusive_time(aTHX_ subr_entry_ix_ptr(save_ix));
Branch: 1 branch
BranchTrueFalseTotal
incr_sub_inclusive_time(aTHX_ subr_entry_ix_ptr(save_ix)); 159647651 0 159647651
2274 159647651 }
2275  
2276  
2277 static CV *
2278 214423363 resolve_sub_to_cv(pTHX_ SV *sv, GV **subname_gv_ptr)
2279 {
2280 GV *dummy_gv;
2281 HV *stash;
2282 CV *cv;
2283  
2284 214423363 ▶ if (!subname_gv_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!subname_gv_ptr) 0 214423363 214423363
2285 0 subname_gv_ptr = &dummy_gv;
2286 else
2287 214423363 *subname_gv_ptr = Nullgv;
2288  
2289 /* copied from top of perl's pp_entersub */
2290 /* modified to return either CV or else a GV */
2291 /* or a NULL in cases that pp_entersub would croak */
2292 214423363 switch (SvTYPE(sv)) {
2293 421009 default:
2294 421009 ▶ if (!SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) { 2030 418979 421009
2295 char *sym;
2296  
2297 2030 ▶ if (sv == &PL_sv_yes) { /* unfound import, ignore */
Branch: 1 branch
BranchTrueFalseTotal
if (sv == &PL_sv_yes) { /* unfound import, ignore */ 0 2030 2030
2298 0 return NULL;
2299 }
2300 2030 ▶ if (SvGMAGICAL(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(sv)) { 0 2030 2030
2301 0 mg_get(sv);
2302 0 ▶ if (SvROK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) 0 0 0
2303 0 goto got_rv;
2304 0 ▶ sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
Branch: 1 branch
BranchTrueFalseTotal
sym = SvPOKp(sv) ? SvPVX(sv) : Nullch; 0 0 0
2305 }
2306 else
2307 4060 sym = SvPV_nolen(sv);
2308 2030 ▶ if (!sym)
Branch: 1 branch
BranchTrueFalseTotal
if (!sym) 0 2030 2030
2309 0 return NULL;
2310 2030 ▶ if (PL_op->op_private & HINT_STRICT_REFS)
Branch: 1 branch
BranchTrueFalseTotal
if (PL_op->op_private & HINT_STRICT_REFS) 4 2026 2030
2311 4 return NULL;
2312 2026 cv = get_cv(sym, TRUE);
2313 2026 break;
2314 }
2315 418979 got_rv:
2316 {
2317 418979 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2318 418979 tryAMAGICunDEREF(to_cv);
2319 }
2320 418979 cv = (CV*)SvRV(sv);
2321 418979 ▶ if (SvTYPE(cv) == SVt_PVCV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(cv) == SVt_PVCV) 418979 0 418979
2322 418979 break;
2323 /* FALL THROUGH */
2324 case SVt_PVHV:
2325 case SVt_PVAV:
2326 0 return NULL;
2327 184470929 case SVt_PVCV:
2328 184470929 cv = (CV*)sv;
2329 184470929 break;
2330 29531425 case SVt_PVGV:
2331 29531425 ▶ if (!(isGV_with_GP(sv) && (cv = GvCVu((GV*)sv))))
Branch: 5 branches
BranchTrueFalseTotal
if (!(isGV_with_GP(sv) && (cv = GvCVu((GV*)sv)))) 29531425 0 29531425
if (!(isGV_with_GP(sv) && (cv = GvCVu((GV*)sv)))) 0 29531425 29531425
if (!(isGV_with_GP(sv) && (cv = GvCVu((GV*)sv)))) 0 0 0
if (!(isGV_with_GP(sv) && (cv = GvCVu((GV*)sv)))) 29531425 0 29531425
if (!(isGV_with_GP(sv) && (cv = GvCVu((GV*)sv)))) 16 29531409 29531425
2332 16 cv = sv_2cv(sv, &stash, subname_gv_ptr, FALSE);
2333 29531425 ▶ if (!cv) /* would autoload in this situation */
Branch: 1 branch
BranchTrueFalseTotal
if (!cv) /* would autoload in this situation */ 16 29531409 29531425
2334 16 return NULL;
2335 29531409 break;
2336 }
2337 214423343 ▶ if (cv && !*subname_gv_ptr && CvGV(cv) && isGV_with_GP(CvGV(cv))) {
Branch: 6 branches
BranchTrueFalseTotal
if (cv && !*subname_gv_ptr && CvGV(cv) && isGV_with_GP(CvGV(cv))) { 214423343 0 214423343
if (cv && !*subname_gv_ptr && CvGV(cv) && isGV_with_GP(CvGV(cv))) { 214423343 0 214423343
if (cv && !*subname_gv_ptr && CvGV(cv) && isGV_with_GP(CvGV(cv))) { 214423343 0 214423343
if (cv && !*subname_gv_ptr && CvGV(cv) && isGV_with_GP(CvGV(cv))) { 214423343 0 214423343
if (cv && !*subname_gv_ptr && CvGV(cv) && isGV_with_GP(CvGV(cv))) { 0 214423343 214423343
if (cv && !*subname_gv_ptr && CvGV(cv) && isGV_with_GP(CvGV(cv))) { 0 0 0
2338 214423343 *subname_gv_ptr = CvGV(cv);
2339 }
2340 214423343 return cv;
2341 }
2342  
2343  
2344  
2345 static CV*
2346 8451 current_cv(pTHX_ I32 ix, PERL_SI *si)
2347 {
2348 /* returning the current cv */
2349 /* logic based on perl's S_deb_curcv in dump.c */
2350 /* see also http://metacpan.org/release/Devel-StackBlech/ */
2351 PERL_CONTEXT *cx;
2352 8451 ▶ if (!si)
Branch: 1 branch
BranchTrueFalseTotal
if (!si) 6731 1720 8451
2353 6731 si = PL_curstackinfo;
2354  
2355 8451 ▶ if (ix < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix < 0) { 135 8316 8451
2356 /* caller isn't on the same stack so we'll walk the stacks as well */
2357 135 ▶ if (si->si_type != PERLSI_MAIN)
Branch: 1 branch
BranchTrueFalseTotal
if (si->si_type != PERLSI_MAIN) 48 87 135
2358 48 return current_cv(aTHX_ si->si_prev->si_cxix, si->si_prev);
2359 87 ▶ if (trace_level >= 9)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 9) 0 87 87
2360 0 logwarn("finding current_cv(%d,%p) si_type %d - context stack empty\n",
2361 0 (int)ix, (void*)si, (int)si->si_type);
2362 87 return Nullcv; /* PL_main_cv ? */
2363 }
2364  
2365 8316 cx = &si->si_cxstack[ix];
2366  
2367 8316 ▶ if (trace_level >= 9)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 9) 0 8316 8316
2368 0 logwarn("finding current_cv(%d,%p) - cx_type %d %s, si_type %d\n",
2369 0 (int)ix, (void*)si, CxTYPE(cx), cx_block_type(cx), (int)si->si_type);
2370  
2371 /* the common case of finding the caller on the same stack */
2372 8316 ▶ if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT)
Branch: 2 branches
BranchTrueFalseTotal
if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) 8306 10 8316
if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) 0 8306 8306
2373 10 return cx->blk_sub.cv;
2374 8306 ▶ else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
Branch: 2 branches
BranchTrueFalseTotal
else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx)) 5418 2888 8306
else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx)) 1058 4360 5418
2375 1058 return current_cv(aTHX_ ix - 1, si); /* recurse up stack */
2376 7248 ▶ else if (ix == 0 && si->si_type == PERLSI_MAIN)
Branch: 2 branches
BranchTrueFalseTotal
else if (ix == 0 && si->si_type == PERLSI_MAIN) 6737 511 7248
else if (ix == 0 && si->si_type == PERLSI_MAIN) 6634 103 6737
2377 6634 return PL_main_cv;
2378 614 ▶ else if (ix > 0) /* more on this stack? */
Branch: 1 branch
BranchTrueFalseTotal
else if (ix > 0) /* more on this stack? */ 511 103 614
2379 511 return current_cv(aTHX_ ix - 1, si); /* recurse up stack */
2380  
2381 /* caller isn't on the same stack so we'll walk the stacks as well */
2382 103 ▶ if (si->si_type != PERLSI_MAIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (si->si_type != PERLSI_MAIN) { 103 0 103
2383 103 return current_cv(aTHX_ si->si_prev->si_cxix, si->si_prev);
2384 }
2385 0 return Nullcv;
2386 }
2387  
2388  
2389 static SSize_t
2390 131538439 subr_entry_setup(pTHX_ COP *prev_cop, subr_entry_t *clone_subr_entry, OPCODE op_type, SV *subr_sv)
2391 {
2392 131538439 int saved_errno = errno;
2393 subr_entry_t *subr_entry;
2394 SSize_t prev_subr_entry_ix;
2395 subr_entry_t *caller_subr_entry;
2396 const char *found_caller_by;
2397 char *file;
2398  
2399 /* allocate struct to save stack (very efficient) */
2400 /* XXX "warning: cast from pointer to integer of different size" with use64bitall=define */
2401 131538439 prev_subr_entry_ix = subr_entry_ix;
2402 131538439 subr_entry_ix = SSNEWa(sizeof(*subr_entry), MEM_ALIGNBYTES);
2403  
2404 131538439 ▶ if (subr_entry_ix <= prev_subr_entry_ix) {
Branch: 1 branch
BranchTrueFalseTotal
if (subr_entry_ix <= prev_subr_entry_ix) { 0 131538439 131538439
2405 /* one cause of this is running NYTProf with threads */
2406 0 logwarn("NYTProf panic: stack is confused, giving up! (Try running with subs=0) ix=%" IVdf " prev_ix=%" IVdf "\n", (IV)subr_entry_ix, (IV)prev_subr_entry_ix);
2407 /* limit the damage */
2408 0 disable_profile(aTHX);
2409 0 return prev_subr_entry_ix;
2410 }
2411  
2412 131538439 ▶ subr_entry = subr_entry_ix_ptr(subr_entry_ix);
Branch: 1 branch
BranchTrueFalseTotal
subr_entry = subr_entry_ix_ptr(subr_entry_ix); 131538439 0 131538439
2413 131538439 ▶ Zero(subr_entry, 1, subr_entry_t);
Branch: 1 branch
BranchTrueFalseTotal
Zero(subr_entry, 1, subr_entry_t); 0 131538439 131538439
2414  
2415 131538439 subr_entry->prev_subr_entry_ix = prev_subr_entry_ix;
2416 131538439 ▶ caller_subr_entry = subr_entry_ix_ptr(prev_subr_entry_ix);
Branch: 1 branch
BranchTrueFalseTotal
caller_subr_entry = subr_entry_ix_ptr(prev_subr_entry_ix); 131531604 6835 131538439
2417 131538439 subr_entry->subr_prof_depth = (caller_subr_entry)
2418 131538439 ▶ ? caller_subr_entry->subr_prof_depth+1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
? caller_subr_entry->subr_prof_depth+1 : 1; 131531604 6835 131538439
2419  
2420 131538439 get_time_of_day(subr_entry->initial_call_timeofday);
2421 131538439 subr_entry->initial_overhead_ticks = cumulative_overhead_ticks;
2422 131538439 subr_entry->initial_subr_ticks = cumulative_subr_ticks;
2423 131538439 subr_entry->subr_call_seqn = (unsigned long)(++cumulative_subr_seqn);
2424  
2425 /* try to work out what sub's being called in advance
2426 * mainly for xsubs because otherwise they're transparent
2427 * because xsub calls don't get a new context
2428 */
2429 131538439 ▶ if (op_type == OP_ENTERSUB || op_type == OP_GOTO) {
Branch: 2 branches
BranchTrueFalseTotal
if (op_type == OP_ENTERSUB || op_type == OP_GOTO) { 10276376 121262063 131538439
if (op_type == OP_ENTERSUB || op_type == OP_GOTO) { 4267 10272109 10276376
2430 121266330 GV *called_gv = Nullgv;
2431 121266330 subr_entry->called_cv = resolve_sub_to_cv(aTHX_ subr_sv, &called_gv);
2432 121266330 ▶ if (called_gv) {
Branch: 1 branch
BranchTrueFalseTotal
if (called_gv) { 121266326 4 121266330
2433 121266326 ▶ char *p = HvNAME(GvSTASH(called_gv));
Branch: 6 branches
BranchTrueFalseTotal
char *p = HvNAME(GvSTASH(called_gv)); 121266326 0 121266326
char *p = HvNAME(GvSTASH(called_gv)); 121266326 0 121266326
char *p = HvNAME(GvSTASH(called_gv)); 0 121266326 121266326
char *p = HvNAME(GvSTASH(called_gv)); 0 0 0
char *p = HvNAME(GvSTASH(called_gv)); 121266326 0 121266326
char *p = HvNAME(GvSTASH(called_gv)); 0 121266326 121266326
2434 121266326 subr_entry->called_subpkg_pv = p;
2435 121266326 subr_entry->called_subnam_sv = newSVpv(GvNAME(called_gv), 0);
2436  
2437 /* detect calls to POSIX::_exit */
2438 121266326 ▶ if ('P'==*p++ && 'O'==*p++ && 'S'==*p++ && 'I'==*p++ && 'X'==*p++ && 0==*p) {
Branch: 6 branches
BranchTrueFalseTotal
if ('P'==*p++ && 'O'==*p++ && 'S'==*p++ && 'I'==*p++ && 'X'==*p++ && 0==*p) { 1541878 119724448 121266326
if ('P'==*p++ && 'O'==*p++ && 'S'==*p++ && 'I'==*p++ && 'X'==*p++ && 0==*p) { 0 1541878 1541878
if ('P'==*p++ && 'O'==*p++ && 'S'==*p++ && 'I'==*p++ && 'X'==*p++ && 0==*p) { 0 0 0
if ('P'==*p++ && 'O'==*p++ && 'S'==*p++ && 'I'==*p++ && 'X'==*p++ && 0==*p) { 0 0 0
if ('P'==*p++ && 'O'==*p++ && 'S'==*p++ && 'I'==*p++ && 'X'==*p++ && 0==*p) { 0 0 0
if ('P'==*p++ && 'O'==*p++ && 'S'==*p++ && 'I'==*p++ && 'X'==*p++ && 0==*p) { 0 0 0
2439 0 char *s = GvNAME(called_gv);
2440 0 ▶ if ('_'==*s++ && 'e'==*s++ && 'x'==*s++ && 'i'==*s++ && 't'==*s++ && 0==*s) {
Branch: 6 branches
BranchTrueFalseTotal
if ('_'==*s++ && 'e'==*s++ && 'x'==*s++ && 'i'==*s++ && 't'==*s++ && 0==*s) { 0 0 0
if ('_'==*s++ && 'e'==*s++ && 'x'==*s++ && 'i'==*s++ && 't'==*s++ && 0==*s) { 0 0 0
if ('_'==*s++ && 'e'==*s++ && 'x'==*s++ && 'i'==*s++ && 't'==*s++ && 0==*s) { 0 0 0
if ('_'==*s++ && 'e'==*s++ && 'x'==*s++ && 'i'==*s++ && 't'==*s++ && 0==*s) { 0 0 0
if ('_'==*s++ && 'e'==*s++ && 'x'==*s++ && 'i'==*s++ && 't'==*s++ && 0==*s) { 0 0 0
if ('_'==*s++ && 'e'==*s++ && 'x'==*s++ && 'i'==*s++ && 't'==*s++ && 0==*s) { 0 0 0
2441 0 finish_profile(aTHX);
2442 }
2443 }
2444 }
2445 else {
2446 /* resolve_sub_to_cv couldn't work out what's being called,
2447 * possibly because it's something that'll cause pp_entersub to croak
2448 * anyway. So we mark the subr_entry in a particular way and hope that
2449 * pp_subcall_profiler() can fill in the details.
2450 * If there is an exception then we'll wind up in incr_sub_inclusive_time
2451 * which will see this mark and ignore the call.
2452 */
2453 4 subr_entry->called_subnam_sv = newSV(0);
2454 }
2455 121266330 subr_entry->called_is_xs = NULL; /* work it out later */
2456 }
2457 else { /* slowop */
2458  
2459 /* pretend slowops (builtins) are xsubs */
2460 10272109 const char *slowop_name = PL_op_name[op_type];
2461 10272109 ▶ if (profile_slowops == 1) { /* 1 == put slowops into 1 package */
Branch: 1 branch
BranchTrueFalseTotal
if (profile_slowops == 1) { /* 1 == put slowops into 1 package */ 0 10272109 10272109
2462 0 subr_entry->called_subpkg_pv = "CORE";
2463 0 subr_entry->called_subnam_sv = newSVpv(slowop_name, 0);
2464 }
2465 else { /* 2 == put slowops into multiple packages */
2466 10272109 SV **opname = NULL;
2467 SV *sv;
2468 10272109 ▶ if (!slowop_name_cache)
Branch: 1 branch
BranchTrueFalseTotal
if (!slowop_name_cache) 579 10271530 10272109
2469 579 slowop_name_cache = newAV();
2470 10272109 opname = av_fetch(slowop_name_cache, op_type, TRUE);
2471 10272109 ▶ if (!opname)
Branch: 1 branch
BranchTrueFalseTotal
if (!opname) 0 10272109 10272109
2472 0 croak("panic: opname cache read for '%s' (%d)\n", slowop_name, op_type);
2473 10272109 sv = *opname;
2474  
2475 10272109 ▶ if(!SvOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(sv)) { 12512 10259597 10272109
2476 12512 const STRLEN len = strlen(slowop_name);
2477 12512 sv_grow(sv, 5 + len + 1);
2478 12512 memcpy(SvPVX(sv), "CORE:", 5);
2479 12512 memcpy(SvPVX(sv) + 5, slowop_name, len + 1);
2480 12512 ▶ SvCUR_set(sv, 5 + len);
Branch: 6 branches
BranchTrueFalseTotal
SvCUR_set(sv, 5 + len); 0 12512 12512
SvCUR_set(sv, 5 + len); 0 12512 12512
SvCUR_set(sv, 5 + len); 0 0 0
SvCUR_set(sv, 5 + len); 0 0 0
SvCUR_set(sv, 5 + len); 0 12512 12512
SvCUR_set(sv, 5 + len); 0 0 0
2481 12512 SvPOK_on(sv);
2482 }
2483 10272109 subr_entry->called_subnam_sv = SvREFCNT_inc(sv);
2484 10272109 ▶ subr_entry->called_subpkg_pv = CopSTASHPV(PL_curcop);
Branch: 7 branches
BranchTrueFalseTotal
subr_entry->called_subpkg_pv = CopSTASHPV(PL_curcop); 10272109 0 10272109
subr_entry->called_subpkg_pv = CopSTASHPV(PL_curcop); 10272109 0 10272109
subr_entry->called_subpkg_pv = CopSTASHPV(PL_curcop); 10272109 0 10272109
subr_entry->called_subpkg_pv = CopSTASHPV(PL_curcop); 0 10272109 10272109
subr_entry->called_subpkg_pv = CopSTASHPV(PL_curcop); 0 0 0
subr_entry->called_subpkg_pv = CopSTASHPV(PL_curcop); 10272109 0 10272109
subr_entry->called_subpkg_pv = CopSTASHPV(PL_curcop); 0 10272109 10272109
2485 }
2486 10272109 subr_entry->called_cv_depth = 1; /* an approximation for slowops */
2487 10272109 subr_entry->called_is_xs = "sop";
2488 /* XXX make configurable eg for wait(), and maybe even subs like FCGI::Accept
2489 * so perhaps use $hide_sub_calls->{$package}{$subname} to make it general.
2490 * Then the logic would have to move out of this block.
2491 */
2492 10272109 ▶ if (OP_ACCEPT == op_type)
Branch: 1 branch
BranchTrueFalseTotal
if (OP_ACCEPT == op_type) 0 10272109 10272109
2493 0 subr_entry->hide_subr_call_time = 1;
2494 }
2495  
2496 /* These refer to the last perl statement executed, so aren't
2497 * strictly correct where an opcode or xsub is making the call,
2498 * but they're still more useful than nothing.
2499 * In reports the references line shows calls made by the
2500 * opcode or xsub that's called at that line.
2501 */
2502 131538439 ▶ file = OutCopFILE(prev_cop);
Branch: 1 branch
BranchTrueFalseTotal
file = OutCopFILE(prev_cop); 131538439 0 131538439
2503 263076878 subr_entry->caller_fid = (file == last_executed_fileptr)
2504 ? last_executed_fid
2505 131538439 ▶ : get_file_id(aTHX_ file, strlen(file), NYTP_FIDf_VIA_SUB);
Branch: 1 branch
BranchTrueFalseTotal
: get_file_id(aTHX_ file, strlen(file), NYTP_FIDf_VIA_SUB); 11534270 120004169 131538439
2506 131538439 subr_entry->caller_line = CopLINE(prev_cop);
2507  
2508 /* Gather details about the calling subroutine */
2509 131538439 ▶ if (clone_subr_entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (clone_subr_entry) { 4267 131534172 131538439
2510 4267 subr_entry->caller_subpkg_pv = clone_subr_entry->caller_subpkg_pv;
2511 4267 subr_entry->caller_subnam_sv = SvREFCNT_inc(clone_subr_entry->caller_subnam_sv);
2512 4267 found_caller_by = "(cloned)";
2513 }
2514 else
2515 /* Should we calculate the caller or can we reuse the caller_subr_entry?
2516 * Sometimes we'll have a caller_subr_entry but it won't have the name yet.
2517 * For example if the caller is an xsub that's callback into perl.
2518 */
2519 131534172 ▶ if (profile_findcaller /* user wants us to calculate each time */
Branch: 1 branch
BranchTrueFalseTotal
if (profile_findcaller /* user wants us to calculate each time */ 131534172 0 131534172
2520 131534172 ▶ || !caller_subr_entry /* we don't have a caller struct */
Branch: 1 branch
BranchTrueFalseTotal
|| !caller_subr_entry /* we don't have a caller struct */ 131527441 6731 131534172
2521 131527441 ▶ || !caller_subr_entry->called_subpkg_pv /* we don't have caller details */
Branch: 1 branch
BranchTrueFalseTotal
|| !caller_subr_entry->called_subpkg_pv /* we don't have caller details */ 131527441 0 131527441
2522 131527441 ▶ || !caller_subr_entry->called_subnam_sv
Branch: 1 branch
BranchTrueFalseTotal
|| !caller_subr_entry->called_subnam_sv 131527441 0 131527441
2523 131527441 ▶ || !SvOK(caller_subr_entry->called_subnam_sv)
Branch: 1 branch
BranchTrueFalseTotal
|| !SvOK(caller_subr_entry->called_subnam_sv) 0 131527441 131527441
2524 6731 ) {
2525  
2526 /* get the current CV and determine the current sub name from that */
2527 6731 CV *caller_cv = current_cv(aTHX_ cxstack_ix, NULL);
2528 6731 subr_entry->caller_subnam_sv = newSV(0); /* XXX add cache/stack thing for these SVs */
2529  
2530 if (0) {
2531 logwarn(" .. caller_subr_entry %p(%s::%s) cxstack_ix=%d: caller_cv=%p\n",
2532 (void*)caller_subr_entry,
2533 caller_subr_entry ? caller_subr_entry->called_subpkg_pv : "(null)",
2534 (caller_subr_entry && caller_subr_entry->called_subnam_sv && SvOK(caller_subr_entry->called_subnam_sv))
2535 ? SvPV_nolen(caller_subr_entry->called_subnam_sv) : "(null)",
2536 (int)cxstack_ix, (void*)caller_cv
2537 );
2538 }
2539  
2540 6731 ▶ if (caller_cv == PL_main_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (caller_cv == PL_main_cv) { 6634 97 6731
2541 /* PL_main_cv is run-time main (compile-time, eg 'use', is a main::BEGIN) */
2542 /* We don't record timing data for main::RUNTIME because timing data
2543 * is stored per calling location, and there is no calling location.
2544 * XXX Currently we don't output a subinfo for main::RUNTIME unless
2545 * some sub is called from main::RUNTIME. That may change.
2546 */
2547 6634 subr_entry->caller_subpkg_pv = "main";
2548 6634 sv_setpvs(subr_entry->caller_subnam_sv, "RUNTIME"); /* *cough* */
2549 6634 ++main_runtime_used;
2550 }
2551 97 ▶ else if (caller_cv == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (caller_cv == 0) { 87 10 97
2552 /* should never happen - but does in PostgreSQL 8.4.1 plperl
2553 * possibly because perl_run() has already returned
2554 */
2555 87 subr_entry->caller_subpkg_pv = "main";
2556 87 sv_setpvs(subr_entry->caller_subnam_sv, "NULL"); /* *cough* */
2557 }
2558 else {
2559 10 HV *stash_hv = NULL;
2560 10 GV *gv = CvGV(caller_cv);
2561 10 GV *egv = GvEGV(gv);
2562 10 ▶ if (!egv)
Branch: 1 branch
BranchTrueFalseTotal
if (!egv) 0 10 10
2563 0 gv = egv;
2564  
2565 10 ▶ if (gv && (stash_hv = GvSTASH(gv))) {
Branch: 2 branches
BranchTrueFalseTotal
if (gv && (stash_hv = GvSTASH(gv))) { 10 0 10
if (gv && (stash_hv = GvSTASH(gv))) { 10 0 10
2566 10 ▶ subr_entry->caller_subpkg_pv = HvNAME(stash_hv);
Branch: 6 branches
BranchTrueFalseTotal
subr_entry->caller_subpkg_pv = HvNAME(stash_hv); 10 0 10
subr_entry->caller_subpkg_pv = HvNAME(stash_hv); 10 0 10
subr_entry->caller_subpkg_pv = HvNAME(stash_hv); 0 10 10
subr_entry->caller_subpkg_pv = HvNAME(stash_hv); 0 0 0
subr_entry->caller_subpkg_pv = HvNAME(stash_hv); 10 0 10
subr_entry->caller_subpkg_pv = HvNAME(stash_hv); 0 10 10
2567 10 sv_setpvn(subr_entry->caller_subnam_sv,GvNAME(gv),GvNAMELEN(gv));
2568 }
2569 else {
2570 0 logwarn("Can't determine name of calling sub (GV %p, Stash %p, CV flags %d) at %s line %d\n",
2571 0 (void*)gv, (void*)stash_hv, (int)CvFLAGS(caller_cv),
2572 0 ▶ OutCopFILE(prev_cop), (int)CopLINE(prev_cop));
Branch: 1 branch
BranchTrueFalseTotal
OutCopFILE(prev_cop), (int)CopLINE(prev_cop)); 0 0 0
2573 0 sv_dump((SV*)caller_cv);
2574  
2575 0 subr_entry->caller_subpkg_pv = "__UNKNOWN__";
2576 0 sv_setpvs(subr_entry->caller_subnam_sv, "__UNKNOWN__");
2577 }
2578 }
2579 6731 ▶ found_caller_by = (profile_findcaller) ? "" : "(calculated)";
Branch: 1 branch
BranchTrueFalseTotal
found_caller_by = (profile_findcaller) ? "" : "(calculated)"; 0 6731 6731
2580 }
2581 else {
2582 131527441 subr_entry_t *caller_se = caller_subr_entry;
2583 131527441 ▶ int caller_is_op = caller_se->called_is_xs && strEQ(caller_se->called_is_xs,"sop");
Branch: 2 branches
BranchTrueFalseTotal
int caller_is_op = caller_se->called_is_xs && strEQ(caller_se->called_is_xs,"sop"); 644 131526797 131527441
int caller_is_op = caller_se->called_is_xs && strEQ(caller_se->called_is_xs,"sop"); 644 0 644
2584 /* if the caller is an op then use the caller of that op as our caller.
2585 * that makes more sense from the users perspective (and is consistent
2586 * with the findcaller=1 option).
2587 * XXX disabled for now because (I'm pretty sure) it needs a corresponding
2588 * change in incr_sub_inclusive_time otherwise the incl/excl times are distorted.
2589 */
2590 if (0 && caller_is_op) {
2591 subr_entry->caller_subpkg_pv = caller_se->caller_subpkg_pv;
2592 subr_entry->caller_subnam_sv = SvREFCNT_inc(caller_se->caller_subnam_sv);
2593 }
2594 else {
2595 131527441 subr_entry->caller_subpkg_pv = caller_se->called_subpkg_pv;
2596 131527441 subr_entry->caller_subnam_sv = SvREFCNT_inc(caller_se->called_subnam_sv);
2597 }
2598 131527441 found_caller_by = "(inherited)";
2599 }
2600  
2601 131538439 ▶ if (trace_level >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 4) { 0 131538439 131538439
2602 0 logwarn("%2u >> %s at %u:%d from %s::%s %s %s\n",
2603 0 (unsigned int)subr_entry->subr_prof_depth,
2604 0 PL_op_name[op_type],
2605 subr_entry->caller_fid, subr_entry->caller_line,
2606 subr_entry->caller_subpkg_pv,
2607 SvPV_nolen(subr_entry->caller_subnam_sv),
2608 found_caller_by,
2609 subr_entry_summary(aTHX_ subr_entry, 0)
2610 );
2611 }
2612  
2613 /* This is our safety-net destructor. For perl subs an identical destructor
2614 * will be pushed onto the stack _inside_ the scope we're interested in.
2615 * That destructor will be more accurate than this one. This one is here
2616 * mainly to catch exceptions thrown from xs subs and slowops.
2617 */
2618 131538439 save_destructor_x(incr_sub_inclusive_time_ix, INT2PTR(void *, (IV)subr_entry_ix));
2619  
2620 131538439 ▶ if (opt_calls >= 2 && out) {
Branch: 2 branches
BranchTrueFalseTotal
if (opt_calls >= 2 && out) { 31370923 100167516 131538439
if (opt_calls >= 2 && out) { 31370923 0 31370923
2621 31370923 NYTP_write_call_entry(out, subr_entry->caller_fid, subr_entry->caller_line);
2622 }
2623  
2624 131538439 SETERRNO(saved_errno, 0);
2625  
2626 131538439 return subr_entry_ix;
2627 }
2628  
2629  
2630 static OP *
2631 208344508 pp_entersub_profiler(pTHX)
2632 {
2633 208344508 return pp_subcall_profiler(aTHX_ 0);
2634 }
2635  
2636 static OP *
2637 20185639 pp_slowop_profiler(pTHX)
2638 {
2639 20185639 return pp_subcall_profiler(aTHX_ 1);
2640 }
2641  
2642 static OP *
2643 228530147 pp_subcall_profiler(pTHX_ int is_slowop)
2644 {
2645 228530147 int saved_errno = errno;
2646 OP *op;
2647 228530147 COP *prev_cop = PL_curcop; /* not PL_curcop_nytprof here */
2648 228530147 OP *next_op = PL_op->op_next; /* op to execute after sub returns */
2649 /* pp_entersub can be called with PL_op->op_type==0 */
2650 228530147 ▶ OPCODE op_type = (is_slowop || (opcode) PL_op->op_type == OP_GOTO) ? (opcode) PL_op->op_type : OP_ENTERSUB;
Branch: 2 branches
BranchTrueFalseTotal
OPCODE op_type = (is_slowop || (opcode) PL_op->op_type == OP_GOTO) ? (opcode) PL_op->op_type : OP_ENTERSUB; 208344508 20185639 228530147
OPCODE op_type = (is_slowop || (opcode) PL_op->op_type == OP_GOTO) ? (opcode) PL_op->op_type : OP_ENTERSUB; 38628 208305880 208344508
2651  
2652 CV *called_cv;
2653 228530147 dSP;
2654 228530147 SV *sub_sv = *SP;
2655 SSize_t this_subr_entry_ix; /* local copy (needed for goto) */
2656  
2657 subr_entry_t *subr_entry;
2658  
2659 /* pre-conditions */
2660 228530147 ▶ if (!profile_subs /* not profiling subs */
Branch: 1 branch
BranchTrueFalseTotal
if (!profile_subs /* not profiling subs */ 228530147 0 228530147
2661 /* don't profile if currently disabled */
2662 228530147 ▶ || !is_profiling
Branch: 1 branch
BranchTrueFalseTotal
|| !is_profiling 131539599 96990548 228530147
2663 /* don't profile calls to non-existant import() methods */
2664 /* or our DB::_INIT as that makes tests perl version sensitive */
2665 131539599 ▶ || (op_type==OP_ENTERSUB && (sub_sv == &PL_sv_yes || sub_sv == DB_CHECK_cv || sub_sv == DB_INIT_cv
Branch: 4 branches
BranchTrueFalseTotal
|| (op_type==OP_ENTERSUB && (sub_sv == &PL_sv_yes || sub_sv == DB_CHECK_cv || sub_sv == DB_INIT_cv 121263191 10276408 131539599
|| (op_type==OP_ENTERSUB && (sub_sv == &PL_sv_yes || sub_sv == DB_CHECK_cv || sub_sv == DB_INIT_cv 121263191 0 121263191
|| (op_type==OP_ENTERSUB && (sub_sv == &PL_sv_yes || sub_sv == DB_CHECK_cv || sub_sv == DB_INIT_cv 121263191 0 121263191
|| (op_type==OP_ENTERSUB && (sub_sv == &PL_sv_yes || sub_sv == DB_CHECK_cv || sub_sv == DB_INIT_cv 121263191 0 121263191
2666 121263191 ▶ || sub_sv == DB_END_cv || sub_sv == DB_fin_cv))
Branch: 2 branches
BranchTrueFalseTotal
|| sub_sv == DB_END_cv || sub_sv == DB_fin_cv)) 121262627 564 121263191
|| sub_sv == DB_END_cv || sub_sv == DB_fin_cv)) 121262063 564 121262627
2667 /* don't profile other kinds of goto */
2668 131538471 ▶ || (op_type==OP_GOTO &&
Branch: 1 branch
BranchTrueFalseTotal
|| (op_type==OP_GOTO && 4299 131534172 131538471
2669 4299 ▶ ( !(SvROK(sub_sv) && SvTYPE(SvRV(sub_sv)) == SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
( !(SvROK(sub_sv) && SvTYPE(SvRV(sub_sv)) == SVt_PVCV) 4267 32 4299
( !(SvROK(sub_sv) && SvTYPE(SvRV(sub_sv)) == SVt_PVCV) 4267 0 4267
2670 4267 ▶ || subr_entry_ix == -1) /* goto out of sub whose entry wasn't profiled */
Branch: 1 branch
BranchTrueFalseTotal
|| subr_entry_ix == -1) /* goto out of sub whose entry wasn't profiled */ 0 4267 4267
2671 )
2672 #ifdef MULTIPLICITY
2673 || (orig_my_perl && my_perl != orig_my_perl)
2674 #endif
2675 ) {
2676 96991708 return run_original_op(op_type);
2677 }
2678  
2679 131538439 ▶ if (!profile_stmts) {
Branch: 1 branch
BranchTrueFalseTotal
if (!profile_stmts) { 5769498 125768941 131538439
2680 5769498 reinit_if_forked(aTHX);
2681 CHECK_SAWAMPERSAND(last_executed_fid, last_executed_line);
2682 }
2683  
2684 131538439 ▶ if (trace_level >= 99) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 99) { 0 131538439 131538439
2685 0 logwarn("profiling a call [op %ld, %s, seix %d]\n",
2686 0 (long)op_type, PL_op_name[op_type], (int)subr_entry_ix);
2687 /* crude, but the only way to deal with the myriad logic at the
2688 * start of pp_entersub (which ought to be available as separate sub)
2689 */
2690 0 sv_dump(sub_sv);
2691 }
2692
2693  
2694 /* Life would be so much simpler if we could reliably tell, at this point,
2695 * what sub was going to get called. But we can't in many cases.
2696 * So we gather up as much into as possible before the call.
2697 */
2698  
2699 131538439 ▶ if (op_type != OP_GOTO) {
Branch: 1 branch
BranchTrueFalseTotal
if (op_type != OP_GOTO) { 131534172 4267 131538439
2700  
2701 /* For normal subs, pp_entersub enters the sub and returns the
2702 * first op *within* the sub (typically a nextstate/dbstate).
2703 * For XS subs, pp_entersub executes the entire sub
2704 * and returns the op *after* the sub (PL_op->op_next).
2705 * Other ops we profile (eg slowops) act like xsubs.
2706 */
2707  
2708 131534172 called_cv = NULL;
2709 131534172 this_subr_entry_ix = subr_entry_setup(aTHX_ prev_cop, NULL, op_type, sub_sv);
2710  
2711 /* This call may exit via an exception, in which case the
2712 * remaining code below doesn't get executed and the sub call
2713 * details are discarded. For perl subs that just means we don't
2714 * see calls the failed with "Unknown sub" errors, etc.
2715 * For xsubs it's a more significant issue. Especially if the
2716 * xsub calls back into perl.
2717 */
2718 131534172 SETERRNO(saved_errno, 0);
2719 131534172 op = run_original_op(op_type);
2720 131534087 saved_errno = errno;
2721  
2722 }
2723 else {
2724  
2725 /* goto &sub opcode acts like a return followed by a call all in one.
2726 * When this op starts executing, the 'current' subr_entry that was
2727 * pushed onto the savestack by pp_subcall_profiler will be 'already_counted'
2728 * so the profiling of that call will be handled naturally for us.
2729 * So far so good.
2730 * Before it gets destroyed we'll take a copy of the subr_entry.
2731 * Then tell subr_entry_setup() to use our copy as a template so it'll
2732 * seem like the sub we goto'd was called by the same sub that called
2733 * the one that executed the goto. Except that we do use the fid:line
2734 * of the goto statement. That way the call graph makes sense and the
2735 * 'calling location' make sense. Got all that?
2736 */
2737 /* save a copy of prev_cop - see t/test18-goto2.p */
2738 4267 COP prev_cop_copy = *prev_cop;
2739 /* save a copy of the subr_entry of the sub we're goto'ing out of */
2740 /* so we can reuse the caller _* info after it's destroyed */
2741 subr_entry_t goto_subr_entry;
2742 4267 ▶ subr_entry_t *src = subr_entry_ix_ptr(subr_entry_ix);
Branch: 1 branch
BranchTrueFalseTotal
subr_entry_t *src = subr_entry_ix_ptr(subr_entry_ix); 4267 0 4267
2743 4267 ▶ Copy(src, &goto_subr_entry, 1, subr_entry_t);
Branch: 1 branch
BranchTrueFalseTotal
Copy(src, &goto_subr_entry, 1, subr_entry_t); 0 4267 4267
2744  
2745 /* XXX if the goto op or goto'd xsub croaks then this'll leak */
2746 /* we can't mortalize here because we're about to leave scope */
2747 4267 (void)SvREFCNT_inc(goto_subr_entry.caller_subnam_sv);
2748 4267 (void)SvREFCNT_inc(goto_subr_entry.called_subnam_sv);
2749 4267 (void)SvREFCNT_inc(sub_sv);
2750  
2751 /* grab the CvSTART of the called sub since it's available */
2752 4267 called_cv = (CV*)SvRV(sub_sv);
2753  
2754 /* if goto &sub then op will be the first op of the called sub
2755 * if goto &xsub then op will be the first op after the call to the
2756 * op we're goto'ing out of.
2757 */
2758 4267 SETERRNO(saved_errno, 0);
2759 4267 op = run_original_op(op_type); /* perform the goto &sub */
2760 4267 saved_errno = errno;
2761  
2762 /* now we're in goto'd sub, mortalize the REFCNT_inc's done above */
2763 4267 sv_2mortal(goto_subr_entry.caller_subnam_sv);
2764 4267 sv_2mortal(goto_subr_entry.called_subnam_sv);
2765 4267 this_subr_entry_ix = subr_entry_setup(aTHX_ &prev_cop_copy, &goto_subr_entry, op_type, sub_sv);
2766 4267 SvREFCNT_dec(sub_sv);
2767 }
2768  
2769 131538354 ▶ subr_entry = subr_entry_ix_ptr(this_subr_entry_ix);
Branch: 1 branch
BranchTrueFalseTotal
subr_entry = subr_entry_ix_ptr(this_subr_entry_ix); 131538354 0 131538354
2770  
2771 /* detect weirdness/corruption */
2772 131538354 ▶ assert(subr_entry);
Branch: 1 branch
BranchTrueFalseTotal
assert(subr_entry); 0 131538354 131538354
2773 131538354 ▶ assert(subr_entry->caller_fid < fidhash.next_id);
Branch: 1 branch
BranchTrueFalseTotal
assert(subr_entry->caller_fid < fidhash.next_id); 0 131538354 131538354
2774  
2775 /* Check if this call has already been counted because the op performed
2776 * a leave_scope(). E.g., OP_SUBSTCONT at end of s/.../\1/
2777 * or Scope::Upper's unwind()
2778 */
2779 131538354 ▶ if (subr_entry->already_counted) {
Branch: 1 branch
BranchTrueFalseTotal
if (subr_entry->already_counted) { 148 131538206 131538354
2780 148 ▶ if (trace_level >= 9)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 9) 0 148 148
2781 0 logwarn("%2u -- %s::%s already counted %s\n",
2782 0 (unsigned int)subr_entry->subr_prof_depth,
2783 subr_entry->called_subpkg_pv,
2784 0 ▶ (subr_entry->called_subnam_sv && SvOK(subr_entry->called_subnam_sv))
Branch: 2 branches
BranchTrueFalseTotal
(subr_entry->called_subnam_sv && SvOK(subr_entry->called_subnam_sv)) 0 0 0
(subr_entry->called_subnam_sv && SvOK(subr_entry->called_subnam_sv)) 0 0 0
2785 0 ? SvPV_nolen(subr_entry->called_subnam_sv)
2786 : "?",
2787 subr_entry_summary(aTHX_ subr_entry, 1));
2788 148 ▶ assert(subr_entry->already_counted < 3);
Branch: 1 branch
BranchTrueFalseTotal
assert(subr_entry->already_counted < 3); 0 148 148
2789 148 goto skip_sub_profile;
2790 }
2791  
2792 131538206 ▶ if (is_slowop) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_slowop) { 121266261 10271945 131538206
2793 /* already fully handled by subr_entry_setup */
2794 }
2795 else {
2796 121266261 char *stash_name = NULL;
2797 121266261 const char *is_xs = NULL;
2798  
2799 121266261 ▶ if (op_type == OP_GOTO) {
Branch: 1 branch
BranchTrueFalseTotal
if (op_type == OP_GOTO) { 4267 121261994 121266261
2800 /* use the called_cv that was the arg to the goto op */
2801 4267 ▶ is_xs = (CvISXSUB(called_cv)) ? "xsub" : NULL;
Branch: 1 branch
BranchTrueFalseTotal
is_xs = (CvISXSUB(called_cv)) ? "xsub" : NULL; 16 4251 4267
2802 }
2803 else
2804 121261994 ▶ if (op != next_op) { /* have entered a sub */
Branch: 1 branch
BranchTrueFalseTotal
if (op != next_op) { /* have entered a sub */ 28104961 93157033 121261994
2805 /* use cv of sub we've just entered to get name */
2806 28104961 called_cv = cxstack[cxstack_ix].blk_sub.cv;
2807 28104961 is_xs = NULL;
2808 }
2809 else { /* have returned from XS so use sub_sv for name */
2810 /* determine the original fully qualified name for sub */
2811 /* CV or NULL */
2812 93157033 GV *gv = NULL;
2813 93157033 called_cv = resolve_sub_to_cv(aTHX_ sub_sv, &gv);
2814
2815 93157033 ▶ if (!called_cv && gv) { /* XXX no test case for this */
Branch: 2 branches
BranchTrueFalseTotal
if (!called_cv && gv) { /* XXX no test case for this */ 0 93157033 93157033
if (!called_cv && gv) { /* XXX no test case for this */ 0 0 0
2816 0 ▶ stash_name = HvNAME(GvSTASH(gv));
Branch: 6 branches
BranchTrueFalseTotal
stash_name = HvNAME(GvSTASH(gv)); 0 0 0
stash_name = HvNAME(GvSTASH(gv)); 0 0 0
stash_name = HvNAME(GvSTASH(gv)); 0 0 0
stash_name = HvNAME(GvSTASH(gv)); 0 0 0
stash_name = HvNAME(GvSTASH(gv)); 0 0 0
stash_name = HvNAME(GvSTASH(gv)); 0 0 0
2817 0 sv_setpv(subr_entry->called_subnam_sv, GvNAME(gv));
2818 0 ▶ if (trace_level >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 0) 0 0 0
2819 0 logwarn("Assuming called sub is named %s::%s at %s line %d (please report as a bug)\n",
2820 stash_name, SvPV_nolen(subr_entry->called_subnam_sv),
2821 0 ▶ OutCopFILE(prev_cop), (int)CopLINE(prev_cop));
Branch: 1 branch
BranchTrueFalseTotal
OutCopFILE(prev_cop), (int)CopLINE(prev_cop)); 0 0 0
2822 }
2823 93157033 is_xs = "xsub";
2824 }
2825  
2826 121266261 ▶ if (called_cv && CvGV(called_cv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (called_cv && CvGV(called_cv)) { 121266261 0 121266261
if (called_cv && CvGV(called_cv)) { 121266261 0 121266261
2827 121266261 GV *gv = CvGV(called_cv);
2828 /* Class::MOP can create CvGV where SvTYPE of GV is SVt_NULL */
2829 121266261 ▶ if (SvTYPE(gv) == SVt_PVGV && GvSTASH(gv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(gv) == SVt_PVGV && GvSTASH(gv)) { 121266261 0 121266261
if (SvTYPE(gv) == SVt_PVGV && GvSTASH(gv)) { 121266261 0 121266261
2830 /* for a plain call of an imported sub the GV is of the current
2831 * package, so we dig to find the original package
2832 */
2833 121266261 ▶ stash_name = HvNAME(GvSTASH(gv));
Branch: 6 branches
BranchTrueFalseTotal
stash_name = HvNAME(GvSTASH(gv)); 121266261 0 121266261
stash_name = HvNAME(GvSTASH(gv)); 121266261 0 121266261
stash_name = HvNAME(GvSTASH(gv)); 0 121266261 121266261
stash_name = HvNAME(GvSTASH(gv)); 0 0 0
stash_name = HvNAME(GvSTASH(gv)); 121266261 0 121266261
stash_name = HvNAME(GvSTASH(gv)); 0 121266261 121266261
2834 121266261 sv_setpv(subr_entry->called_subnam_sv, GvNAME(gv));
2835 }
2836 0 ▶ else if (trace_level >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (trace_level >= 1) { 0 0 0
2837 0 logwarn("NYTProf is confused about CV %p called as %s at %s line %d (please report as a bug)\n",
2838 0 ▶ (void*)called_cv, SvPV_nolen(sub_sv), OutCopFILE(prev_cop), (int)CopLINE(prev_cop));
Branch: 1 branch
BranchTrueFalseTotal
(void*)called_cv, SvPV_nolen(sub_sv), OutCopFILE(prev_cop), (int)CopLINE(prev_cop)); 0 0 0
2839 /* looks like Class::MOP doesn't give the CV GV stash a name */
2840 0 ▶ if (trace_level >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 2) { 0 0 0
2841 0 sv_dump((SV*)called_cv); /* coredumps in Perl_do_gvgv_dump, looks line GvXPVGV is false, presumably on a Class::MOP weirdo sub */
2842 0 sv_dump((SV*)gv);
2843 }
2844 }
2845 }
2846  
2847 /* called_subnam_sv should have been set by now - else we're getting desperate */
2848 121266261 ▶ if (!SvOK(subr_entry->called_subnam_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(subr_entry->called_subnam_sv)) { 0 121266261 121266261
2849 0 ▶ const char *what = (is_xs) ? is_xs : "sub";
Branch: 1 branch
BranchTrueFalseTotal
const char *what = (is_xs) ? is_xs : "sub"; 0 0 0
2850  
2851 0 ▶ if (!called_cv) { /* should never get here as pp_entersub would have croaked */
Branch: 1 branch
BranchTrueFalseTotal
if (!called_cv) { /* should never get here as pp_entersub would have croaked */ 0 0 0
2852 0 logwarn("unknown entersub %s '%s' (please report this as a bug)\n", what, SvPV_nolen(sub_sv));
2853 0 ▶ stash_name = CopSTASHPV(PL_curcop);
Branch: 7 branches
BranchTrueFalseTotal
stash_name = CopSTASHPV(PL_curcop); 0 0 0
stash_name = CopSTASHPV(PL_curcop); 0 0 0
stash_name = CopSTASHPV(PL_curcop); 0 0 0
stash_name = CopSTASHPV(PL_curcop); 0 0 0
stash_name = CopSTASHPV(PL_curcop); 0 0 0
stash_name = CopSTASHPV(PL_curcop); 0 0 0
stash_name = CopSTASHPV(PL_curcop); 0 0 0
2854 0 sv_setpvf(subr_entry->called_subnam_sv, "__UNKNOWN__[%s,%s])", what, SvPV_nolen(sub_sv));
2855 }
2856 else { /* unnamed CV, e.g. seen in mod_perl/Class::MOP. XXX do better? */
2857 0 ▶ stash_name = HvNAME(CvSTASH(called_cv));
Branch: 6 branches
BranchTrueFalseTotal
stash_name = HvNAME(CvSTASH(called_cv)); 0 0 0
stash_name = HvNAME(CvSTASH(called_cv)); 0 0 0
stash_name = HvNAME(CvSTASH(called_cv)); 0 0 0
stash_name = HvNAME(CvSTASH(called_cv)); 0 0 0
stash_name = HvNAME(CvSTASH(called_cv)); 0 0 0
stash_name = HvNAME(CvSTASH(called_cv)); 0 0 0
2858 0 sv_setpvf(subr_entry->called_subnam_sv, "__UNKNOWN__[%s,0x%p]", what, (void*)called_cv);
2859 0 ▶ if (trace_level)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level) 0 0 0
2860 0 logwarn("unknown entersub %s assumed to be anon called_cv '%s'\n",
2861 what, SvPV_nolen(sub_sv));
2862 }
2863 0 ▶ if (trace_level >= 9)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 9) 0 0 0
2864 0 sv_dump(sub_sv);
2865 }
2866
2867 121266261 subr_entry->called_subpkg_pv = stash_name;
2868 121266261 ▶ if (*SvPVX(subr_entry->called_subnam_sv) == 'B')
Branch: 1 branch
BranchTrueFalseTotal
if (*SvPVX(subr_entry->called_subnam_sv) == 'B') 37184 121229077 121266261
2869 37184 append_linenum_to_begin(aTHX_ subr_entry);
2870  
2871 /* if called was xsub then we've already left it, so use depth+1 */
2872 121266261 ▶ subr_entry->called_cv_depth = (called_cv) ? CvDEPTH(called_cv)+(is_xs?1:0) : 0;
Branch: 1 branch
BranchTrueFalseTotal
subr_entry->called_cv_depth = (called_cv) ? CvDEPTH(called_cv)+(is_xs?1:0) : 0; 121266261 0 121266261
2873 121266261 subr_entry->called_cv = called_cv;
2874 121266261 subr_entry->called_is_xs = is_xs;
2875 }
2876  
2877 /* ignore our own DB::_INIT sub - only shows up with 5.8.9+ & 5.10.1+ */
2878 131538206 ▶ if (subr_entry->called_is_xs
Branch: 1 branch
BranchTrueFalseTotal
if (subr_entry->called_is_xs 103428994 28109212 131538206
2879 103428994 ▶ && subr_entry->called_subpkg_pv[0] == 'D'
Branch: 1 branch
BranchTrueFalseTotal
&& subr_entry->called_subpkg_pv[0] == 'D' 3818584 99610410 103428994
2880 3818584 ▶ && subr_entry->called_subpkg_pv[1] == 'B'
Branch: 1 branch
BranchTrueFalseTotal
&& subr_entry->called_subpkg_pv[1] == 'B' 96 3818488 3818584
2881 96 ▶ && subr_entry->called_subpkg_pv[2] == '\0'
Branch: 1 branch
BranchTrueFalseTotal
&& subr_entry->called_subpkg_pv[2] == '\0' 96 0 96
2882 ) {
2883 STRLEN len;
2884 96 char *p = SvPV(subr_entry->called_subnam_sv, len);
2885  
2886 96 ▶ if(*p == '_' && (memEQs(p, len, "_CHECK") || memEQs(p, len, "_INIT") || memEQs(p, len, "_END"))) {
Branch: 7 branches
BranchTrueFalseTotal
if(*p == '_' && (memEQs(p, len, "_CHECK") || memEQs(p, len, "_INIT") || memEQs(p, len, "_END"))) { 0 96 96
if(*p == '_' && (memEQs(p, len, "_CHECK") || memEQs(p, len, "_INIT") || memEQs(p, len, "_END"))) { 0 0 0
if(*p == '_' && (memEQs(p, len, "_CHECK") || memEQs(p, len, "_INIT") || memEQs(p, len, "_END"))) { 0 0 0
if(*p == '_' && (memEQs(p, len, "_CHECK") || memEQs(p, len, "_INIT") || memEQs(p, len, "_END"))) { 0 0 0
if(*p == '_' && (memEQs(p, len, "_CHECK") || memEQs(p, len, "_INIT") || memEQs(p, len, "_END"))) { 0 0 0
if(*p == '_' && (memEQs(p, len, "_CHECK") || memEQs(p, len, "_INIT") || memEQs(p, len, "_END"))) { 0 0 0
if(*p == '_' && (memEQs(p, len, "_CHECK") || memEQs(p, len, "_INIT") || memEQs(p, len, "_END"))) { 0 0 0
2887 0 subr_entry->already_counted++;
2888 0 goto skip_sub_profile;
2889 }
2890 }
2891 /* catch profile_subs being turned off by disable_profile call */
2892 131538206 ▶ if (!profile_subs)
Branch: 1 branch
BranchTrueFalseTotal
if (!profile_subs) 0 131538206 131538206
2893 0 subr_entry->already_counted++;
2894  
2895 131538206 ▶ if (trace_level >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 4) { 0 131538206 131538206
2896 0 logwarn("%2u ->%4s %s::%s from %s::%s @%u:%u (d%d, oh %" NVff "t, sub %" NVff "s) #%lu\n",
2897 0 (unsigned int)subr_entry->subr_prof_depth,
2898 0 ▶ (subr_entry->called_is_xs) ? subr_entry->called_is_xs : "sub",
Branch: 1 branch
BranchTrueFalseTotal
(subr_entry->called_is_xs) ? subr_entry->called_is_xs : "sub", 0 0 0
2899 subr_entry->called_subpkg_pv,
2900 0 ▶ subr_entry->called_subnam_sv ? SvPV_nolen(subr_entry->called_subnam_sv) : "(null)",
Branch: 1 branch
BranchTrueFalseTotal
subr_entry->called_subnam_sv ? SvPV_nolen(subr_entry->called_subnam_sv) : "(null)", 0 0 0
2901 subr_entry->caller_subpkg_pv,
2902 0 subr_entry->caller_subnam_sv ? SvPV_nolen(subr_entry->caller_subnam_sv) : "(null)",
2903 subr_entry->caller_fid, subr_entry->caller_line,
2904 subr_entry->called_cv_depth,
2905 subr_entry->initial_overhead_ticks,
2906 0 ▶ subr_entry->initial_subr_ticks / ticks_per_sec,
Branch: 1 branch
BranchTrueFalseTotal
subr_entry->initial_subr_ticks / ticks_per_sec, 0 0 0
2907 subr_entry->subr_call_seqn
2908 );
2909 }
2910  
2911 131538206 ▶ if (subr_entry->called_is_xs) {
Branch: 1 branch
BranchTrueFalseTotal
if (subr_entry->called_is_xs) { 103428994 28109212 131538206
2912 /* for xsubs/builtins we've already left the sub, so end the timing now
2913 * rather than wait for the calling scope to get cleaned up.
2914 */
2915 103428994 incr_sub_inclusive_time(aTHX_ subr_entry);
2916 }
2917 else {
2918 /* push a destructor hook onto the context stack to ensure we account
2919 * for time in the sub when we leave it, even if via an exception.
2920 */
2921 28109212 save_destructor_x(incr_sub_inclusive_time_ix, INT2PTR(void *, (IV)this_subr_entry_ix));
2922 }
2923  
2924 131538354 skip_sub_profile:
2925 131538354 SETERRNO(saved_errno, 0);
2926  
2927 131538354 return op;
2928 }
2929  
2930  
2931 static OP *
2932 177709137 pp_stmt_profiler(pTHX) /* handles OP_DBSTATE, OP_SETSTATE, etc */
2933 {
2934 177709137 OP *op = run_original_op(PL_op->op_type);
2935 177709137 DB_stmt(aTHX_ NULL, op);
2936 177709137 return op;
2937 }
2938  
2939 static OP *
2940 21498827 pp_leave_profiler(pTHX) /* handles OP_LEAVESUB, OP_LEAVEEVAL, etc */
2941 {
2942 21498827 OP *prev_op = PL_op;
2943 21498827 OP *op = run_original_op(PL_op->op_type);
2944 21498827 DB_leave(aTHX_ op, prev_op);
2945 21498827 return op;
2946 }
2947  
2948 static OP *
2949 57 pp_fork_profiler(pTHX) /* handles OP_FORK */
2950 {
2951 57 OP *op = run_original_op(PL_op->op_type);
2952 57 reinit_if_forked(aTHX);
2953 57 return op;
2954 }
2955  
2956 static OP *
2957 28 pp_exit_profiler(pTHX) /* handles OP_EXIT, OP_EXEC, etc */
2958 {
2959 28 DB_leave(aTHX_ NULL, PL_op); /* call DB_leave *before* run_original_op() */
2960 28 ▶ if (PL_op->op_type == OP_EXEC)
Branch: 1 branch
BranchTrueFalseTotal
if (PL_op->op_type == OP_EXEC) 0 28 28
2961 0 finish_profile(aTHX); /* this is the last chance we'll get */
2962 28 return run_original_op(PL_op->op_type);
2963 }
2964  
2965  
2966 static int
2967 664 enable_profile(pTHX_ char *file)
2968 {
2969 /* enable the run-time aspects to profiling */
2970 664 int prev_is_profiling = is_profiling;
2971 #ifdef MULTIPLICITY
2972 if (orig_my_perl && my_perl != orig_my_perl) {
2973 if (trace_level)
2974 logwarn("~ enable_profile call from different interpreter ignored\n");
2975 return 0;
2976 }
2977 #endif
2978  
2979 664 ▶ if (profile_usecputime) {
Branch: 1 branch
BranchTrueFalseTotal
if (profile_usecputime) { 0 664 664
2980 0 warn("The NYTProf usecputime option has been removed (try using clock=N if possible)");
2981 0 return 0;
2982 }
2983  
2984 664 ▶ if (trace_level)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level) 0 664 664
2985 0 ▶ logwarn("~ enable_profile (previously %s) to %s\n",
Branch: 2 branches
BranchTrueFalseTotal
logwarn("~ enable_profile (previously %s) to %s\n", 0 0 0
logwarn("~ enable_profile (previously %s) to %s\n", 0 0 0
2986 prev_is_profiling ? "enabled" : "disabled",
2987 0 ▶ (file && *file) ? file : PROF_output_file);
Branch: 1 branch
BranchTrueFalseTotal
(file && *file) ? file : PROF_output_file); 0 0 0
2988  
2989 664 reinit_if_forked(aTHX);
2990  
2991 664 ▶ if (file && *file && strNE(file, PROF_output_file)) {
Branch: 3 branches
BranchTrueFalseTotal
if (file && *file && strNE(file, PROF_output_file)) { 32 632 664
if (file && *file && strNE(file, PROF_output_file)) { 32 0 32
if (file && *file && strNE(file, PROF_output_file)) { 32 0 32
2992 /* caller wants output to go to a new file */
2993 32 close_output_file(aTHX);
2994 32 strncpy(PROF_output_file, file, sizeof(PROF_output_file)-1);
2995 }
2996  
2997 664 ▶ if (!out) {
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { 648 16 664
2998 648 open_output_file(aTHX_ PROF_output_file);
2999 }
3000  
3001 664 last_executed_fileptr = NULL; /* discard cached OutCopFILE */
3002 664 is_profiling = 1; /* enable NYTProf profilers */
3003 664 ▶ if (opt_use_db_sub) /* set PL_DBsingle if required */
Branch: 1 branch
BranchTrueFalseTotal
if (opt_use_db_sub) /* set PL_DBsingle if required */ 323 341 664
3004 323 sv_setiv(PL_DBsingle, 1);
3005  
3006 /* discard time spent since profiler was disabled */
3007 664 get_time_of_day(start_time);
3008  
3009 664 return prev_is_profiling;
3010 }
3011  
3012  
3013 static int
3014 701 disable_profile(pTHX)
3015 {
3016 701 int prev_is_profiling = is_profiling;
3017 #ifdef MULTIPLICITY
3018 if (orig_my_perl && my_perl != orig_my_perl) {
3019 if (trace_level)
3020 logwarn("~ disable_profile call from different interpreter ignored\n");
3021 return 0;
3022 }
3023 #endif
3024 701 ▶ if (is_profiling) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_profiling) { 664 37 701
3025 664 ▶ if (opt_use_db_sub)
Branch: 1 branch
BranchTrueFalseTotal
if (opt_use_db_sub) 323 341 664
3026 323 sv_setiv(PL_DBsingle, 0);
3027 664 ▶ if (out)
Branch: 1 branch
BranchTrueFalseTotal
if (out) 661 3 664
3028 661 NYTP_flush(out);
3029 664 is_profiling = 0;
3030 }
3031 701 ▶ if (trace_level)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level) 0 701 701
3032 0 ▶ logwarn("~ disable_profile (previously %s, pid %d, trace %" IVdf ")\n",
Branch: 1 branch
BranchTrueFalseTotal
logwarn("~ disable_profile (previously %s, pid %d, trace %" IVdf ")\n", 0 0 0
3033 prev_is_profiling ? "enabled" : "disabled", getpid(), trace_level);
3034 701 return prev_is_profiling;
3035 }
3036  
3037  
3038 static void
3039 649 finish_profile(pTHX)
3040 {
3041 /* can be called after the perl interp is destroyed, via libcexit */
3042 649 int saved_errno = errno;
3043 #ifdef MULTIPLICITY
3044 if (orig_my_perl && my_perl != orig_my_perl)
3045 if (trace_level) {
3046 logwarn("~ finish_profile call from different interpreter ignored\n");
3047 return;
3048 }
3049 #endif
3050  
3051 649 ▶ if (trace_level >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) 0 649 649
3052 0 logwarn("~ finish_profile (overhead %" NVgf "t, is_profiling %d)\n",
3053 cumulative_overhead_ticks, is_profiling);
3054  
3055 /* write data for final statement, unless DB_leave has already */
3056 649 ▶ if (!profile_leave || opt_use_db_sub)
Branch: 2 branches
BranchTrueFalseTotal
if (!profile_leave || opt_use_db_sub) 326 323 649
if (!profile_leave || opt_use_db_sub) 154 172 326
3057 477 DB_stmt(aTHX_ NULL, NULL);
3058  
3059 649 disable_profile(aTHX);
3060  
3061 649 close_output_file(aTHX);
3062  
3063 649 ▶ if (trace_level >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 2) { 0 649 649
3064 0 hash_stats(&fidhash, 0);
3065 0 hash_stats(&strhash, 0);
3066 }
3067  
3068 /* reset sub profiler data */
3069 649 ▶ if (HvKEYS(sub_callers_hv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (HvKEYS(sub_callers_hv)) { 0 649 649
if (HvKEYS(sub_callers_hv)) { 647 2 649
3070 /* HvKEYS check avoids hv_clear() if interp has been destroyed RT#86548 */
3071 647 hv_clear(sub_callers_hv);
3072 }
3073  
3074 /* reset other state */
3075 649 cumulative_overhead_ticks = 0;
3076 649 cumulative_subr_ticks = 0;
3077  
3078 649 SETERRNO(saved_errno, 0);
3079 649 }
3080  
3081  
3082 static void
3083 0 finish_profile_nocontext()
3084 {
3085 /* can be called after the perl interp is destroyed, via libcexit */
3086 dTHX;
3087 0 finish_profile(aTHX);
3088 0 }
3089  
3090  
3091 static void
3092 600 _init_profiler_clock(pTHX)
3093 {
3094 #ifdef HAS_CLOCK_GETTIME
3095 600 ▶ if (profile_clock == -1) { /* auto select */
Branch: 1 branch
BranchTrueFalseTotal
if (profile_clock == -1) { /* auto select */ 600 0 600
3096 # ifdef CLOCK_MONOTONIC
3097 600 profile_clock = CLOCK_MONOTONIC;
3098 # else
3099 profile_clock = CLOCK_REALTIME;
3100 # endif
3101 }
3102 /* downgrade to CLOCK_REALTIME if desired clock not available */
3103 600 ▶ if (clock_gettime(profile_clock, &start_time) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (clock_gettime(profile_clock, &start_time) != 0) { 0 600 600
3104 0 ▶ if (trace_level)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level) 0 0 0
3105 0 logwarn("~ clock_gettime clock %ld not available (%s) using CLOCK_REALTIME instead\n",
3106 0 (long)profile_clock, strerror(errno));
3107 0 profile_clock = CLOCK_REALTIME;
3108 /* check CLOCK_REALTIME as well, just in case */
3109 0 ▶ if (clock_gettime(profile_clock, &start_time) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (clock_gettime(profile_clock, &start_time) != 0) 0 0 0
3110 0 croak("clock_gettime CLOCK_REALTIME not available (%s), aborting",
3111 strerror(errno));
3112 }
3113 #else
3114 if (profile_clock != -1) { /* user tried to select different clock */
3115 logwarn("clock %ld not available (clock_gettime not supported on this system)\n", (long)profile_clock);
3116 profile_clock = -1;
3117 }
3118 #endif
3119 #ifdef HAS_QPC
3120 {
3121 const char * fnname;
3122 if(!QueryPerformanceFrequency((LARGE_INTEGER *)&time_frequency)) {
3123 fnname = "QueryPerformanceFrequency";
3124 goto win32_failed;
3125 }
3126 {
3127 LARGE_INTEGER tmp; /* do 1 test call, dont check return value for
3128 further calls for performance reasons */
3129 if(!QueryPerformanceCounter(&tmp)) {
3130 fnname = "QueryPerformanceCounter";
3131 win32_failed:
3132 croak("%s failed with Win32 error %lu, no clocks available", fnname, GetLastError());
3133 }
3134 }
3135 }
3136 #endif
3137 600 ticks_per_sec = TICKS_PER_SEC;
3138 600 }
3139  
3140  
3141 /* Initial setup - should only be called once */
3142  
3143 static int
3144 600 init_profiler(pTHX)
3145 {
3146 #ifndef HAS_GETTIMEOFDAY
3147 SV **svp;
3148 #endif
3149  
3150 #ifdef MULTIPLICITY
3151 if (!orig_my_perl) {
3152 if (1)
3153 orig_my_perl = my_perl;
3154 }
3155 else if (orig_my_perl && orig_my_perl != my_perl) {
3156 logwarn("NYTProf: perl interpreter address changed after init (threads/multiplicity not supported)\n");
3157 return 0;
3158 }
3159 #endif
3160  
3161 /* Save the process id early. We monitor it to detect forks */
3162 600 last_pid = getpid();
3163 600 DB_CHECK_cv = (SV*)GvCV(gv_fetchpv("DB::_CHECK", FALSE, SVt_PVCV));
3164 600 DB_INIT_cv = (SV*)GvCV(gv_fetchpv("DB::_INIT", FALSE, SVt_PVCV));
3165 600 DB_END_cv = (SV*)GvCV(gv_fetchpv("DB::_END", FALSE, SVt_PVCV));
3166 600 DB_fin_cv = (SV*)GvCV(gv_fetchpv("DB::finish_profile", FALSE, SVt_PVCV));
3167  
3168 600 ▶ if (opt_use_db_sub) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt_use_db_sub) { 291 309 600
3169 291 PL_perldb |= PERLDBf_LINE; /* line-by-line profiling via DB::DB (if $DB::single true) */
3170 291 PL_perldb |= PERLDBf_SINGLE; /* start (after BEGINs) with single-step on XXX still needed? */
3171 }
3172  
3173 600 ▶ if (profile_opts & NYTP_OPTf_OPTIMIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (profile_opts & NYTP_OPTf_OPTIMIZE) 600 0 600
3174 600 PL_perldb &= ~PERLDBf_NOOPT;
3175 0 else PL_perldb |= PERLDBf_NOOPT;
3176  
3177 600 ▶ if (profile_opts & NYTP_OPTf_SAVESRC) {
Branch: 1 branch
BranchTrueFalseTotal
if (profile_opts & NYTP_OPTf_SAVESRC) { 307 293 600
3178 /* ask perl to keep the source lines so we can copy them */
3179 307 PL_perldb |= PERLDBf_SAVESRC | PERLDBf_SAVESRC_NOSUBS;
3180 }
3181  
3182 600 ▶ if (!opt_nameevals)
Branch: 1 branch
BranchTrueFalseTotal
if (!opt_nameevals) 0 600 600
3183 0 PL_perldb &= PERLDBf_NAMEEVAL;
3184 600 ▶ if (!opt_nameanonsubs)
Branch: 1 branch
BranchTrueFalseTotal
if (!opt_nameanonsubs) 0 600 600
3185 0 PL_perldb &= PERLDBf_NAMEANON;
3186  
3187 600 ▶ if (opt_perldb) /* force a PL_perldb value - for testing only, not documented */
Branch: 1 branch
BranchTrueFalseTotal
if (opt_perldb) /* force a PL_perldb value - for testing only, not documented */ 0 600 600
3188 0 PL_perldb = opt_perldb;
3189  
3190 600 _init_profiler_clock(aTHX);
3191  
3192 600 ▶ if (trace_level)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level) 0 600 600
3193 0 logwarn("~ init_profiler for pid %d, clock %ld, tps %d, start %d, perldb 0x%lx, exitf 0x%lx\n",
3194 0 last_pid, (long)profile_clock, ticks_per_sec, profile_start,
3195 (long unsigned)PL_perldb, (long unsigned)PL_exit_flags);
3196  
3197 600 ▶ if (get_hv("DB::sub", 0) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_hv("DB::sub", 0) == NULL) { 0 600 600
3198 0 logwarn("NYTProf internal error - perl not in debug mode\n");
3199 0 return 0;
3200 }
3201  
3202 #ifdef WANT_TIME_HIRES
3203 require_pv("Time/HiRes.pm"); /* before opcode redirection */
3204 svp = hv_fetch(PL_modglobal, "Time::U2time", 12, 0);
3205 if (!svp || !SvIOK(*svp)) croak("Time::HiRes is required");
3206 time_hires_u2time_hook = INT2PTR(int(*)(pTHX_ UV*), SvIV(*svp));
3207 if (trace_level || !time_hires_u2time_hook)
3208 logwarn("NYTProf using Time::HiRes %p\n", time_hires_u2time_hook);
3209 #endif
3210  
3211 /* create file id mapping hash */
3212 600 fidhash.table = (Hash_entry**)safemalloc(sizeof(Hash_entry*) * fidhash.size);
3213 600 memset(fidhash.table, 0, sizeof(Hash_entry*) * fidhash.size);
3214  
3215 /* redirect opcodes for statement profiling */
3216 600 Newxc(PL_ppaddr_orig, OP_max, void *, orig_ppaddr_t);
3217 600 ▶ Copy(PL_ppaddr, PL_ppaddr_orig, OP_max, void *);
Branch: 2 branches
BranchTrueFalseTotal
Copy(PL_ppaddr, PL_ppaddr_orig, OP_max, void *); 0 600 600
Copy(PL_ppaddr, PL_ppaddr_orig, OP_max, void *); 0 600 600
3218 600 ▶ if (profile_stmts && !opt_use_db_sub) {
Branch: 2 branches
BranchTrueFalseTotal
if (profile_stmts && !opt_use_db_sub) { 580 20 600
if (profile_stmts && !opt_use_db_sub) { 299 281 580
3219 299 PL_ppaddr[OP_NEXTSTATE] = pp_stmt_profiler;
3220 299 PL_ppaddr[OP_DBSTATE] = pp_stmt_profiler;
3221 #ifdef OP_SETSTATE
3222 299 PL_ppaddr[OP_SETSTATE] = pp_stmt_profiler;
3223 #endif
3224 299 ▶ if (profile_leave) {
Branch: 1 branch
BranchTrueFalseTotal
if (profile_leave) { 158 141 299
3225 158 PL_ppaddr[OP_LEAVESUB] = pp_leave_profiler;
3226 158 PL_ppaddr[OP_LEAVESUBLV] = pp_leave_profiler;
3227 158 PL_ppaddr[OP_LEAVE] = pp_leave_profiler;
3228 158 PL_ppaddr[OP_LEAVELOOP] = pp_leave_profiler;
3229 158 PL_ppaddr[OP_LEAVEWRITE] = pp_leave_profiler;
3230 158 PL_ppaddr[OP_LEAVEEVAL] = pp_leave_profiler;
3231 158 PL_ppaddr[OP_LEAVETRY] = pp_leave_profiler;
3232 158 PL_ppaddr[OP_RETURN] = pp_leave_profiler;
3233 /* natural end of simple loop */
3234 158 PL_ppaddr[OP_UNSTACK] = pp_leave_profiler;
3235 /* OP_NEXT is missing because that jumps to OP_UNSTACK */
3236 /* OP_EXIT and OP_EXEC need special handling */
3237 158 PL_ppaddr[OP_EXIT] = pp_exit_profiler;
3238 158 PL_ppaddr[OP_EXEC] = pp_exit_profiler;
3239 }
3240 }
3241 /* calls reinit_if_forked() asap after a fork */
3242 600 PL_ppaddr[OP_FORK] = pp_fork_profiler;
3243  
3244 600 ▶ if (profile_slowops) {
Branch: 1 branch
BranchTrueFalseTotal
if (profile_slowops) { 580 20 600
3245 /* XXX this should turn into a loop over an array that maps
3246 * opcodes to the subname we'll use: OP_PRTF => "printf"
3247 */
3248 #include "slowops.h"
3249 }
3250  
3251 /* redirect opcodes for caller tracking */
3252 600 ▶ if (!sub_callers_hv)
Branch: 1 branch
BranchTrueFalseTotal
if (!sub_callers_hv) 600 0 600
3253 600 sub_callers_hv = newHV();
3254 600 ▶ if (!pkg_fids_hv)
Branch: 1 branch
BranchTrueFalseTotal
if (!pkg_fids_hv) 600 0 600
3255 600 pkg_fids_hv = newHV();
3256 600 PL_ppaddr[OP_ENTERSUB] = pp_entersub_profiler;
3257 600 PL_ppaddr[OP_GOTO] = pp_entersub_profiler;
3258  
3259 600 ▶ if (!PL_checkav) PL_checkav = newAV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_checkav) PL_checkav = newAV(); 598 2 600
3260 600 ▶ if (!PL_initav) PL_initav = newAV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_initav) PL_initav = newAV(); 598 2 600
3261 600 ▶ if (!PL_endav) PL_endav = newAV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_endav) PL_endav = newAV(); 598 2 600
3262 /* pre-extend PL_endav to reduce the chance of DB::_END realloc'ing
3263 * it while END blocks are executed (which could upset some embedded
3264 * applications that don't handle PL_endav carefully, like mod_perl)
3265 */
3266 600 av_extend(PL_endav, av_len(PL_endav)+30);
3267  
3268 600 ▶ if (profile_start == NYTP_START_BEGIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (profile_start == NYTP_START_BEGIN) { 63 537 600
3269 63 enable_profile(aTHX_ NULL);
3270 } else {
3271 /* handled by _INIT */
3272 537 av_push(PL_initav, SvREFCNT_inc(get_cv("DB::_INIT", GV_ADDWARN)));
3273 }
3274 600 ▶ if (PL_minus_c) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_minus_c) { 0 600 600
3275 0 av_push(PL_checkav, SvREFCNT_inc(get_cv("DB::_CHECK", GV_ADDWARN)));
3276 } else {
3277 600 av_push(PL_endav, SvREFCNT_inc(get_cv("DB::_END", GV_ADDWARN)));
3278 }
3279  
3280 /* seed first run time */
3281 600 get_time_of_day(start_time);
3282  
3283 600 ▶ if (trace_level >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) 0 600 600
3284 0 logwarn("~ init_profiler done\n");
3285  
3286 600 return 1;
3287 }
3288  
3289  
3290 /************************************
3291 * Devel::NYTProf::Reader Functions *
3292 ************************************/
3293  
3294 static void
3295 842897496 add_entry(pTHX_ AV *dest_av, unsigned int file_num, unsigned int line_num,
3296 NV time, unsigned int eval_file_num, unsigned int eval_line_num, int count)
3297 {
3298 /* get ref to array of per-line data */
3299 842897496 ▶ unsigned int fid = (eval_line_num) ? eval_file_num : file_num;
Branch: 1 branch
BranchTrueFalseTotal
unsigned int fid = (eval_line_num) ? eval_file_num : file_num; 0 842897496 842897496
3300 842897496 SV *line_time_rvav = *av_fetch(dest_av, fid, 1);
3301  
3302 842897496 ▶ if (!SvROK(line_time_rvav)) /* autoviv */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(line_time_rvav)) /* autoviv */ 62040 842835456 842897496
3303 62040 sv_setsv(line_time_rvav, newRV_noinc((SV*)newAV()));
3304  
3305 842897496 store_profile_line_entry(aTHX_ line_time_rvav, line_num, time, count, fid);
3306 842897496 }
3307  
3308  
3309 static AV *
3310 842897496 store_profile_line_entry(pTHX_ SV *rvav, unsigned int line_num, NV time,
3311 int count, unsigned int fid)
3312 {
3313 842897496 SV *time_rvav = *av_fetch((AV*)SvRV(rvav), line_num, 1);
3314 AV *line_av;
3315 842897496 ▶ if (!SvROK(time_rvav)) { /* autoviv */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(time_rvav)) { /* autoviv */ 1078563 841818933 842897496
3316 1078563 line_av = newAV();
3317 1078563 sv_setsv(time_rvav, newRV_noinc((SV*)line_av));
3318 1078563 av_store(line_av, 0, newSVnv(time));
3319 1078563 av_store(line_av, 1, newSViv(count));
3320 /* if eval then 2 is used for lines within the string eval */
3321 1078563 ▶ if (embed_fid_line) { /* used to optimize reporting */
Branch: 1 branch
BranchTrueFalseTotal
if (embed_fid_line) { /* used to optimize reporting */ 0 1078563 1078563
3322 0 av_store(line_av, 3, newSVuv(fid));
3323 0 av_store(line_av, 4, newSVuv(line_num));
3324 }
3325 }
3326 else {
3327 SV *time_sv;
3328 841818933 line_av = (AV*)SvRV(time_rvav);
3329 841818933 time_sv = *av_fetch(line_av, 0, 1);
3330 841818933 sv_setnv(time_sv, time + SvNV(time_sv));
3331 841818933 ▶ if (count) {
Branch: 1 branch
BranchTrueFalseTotal
if (count) { 796034067 45784866 841818933
3332 796034067 SV *sv = *av_fetch(line_av, 1, 1);
3333 796034067 ▶ (count == 1) ? sv_inc(sv) : sv_setiv(sv, (IV)time + SvIV(sv));
Branch: 1 branch
BranchTrueFalseTotal
(count == 1) ? sv_inc(sv) : sv_setiv(sv, (IV)time + SvIV(sv)); 796034067 0 796034067
3334 }
3335 }
3336 842897496 return line_av;
3337 }
3338  
3339  
3340 /* Given a fully-qualified name, return the length of the package name.
3341 * As most callers get len via the hash API, they will have an I32, where
3342 * "negative" length signifies UTF-8. As we're only dealing with looking for
3343 * ASCII here, it doesn't matter to use which encoding sub_name is in, but it
3344 * reduces total code by doing the abs(len) in here.
3345 */
3346 static STRLEN
3347 157294 pkg_name_len(pTHX_ char *sub_name, I32 len)
3348 {
3349 /* pTHX_ needed for old rninstr in old perl versions */
3350 157294 const char *delim = "::";
3351 /* find end of package name */
3352 157294 ▶ char *colon = rninstr(sub_name, sub_name+(len > 0 ? len : -len), delim, delim+2);
Branch: 1 branch
BranchTrueFalseTotal
char *colon = rninstr(sub_name, sub_name+(len > 0 ? len : -len), delim, delim+2); 157294 0 157294
3353 157294 ▶ if (!colon || colon == sub_name)
Branch: 2 branches
BranchTrueFalseTotal
if (!colon || colon == sub_name) 157294 0 157294
if (!colon || colon == sub_name) 0 157294 157294
3354 0 return 0; /* no :: delimiter */
3355 157294 return (colon - sub_name);
3356 }
3357  
3358 /* Given a fully-qualified sub_name lookup the package name portion in
3359 * the pkg_fids_hv hash. Return Nullsv if there's no package name or no
3360 * corresponding entry, else returns the SV.
3361 *
3362 * About pkg_fids_hv:
3363 * pp_subcall_profiler() creates undef entries for a package
3364 * name the first time a sub in the package is called.
3365 * write_sub_line_ranges() updates the SV with the filename associated
3366 * with the package, or at least its best guess.
3367 */
3368 static SV *
3369 157294 sub_pkg_filename_sv(pTHX_ char *sub_name, I32 len)
3370 {
3371 SV **svp;
3372 157294 STRLEN pkg_len = pkg_name_len(aTHX_ sub_name, len);
3373 157294 ▶ if (!pkg_len)
Branch: 1 branch
BranchTrueFalseTotal
if (!pkg_len) 0 157294 157294
3374 0 return Nullsv; /* no :: delimiter */
3375 157294 svp = hv_fetch(pkg_fids_hv, sub_name, (I32)pkg_len, 0);
3376 157294 ▶ if (!svp)
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) 109328 47966 157294
3377 109328 return Nullsv; /* not a package we've profiled sub calls into */
3378 47966 return *svp;
3379 }
3380  
3381  
3382 static int
3383 173056 parse_DBsub_value(pTHX_ SV *sv, STRLEN *filename_len_p, UV *first_line_p, UV *last_line_p, char *sub_name) {
3384 /* "filename:first-last" */
3385 173056 char *filename = SvPV_nolen(sv);
3386 173056 char *first = strrchr(filename, ':'); /* find last colon */
3387 char *last;
3388 173056 int first_is_neg = 0;
3389  
3390 173056 ▶ if (first && filename_len_p)
Branch: 2 branches
BranchTrueFalseTotal
if (first && filename_len_p) 173056 0 173056
if (first && filename_len_p) 137032 36024 173056
3391 137032 *filename_len_p = first - filename;
3392  
3393 173056 ▶ if (!first++) /* start of first number, if colon was found */
Branch: 1 branch
BranchTrueFalseTotal
if (!first++) /* start of first number, if colon was found */ 0 173056 173056
3394 0 return 0;
3395 173056 ▶ if ('-' == *first) { /* first number is negative */
Branch: 1 branch
BranchTrueFalseTotal
if ('-' == *first) { /* first number is negative */ 0 173056 173056
3396 0 ++first;
3397 0 first_is_neg = 1;
3398 }
3399 173056 last = strchr(first, '-'); /* find separator dash */
3400  
3401 173056 ▶ if (!last || !grok_number(first, last-first, first_line_p))
Branch: 2 branches
BranchTrueFalseTotal
if (!last || !grok_number(first, last-first, first_line_p)) 173056 0 173056
if (!last || !grok_number(first, last-first, first_line_p)) 0 173056 173056
3402 0 return 0;
3403 173056 ▶ if (first_is_neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (first_is_neg) { 0 173056 173056
3404 0 warn("Negative first line number in %%DB::sub entry '%s' for %s\n",
3405 filename, sub_name);
3406 0 *first_line_p = 0;
3407 }
3408  
3409 173056 ▶ if ('-' == *++last) { /* skip past dash, is next char a minus? */
Branch: 1 branch
BranchTrueFalseTotal
if ('-' == *++last) { /* skip past dash, is next char a minus? */ 0 173056 173056
3410 0 warn("Negative last line number in %%DB::sub entry '%s' for %s\n",
3411 filename, sub_name);
3412 0 last = (char *)"0";
3413 }
3414 173056 ▶ if (last_line_p)
Branch: 1 branch
BranchTrueFalseTotal
if (last_line_p) 137032 36024 173056
3415 137032 *last_line_p = atoi(last);
3416  
3417 173056 return 1;
3418 }
3419  
3420  
3421 static void
3422 645 write_sub_line_ranges(pTHX)
3423 {
3424 char *sub_name;
3425 I32 sub_name_len;
3426 SV *file_lines_sv;
3427 645 HV *hv = GvHV(PL_DBsub);
3428 unsigned int fid;
3429  
3430 645 ▶ if (trace_level >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) 0 645 645
3431 0 logwarn("~ writing sub line ranges - prescan\n");
3432  
3433 /* Skim through PL_DBsub hash to build a package to filename hash
3434 * by associating the package part of the sub_name in the key
3435 * with the filename part of the value.
3436 * but only for packages we already know we're interested in
3437 */
3438 645 hv_iterinit(hv);
3439 137081 ▶ while (NULL != (file_lines_sv = hv_iternextsv(hv, &sub_name, &sub_name_len))) {
Branch: 1 branch
BranchTrueFalseTotal
while (NULL != (file_lines_sv = hv_iternextsv(hv, &sub_name, &sub_name_len))) { 136436 645 137081
3440 STRLEN file_lines_len;
3441 136436 char *filename = SvPV(file_lines_sv, file_lines_len);
3442 char *first;
3443 STRLEN filename_len;
3444 SV *pkg_filename_sv;
3445  
3446 /* This is a heuristic, and might not be robust, but it seems that
3447 it's possible to get problematically bogus entries in this hash.
3448 Specifically, setting the 'lvalue' attribute on an XS subroutine
3449 during a bootstrap can cause op.c to load attributes, and in turn
3450 cause a DynaLoader::BEGIN entry in %DB::sub associated with the
3451 .pm file of the XS sub's module, not DynaLoader. This has the result
3452 that if we try to associate XSUBs with filenames using %DB::sub,
3453 we can go very wrong.
3454  
3455 Fortunately all "wrong" entries so far spotted have a line range
3456 with a non-zero start, and a zero end. This cannot be legal, so we
3457 ignore those.
3458 */
3459  
3460 136436 ▶ if (file_lines_len > 4
Branch: 1 branch
BranchTrueFalseTotal
if (file_lines_len > 4 116466 19970 136436
3461 116466 ▶ && filename[file_lines_len - 2] == '-' && filename[file_lines_len - 1] == '0'
Branch: 2 branches
BranchTrueFalseTotal
&& filename[file_lines_len - 2] == '-' && filename[file_lines_len - 1] == '0' 2801 113665 116466
&& filename[file_lines_len - 2] == '-' && filename[file_lines_len - 1] == '0' 256 2545 2801
3462 256 ▶ && filename[file_lines_len - 4] != ':' && filename[file_lines_len - 3] != '0')
Branch: 2 branches
BranchTrueFalseTotal
&& filename[file_lines_len - 4] != ':' && filename[file_lines_len - 3] != '0') 192 64 256
&& filename[file_lines_len - 4] != ':' && filename[file_lines_len - 3] != '0') 192 0 192
3463 136403 continue; /* ignore filenames from %DB::sub that match /:[^0]-0$/ */
3464  
3465 136244 first = strrchr(filename, ':');
3466 136244 ▶ filename_len = (first) ? first - filename : 0;
Branch: 1 branch
BranchTrueFalseTotal
filename_len = (first) ? first - filename : 0; 136244 0 136244
3467  
3468 /* get sv for package-of-subname to filename mapping */
3469 136244 pkg_filename_sv = sub_pkg_filename_sv(aTHX_ sub_name, sub_name_len);
3470  
3471 136244 ▶ if (!pkg_filename_sv) /* we don't know package */
Branch: 1 branch
BranchTrueFalseTotal
if (!pkg_filename_sv) /* we don't know package */ 109184 27060 136244
3472 109184 continue;
3473  
3474 /* already got a cached filename for this package XXX should allow multiple */
3475 27060 ▶ if (SvOK(pkg_filename_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(pkg_filename_sv)) { 7117 19943 27060
3476 STRLEN cached_len;
3477 7117 char *cached_filename = SvPV(pkg_filename_sv, cached_len);
3478  
3479 /*
3480 * if the cached filename is an eval and the current one isn't
3481 * then we should cache the current one instead
3482 */
3483 7117 ▶ if (filename_len > 0
Branch: 1 branch
BranchTrueFalseTotal
if (filename_len > 0 7057 60 7117
3484 7057 ▶ && filename_is_eval(cached_filename, cached_len)
Branch: 1 branch
BranchTrueFalseTotal
&& filename_is_eval(cached_filename, cached_len) 1 7056 7057
3485 1 ▶ && !filename_is_eval(filename, filename_len)
Branch: 1 branch
BranchTrueFalseTotal
&& !filename_is_eval(filename, filename_len) 1 0 1
3486 ) {
3487 1 ▶ if (trace_level >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 3) 0 1 1
3488 0 logwarn("Package '%.*s' (of sub %.*s) association promoted from '%.*s' to '%.*s'\n",
3489 0 (int)pkg_name_len(aTHX_ sub_name, sub_name_len), sub_name,
3490 (int)sub_name_len, sub_name,
3491 (int)cached_len, cached_filename,
3492 (int)filename_len, filename);
3493 1 sv_setpvn(pkg_filename_sv, filename, filename_len);
3494 1 continue;
3495 }
3496  
3497 7116 ▶ if (trace_level >= 3
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 3 0 7116 7116
3498 0 ▶ && strnNE(SvPV_nolen(pkg_filename_sv), filename, filename_len)
Branch: 1 branch
BranchTrueFalseTotal
&& strnNE(SvPV_nolen(pkg_filename_sv), filename, filename_len) 0 0 0
3499 0 ▶ && !filename_is_eval(filename, filename_len)
Branch: 1 branch
BranchTrueFalseTotal
&& !filename_is_eval(filename, filename_len) 0 0 0
3500 ) {
3501 /* eg utf8::SWASHNEW is already associated with .../utf8.pm not .../utf8_heavy.pl */
3502 0 logwarn("Package '%.*s' (of sub %.*s) not associated with '%.*s' because already associated with '%s'\n",
3503 0 (int)pkg_name_len(aTHX_ sub_name, sub_name_len), sub_name,
3504 (int)sub_name_len, sub_name,
3505 (int)filename_len, filename,
3506 SvPV_nolen(pkg_filename_sv)
3507 );
3508 }
3509 7116 continue;
3510 }
3511  
3512 /* ignore if filename is empty (eg xs) */
3513 19943 ▶ if (!filename_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (!filename_len) { 19910 33 19943
3514 19910 ▶ if (trace_level >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 3) 0 19910 19910
3515 0 logwarn("Sub %.*s has no filename associated (%s)\n",
3516 (int)sub_name_len, sub_name, filename);
3517 19910 continue;
3518 }
3519  
3520 /* associate the filename with the package */
3521 33 sv_setpvn(pkg_filename_sv, filename, filename_len);
3522  
3523 /* ensure a fid is assigned since we don't allow it below */
3524 33 fid = get_file_id(aTHX_ filename, filename_len, NYTP_FIDf_VIA_SUB);
3525  
3526 33 ▶ if (trace_level >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 3) 0 33 33
3527 0 logwarn("Associating package of %s with %.*s (fid %d)\n",
3528 sub_name, (int)filename_len, filename, fid );
3529 }
3530  
3531 645 ▶ if (main_runtime_used) { /* Create fake entry for main::RUNTIME sub */
Branch: 1 branch
BranchTrueFalseTotal
if (main_runtime_used) { /* Create fake entry for main::RUNTIME sub */ 644 1 645
3532 644 char runtime[] = "main::RUNTIME";
3533 644 const I32 runtime_len = sizeof(runtime) - 1;
3534 644 SV *sv = *hv_fetch(hv, runtime, runtime_len, 1);
3535  
3536 /* get name of file that contained first profiled sub in 'main::' */
3537 644 SV *pkg_filename_sv = sub_pkg_filename_sv(aTHX_ runtime, runtime_len);
3538 644 ▶ if (!pkg_filename_sv) { /* no subs in main, so guess */
Branch: 1 branch
BranchTrueFalseTotal
if (!pkg_filename_sv) { /* no subs in main, so guess */ 144 500 644
3539 144 sv_setpvn(sv, fidhash.first_inserted->key, fidhash.first_inserted->key_len);
3540 }
3541 500 ▶ else if (SvOK(pkg_filename_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvOK(pkg_filename_sv)) { 64 436 500
3542 64 sv_setsv(sv, pkg_filename_sv);
3543 }
3544 else {
3545 436 sv_setpvn(sv, "", 0);
3546 }
3547 644 sv_catpvs(sv, ":1-1");
3548 }
3549  
3550 645 ▶ if (trace_level >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) 0 645 645
3551 0 ▶ logwarn("~ writing sub line ranges of %ld subs\n", (long)HvKEYS(hv));
Branch: 1 branch
BranchTrueFalseTotal
logwarn("~ writing sub line ranges of %ld subs\n", (long)HvKEYS(hv)); 0 0 0
3552  
3553 /* Iterate over PL_DBsub writing out fid and source line range of subs.
3554 * If filename is missing (i.e., because it's an xsub so has no source file)
3555 * then use the filename of another sub in the same package.
3556 */
3557 137677 ▶ while (NULL != (file_lines_sv = hv_iternextsv(hv, &sub_name, &sub_name_len))) {
Branch: 1 branch
BranchTrueFalseTotal
while (NULL != (file_lines_sv = hv_iternextsv(hv, &sub_name, &sub_name_len))) { 137032 645 137677
3558 /* "filename:first-last" */
3559 137032 char *filename = SvPV_nolen(file_lines_sv);
3560 STRLEN filename_len;
3561 UV first_line, last_line;
3562  
3563 137032 ▶ if (!parse_DBsub_value(aTHX_ file_lines_sv, &filename_len, &first_line, &last_line, sub_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_DBsub_value(aTHX_ file_lines_sv, &filename_len, &first_line, &last_line, sub_name)) { 0 137032 137032
3564 0 logwarn("Can't parse %%DB::sub entry for %s '%s'\n", sub_name, filename);
3565 129526 continue;
3566 }
3567  
3568 137032 ▶ if (!filename_len) { /* no filename, so presumably a fake entry for xsub */
Branch: 1 branch
BranchTrueFalseTotal
if (!filename_len) { /* no filename, so presumably a fake entry for xsub */ 20406 116626 137032
3569 /* do we know a filename that contains subs in the same package */
3570 20406 SV *pkg_filename_sv = sub_pkg_filename_sv(aTHX_ sub_name, sub_name_len);
3571 20406 ▶ if (pkg_filename_sv && SvOK(pkg_filename_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (pkg_filename_sv && SvOK(pkg_filename_sv)) { 20406 0 20406
if (pkg_filename_sv && SvOK(pkg_filename_sv)) { 64 20342 20406
3572 64 filename = SvPV(pkg_filename_sv, filename_len);
3573 64 ▶ if (trace_level >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 2) 0 64 64
3574 0 logwarn("Sub %s is xsub, we'll associate it with filename %.*s\n",
3575 sub_name, (int)filename_len, filename);
3576 }
3577 }
3578  
3579 137032 fid = get_file_id(aTHX_ filename, filename_len, 0);
3580 137032 ▶ if (!fid) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fid) { 129526 7506 137032
3581 129526 ▶ if (trace_level >= 4)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 4) 0 129526 129526
3582 0 logwarn("Sub %s has no fid assigned (for file '%.*s')\n",
3583 sub_name, (int)filename_len, filename);
3584 129526 continue; /* no point in writing subs in files we've not profiled */
3585 }
3586  
3587 7506 ▶ if (trace_level >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 2) 0 7506 7506
3588 0 logwarn("Sub %s fid %u lines %lu..%lu\n",
3589 sub_name, fid, (unsigned long)first_line, (unsigned long)last_line);
3590  
3591 7506 NYTP_write_sub_info(out, fid, sub_name, sub_name_len, (unsigned long)first_line,
3592 (unsigned long)last_line);
3593 }
3594 645 }
3595  
3596  
3597 static void
3598 645 write_sub_callers(pTHX)
3599 {
3600 char *called_subname;
3601 I32 called_subname_len;
3602 SV *fid_line_rvhv;
3603 645 int negative_time_calls = 0;
3604  
3605 645 ▶ if (!sub_callers_hv)
Branch: 1 branch
BranchTrueFalseTotal
if (!sub_callers_hv) 0 645 645
3606 0 return;
3607 645 ▶ if (trace_level >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) 0 645 645
3608 0 ▶ logwarn("~ writing sub callers for %ld subs\n", (long)HvKEYS(sub_callers_hv));
Branch: 1 branch
BranchTrueFalseTotal
logwarn("~ writing sub callers for %ld subs\n", (long)HvKEYS(sub_callers_hv)); 0 0 0
3609  
3610 645 hv_iterinit(sub_callers_hv);
3611 207659 ▶ while (NULL != (fid_line_rvhv = hv_iternextsv(sub_callers_hv, &called_subname, &called_subname_len))) {
Branch: 1 branch
BranchTrueFalseTotal
while (NULL != (fid_line_rvhv = hv_iternextsv(sub_callers_hv, &called_subname, &called_subname_len))) { 207014 645 207659
3612 HV *fid_lines_hv;
3613 char *caller_subname;
3614 I32 caller_subname_len;
3615 SV *sv;
3616  
3617 207014 ▶ if (!SvROK(fid_line_rvhv) || SvTYPE(SvRV(fid_line_rvhv))!=SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(fid_line_rvhv) || SvTYPE(SvRV(fid_line_rvhv))!=SVt_PVHV) { 207014 0 207014
if (!SvROK(fid_line_rvhv) || SvTYPE(SvRV(fid_line_rvhv))!=SVt_PVHV) { 0 207014 207014
3618 0 logwarn("bad entry %s in sub_callers_hv\n", called_subname);
3619 0 continue;
3620 }
3621 207014 fid_lines_hv = (HV*)SvRV(fid_line_rvhv);
3622  
3623 if (0) {
3624 logwarn("Callers of %s:\n", called_subname);
3625 /* level, *file, *sv, I32 nest, I32 maxnest, bool dumpops, STRLEN pvlim */
3626 do_sv_dump(0, Perl_debug_log, fid_line_rvhv, 0, 5, 0, 100);
3627 }
3628  
3629 /* iterate over callers to this sub ({ "subname[fid:line]" => [ ... ] }) */
3630 207014 hv_iterinit(fid_lines_hv);
3631 630171 ▶ while (NULL != (sv = hv_iternextsv(fid_lines_hv, &caller_subname, &caller_subname_len))) {
Branch: 1 branch
BranchTrueFalseTotal
while (NULL != (sv = hv_iternextsv(fid_lines_hv, &caller_subname, &caller_subname_len))) { 423157 207014 630171
3632 NV sc[NYTP_SCi_elements];
3633 423157 AV *av = (AV *)SvRV(sv);
3634 423157 int trace = (trace_level >= 3);
3635 UV count;
3636 UV depth;
3637  
3638 423157 unsigned int fid = 0, line = 0;
3639 423157 const char *fid_line_delim = "[";
3640 423157 char *fid_line_start = rninstr(caller_subname, caller_subname+caller_subname_len, fid_line_delim, fid_line_delim+1);
3641 423157 ▶ if (!fid_line_start) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fid_line_start) { 0 423157 423157
3642 0 logwarn("bad fid_lines_hv key '%s'\n", caller_subname);
3643 0 continue;
3644 }
3645 423157 ▶ if (2 != sscanf(fid_line_start+1, "%u:%u", &fid, &line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 != sscanf(fid_line_start+1, "%u:%u", &fid, &line)) { 0 423157 423157
3646 0 logwarn("bad fid_lines_hv format '%s'\n", caller_subname);
3647 0 continue;
3648 }
3649 /* trim length to effectively hide the [fid:line] suffix */
3650 423157 caller_subname_len = (I32)(fid_line_start-caller_subname);
3651  
3652 /* catch negative line numbers that have been stored unsigned */
3653 423157 ▶ if (line > 2147483600) { /* ~2**31 */
Branch: 1 branch
BranchTrueFalseTotal
if (line > 2147483600) { /* ~2**31 */ 0 423157 423157
3654 0 logwarn("%s called by %.*s at fid %u line %u - crazy line number changed to 0\n",
3655 called_subname, (int)caller_subname_len, caller_subname, fid, line);
3656 0 line = 0;
3657 }
3658  
3659 423157 count = uv_from_av(aTHX_ av, NYTP_SCi_CALL_COUNT, 0);
3660 423157 sc[NYTP_SCi_CALL_COUNT] = count * 1.0;
3661 423157 sc[NYTP_SCi_INCL_RTIME] = nv_from_av(aTHX_ av, NYTP_SCi_INCL_TICKS, 0.0) / ticks_per_sec;
3662 423157 sc[NYTP_SCi_EXCL_RTIME] = nv_from_av(aTHX_ av, NYTP_SCi_EXCL_TICKS, 0.0) / ticks_per_sec;
3663 423157 sc[NYTP_SCi_RECI_RTIME] = nv_from_av(aTHX_ av, NYTP_SCi_RECI_RTIME, 0.0);
3664 423157 depth = uv_from_av(aTHX_ av, NYTP_SCi_REC_DEPTH , 0);
3665 423157 sc[NYTP_SCi_REC_DEPTH] = depth * 1.0;
3666  
3667 423157 NYTP_write_sub_callers(out, fid, line,
3668 caller_subname, caller_subname_len,
3669 (unsigned int)count,
3670 sc[NYTP_SCi_INCL_RTIME],
3671 sc[NYTP_SCi_EXCL_RTIME],
3672 sc[NYTP_SCi_RECI_RTIME],
3673 (unsigned int)depth,
3674 called_subname, called_subname_len);
3675  
3676 /* sanity check - early warning */
3677 423157 ▶ if (sc[NYTP_SCi_INCL_RTIME] < 0.0 || sc[NYTP_SCi_EXCL_RTIME] < 0.0) {
Branch: 2 branches
BranchTrueFalseTotal
if (sc[NYTP_SCi_INCL_RTIME] < 0.0 || sc[NYTP_SCi_EXCL_RTIME] < 0.0) { 423157 0 423157
if (sc[NYTP_SCi_INCL_RTIME] < 0.0 || sc[NYTP_SCi_EXCL_RTIME] < 0.0) { 0 423157 423157
3678 0 ++negative_time_calls;
3679 0 ▶ if (trace_level) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level) { 0 0 0
3680 0 logwarn("%s call has negative time: incl %" NVff "s, excl %" NVff "s:\n",
3681 called_subname, sc[NYTP_SCi_INCL_RTIME], sc[NYTP_SCi_EXCL_RTIME]);
3682 0 trace = 1;
3683 }
3684 }
3685  
3686 423157 ▶ if (trace) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace) { 0 423157 423157
3687 0 ▶ if (!fid && !line) {
Branch: 2 branches
BranchTrueFalseTotal
if (!fid && !line) { 0 0 0
if (!fid && !line) { 0 0 0
3688 0 logwarn("%s is xsub\n", called_subname);
3689 }
3690 else {
3691 0 logwarn("%s called by %.*s at %u:%u: count %ld (i%" NVff "s e%" NVff "s, d%d ri%" NVff "s)\n",
3692 called_subname, (int)caller_subname_len, caller_subname, fid, line,
3693 0 (long)sc[NYTP_SCi_CALL_COUNT], sc[NYTP_SCi_INCL_RTIME], sc[NYTP_SCi_EXCL_RTIME],
3694 0 (int)sc[NYTP_SCi_REC_DEPTH], sc[NYTP_SCi_RECI_RTIME]);
3695 }
3696 }
3697 }
3698 }
3699 645 ▶ if (negative_time_calls) {
Branch: 1 branch
BranchTrueFalseTotal
if (negative_time_calls) { 0 645 645
3700 0 logwarn("Warning: %d subroutine calls had negative time! See TROUBLESHOOTING in the NYTProf documentation. (Clock %ld)\n",
3701 0 negative_time_calls, (long)profile_clock);
3702 }
3703 }
3704  
3705  
3706 static void
3707 645 write_src_of_files(pTHX)
3708 {
3709 fid_hash_entry *e;
3710 645 int t_has_src = 0;
3711 645 int t_save_src = 0;
3712 645 int t_no_src = 0;
3713 645 long t_lines = 0;
3714  
3715 645 ▶ if (trace_level >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) 0 645 645
3716 0 logwarn("~ writing file source code\n");
3717  
3718 21435 ▶ for (e = (fid_hash_entry*)fidhash.first_inserted; e; e = (fid_hash_entry*)e->he.next_inserted) {
Branch: 1 branch
BranchTrueFalseTotal
for (e = (fid_hash_entry*)fidhash.first_inserted; e; e = (fid_hash_entry*)e->he.next_inserted) { 20790 645 21435
3719 I32 lines;
3720 int line;
3721 20790 AV *src_av = GvAV(gv_fetchfile_flags(e->he.key, e->he.key_len, 0));
3722  
3723 20790 ▶ if ( !(e->fid_flags & NYTP_FIDf_HAS_SRC) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !(e->fid_flags & NYTP_FIDf_HAS_SRC) ) { 4951 15839 20790
3724 4951 const char *hint = "";
3725 4951 ++t_no_src;
3726 4951 ▶ if (src_av && av_len(src_av) > -1) /* sanity check */
Branch: 2 branches
BranchTrueFalseTotal
if (src_av && av_len(src_av) > -1) /* sanity check */ 91 4860 4951
if (src_av && av_len(src_av) > -1) /* sanity check */ 0 91 91
3727 0 hint = " (NYTP_FIDf_HAS_SRC not set but src available!)";
3728 4951 ▶ if (trace_level >= 3 || *hint)
Branch: 2 branches
BranchTrueFalseTotal
if (trace_level >= 3 || *hint) 4951 0 4951
if (trace_level >= 3 || *hint) 0 4951 4951
3729 0 logwarn("fid %d has no src saved for %.*s%s\n",
3730 e->he.id, e->he.key_len, e->he.key, hint);
3731 4951 continue;
3732 }
3733 15839 ▶ if (!src_av) { /* sanity check */
Branch: 1 branch
BranchTrueFalseTotal
if (!src_av) { /* sanity check */ 0 15839 15839
3734 0 ++t_no_src;
3735 0 logwarn("fid %d has no src but NYTP_FIDf_HAS_SRC is set! (%.*s)\n",
3736 e->he.id, e->he.key_len, e->he.key);
3737 0 continue;
3738 }
3739 15839 ++t_has_src;
3740  
3741 15839 ▶ if ( !(e->fid_flags & NYTP_FIDf_SAVE_SRC) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !(e->fid_flags & NYTP_FIDf_SAVE_SRC) ) { 3620 12219 15839
3742 3620 continue;
3743 }
3744 12219 ++t_save_src;
3745  
3746 12219 lines = av_len(src_av); /* -1 is empty, 1 is 1 line etc, 0 shouldn't happen */
3747 12219 ▶ if (trace_level >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 3) 0 12219 12219
3748 0 logwarn("fid %d has %ld src lines for %.*s\n",
3749 e->he.id, (long)lines, e->he.key_len, e->he.key);
3750 4035773 ▶ for (line = 1; line <= lines; ++line) { /* lines start at 1 */
Branch: 1 branch
BranchTrueFalseTotal
for (line = 1; line <= lines; ++line) { /* lines start at 1 */ 4023554 12219 4035773
3751 4023554 SV **svp = av_fetch(src_av, line, 0);
3752 4023554 STRLEN len = 0;
3753 8046037 ▶ const char *src = (svp) ? SvPV(*svp, len) : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *src = (svp) ? SvPV(*svp, len) : ""; 4022483 1071 4023554
3754 /* outputting the tag and fid for each (non empty) line
3755 * is a little inefficient, but not enough to worry about */
3756 4023554 NYTP_write_src_line(out, e->he.id, line, src, (I32)len); /* includes newline */
3757 4023554 ▶ if (trace_level >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 8) { 0 4023554 4023554
3758 0 logwarn("fid %d src line %d: %s%s", e->he.id, line, src,
3759 0 ▶ (len && src[len-1]=='\n') ? "" : "\n");
Branch: 2 branches
BranchTrueFalseTotal
(len && src[len-1]=='\n') ? "" : "\n"); 0 0 0
(len && src[len-1]=='\n') ? "" : "\n"); 0 0 0
3760 }
3761 4023554 ++t_lines;
3762 }
3763 }
3764  
3765 645 ▶ if (trace_level >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 2) 0 645 645
3766 0 logwarn("~ wrote %ld source lines for %d files (%d skipped without savesrc option, %d others had no source available)\n",
3767 t_lines, t_save_src, t_has_src-t_save_src, t_no_src);
3768 645 }
3769  
3770  
3771 static void
3772 1155140 normalize_eval_seqn(pTHX_ SV *sv) {
3773 /* in-place-edit any eval sequence numbers to 0 */
3774 STRLEN len;
3775 1155140 char *start = SvPV(sv, len);
3776 char *first_space;
3777  
3778 1155140 return; /* disabled, again */
3779  
3780 /* effectively does
3781 s/(
3782 \( # first character is literal (
3783 (?:re_)?eval\ # eval or re_eval followed by space
3784 ) # [capture that]
3785 [0-9]+ # digits
3786 (?=\)) # look ahead for literal )
3787 /$1 0/xg # and rebuild, replacing the digits with 0
3788 */
3789  
3790 /* Assumption is that space is the least common character in a filename. */
3791  
3792 for (; len >= 8 && (first_space = (char *)memchr(start, ' ', len));
3793 (len -= first_space +1 - start), (start = first_space + 1)) {
3794 char *first_digit;
3795 char *close;
3796  
3797 if (!((first_space - start >= 5
3798 && memEQ(first_space - 5, "(eval", 5))
3799 || (first_space - start >= 8
3800 && memEQ(first_space - 8, "(re_eval", 8)))) {
3801 /* Fixed string not found. Try again. */
3802 continue;
3803 }
3804  
3805 first_digit = first_space + 1;
3806 if (*first_digit < '0' || *first_digit > '9')
3807 continue;
3808  
3809 close = first_digit + 1;
3810  
3811 while (*close >= '0' && *close <= '9')
3812 ++close;
3813  
3814 if (*close != ')')
3815 continue;
3816  
3817 if (trace_level >= 15)
3818 logwarn("recognized eval in name at '%s' in %s\n", first_digit, start);
3819  
3820 *first_digit++ = '0';
3821  
3822 /* first_digit now points to the target of the move. */
3823  
3824 if (close != first_digit) {
3825 /* 2 or more digits */
3826 memmove(first_digit, close,
3827 start + len + 1 /* pointer beyond the trailing '\0' */
3828 - close); /* pointer to the ) */
3829  
3830 len -= (close - first_digit);
3831 SvCUR_set(sv, SvCUR(sv) - (close - first_digit));
3832 }
3833  
3834 if (trace_level >= 15)
3835 logwarn("edited it to: %s\n", start);
3836 }
3837 }
3838  
3839  
3840 static AV *
3841 582316 lookup_subinfo_av(pTHX_ SV *subname_sv, HV *sub_subinfo_hv)
3842 {
3843 /* { 'pkg::sub' => [
3844 * fid, first_line, last_line, incl_time
3845 * ], ... }
3846 */
3847 582316 HE *he = hv_fetch_ent(sub_subinfo_hv, subname_sv, 1, 0);
3848 582316 SV *sv = HeVAL(he);
3849 582316 ▶ if (!SvROK(sv)) { /* autoviv */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) { /* autoviv */ 287719 294597 582316
3850 287719 AV *av = newAV();
3851 287719 SV *rv = newRV_noinc((SV *)av);
3852 /* 0: fid - may be undef
3853 * 1: start_line - may be undef if not known and not known to be xs
3854 * 2: end_line - ditto
3855 * typically due to an xsub that was called but exited via an exception
3856 */
3857 287719 sv_setsv(*av_fetch(av, NYTP_SIi_SUB_NAME, 1), newSVsv(subname_sv));
3858 287719 sv_setuv(*av_fetch(av, NYTP_SIi_CALL_COUNT, 1), 0); /* call count */
3859 287719 sv_setnv(*av_fetch(av, NYTP_SIi_INCL_RTIME, 1), 0.0); /* incl_time */
3860 287719 sv_setnv(*av_fetch(av, NYTP_SIi_EXCL_RTIME, 1), 0.0); /* excl_time */
3861 287719 sv_setsv(*av_fetch(av, NYTP_SIi_PROFILE, 1), &PL_sv_undef); /* ref to profile */
3862 287719 sv_setuv(*av_fetch(av, NYTP_SIi_REC_DEPTH, 1), 0); /* rec_depth */
3863 287719 sv_setnv(*av_fetch(av, NYTP_SIi_RECI_RTIME, 1), 0.0); /* reci_time */
3864 287719 sv_setsv(sv, rv);
3865 }
3866 582316 return (AV *)SvRV(sv);
3867 }
3868  
3869  
3870 static void
3871 16116 store_attrib_sv(pTHX_ HV *attr_hv, const char *text, I32 text_len, SV *value_sv)
3872 {
3873 16116 (void)hv_store(attr_hv, text, text_len, value_sv, 0);
3874 16116 ▶ if (trace_level >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) 0 16116 16116
3875 0 logwarn(": %.*s = '%s'\n", (int) text_len, text, SvPV_nolen(value_sv));
3876 16116 }
3877  
3878 #if 0 /* not used at the moment */
3879 static int
3880 eval_outer_fid(pTHX_
3881 AV *fid_fileinfo_av,
3882 unsigned int fid,
3883 int recurse,
3884 unsigned int *eval_file_num_ptr,
3885 unsigned int *eval_line_num_ptr
3886 ) {
3887 unsigned int outer_fid;
3888 AV *av;
3889 SV *fid_info_rvav = *av_fetch(fid_fileinfo_av, fid, 1);
3890 if (!SvROK(fid_info_rvav)) /* should never happen */
3891 return 0;
3892 av = (AV *)SvRV(fid_info_rvav);
3893 outer_fid = (unsigned int)SvUV(*av_fetch(av,NYTP_FIDi_EVAL_FID,1));
3894 if (!outer_fid)
3895 return 0;
3896 if (outer_fid == fid) {
3897 logwarn("Possible corruption: eval_outer_fid of %d is %d!\n", fid, outer_fid);
3898 return 0;
3899 }
3900 if (eval_file_num_ptr)
3901 *eval_file_num_ptr = outer_fid;
3902 if (eval_line_num_ptr)
3903 *eval_line_num_ptr = (unsigned int)SvUV(*av_fetch(av,NYTP_FIDi_EVAL_LINE,1));
3904 if (recurse)
3905 eval_outer_fid(aTHX_ fid_fileinfo_av, outer_fid, recurse, eval_file_num_ptr, eval_line_num_ptr);
3906 return 1;
3907 }
3908 #endif
3909  
3910 typedef struct loader_state_base {
3911 unsigned long input_chunk_seqn;
3912 } Loader_state_base;
3913  
3914 typedef void (*loader_callback)(Loader_state_base *cb_data, const int tag, ...);
3915  
3916 typedef struct loader_state_callback {
3917 Loader_state_base base_state;
3918 #ifdef MULTIPLICITY
3919 PerlInterpreter *interp;
3920 #endif
3921 CV *cb[nytp_tag_max];
3922 SV *cb_args[11]; /* must be large enough for the largest callback argument list */
3923 SV *tag_names[nytp_tag_max];
3924 SV *input_chunk_seqn_sv;
3925 } Loader_state_callback;
3926  
3927 typedef struct loader_state_profiler {
3928 Loader_state_base base_state;
3929 #ifdef MULTIPLICITY
3930 PerlInterpreter *interp;
3931 #endif
3932 unsigned int last_file_num;
3933 unsigned int last_line_num;
3934 int statement_discount;
3935 UV total_stmts_discounted;
3936 UV total_stmts_measured;
3937 NV total_stmts_duration;
3938 UV total_sub_calls;
3939 AV *fid_line_time_av;
3940 AV *fid_block_time_av;
3941 AV *fid_sub_time_av;
3942 AV *fid_srclines_av;
3943 AV *fid_fileinfo_av;
3944 HV *sub_subinfo_hv;
3945 HV *live_pids_hv;
3946 HV *attr_hv;
3947 HV *option_hv;
3948 HV *file_info_stash;
3949 /* these times don't reflect profile_enable & profile_disable calls */
3950 NV profiler_start_time;
3951 NV profiler_end_time;
3952 NV profiler_duration;
3953 } Loader_state_profiler;
3954  
3955 static void
3956 15263478 load_discount_callback(Loader_state_base *cb_data, const int tag, ...)
3957 {
3958 15263478 Loader_state_profiler *state = (Loader_state_profiler *)cb_data;
3959 PERL_UNUSED_ARG(tag);
3960  
3961 15263478 ▶ if (trace_level >= 8)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 8) 0 15263478 15263478
3962 0 logwarn("discounting next statement after %u:%d\n",
3963 state->last_file_num, state->last_line_num);
3964 15263478 ▶ if (state->statement_discount)
Branch: 1 branch
BranchTrueFalseTotal
if (state->statement_discount) 0 15263478 15263478
3965 0 logwarn("multiple statement discount after %u:%d\n",
3966 state->last_file_num, state->last_line_num);
3967 15263478 ++state->statement_discount;
3968 15263478 ++state->total_stmts_discounted;
3969 15263478 }
3970  
3971 static void
3972 280965832 load_time_callback(Loader_state_base *cb_data, const int tag, ...)
3973 {
3974 280965832 Loader_state_profiler *state = (Loader_state_profiler *)cb_data;
3975 dTHXa(state->interp);
3976 va_list args;
3977 280965832 char trace_note[80] = "";
3978 SV *fid_info_rvav;
3979 NV seconds;
3980 280965832 unsigned int eval_file_num = 0;
3981 280965832 unsigned int eval_line_num = 0;
3982 I32 ticks;
3983 unsigned int file_num;
3984 unsigned int line_num;
3985  
3986 280965832 va_start(args, tag);
3987  
3988 280965832 ticks = va_arg(args, I32);
3989 280965832 file_num = va_arg(args, unsigned int);
3990 280965832 line_num = va_arg(args, unsigned int);
3991  
3992 280965832 seconds = (NV)ticks / ticks_per_sec;
3993  
3994 280965832 fid_info_rvav = *av_fetch(state->fid_fileinfo_av, file_num, 1);
3995 280965832 ▶ if (!SvROK(fid_info_rvav)) { /* should never happen */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(fid_info_rvav)) { /* should never happen */ 0 280965832 280965832
3996 0 ▶ if (!SvOK(fid_info_rvav)) { /* only warn once */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(fid_info_rvav)) { /* only warn once */ 0 0 0
3997 0 logwarn("Fid %u used but not defined\n", file_num);
3998 0 sv_setsv(fid_info_rvav, &PL_sv_no);
3999 }
4000 }
4001  
4002 280965832 ▶ if (trace_level >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 8) { 0 280965832 280965832
4003 0 const char *new_file_name = "";
4004 0 ▶ if (file_num != state->last_file_num && SvROK(fid_info_rvav))
Branch: 2 branches
BranchTrueFalseTotal
if (file_num != state->last_file_num && SvROK(fid_info_rvav)) 0 0 0
if (file_num != state->last_file_num && SvROK(fid_info_rvav)) 0 0 0
4005 0 new_file_name = SvPV_nolen(*av_fetch((AV *)SvRV(fid_info_rvav), NYTP_FIDi_FILENAME, 1));
4006 0 logwarn("Read %d:%-4d %2ld ticks%s %s\n",
4007 file_num, line_num, (long)ticks, trace_note, new_file_name);
4008 }
4009  
4010 280965832 add_entry(aTHX_ state->fid_line_time_av, file_num, line_num,
4011 seconds, eval_file_num, eval_line_num,
4012 280965832 1 - state->statement_discount
4013 );
4014  
4015 280965832 ▶ if (tag == nytp_time_block) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag == nytp_time_block) { 280965832 0 280965832
4016 280965832 unsigned int block_line_num = va_arg(args, unsigned int);
4017 280965832 unsigned int sub_line_num = va_arg(args, unsigned int);
4018  
4019 280965832 ▶ if (!state->fid_block_time_av)
Branch: 1 branch
BranchTrueFalseTotal
if (!state->fid_block_time_av) 928 280964904 280965832
4020 928 state->fid_block_time_av = newAV();
4021 280965832 add_entry(aTHX_ state->fid_block_time_av, file_num, block_line_num,
4022 seconds, eval_file_num, eval_line_num,
4023 280965832 1 - state->statement_discount
4024 );
4025  
4026 280965832 ▶ if (!state->fid_sub_time_av)
Branch: 1 branch
BranchTrueFalseTotal
if (!state->fid_sub_time_av) 928 280964904 280965832
4027 928 state->fid_sub_time_av = newAV();
4028 280965832 add_entry(aTHX_ state->fid_sub_time_av, file_num, sub_line_num,
4029 seconds, eval_file_num, eval_line_num,
4030 280965832 1 - state->statement_discount
4031 );
4032  
4033 280965832 ▶ if (trace_level >= 8)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 8) 0 280965832 280965832
4034 0 logwarn("\tblock %u, sub %u\n", block_line_num, sub_line_num);
4035 }
4036  
4037 280965832 va_end(args);
4038  
4039 280965832 state->total_stmts_measured++;
4040 280965832 state->total_stmts_duration += seconds;
4041 280965832 state->statement_discount = 0;
4042 280965832 state->last_file_num = file_num;
4043 280965832 state->last_line_num = line_num;
4044 280965832 }
4045  
4046 static void
4047 25901 load_new_fid_callback(Loader_state_base *cb_data, const int tag, ...)
4048 {
4049 25901 Loader_state_profiler *state = (Loader_state_profiler *)cb_data;
4050 dTHXa(state->interp);
4051 va_list args;
4052 AV *av;
4053 SV *rv;
4054 SV **svp;
4055 SV *filename_sv;
4056 unsigned int file_num;
4057 unsigned int eval_file_num;
4058 unsigned int eval_line_num;
4059 unsigned int fid_flags;
4060 unsigned int file_size;
4061 unsigned int file_mtime;
4062  
4063 25901 va_start(args, tag);
4064  
4065 25901 file_num = va_arg(args, unsigned int);
4066 25901 eval_file_num = va_arg(args, unsigned int);
4067 25901 eval_line_num = va_arg(args, unsigned int);
4068 25901 fid_flags = va_arg(args, unsigned int);
4069 25901 file_size = va_arg(args, unsigned int);
4070 25901 file_mtime = va_arg(args, unsigned int);
4071 25901 filename_sv = va_arg(args, SV *);
4072  
4073 25901 va_end(args);
4074  
4075 25901 ▶ if (trace_level >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 2) { 0 25901 25901
4076 char buf[80];
4077 char parent_fid[80];
4078 0 ▶ if (eval_file_num || eval_line_num)
Branch: 2 branches
BranchTrueFalseTotal
if (eval_file_num || eval_line_num) 0 0 0
if (eval_file_num || eval_line_num) 0 0 0
4079 0 sprintf(parent_fid, " (is eval at %u:%u)", eval_file_num, eval_line_num);
4080 else
4081 0 sprintf(parent_fid, " (file sz%d mt%d)", file_size, file_mtime);
4082  
4083 0 logwarn("Fid %2u is %s%s 0x%x(%s)\n",
4084 file_num, SvPV_nolen(filename_sv), parent_fid,
4085 fid_flags, fmt_fid_flags(aTHX_ fid_flags, buf, sizeof(buf)));
4086 }
4087  
4088 /* [ name, eval_file_num, eval_line_num, fid, flags, size, mtime, ... ]
4089 */
4090 25901 av = newAV();
4091 25901 rv = newRV_noinc((SV*)av);
4092 25901 sv_bless(rv, state->file_info_stash);
4093  
4094 25901 svp = av_fetch(state->fid_fileinfo_av, file_num, 1);
4095 25901 ▶ if (SvOK(*svp)) { /* should never happen, perhaps file is corrupt */
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(*svp)) { /* should never happen, perhaps file is corrupt */ 0 25901 25901
4096 0 AV *old_av = (AV *)SvRV(*av_fetch(state->fid_fileinfo_av, file_num, 1));
4097 0 SV *old_name = *av_fetch(old_av, 0, 1);
4098 0 ▶ logwarn("Fid %d redefined from %s to %s\n", file_num,
Branch: 1 branch
BranchTrueFalseTotal
logwarn("Fid %d redefined from %s to %s\n", file_num, 0 0 0
4099 SvPV_nolen(old_name), SvPV_nolen(filename_sv));
4100 }
4101 25901 sv_setsv(*svp, rv);
4102  
4103 25901 av_store(av, NYTP_FIDi_FILENAME, filename_sv); /* av now owns the sv */
4104 25901 ▶ if (eval_file_num) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval_file_num) { 6831 19070 25901
4105 SV *has_evals;
4106 /* this eval fid refers to the fid that contained the eval */
4107 6831 SV *eval_fi = *av_fetch(state->fid_fileinfo_av, eval_file_num, 1);
4108 6831 ▶ if (!SvROK(eval_fi)) { /* should never happen */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(eval_fi)) { /* should never happen */ 0 6831 6831
4109 char buf[80];
4110 0 logwarn("Eval '%s' (fid %d, flags:%s) has unknown invoking fid %d\n",
4111 SvPV_nolen(filename_sv), file_num,
4112 fmt_fid_flags(aTHX_ fid_flags, buf, sizeof(buf)), eval_file_num);
4113 /* so make it look like a real file instead of an eval */
4114 0 av_store(av, NYTP_FIDi_EVAL_FI, NULL);
4115 0 eval_file_num = 0;
4116 0 eval_line_num = 0;
4117 }
4118 else {
4119 6831 av_store(av, NYTP_FIDi_EVAL_FI, sv_rvweaken(newSVsv(eval_fi)));
4120 /* the fid that contained the eval has a list of eval fids */
4121 6831 has_evals = *av_fetch((AV *)SvRV(eval_fi), NYTP_FIDi_HAS_EVALS, 1);
4122 6831 ▶ if (!SvROK(has_evals)) /* autoviv */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(has_evals)) /* autoviv */ 2279 4552 6831
4123 2279 sv_setsv(has_evals, newRV_noinc((SV*)newAV()));
4124 6831 av_push((AV *)SvRV(has_evals), sv_rvweaken(newSVsv(rv)));
4125 }
4126 }
4127 else {
4128 19070 av_store(av, NYTP_FIDi_EVAL_FI, NULL);
4129 }
4130 25901 ▶ av_store(av, NYTP_FIDi_EVAL_FID, (eval_file_num) ? newSVuv(eval_file_num) : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
av_store(av, NYTP_FIDi_EVAL_FID, (eval_file_num) ? newSVuv(eval_file_num) : &PL_sv_no); 6831 19070 25901
4131 25901 ▶ av_store(av, NYTP_FIDi_EVAL_LINE, (eval_file_num) ? newSVuv(eval_line_num) : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
av_store(av, NYTP_FIDi_EVAL_LINE, (eval_file_num) ? newSVuv(eval_line_num) : &PL_sv_no); 6831 19070 25901
4132 25901 av_store(av, NYTP_FIDi_FID, newSVuv(file_num));
4133 25901 av_store(av, NYTP_FIDi_FLAGS, newSVuv(fid_flags));
4134 25901 av_store(av, NYTP_FIDi_FILESIZE, newSVuv(file_size));
4135 25901 av_store(av, NYTP_FIDi_FILEMTIME, newSVuv(file_mtime));
4136 25901 av_store(av, NYTP_FIDi_PROFILE, NULL);
4137 25901 av_store(av, NYTP_FIDi_HAS_EVALS, NULL);
4138 25901 av_store(av, NYTP_FIDi_SUBS_DEFINED, newRV_noinc((SV*)newHV()));
4139 25901 av_store(av, NYTP_FIDi_SUBS_CALLED, newRV_noinc((SV*)newHV()));
4140 25901 }
4141  
4142 static void
4143 4948333 load_src_line_callback(Loader_state_base *cb_data, const int tag, ...)
4144 {
4145 4948333 Loader_state_profiler *state = (Loader_state_profiler *)cb_data;
4146 dTHXa(state->interp);
4147 va_list args;
4148 unsigned int file_num;
4149 unsigned int line_num;
4150 SV *src;
4151 AV *file_av;
4152  
4153 4948333 va_start(args, tag);
4154  
4155 4948333 file_num = va_arg(args, unsigned int);
4156 4948333 line_num = va_arg(args, unsigned int);
4157 4948333 src = va_arg(args, SV *);
4158  
4159 4948333 va_end(args);
4160  
4161 /* first line in the file seen */
4162 4948333 ▶ if (!av_exists(state->fid_srclines_av, file_num)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!av_exists(state->fid_srclines_av, file_num)) { 12906 4935427 4948333
4163 12906 file_av = newAV();
4164 12906 av_store(state->fid_srclines_av, file_num, newRV_noinc((SV*)file_av));
4165 }
4166 else {
4167 4935427 file_av = (AV *)SvRV(*av_fetch(state->fid_srclines_av, file_num, 1));
4168 }
4169
4170 4948333 av_store(file_av, line_num, src);
4171  
4172 4948333 ▶ if (trace_level >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 8) { 0 4948333 4948333
4173 0 logwarn("Fid %2u:%u src: %s\n", file_num, line_num, SvPV_nolen(src));
4174 }
4175 4948333 }
4176  
4177 static void
4178 9492 load_sub_info_callback(Loader_state_base *cb_data, const int tag, ...)
4179 {
4180 9492 Loader_state_profiler *state = (Loader_state_profiler *)cb_data;
4181 dTHXa(state->interp);
4182 va_list args;
4183 unsigned int fid;
4184 unsigned int first_line;
4185 unsigned int last_line;
4186 SV *subname_sv;
4187 9492 int skip_subinfo_store = 0;
4188 STRLEN subname_len;
4189 char *subname_pv;
4190 AV *av;
4191 SV *sv;
4192  
4193 9492 va_start(args, tag);
4194  
4195 9492 fid = va_arg(args, unsigned int);
4196 9492 first_line = va_arg(args, unsigned int);
4197 9492 last_line = va_arg(args, unsigned int);
4198 9492 subname_sv = va_arg(args, SV *);
4199  
4200 9492 va_end(args);
4201  
4202 9492 normalize_eval_seqn(aTHX_ subname_sv);
4203  
4204 9492 subname_pv = SvPV(subname_sv, subname_len);
4205 9492 ▶ if (trace_level >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 2) 0 9492 9492
4206 0 logwarn("Sub %s fid %u lines %u..%u\n",
4207 subname_pv, fid, first_line, last_line);
4208  
4209 9492 av = lookup_subinfo_av(aTHX_ subname_sv, state->sub_subinfo_hv);
4210 9492 ▶ if (SvOK(*av_fetch(av, NYTP_SIi_FID, 1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(*av_fetch(av, NYTP_SIi_FID, 1))) { 0 9492 9492
4211 /* We've already seen this subroutine name.
4212 * Should only happen for anon subs in string evals so we warn
4213 * for other cases.
4214 */
4215 0 ▶ if (!instr(subname_pv, "__ANON__[(eval"))
Branch: 1 branch
BranchTrueFalseTotal
if (!instr(subname_pv, "__ANON__[(eval")) 0 0 0
4216 0 logwarn("Sub %s already defined!\n", subname_pv);
4217  
4218 /* We could always discard the fid+first_line+last_line here,
4219 * because we already have them stored, but for consistency
4220 * (and for the stability of the tests) we'll prefer the lowest fid
4221 */
4222 0 ▶ if (fid > SvUV(*av_fetch(av, NYTP_SIi_FID, 1)))
Branch: 1 branch
BranchTrueFalseTotal
if (fid > SvUV(*av_fetch(av, NYTP_SIi_FID, 1))) 0 0 0
4223 0 skip_subinfo_store = 1;
4224  
4225 /* Finally, note that the fileinfo NYTP_FIDi_SUBS_DEFINED hash,
4226 * updated below, does get an entry for the sub *from each fid*
4227 * (ie string eval) that defines the subroutine.
4228 */
4229 }
4230 9492 ▶ if (!skip_subinfo_store) {
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_subinfo_store) { 9492 0 9492
4231 9492 sv_setuv(*av_fetch(av, NYTP_SIi_FID, 1), fid);
4232 9492 sv_setuv(*av_fetch(av, NYTP_SIi_FIRST_LINE, 1), first_line);
4233 9492 sv_setuv(*av_fetch(av, NYTP_SIi_LAST_LINE, 1), last_line);
4234 }
4235  
4236 /* add sub to NYTP_FIDi_SUBS_DEFINED hash */
4237 9492 sv = SvRV(*av_fetch(state->fid_fileinfo_av, fid, 1));
4238 9492 sv = SvRV(*av_fetch((AV *)sv, NYTP_FIDi_SUBS_DEFINED, 1));
4239 9492 (void)hv_store((HV *)sv, subname_pv, (I32)subname_len, newRV_inc((SV*)av), 0);
4240 9492 }
4241  
4242 static void
4243 572824 load_sub_callers_callback(Loader_state_base *cb_data, const int tag, ...)
4244 {
4245 572824 Loader_state_profiler *state = (Loader_state_profiler *)cb_data;
4246 dTHXa(state->interp);
4247 va_list args;
4248 unsigned int fid;
4249 unsigned int line;
4250 SV *caller_subname_sv;
4251 unsigned int count;
4252 NV incl_time;
4253 NV excl_time;
4254 NV reci_time;
4255 unsigned int rec_depth;
4256 SV *called_subname_sv;
4257 char text[MAXPATHLEN*2];
4258 SV *sv;
4259 AV *subinfo_av;
4260 int len;
4261  
4262 572824 va_start(args, tag);
4263  
4264 572824 fid = va_arg(args, unsigned int);
4265 572824 line = va_arg(args, unsigned int);
4266 572824 count = va_arg(args, unsigned int);
4267 572824 incl_time = va_arg(args, NV);
4268 572824 excl_time = va_arg(args, NV);
4269 572824 reci_time = va_arg(args, NV);
4270 572824 rec_depth = va_arg(args, unsigned int);
4271 572824 called_subname_sv = va_arg(args, SV *);
4272 572824 caller_subname_sv = va_arg(args, SV *);
4273  
4274 572824 va_end(args);
4275  
4276 572824 normalize_eval_seqn(aTHX_ caller_subname_sv);
4277 572824 normalize_eval_seqn(aTHX_ called_subname_sv);
4278  
4279 572824 ▶ if (trace_level >= 6)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 6) 0 572824 572824
4280 0 ▶ logwarn("Sub %s called by %s %u:%u: count %d, incl %" NVff ", excl %" NVff "\n",
Branch: 1 branch
BranchTrueFalseTotal
logwarn("Sub %s called by %s %u:%u: count %d, incl %" NVff ", excl %" NVff "\n", 0 0 0
4281 SvPV_nolen(called_subname_sv), SvPV_nolen(caller_subname_sv),
4282 fid, line, count, incl_time, excl_time);
4283  
4284 572824 subinfo_av = lookup_subinfo_av(aTHX_ called_subname_sv, state->sub_subinfo_hv);
4285  
4286 /* subinfo_av's NYTP_SIi_CALLED_BY element is a hash ref:
4287 * { caller_fid => { caller_line => [ count, incl_time, ... ] } }
4288 */
4289 572824 sv = *av_fetch(subinfo_av, NYTP_SIi_CALLED_BY, 1);
4290 572824 ▶ if (!SvROK(sv)) /* autoviv */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) /* autoviv */ 278507 294317 572824
4291 278507 sv_setsv(sv, newRV_noinc((SV*)newHV()));
4292  
4293 572824 len = sprintf(text, "%u", fid);
4294 572824 sv = *hv_fetch((HV*)SvRV(sv), text, len, 1);
4295 572824 ▶ if (!SvROK(sv)) /* autoviv */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) /* autoviv */ 423878 148946 572824
4296 423878 sv_setsv(sv, newRV_noinc((SV*)newHV()));
4297  
4298 /* XXX gets called with fid=0 to indicate is_xsub
4299 * That's a hack that should be removed once we have per-sub flags
4300 */
4301 572824 ▶ if (fid) {
Branch: 1 branch
BranchTrueFalseTotal
if (fid) { 458469 114355 572824
4302 SV *fi;
4303 AV *av;
4304 458469 len = sprintf(text, "%u", line);
4305  
4306 458469 sv = *hv_fetch((HV*)SvRV(sv), text, len, 1);
4307 458469 ▶ if (!SvROK(sv)) /* autoviv */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) /* autoviv */ 457379 1090 458469
4308 457379 sv_setsv(sv, newRV_noinc((SV*)newAV()));
4309 1090 ▶ else if (trace_level)
Branch: 1 branch
BranchTrueFalseTotal
else if (trace_level) 0 1090 1090
4310 /* calls to sub1 from the same fid:line could have different caller
4311 * subs due to evals or if profile_findcaller is off.
4312 */
4313 0 logwarn("Merging extra sub caller info for %s called at %d:%d\n",
4314 SvPV_nolen(called_subname_sv), fid, line);
4315  
4316 458469 av = (AV *)SvRV(sv);
4317 458469 sv = *av_fetch(av, NYTP_SCi_CALL_COUNT, 1);
4318 458469 ▶ sv_setuv(sv, (SvOK(sv)) ? SvUV(sv) + count : count);
Branch: 1 branch
BranchTrueFalseTotal
sv_setuv(sv, (SvOK(sv)) ? SvUV(sv) + count : count); 1090 457379 458469
4319 458469 sv = *av_fetch(av, NYTP_SCi_INCL_RTIME, 1);
4320 458469 ▶ sv_setnv(sv, (SvOK(sv)) ? SvNV(sv) + incl_time : incl_time);
Branch: 1 branch
BranchTrueFalseTotal
sv_setnv(sv, (SvOK(sv)) ? SvNV(sv) + incl_time : incl_time); 1090 457379 458469
4321 458469 sv = *av_fetch(av, NYTP_SCi_EXCL_RTIME, 1);
4322 458469 ▶ sv_setnv(sv, (SvOK(sv)) ? SvNV(sv) + excl_time : excl_time);
Branch: 1 branch
BranchTrueFalseTotal
sv_setnv(sv, (SvOK(sv)) ? SvNV(sv) + excl_time : excl_time); 1090 457379 458469
4323 458469 sv = *av_fetch(av, NYTP_SCi_INCL_TICKS, 1);
4324 458469 sv_setnv(sv, 0.0);
4325 458469 sv = *av_fetch(av, NYTP_SCi_EXCL_TICKS, 1);
4326 458469 sv_setnv(sv, 0.0);
4327 458469 sv = *av_fetch(av, NYTP_SCi_RECI_RTIME, 1);
4328 458469 ▶ sv_setnv(sv, (SvOK(sv)) ? SvNV(sv) + reci_time : reci_time);
Branch: 1 branch
BranchTrueFalseTotal
sv_setnv(sv, (SvOK(sv)) ? SvNV(sv) + reci_time : reci_time); 1090 457379 458469
4329 458469 sv = *av_fetch(av, NYTP_SCi_REC_DEPTH, 1);
4330 458469 ▶ if (!SvOK(sv) || SvUV(sv) < rec_depth) /* max() */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(sv) || SvUV(sv) < rec_depth) /* max() */ 1090 457379 458469
if (!SvOK(sv) || SvUV(sv) < rec_depth) /* max() */ 0 1090 1090
4331 457379 sv_setuv(sv, rec_depth);
4332 /* XXX temp hack way to store calling subname as key with undef value */
4333 /* ideally we should assign ids to subs (sid) the way we do with files (fid) */
4334 458469 sv = *av_fetch(av, NYTP_SCi_CALLING_SUB, 1);
4335 458469 ▶ if (!SvROK(sv)) /* autoviv */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) /* autoviv */ 457379 1090 458469
4336 457379 sv_setsv(sv, newRV_noinc((SV*)newHV()));
4337 458469 (void)hv_fetch_ent((HV *)SvRV(sv), caller_subname_sv, 1, 0);
4338  
4339 /* also reference this sub call info array from the calling fileinfo
4340 * fi->[NYTP_FIDi_SUBS_CALLED] => { line => { subname => [ ... ] } }
4341 */
4342 458469 fi = SvRV(*av_fetch(state->fid_fileinfo_av, fid, 1));
4343 458469 fi = *av_fetch((AV *)fi, NYTP_FIDi_SUBS_CALLED, 1);
4344 458469 fi = *hv_fetch((HV*)SvRV(fi), text, len, 1);
4345 458469 ▶ if (!SvROK(fi)) /* autoviv */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(fi)) /* autoviv */ 352097 106372 458469
4346 352097 sv_setsv(fi, newRV_noinc((SV*)newHV()));
4347 458469 fi = HeVAL(hv_fetch_ent((HV *)SvRV(fi), called_subname_sv, 1, 0));
4348 if (1) { /* ref a clone of the sub call info array */
4349 458469 AV *av2 = av_make(AvFILL(av)+1, AvARRAY(av));
4350 458469 av = av2;
4351 }
4352 458469 sv_setsv(fi, newRV_inc((SV *)av));
4353 }
4354 else { /* is meta-data about sub */
4355 /* line == 0: is_xs - set line range to 0,0 as marker */
4356 114355 sv_setiv(*av_fetch(subinfo_av, NYTP_SIi_FIRST_LINE, 1), 0);
4357 114355 sv_setiv(*av_fetch(subinfo_av, NYTP_SIi_LAST_LINE, 1), 0);
4358 }
4359  
4360 /* accumulate per-sub totals into subinfo */
4361 572824 sv = *av_fetch(subinfo_av, NYTP_SIi_CALL_COUNT, 1);
4362 572824 ▶ sv_setuv(sv, count + (SvOK(sv) ? SvUV(sv) : 0));
Branch: 1 branch
BranchTrueFalseTotal
sv_setuv(sv, count + (SvOK(sv) ? SvUV(sv) : 0)); 572824 0 572824
4363 572824 sv = *av_fetch(subinfo_av, NYTP_SIi_INCL_RTIME, 1);
4364 572824 ▶ sv_setnv(sv, incl_time + (SvOK(sv) ? SvNV(sv) : 0.0));
Branch: 1 branch
BranchTrueFalseTotal
sv_setnv(sv, incl_time + (SvOK(sv) ? SvNV(sv) : 0.0)); 572824 0 572824
4365 572824 sv = *av_fetch(subinfo_av, NYTP_SIi_EXCL_RTIME, 1);
4366 572824 ▶ sv_setnv(sv, excl_time + (SvOK(sv) ? SvNV(sv) : 0.0));
Branch: 1 branch
BranchTrueFalseTotal
sv_setnv(sv, excl_time + (SvOK(sv) ? SvNV(sv) : 0.0)); 572824 0 572824
4367 /* sub rec_depth - record the maximum */
4368 572824 sv = *av_fetch(subinfo_av, NYTP_SIi_REC_DEPTH, 1);
4369 572824 ▶ if (!SvOK(sv) || rec_depth > SvUV(sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(sv) || rec_depth > SvUV(sv)) 572824 0 572824
if (!SvOK(sv) || rec_depth > SvUV(sv)) 4043 568781 572824
4370 4043 sv_setuv(sv, rec_depth);
4371 572824 sv = *av_fetch(subinfo_av, NYTP_SIi_RECI_RTIME, 1);
4372 572824 ▶ sv_setnv(sv, reci_time + (SvOK(sv) ? SvNV(sv) : 0.0));
Branch: 1 branch
BranchTrueFalseTotal
sv_setnv(sv, reci_time + (SvOK(sv) ? SvNV(sv) : 0.0)); 572824 0 572824
4373  
4374 572824 state->total_sub_calls += count;
4375 572824 }
4376  
4377 static void
4378 948 load_pid_start_callback(Loader_state_base *cb_data, const int tag, ...)
4379 {
4380 948 Loader_state_profiler *state = (Loader_state_profiler *)cb_data;
4381 dTHXa(state->interp);
4382 va_list args;
4383 unsigned int pid;
4384 unsigned int ppid;
4385 NV start_time;
4386 char text[MAXPATHLEN*2];
4387 int len;
4388  
4389 948 va_start(args, tag);
4390  
4391 948 pid = va_arg(args, unsigned int);
4392 948 ppid = va_arg(args, unsigned int);
4393 948 start_time = va_arg(args, NV);
4394  
4395 948 va_end(args);
4396  
4397 948 state->profiler_start_time = start_time;
4398  
4399 948 len = sprintf(text, "%d", pid);
4400 948 (void)hv_store(state->live_pids_hv, text, len, newSVuv(ppid), 0);
4401 948 ▶ if (trace_level)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level) 0 948 948
4402 0 logwarn("Start of profile data for pid %s (ppid %d, %" IVdf " pids live) at %" NVff "\n",
4403 0 ▶ text, ppid, (IV)HvKEYS(state->live_pids_hv), start_time);
Branch: 1 branch
BranchTrueFalseTotal
text, ppid, (IV)HvKEYS(state->live_pids_hv), start_time); 0 0 0
4404  
4405 948 store_attrib_sv(aTHX_ state->attr_hv, STR_WITH_LEN("profiler_start_time"),
4406 newSVnv(start_time));
4407 948 }
4408  
4409 static void
4410 948 load_pid_end_callback(Loader_state_base *cb_data, const int tag, ...)
4411 {
4412 948 Loader_state_profiler *state = (Loader_state_profiler *)cb_data;
4413 dTHXa(state->interp);
4414 va_list args;
4415 unsigned int pid;
4416 NV end_time;
4417 char text[MAXPATHLEN*2];
4418 int len;
4419  
4420 948 va_start(args, tag);
4421  
4422 948 pid = va_arg(args, unsigned int);
4423 948 end_time = va_arg(args, NV);
4424  
4425 948 va_end(args);
4426  
4427 948 state->profiler_end_time = end_time;
4428  
4429 948 len = sprintf(text, "%d", pid);
4430 948 ▶ if (!hv_delete(state->live_pids_hv, text, len, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_delete(state->live_pids_hv, text, len, 0)) 0 948 948
4431 0 logwarn("Inconsistent pids in profile data (pid %d not introduced)\n",
4432 pid);
4433 948 ▶ if (trace_level)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level) 0 948 948
4434 0 logwarn("End of profile data for pid %s (%" IVdf " remaining) at %" NVff "\n", text,
4435 0 ▶ (IV)HvKEYS(state->live_pids_hv), state->profiler_end_time);
Branch: 1 branch
BranchTrueFalseTotal
(IV)HvKEYS(state->live_pids_hv), state->profiler_end_time); 0 0 0
4436  
4437 948 store_attrib_sv(aTHX_ state->attr_hv, STR_WITH_LEN("profiler_end_time"),
4438 newSVnv(end_time));
4439 948 state->profiler_duration += state->profiler_end_time - state->profiler_start_time;
4440 948 store_attrib_sv(aTHX_ state->attr_hv, STR_WITH_LEN("profiler_duration"),
4441 newSVnv(state->profiler_duration));
4442  
4443 948 }
4444  
4445 static void
4446 8532 load_attribute_callback(Loader_state_base *cb_data, const int tag, ...)
4447 {
4448 8532 Loader_state_profiler *state = (Loader_state_profiler *)cb_data;
4449 dTHXa(state->interp);
4450 va_list args;
4451 char *key;
4452 unsigned long key_len;
4453 unsigned int key_utf8;
4454 char *value;
4455 unsigned long value_len;
4456 unsigned int value_utf8;
4457  
4458 8532 va_start(args, tag);
4459  
4460 8532 key = va_arg(args, char *);
4461 8532 key_len = va_arg(args, unsigned long);
4462 8532 key_utf8 = va_arg(args, unsigned int);
4463  
4464 8532 value = va_arg(args, char *);
4465 8532 value_len = va_arg(args, unsigned long);
4466 8532 value_utf8 = va_arg(args, unsigned int);
4467  
4468 8532 va_end(args);
4469  
4470 8532 ▶ store_attrib_sv(aTHX_ state->attr_hv, key,
Branch: 2 branches
BranchTrueFalseTotal
store_attrib_sv(aTHX_ state->attr_hv, key, 0 8532 8532
store_attrib_sv(aTHX_ state->attr_hv, key, 0 8532 8532
4471 0 key_utf8 ? -(I32)key_len : key_len,
4472 newSVpvn_flags(value, value_len,
4473 value_utf8 ? SVf_UTF8 : 0));
4474 8532 }
4475  
4476 static void
4477 17064 load_option_callback(Loader_state_base *cb_data, const int tag, ...)
4478 {
4479 17064 Loader_state_profiler *state = (Loader_state_profiler *)cb_data;
4480 dTHXa(state->interp);
4481 va_list args;
4482 char *key;
4483 unsigned long key_len;
4484 unsigned int key_utf8;
4485 char *value;
4486 unsigned long value_len;
4487 unsigned int value_utf8;
4488 SV *value_sv;
4489  
4490 17064 va_start(args, tag);
4491  
4492 17064 key = va_arg(args, char *);
4493 17064 key_len = va_arg(args, unsigned long);
4494 17064 key_utf8 = va_arg(args, unsigned int);
4495  
4496 17064 value = va_arg(args, char *);
4497 17064 value_len = va_arg(args, unsigned long);
4498 17064 value_utf8 = va_arg(args, unsigned int);
4499  
4500 17064 va_end(args);
4501  
4502 17064 ▶ value_sv = newSVpvn_flags(value, value_len, value_utf8 ? SVf_UTF8 : 0);
Branch: 1 branch
BranchTrueFalseTotal
value_sv = newSVpvn_flags(value, value_len, value_utf8 ? SVf_UTF8 : 0); 0 17064 17064
4503 17064 ▶ (void)hv_store(state->option_hv, key, key_utf8 ? -(I32)key_len : key_len, value_sv, 0);
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store(state->option_hv, key, key_utf8 ? -(I32)key_len : key_len, value_sv, 0); 0 17064 17064
4504 17064 ▶ if (trace_level >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) 0 17064 17064
4505 0 logwarn("! %.*s = '%s'\n", (int) key_len, key, SvPV_nolen(value_sv));
4506 17064 }
4507  
4508 struct perl_callback_info_t {
4509 const char *description;
4510 STRLEN len;
4511 const char *args;
4512 };
4513  
4514 static struct perl_callback_info_t callback_info[nytp_tag_max] =
4515 {
4516 {STR_WITH_LEN("[no tag]"), NULL},
4517 {STR_WITH_LEN("VERSION"), "uu"},
4518 {STR_WITH_LEN("ATTRIBUTE"), "33"},
4519 {STR_WITH_LEN("OPTION"), "33"},
4520 {STR_WITH_LEN("COMMENT"), "3"},
4521 {STR_WITH_LEN("TIME_BLOCK"), "iuuuu"},
4522 {STR_WITH_LEN("TIME_LINE"), "iuu"},
4523 {STR_WITH_LEN("DISCOUNT"), ""},
4524 {STR_WITH_LEN("NEW_FID"), "uuuuuuS"},
4525 {STR_WITH_LEN("SRC_LINE"), "uuS"},
4526 {STR_WITH_LEN("SUB_INFO"), "uuus"},
4527 {STR_WITH_LEN("SUB_CALLERS"), "uuunnnuss"},
4528 {STR_WITH_LEN("PID_START"), "uun"},
4529 {STR_WITH_LEN("PID_END"), "un"},
4530 {STR_WITH_LEN("[string]"), NULL},
4531 {STR_WITH_LEN("[string utf8]"), NULL},
4532 {STR_WITH_LEN("START_DEFLATE"), ""},
4533 {STR_WITH_LEN("SUB_ENTRY"), "uu"},
4534 {STR_WITH_LEN("SUB_RETURN"), "unns"}
4535 };
4536  
4537 static void
4538 215430382 load_perl_callback(Loader_state_base *cb_data, const int tag, ...)
4539 {
4540 215430382 Loader_state_callback *state = (Loader_state_callback *)cb_data;
4541 dTHXa(state->interp);
4542 215430382 dSP;
4543 va_list args;
4544 215430382 SV **cb_args = state->cb_args;
4545 215430382 int i = 0;
4546 char type;
4547 215430382 const char *arglist = callback_info[tag].args;
4548 215430382 const char *const description = callback_info[tag].description;
4549  
4550 215430382 ▶ if (!arglist) {
Branch: 1 branch
BranchTrueFalseTotal
if (!arglist) { 0 215430382 215430382
4551 0 ▶ if (description)
Branch: 1 branch
BranchTrueFalseTotal
if (description) 0 0 0
4552 0 croak("Type '%s' passed to perl callback incorrectly", description);
4553 else
4554 0 croak("Unknown type %d passed to perl callback", tag);
4555 }
4556  
4557 215430382 ▶ if (!state->cb[tag])
Branch: 1 branch
BranchTrueFalseTotal
if (!state->cb[tag]) 143679664 71750718 215430382
4558 143679664 return;
4559  
4560 71750718 ▶ if (trace_level >= 9) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 9) { 0 71750718 71750718
4561 0 logwarn("\tcallback %s[%s] \n", description, arglist);
4562 }
4563  
4564 71750718 sv_setuv_mg(state->input_chunk_seqn_sv, state->base_state.input_chunk_seqn);
4565  
4566 71750718 va_start(args, tag);
4567  
4568 71750718 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 71750718 71750718
4569  
4570 71750718 ▶ XPUSHs(state->tag_names[tag]);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(state->tag_names[tag]); 0 71750718 71750718
4571  
4572 358133720 ▶ while ((type = *arglist++)) {
Branch: 1 branch
BranchTrueFalseTotal
while ((type = *arglist++)) { 286383002 71750718 358133720
4573 286383002 switch(type) {
4574 74207940 case 'u':
4575 {
4576 74207940 unsigned int u = va_arg(args, unsigned int);
4577  
4578 74207940 sv_setuv(cb_args[i], u);
4579 74207940 ▶ XPUSHs(cb_args[i++]);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(cb_args[i++]); 0 74207940 74207940
4580 74207940 break;
4581 }
4582 725281 case 'i':
4583 {
4584 725281 I32 i32 = va_arg(args, I32);
4585  
4586 725281 sv_setuv(cb_args[i], i32);
4587 725281 ▶ XPUSHs(cb_args[i++]);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(cb_args[i++]); 0 725281 725281
4588 725281 break;
4589 }
4590 140927433 case 'n':
4591 {
4592 140927433 NV n = va_arg(args, NV);
4593  
4594 140927433 sv_setnv(cb_args[i], n);
4595 140927433 ▶ XPUSHs(cb_args[i++]);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(cb_args[i++]); 0 140927433 140927433
4596 140927433 break;
4597 }
4598 70464839 case 's':
4599 {
4600 70464839 SV *sv = va_arg(args, SV *);
4601  
4602 70464839 sv_setsv(cb_args[i], sv);
4603 70464839 ▶ XPUSHs(cb_args[i++]);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(cb_args[i++]); 0 70464839 70464839
4604 70464839 break;
4605 }
4606 38661 case 'S':
4607 {
4608 38661 SV *sv = va_arg(args, SV *);
4609  
4610 38661 ▶ XPUSHs(sv_2mortal(sv));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(sv)); 0 38661 38661
4611 38661 break;
4612 }
4613 18848 case '3':
4614 {
4615 18848 char *p = va_arg(args, char *);
4616 18848 unsigned long len = va_arg(args, unsigned long);
4617 18848 unsigned int utf8 = va_arg(args, unsigned int);
4618
4619 18848 sv_setpvn(cb_args[i], p, len);
4620 18848 ▶ if (utf8)
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) 0 18848 18848
4621 0 SvUTF8_on(cb_args[i]);
4622 else
4623 18848 SvUTF8_off(cb_args[i]);
4624  
4625 18848 ▶ XPUSHs(cb_args[i++]);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(cb_args[i++]); 0 18848 18848
4626 18848 break;
4627 }
4628  
4629 0 default:
4630 0 croak("Bad type '%c' in perl callback", type);
4631 }
4632 }
4633 71750718 va_end(args);
4634 71750718 ▶ assert(i <= C_ARRAY_LENGTH(state->cb_args));
Branch: 1 branch
BranchTrueFalseTotal
assert(i <= C_ARRAY_LENGTH(state->cb_args)); 0 71750718 71750718
4635  
4636 71750718 PUTBACK;
4637 71750718 call_sv((SV *)state->cb[tag], G_DISCARD);
4638 }
4639  
4640  
4641 static loader_callback perl_callbacks[nytp_tag_max] =
4642 {
4643 0,
4644 load_perl_callback,
4645 load_perl_callback,
4646 load_perl_callback,
4647 load_perl_callback,
4648 load_perl_callback,
4649 load_perl_callback,
4650 load_perl_callback,
4651 load_perl_callback,
4652 load_perl_callback,
4653 load_perl_callback,
4654 load_perl_callback,
4655 load_perl_callback,
4656 load_perl_callback,
4657 load_perl_callback,
4658 load_perl_callback,
4659 load_perl_callback,
4660 load_perl_callback,
4661 load_perl_callback
4662 };
4663 static loader_callback processing_callbacks[nytp_tag_max] =
4664 {
4665 0,
4666 0, /* version */
4667 load_attribute_callback,
4668 load_option_callback,
4669 0, /* comment */
4670 load_time_callback,
4671 load_time_callback,
4672 load_discount_callback,
4673 load_new_fid_callback,
4674 load_src_line_callback,
4675 load_sub_info_callback,
4676 load_sub_callers_callback,
4677 load_pid_start_callback,
4678 load_pid_end_callback,
4679 0, /* string */
4680 0, /* string utf8 */
4681 0, /* sub entry */
4682 0, /* sub return */
4683 0 /* start deflate */
4684 };
4685  
4686 /**
4687 * Process a profile output file and return the results in a hash like
4688 * { fid_fileinfo => [ [file, other...info ], ... ], # index by [fid]
4689 * fid_line_time => [ [...],[...],.. ] # index by [fid][line]
4690 * }
4691 * The value of each [fid][line] is an array ref containing:
4692 * [ number of calls, total time spent ]
4693 * lines containing string evals also get an extra element
4694 * [ number of calls, total time spent, [...] ]
4695 * which is an reference to an array containing the [calls,time]
4696 * data for each line of the string eval.
4697 */
4698 static void
4699 1297 load_profile_data_from_stream(pTHX_ loader_callback *callbacks,
4700 Loader_state_base *state, NYTP_file in)
4701 {
4702 int file_major, file_minor;
4703  
4704 1297 SV *tmp_str1_sv = newSVpvn("",0);
4705 1297 SV *tmp_str2_sv = newSVpvn("",0);
4706  
4707 1297 size_t buffer_len = MAXPATHLEN * 2;
4708 1297 char *buffer = (char *)safemalloc(buffer_len);
4709  
4710 if (1) {
4711 1297 ▶ if (!NYTP_gets(in, &buffer, &buffer_len))
Branch: 1 branch
BranchTrueFalseTotal
if (!NYTP_gets(in, &buffer, &buffer_len)) 0 1297 1297
4712 0 croak("NYTProf data format error while reading header");
4713 1297 ▶ if (2 != sscanf(buffer, "NYTProf %d %d\n", &file_major, &file_minor))
Branch: 1 branch
BranchTrueFalseTotal
if (2 != sscanf(buffer, "NYTProf %d %d\n", &file_major, &file_minor)) 0 1297 1297
4714 0 croak("NYTProf data format error while parsing header");
4715 1297 ▶ if (file_major != NYTP_FILE_MAJOR_VERSION)
Branch: 1 branch
BranchTrueFalseTotal
if (file_major != NYTP_FILE_MAJOR_VERSION) 0 1297 1297
4716 0 croak("NYTProf data format version %d.%d is not supported by NYTProf %s (which expects version %d.%d)",
4717 file_major, file_minor, XS_VERSION, NYTP_FILE_MAJOR_VERSION, NYTP_FILE_MINOR_VERSION);
4718  
4719 1297 ▶ if (file_minor > NYTP_FILE_MINOR_VERSION)
Branch: 1 branch
BranchTrueFalseTotal
if (file_minor > NYTP_FILE_MINOR_VERSION) 0 1297 1297
4720 0 warn("NYTProf data format version %d.%d is newer than that understood by this NYTProf %s, so errors are likely",
4721 file_major, file_minor, XS_VERSION);
4722 }
4723  
4724 1297 ▶ if (callbacks[nytp_version])
Branch: 1 branch
BranchTrueFalseTotal
if (callbacks[nytp_version]) 349 948 1297
4725 349 callbacks[nytp_version](state, nytp_version, file_major, file_minor);
4726  
4727 700621197 while (1) {
4728 /* Loop "forever" until EOF. We can only check the EOF flag *after* we
4729 attempt a read. */
4730 char c;
4731  
4732 700622494 ▶ if (NYTP_read_unchecked(in, &c, sizeof(c)) != sizeof(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (NYTP_read_unchecked(in, &c, sizeof(c)) != sizeof(c)) { 1297 700621197 700622494
4733 1297 ▶ if (NYTP_eof(in))
Branch: 1 branch
BranchTrueFalseTotal
if (NYTP_eof(in)) 1297 0 1297
4734 1297 break;
4735 0 croak("Profile format error '%s' whilst reading tag at %ld (see TROUBLESHOOTING in NYTProf docs)",
4736 NYTP_fstrerror(in), NYTP_tell(in));
4737 }
4738  
4739 700621197 state->input_chunk_seqn++;
4740 700621197 ▶ if (trace_level >= 9)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 9) 0 700621197 700621197
4741 0 logwarn("Chunk %lu token is %d ('%c') at %ld%s\n",
4742 0 state->input_chunk_seqn, c, c, NYTP_tell(in)-1,
4743 NYTP_type_of_offset(in));
4744  
4745 700621197 switch (c) {
4746 21389528 case NYTP_TAG_DISCOUNT:
4747 {
4748 21389528 callbacks[nytp_discount](state, nytp_discount);
4749 21389528 break;
4750 }
4751  
4752 393426154 case NYTP_TAG_TIME_LINE: /*FALLTHRU*/
4753 case NYTP_TAG_TIME_BLOCK:
4754 {
4755 393426154 I32 ticks = read_i32(in);
4756 393426154 unsigned int file_num = read_u32(in);
4757 393426154 unsigned int line_num = read_u32(in);
4758 393426154 unsigned int block_line_num = 0;
4759 393426154 unsigned int sub_line_num = 0;
4760 393426154 nytp_tax_index tag = nytp_time_line;
4761  
4762 393426154 ▶ if (c == NYTP_TAG_TIME_BLOCK) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == NYTP_TAG_TIME_BLOCK) { 393426154 0 393426154
4763 393426154 block_line_num = read_u32(in);
4764 393426154 sub_line_num = read_u32(in);
4765 393426154 tag = nytp_time_block;
4766 }
4767  
4768 /* Because it happens that the two "optional" arguments are
4769 last, a single call will work. */
4770 393426154 callbacks[tag](state, tag, ticks, file_num, line_num,
4771 block_line_num, sub_line_num);
4772 393426154 break;
4773 }
4774  
4775 34503 case NYTP_TAG_NEW_FID: /* file */
4776 {
4777 SV *filename_sv;
4778 34503 unsigned int file_num = read_u32(in);
4779 34503 unsigned int eval_file_num = read_u32(in);
4780 34503 unsigned int eval_line_num = read_u32(in);
4781 34503 unsigned int fid_flags = read_u32(in);
4782 34503 unsigned int file_size = read_u32(in);
4783 34503 unsigned int file_mtime = read_u32(in);
4784  
4785 34503 filename_sv = read_str(aTHX_ in, NULL);
4786  
4787 34503 callbacks[nytp_new_fid](state, nytp_new_fid, file_num,
4788 eval_file_num, eval_line_num,
4789 fid_flags, file_size, file_mtime,
4790 filename_sv);
4791 34503 break;
4792 }
4793  
4794 7357566 case NYTP_TAG_SRC_LINE:
4795 {
4796 7357566 unsigned int file_num = read_u32(in);
4797 7357566 unsigned int line_num = read_u32(in);
4798 7357566 SV *src = read_str(aTHX_ in, NULL);
4799  
4800 7357566 callbacks[nytp_src_line](state, nytp_src_line, file_num,
4801 line_num, src);
4802 7357566 break;
4803 }
4804  
4805 69416113 case NYTP_TAG_SUB_ENTRY:
4806 {
4807 69416113 unsigned int file_num = read_u32(in);
4808 69416113 unsigned int line_num = read_u32(in);
4809  
4810 69416113 ▶ if (callbacks[nytp_sub_entry])
Branch: 1 branch
BranchTrueFalseTotal
if (callbacks[nytp_sub_entry]) 23750904 45665209 69416113
4811 23750904 callbacks[nytp_sub_entry](state, nytp_sub_entry, file_num, line_num);
4812 69416113 break;
4813 }
4814  
4815 208171002 case NYTP_TAG_SUB_RETURN:
4816 {
4817 208171002 unsigned int depth = read_u32(in);
4818 208171002 NV incl_time = read_nv(in);
4819 208171002 NV excl_time = read_nv(in);
4820 208171002 SV *subname = read_str(aTHX_ in, tmp_str1_sv);
4821  
4822 208171002 ▶ if (callbacks[nytp_sub_return])
Branch: 1 branch
BranchTrueFalseTotal
if (callbacks[nytp_sub_return]) 70460345 137710657 208171002
4823 70460345 callbacks[nytp_sub_return](state, nytp_sub_return, depth, incl_time, excl_time, subname);
4824 208171002 break;
4825 }
4826  
4827 10956 case NYTP_TAG_SUB_INFO:
4828 {
4829 10956 unsigned int fid = read_u32(in);
4830 10956 SV *subname_sv = read_str(aTHX_ in, tmp_str1_sv);
4831 10956 unsigned int first_line = read_u32(in);
4832 10956 unsigned int last_line = read_u32(in);
4833  
4834 10956 callbacks[nytp_sub_info](state, nytp_sub_info, fid,
4835 first_line, last_line, subname_sv);
4836 10956 break;
4837 }
4838  
4839 775001 case NYTP_TAG_SUB_CALLERS:
4840 {
4841 775001 unsigned int fid = read_u32(in);
4842 775001 unsigned int line = read_u32(in);
4843 775001 SV *caller_subname_sv = read_str(aTHX_ in, tmp_str2_sv);
4844 775001 unsigned int count = read_u32(in);
4845 775001 NV incl_time = read_nv(in);
4846 775001 NV excl_time = read_nv(in);
4847 775001 NV reci_time = read_nv(in);
4848 775001 unsigned int rec_depth = read_u32(in);
4849 775001 SV *called_subname_sv = read_str(aTHX_ in, tmp_str1_sv);
4850  
4851 775001 callbacks[nytp_sub_callers](state, nytp_sub_callers, fid,
4852 line, count, incl_time, excl_time,
4853 reci_time, rec_depth,
4854 called_subname_sv,
4855 caller_subname_sv);
4856 775001 break;
4857 }
4858  
4859 1297 case NYTP_TAG_PID_START:
4860 {
4861 1297 unsigned int pid = read_u32(in);
4862 1297 unsigned int ppid = read_u32(in);
4863 1297 NV start_time = read_nv(in);
4864  
4865 1297 callbacks[nytp_pid_start](state, nytp_pid_start, pid, ppid,
4866 start_time);
4867 1297 break;
4868 }
4869  
4870 1297 case NYTP_TAG_PID_END:
4871 {
4872 1297 unsigned int pid = read_u32(in);
4873 1297 NV end_time = read_nv(in);
4874  
4875 1297 callbacks[nytp_pid_end](state, nytp_pid_end, pid, end_time);
4876 1297 break;
4877 }
4878  
4879 11673 case NYTP_TAG_ATTRIBUTE:
4880 {
4881 char *value, *key_end;
4882 11673 char *end = NYTP_gets(in, &buffer, &buffer_len);
4883 11673 ▶ if (NULL == end)
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == end) 0 11673 11673
4884 /* probably EOF */
4885 0 croak("Profile format error reading attribute (see TROUBLESHOOTING in NYTProf docs)");
4886 11673 --end; /* End, as returned, points 1 after the \n */
4887 11673 ▶ if ((NULL == (value = (char *)memchr(buffer, '=', end - buffer)))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((NULL == (value = (char *)memchr(buffer, '=', end - buffer)))) { 0 11673 11673
4888 0 logwarn("attribute malformed '%s'\n", buffer);
4889 0 continue;
4890 }
4891 11673 key_end = value++;
4892  
4893 11673 callbacks[nytp_attribute](state, nytp_attribute, buffer,
4894 11673 (unsigned long)(key_end - buffer),
4895 0, value,
4896 11673 (unsigned long)(end - value), 0);
4897  
4898 11673 ▶ if (memEQs(buffer, key_end - buffer, "ticks_per_sec")) {
Branch: 2 branches
BranchTrueFalseTotal
if (memEQs(buffer, key_end - buffer, "ticks_per_sec")) { 1297 10376 11673
if (memEQs(buffer, key_end - buffer, "ticks_per_sec")) { 1297 0 1297
4899 1297 ticks_per_sec = (unsigned int)atoi(value);
4900 }
4901 10376 ▶ else if (memEQs(buffer, key_end - buffer, "nv_size")) {
Branch: 2 branches
BranchTrueFalseTotal
else if (memEQs(buffer, key_end - buffer, "nv_size")) { 1297 9079 10376
else if (memEQs(buffer, key_end - buffer, "nv_size")) { 1297 0 1297
4902 1297 ▶ if (sizeof(NV) != atoi(value))
Branch: 1 branch
BranchTrueFalseTotal
if (sizeof(NV) != atoi(value)) 0 1297 1297
4903 0 croak("Profile data created by incompatible perl config (NV size %d but ours is %d)",
4904 atoi(value), (int)sizeof(NV));
4905 }
4906
4907 11673 break;
4908 }
4909  
4910 23346 case NYTP_TAG_OPTION:
4911 {
4912 char *value, *key_end;
4913 23346 char *end = NYTP_gets(in, &buffer, &buffer_len);
4914 23346 ▶ if (NULL == end)
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == end) 0 23346 23346
4915 /* probably EOF */
4916 0 croak("Profile format error reading attribute (see TROUBLESHOOTING in NYTProf docs)");
4917 23346 --end; /* end, as returned, points 1 after the \n */
4918 23346 ▶ if ((NULL == (value = (char *)memchr(buffer, '=', end - buffer)))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((NULL == (value = (char *)memchr(buffer, '=', end - buffer)))) { 0 23346 23346
4919 0 logwarn("option malformed '%s'\n", buffer);
4920 0 continue;
4921 }
4922 23346 key_end = value++;
4923  
4924 23346 callbacks[nytp_option](state, nytp_option, buffer,
4925 23346 (unsigned long)(key_end - buffer),
4926 0, value,
4927 23346 (unsigned long)(end - value), 0);
4928 23346 break;
4929 }
4930  
4931 2029 case NYTP_TAG_COMMENT:
4932 {
4933 2029 char *end = NYTP_gets(in, &buffer, &buffer_len);
4934 2029 ▶ if (!end)
Branch: 1 branch
BranchTrueFalseTotal
if (!end) 0 2029 2029
4935 /* probably EOF */
4936 0 croak("Profile format error reading comment (see TROUBLESHOOTING in NYTProf docs)");
4937  
4938 2029 ▶ if (callbacks[nytp_comment])
Branch: 1 branch
BranchTrueFalseTotal
if (callbacks[nytp_comment]) 582 1447 2029
4939 582 callbacks[nytp_comment](state, nytp_comment, buffer,
4940 582 (unsigned long)(end - buffer), 0);
4941  
4942 2029 ▶ if (trace_level >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) 0 2029 2029
4943 0 logwarn("# %s", buffer); /* includes \n */
4944 2029 break;
4945 }
4946  
4947 732 case NYTP_TAG_START_DEFLATE:
4948 {
4949 #ifdef HAS_ZLIB
4950 732 ▶ if (callbacks[nytp_start_deflate]) {
Branch: 1 branch
BranchTrueFalseTotal
if (callbacks[nytp_start_deflate]) { 233 499 732
4951 233 callbacks[nytp_start_deflate](state, nytp_start_deflate);
4952 }
4953 732 NYTP_start_inflate(in);
4954 #else
4955 croak("File uses compression but compression is not supported by this build of NYTProf");
4956 #endif
4957 732 break;
4958 }
4959  
4960 0 default:
4961 0 croak("Profile format error: token %d ('%c'), chunk %lu, pos %ld%s (see TROUBLESHOOTING in NYTProf docs)",
4962 c, c, state->input_chunk_seqn, NYTP_tell(in)-1,
4963 NYTP_type_of_offset(in));
4964 }
4965 }
4966  
4967 1297 sv_free(tmp_str1_sv);
4968 1297 sv_free(tmp_str2_sv);
4969 1297 Safefree(buffer);
4970 1297 }
4971  
4972 static HV*
4973 948 load_profile_to_hv(pTHX_ NYTP_file in)
4974 {
4975 Loader_state_profiler state;
4976 HV *profile_hv;
4977 HV *profile_modes;
4978  
4979 948 Zero(&state, 1, Loader_state_profiler);
4980 948 state.total_stmts_duration = 0.0;
4981 948 state.profiler_start_time = 0.0;
4982 948 state.profiler_end_time = 0.0;
4983 948 state.profiler_duration = 0.0;
4984 #ifdef MULTIPLICITY
4985 state.interp = my_perl;
4986 #endif
4987 948 state.fid_line_time_av = newAV();
4988 948 state.fid_srclines_av = newAV();
4989 948 state.fid_fileinfo_av = newAV();
4990 948 state.sub_subinfo_hv = newHV();
4991 948 state.live_pids_hv = newHV();
4992 948 state.attr_hv = newHV();
4993 948 state.option_hv = newHV();
4994 948 state.file_info_stash = gv_stashpv("Devel::NYTProf::FileInfo", GV_ADDWARN);
4995  
4996 948 av_extend(state.fid_fileinfo_av, 64); /* grow them up front. */
4997 948 av_extend(state.fid_srclines_av, 64);
4998 948 av_extend(state.fid_line_time_av, 64);
4999  
5000 948 load_profile_data_from_stream(aTHX_ processing_callbacks,
5001 (Loader_state_base *)&state, in);
5002  
5003  
5004 948 ▶ if (HvKEYS(state.live_pids_hv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (HvKEYS(state.live_pids_hv)) { 0 948 948
if (HvKEYS(state.live_pids_hv)) { 0 948 948
5005 0 logwarn("Profile data incomplete, no terminator for %" IVdf " pids %s\n",
5006 0 ▶ (IV)HvKEYS(state.live_pids_hv),
Branch: 1 branch
BranchTrueFalseTotal
(IV)HvKEYS(state.live_pids_hv), 0 0 0
5007 "(refer to TROUBLESHOOTING in the NYTProf documentation)");
5008 0 store_attrib_sv(aTHX_ state.attr_hv, STR_WITH_LEN("complete"),
5009 &PL_sv_no);
5010 }
5011 else {
5012 948 store_attrib_sv(aTHX_ state.attr_hv, STR_WITH_LEN("complete"),
5013 &PL_sv_yes);
5014 }
5015  
5016 948 sv_free((SV*)state.live_pids_hv);
5017  
5018 948 ▶ if (state.statement_discount) /* discard unused statement_discount */
Branch: 1 branch
BranchTrueFalseTotal
if (state.statement_discount) /* discard unused statement_discount */ 200 748 948
5019 200 state.total_stmts_discounted -= state.statement_discount;
5020 948 store_attrib_sv(aTHX_ state.attr_hv, STR_WITH_LEN("total_stmts_measured"),
5021 948 newSVnv(state.total_stmts_measured));
5022 948 store_attrib_sv(aTHX_ state.attr_hv, STR_WITH_LEN("total_stmts_discounted"),
5023 948 newSVnv(state.total_stmts_discounted));
5024 948 store_attrib_sv(aTHX_ state.attr_hv, STR_WITH_LEN("total_stmts_duration"),
5025 newSVnv(state.total_stmts_duration));
5026 948 store_attrib_sv(aTHX_ state.attr_hv, STR_WITH_LEN("total_sub_calls"),
5027 948 newSVnv(state.total_sub_calls));
5028  
5029 if (1) {
5030 948 int show_summary_stats = (trace_level >= 1);
5031  
5032 948 ▶ if (state.profiler_end_time
Branch: 1 branch
BranchTrueFalseTotal
if (state.profiler_end_time 948 0 948
5033 948 ▶ && state.total_stmts_duration > state.profiler_duration * 1.1
Branch: 1 branch
BranchTrueFalseTotal
&& state.total_stmts_duration > state.profiler_duration * 1.1 0 948 948
5034 /* GetSystemTimeAsFiletime/gettimeofday_nv on Win32 have 15.625 ms resolution
5035 by default. 1 ms best case scenario if you use special options which Perl
5036 land doesn't use, and MS strongly discourages in
5037 "Timers, Timer Resolution, and Development of Efficient Code". So for short
5038 programs profiler_duration winds up being 0. If necessary, in the future
5039 profiler_duration could be set to 15.625 ms automatically on NYTProf start
5040 because of the argument that a process can not execute in 0 ms according to
5041 the laws of space and time, or at "the end" if profiler_duration is 0.0, set
5042 it to 15.625 ms*/
5043 #ifdef HAS_QPC
5044 && state.profiler_duration != 0.0
5045 #endif
5046 ) {
5047 0 logwarn("The sum of the statement timings is %.1" NVff "%% of the total time profiling."
5048 " (Values slightly over 100%% can be due simply to cumulative timing errors,"
5049 " whereas larger values can indicate a problem with the clock used.)\n",
5050 0 state.total_stmts_duration / state.profiler_duration * 100);
5051 0 show_summary_stats = 1;
5052 }
5053  
5054 948 ▶ if (show_summary_stats)
Branch: 1 branch
BranchTrueFalseTotal
if (show_summary_stats) 0 948 948
5055 0 logwarn("Summary: statements profiled %lu (=%lu-%lu), sum of time %" NVff "s, profile spanned %" NVff "s\n",
5056 0 (unsigned long)(state.total_stmts_measured - state.total_stmts_discounted),
5057 0 (unsigned long)state.total_stmts_measured, (unsigned long)state.total_stmts_discounted,
5058 state.total_stmts_duration,
5059 0 state.profiler_end_time - state.profiler_start_time);
5060 }
5061  
5062 948 profile_hv = newHV();
5063 948 profile_modes = newHV();
5064 948 (void)hv_stores(profile_hv, "attribute",
5065 newRV_noinc((SV*)state.attr_hv));
5066 948 (void)hv_stores(profile_hv, "option",
5067 newRV_noinc((SV*)state.option_hv));
5068 948 (void)hv_stores(profile_hv, "fid_fileinfo",
5069 newRV_noinc((SV*)state.fid_fileinfo_av));
5070 948 (void)hv_stores(profile_hv, "fid_srclines",
5071 newRV_noinc((SV*)state.fid_srclines_av));
5072 948 (void)hv_stores(profile_hv, "fid_line_time",
5073 newRV_noinc((SV*)state.fid_line_time_av));
5074 948 (void)hv_stores(profile_modes, "fid_line_time", newSVpvs("line"));
5075 948 ▶ if (state.fid_block_time_av) {
Branch: 1 branch
BranchTrueFalseTotal
if (state.fid_block_time_av) { 928 20 948
5076 928 (void)hv_stores(profile_hv, "fid_block_time",
5077 newRV_noinc((SV*)state.fid_block_time_av));
5078 928 (void)hv_stores(profile_modes, "fid_block_time", newSVpvs("block"));
5079 }
5080 948 ▶ if (state.fid_sub_time_av) {
Branch: 1 branch
BranchTrueFalseTotal
if (state.fid_sub_time_av) { 928 20 948
5081 928 (void)hv_stores(profile_hv, "fid_sub_time",
5082 newRV_noinc((SV*)state.fid_sub_time_av));
5083 928 (void)hv_stores(profile_modes, "fid_sub_time", newSVpvs("sub"));
5084 }
5085 948 (void)hv_stores(profile_hv, "sub_subinfo",
5086 newRV_noinc((SV*)state.sub_subinfo_hv));
5087 948 (void)hv_stores(profile_hv, "profile_modes",
5088 newRV_noinc((SV*)profile_modes));
5089 948 return profile_hv;
5090 }
5091  
5092 static void
5093 349 load_profile_to_callback(pTHX_ NYTP_file in, SV *cb)
5094 {
5095 Loader_state_callback state;
5096 int i;
5097 349 HV *cb_hv = NULL;
5098 349 CV *default_cb = NULL;
5099  
5100 349 ▶ if (SvTYPE(cb) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(cb) == SVt_PVHV) { 348 1 349
5101 /* A default callback is stored with an empty key. */
5102 SV **svp;
5103  
5104 348 cb_hv = (HV *)cb;
5105 348 svp = hv_fetch(cb_hv, "", 0, 0);
5106  
5107 348 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 0 348 348
5108 0 ▶ if (!SvROK(*svp) && SvTYPE(SvRV(*svp)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(*svp) && SvTYPE(SvRV(*svp)) != SVt_PVCV) 0 0 0
if (!SvROK(*svp) && SvTYPE(SvRV(*svp)) != SVt_PVCV) 0 0 0
5109 0 croak("Default callback is not a CODE reference");
5110 0 default_cb = (CV *)SvRV(*svp);
5111 }
5112 1 ▶ } else if (SvTYPE(cb) == SVt_PVCV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvTYPE(cb) == SVt_PVCV) { 1 0 1
5113 1 default_cb = (CV *) cb;
5114 } else
5115 0 croak("Not a CODE or HASH reference");
5116  
5117 #ifdef MULTIPLICITY
5118 state.interp = my_perl;
5119 #endif
5120  
5121 349 state.base_state.input_chunk_seqn = 0;
5122  
5123 349 state.input_chunk_seqn_sv = save_scalar(gv_fetchpv(".", GV_ADD, SVt_IV));
5124  
5125 349 i = C_ARRAY_LENGTH(state.tag_names);
5126 6631 ▶ while (--i) {
Branch: 1 branch
BranchTrueFalseTotal
while (--i) { 6282 349 6631
5127 6282 ▶ if (callback_info[i].args) {
Branch: 1 branch
BranchTrueFalseTotal
if (callback_info[i].args) { 5584 698 6282
5128 state.tag_names[i]
5129 5584 = newSVpvn_flags(callback_info[i].description,
5130 callback_info[i].len, SVs_TEMP);
5131 5584 SvREADONLY_on(state.tag_names[i]);
5132 /* Don't steal the string buffer. */
5133 5584 SvTEMP_off(state.tag_names[i]);
5134 } else
5135 698 state.tag_names[i] = NULL;
5136  
5137 6282 ▶ if (cb_hv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb_hv) { 6264 18 6282
5138 6264 SV **svp = hv_fetch(cb_hv, callback_info[i].description,
5139 (I32)(callback_info[i].len), 0);
5140  
5141 6264 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 1044 5220 6264
5142 1044 ▶ if (!SvROK(*svp) && SvTYPE(SvRV(*svp)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(*svp) && SvTYPE(SvRV(*svp)) != SVt_PVCV) 0 1044 1044
if (!SvROK(*svp) && SvTYPE(SvRV(*svp)) != SVt_PVCV) 0 0 0
5143 0 croak("Callback for %s is not a CODE reference",
5144 callback_info[i].description);
5145 1044 state.cb[i] = (CV *)SvRV(*svp);
5146 } else
5147 5220 state.cb[i] = default_cb;
5148 } else
5149 18 state.cb[i] = default_cb;
5150 }
5151 4188 ▶ for (i = 0; i < C_ARRAY_LENGTH(state.cb_args); i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < C_ARRAY_LENGTH(state.cb_args); i++) 3839 349 4188
5152 3839 state.cb_args[i] = sv_newmortal();
5153  
5154 349 load_profile_data_from_stream(aTHX_ perl_callbacks, (Loader_state_base *)&state,
5155 in);
5156 349 }
5157  
5158 struct int_constants_t {
5159 const char *name;
5160 int value;
5161 };
5162  
5163 static struct int_constants_t int_constants[] = {
5164 /* NYTP_FIDf_* */
5165 {"NYTP_FIDf_IS_PMC", NYTP_FIDf_IS_PMC},
5166 {"NYTP_FIDf_VIA_STMT", NYTP_FIDf_VIA_STMT},
5167 {"NYTP_FIDf_VIA_SUB", NYTP_FIDf_VIA_SUB},
5168 {"NYTP_FIDf_IS_AUTOSPLIT", NYTP_FIDf_IS_AUTOSPLIT},
5169 {"NYTP_FIDf_HAS_SRC", NYTP_FIDf_HAS_SRC},
5170 {"NYTP_FIDf_SAVE_SRC", NYTP_FIDf_SAVE_SRC},
5171 {"NYTP_FIDf_IS_ALIAS", NYTP_FIDf_IS_ALIAS},
5172 {"NYTP_FIDf_IS_FAKE", NYTP_FIDf_IS_FAKE},
5173 {"NYTP_FIDf_IS_EVAL", NYTP_FIDf_IS_EVAL},
5174 /* NYTP_FIDi_* */
5175 {"NYTP_FIDi_FILENAME", NYTP_FIDi_FILENAME},
5176 {"NYTP_FIDi_EVAL_FID", NYTP_FIDi_EVAL_FID},
5177 {"NYTP_FIDi_EVAL_LINE", NYTP_FIDi_EVAL_LINE},
5178 {"NYTP_FIDi_FID", NYTP_FIDi_FID},
5179 {"NYTP_FIDi_FLAGS", NYTP_FIDi_FLAGS},
5180 {"NYTP_FIDi_FILESIZE", NYTP_FIDi_FILESIZE},
5181 {"NYTP_FIDi_FILEMTIME", NYTP_FIDi_FILEMTIME},
5182 {"NYTP_FIDi_PROFILE", NYTP_FIDi_PROFILE},
5183 {"NYTP_FIDi_EVAL_FI", NYTP_FIDi_EVAL_FI},
5184 {"NYTP_FIDi_HAS_EVALS", NYTP_FIDi_HAS_EVALS},
5185 {"NYTP_FIDi_SUBS_DEFINED", NYTP_FIDi_SUBS_DEFINED},
5186 {"NYTP_FIDi_SUBS_CALLED", NYTP_FIDi_SUBS_CALLED},
5187 {"NYTP_FIDi_elements", NYTP_FIDi_elements},
5188 /* NYTP_SIi_* */
5189 {"NYTP_SIi_FID", NYTP_SIi_FID},
5190 {"NYTP_SIi_FIRST_LINE", NYTP_SIi_FIRST_LINE},
5191 {"NYTP_SIi_LAST_LINE", NYTP_SIi_LAST_LINE},
5192 {"NYTP_SIi_CALL_COUNT", NYTP_SIi_CALL_COUNT},
5193 {"NYTP_SIi_INCL_RTIME", NYTP_SIi_INCL_RTIME},
5194 {"NYTP_SIi_EXCL_RTIME", NYTP_SIi_EXCL_RTIME},
5195 {"NYTP_SIi_SUB_NAME", NYTP_SIi_SUB_NAME},
5196 {"NYTP_SIi_PROFILE", NYTP_SIi_PROFILE},
5197 {"NYTP_SIi_REC_DEPTH", NYTP_SIi_REC_DEPTH},
5198 {"NYTP_SIi_RECI_RTIME", NYTP_SIi_RECI_RTIME},
5199 {"NYTP_SIi_CALLED_BY", NYTP_SIi_CALLED_BY},
5200 {"NYTP_SIi_elements", NYTP_SIi_elements},
5201 /* NYTP_SCi_* */
5202 {"NYTP_SCi_CALL_COUNT", NYTP_SCi_CALL_COUNT},
5203 {"NYTP_SCi_INCL_RTIME", NYTP_SCi_INCL_RTIME},
5204 {"NYTP_SCi_EXCL_RTIME", NYTP_SCi_EXCL_RTIME},
5205 {"NYTP_SCi_INCL_TICKS", NYTP_SCi_INCL_TICKS},
5206 {"NYTP_SCi_EXCL_TICKS", NYTP_SCi_EXCL_TICKS},
5207 {"NYTP_SCi_RECI_RTIME", NYTP_SCi_RECI_RTIME},
5208 {"NYTP_SCi_REC_DEPTH", NYTP_SCi_REC_DEPTH},
5209 {"NYTP_SCi_CALLING_SUB", NYTP_SCi_CALLING_SUB},
5210 {"NYTP_SCi_elements", NYTP_SCi_elements},
5211 /* others */
5212 {"NYTP_DEFAULT_COMPRESSION", default_compression_level},
5213 {"NYTP_FILE_MAJOR_VERSION", NYTP_FILE_MAJOR_VERSION},
5214 {"NYTP_FILE_MINOR_VERSION", NYTP_FILE_MINOR_VERSION},
5215 };
5216  
5217 /***********************************
5218 * Perl XS Code Below Here *
5219 ***********************************/
5220  
5221 MODULE = Devel::NYTProf PACKAGE = Devel::NYTProf::Constants
5222  
5223 PROTOTYPES: DISABLE
5224  
5225 BOOT:
5226 {
5227 1302 HV *stash = gv_stashpv("Devel::NYTProf::Constants", GV_ADDWARN);
5228 1302 struct int_constants_t *constant = int_constants;
5229 1302 const struct int_constants_t *end = constant + C_ARRAY_LENGTH(int_constants);
5230  
5231 do {
5232 /* 5.8.x and earlier don't declare newCONSTSUB() as const char *, even
5233 though it is. */
5234 59892 newCONSTSUB(stash, (char *) constant->name, newSViv(constant->value));
5235 59892 ▶ } while (++constant < end);
Branch: 1 branch
BranchTrueFalseTotal
} while (++constant < end); 58590 1302 59892
5236 1302 newCONSTSUB(stash, "NYTP_ZLIB_VERSION", newSVpv(ZLIB_VERSION, 0));
5237 }
5238  
5239  
5240 MODULE = Devel::NYTProf PACKAGE = Devel::NYTProf::Util
5241  
5242 PROTOTYPES: DISABLE
5243  
5244 void
5245 trace_level()
5246 PPCODE:
5247 6664074 ▶ XSRETURN_IV(trace_level);
Branch: 1 branch
BranchTrueFalseTotal
XSRETURN_IV(trace_level); 0 6664074 6664074
5248  
5249  
5250 MODULE = Devel::NYTProf PACKAGE = Devel::NYTProf::Test
5251  
5252 PROTOTYPES: DISABLE
5253  
5254 void
5255 example_xsub(const char *unused="", SV *action=Nullsv, SV *arg=Nullsv)
5256 CODE:
5257 PERL_UNUSED_VAR(unused);
5258 37194 ▶ if (!action)
Branch: 1 branch
BranchTrueFalseTotal
if (!action) 37145 49 37194
5259 37145 ▶ XSRETURN(0);
Branch: 1 branch
BranchTrueFalseTotal
XSRETURN(0); 0 37145 37145
5260 49 ▶ if (SvROK(action) && SvTYPE(SvRV(action))==SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(action) && SvTYPE(SvRV(action))==SVt_PVCV) { 16 33 49
if (SvROK(action) && SvTYPE(SvRV(action))==SVt_PVCV) { 16 0 16
5261 /* perl <= 5.8.8 doesn't use OP_ENTERSUB so won't be seen by NYTProf */
5262 16 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 16 16
5263 16 call_sv(action, G_VOID|G_DISCARD);
5264 }
5265 33 ▶ else if (strEQ(SvPV_nolen(action),"eval"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(SvPV_nolen(action),"eval")) 0 33 33
5266 0 eval_pv(SvPV_nolen(arg), TRUE);
5267 33 ▶ else if (strEQ(SvPV_nolen(action),"die"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(SvPV_nolen(action),"die")) 33 0 33
5268 33 croak("example_xsub(die)");
5269 0 logwarn("example_xsub: unknown action '%s'\n", SvPV_nolen(action));
5270  
5271 void
5272 example_xsub_eval(...)
5273 CODE:
5274 PERL_UNUSED_VAR(items);
5275 /* to enable testing of string evals in embedded environments
5276 * where there's no caller file information available.
5277 * Only it doesn't actually do that because perl knows
5278 * what it's executing at the time eval_pv() gets called.
5279 * We need a better test, closer to true embedded.
5280 */
5281 0 eval_pv("Devel::NYTProf::Test::example_xsub()", 1);
5282  
5283  
5284 void
5285 set_errno(int e)
5286 CODE:
5287 1 ▶ SETERRNO(e, 0);
Branch: 1 branch
BranchTrueFalseTotal
SETERRNO(e, 0); 0 1 1
5288  
5289  
5290 void
5291 ticks_for_usleep(long u_seconds)
5292 PPCODE:
5293 0 NV elapsed = -1;
5294 0 NV overflow = -1;
5295 #ifdef HAS_SELECT
5296 time_of_day_t s_time;
5297 time_of_day_t e_time;
5298 struct timeval timebuf;
5299 0 timebuf.tv_sec = (long)(u_seconds / 1000000);
5300 0 timebuf.tv_usec = u_seconds - (timebuf.tv_sec * 1000000);
5301 0 ▶ if (!last_pid)
Branch: 1 branch
BranchTrueFalseTotal
if (!last_pid) 0 0 0
5302 0 _init_profiler_clock(aTHX);
5303 0 get_time_of_day(s_time);
5304 0 PerlSock_select(0, 0, 0, 0, &timebuf);
5305 0 get_time_of_day(e_time);
5306 0 get_NV_ticks_between(s_time, e_time, elapsed, overflow);
5307 #else
5308 PERL_UNUSED_VAR(u_seconds);
5309 #endif
5310 0 ▶ EXTEND(SP, 4);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 4); 0 0 0
5311 0 PUSHs(sv_2mortal(newSVnv(elapsed)));
5312 0 PUSHs(sv_2mortal(newSVnv(overflow)));
5313 0 PUSHs(sv_2mortal(newSVnv(ticks_per_sec)));
5314 0 PUSHs(sv_2mortal(newSViv(profile_clock)));
5315  
5316  
5317 MODULE = Devel::NYTProf PACKAGE = DB
5318  
5319 PROTOTYPES: DISABLE
5320  
5321 void
5322 DB_profiler(...)
5323 CODE:
5324 /* this sub gets aliased as "DB::DB" by NYTProf.pm if use_db_sub is true */
5325 PERL_UNUSED_VAR(items);
5326 90559921 ▶ if (opt_use_db_sub)
Branch: 1 branch
BranchTrueFalseTotal
if (opt_use_db_sub) 90559921 0 90559921
5327 90559921 DB_stmt(aTHX_ NULL, PL_op);
5328 else
5329 0 logwarn("DB::DB called unexpectedly\n");
5330  
5331 void
5332 set_option(const char *opt, const char *value)
5333 C_ARGS:
5334 aTHX_ opt, value
5335  
5336 int
5337 init_profiler()
5338 C_ARGS:
5339 aTHX
5340  
5341 int
5342 enable_profile(char *file = NULL)
5343 C_ARGS:
5344 aTHX_ file
5345 POSTCALL:
5346 /* if profiler was previously disabled */
5347 /* then arrange for the enable_profile call to be noted */
5348 64 ▶ if (!RETVAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL) { 64 0 64
5349 64 DB_stmt(aTHX_ PL_curcop, PL_op);
5350 }
5351  
5352  
5353 int
5354 disable_profile()
5355 C_ARGS:
5356 aTHX
5357  
5358 void
5359 finish_profile(...)
5360 ALIAS:
5361 _finish = 1
5362 C_ARGS:
5363 aTHX
5364 INIT:
5365 PERL_UNUSED_ARG(ix);
5366 PERL_UNUSED_ARG(items);
5367  
5368 void
5369 _INIT()
5370 CODE:
5371 537 ▶ if (profile_start == NYTP_START_INIT) {
Branch: 1 branch
BranchTrueFalseTotal
if (profile_start == NYTP_START_INIT) { 537 0 537
5372 537 enable_profile(aTHX_ NULL);
5373 }
5374 0 ▶ else if (profile_start == NYTP_START_END) {
Branch: 1 branch
BranchTrueFalseTotal
else if (profile_start == NYTP_START_END) { 0 0 0
5375 0 SV *enable_profile_sv = (SV *)get_cv("DB::enable_profile", GV_ADDWARN);
5376 0 ▶ if (trace_level >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) 0 0 0
5377 0 logwarn("~ enable_profile deferred until END\n");
5378 0 ▶ if (!PL_endav)
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_endav) 0 0 0
5379 0 PL_endav = newAV();
5380 0 av_unshift(PL_endav, 1); /* we want to be first */
5381 0 av_store(PL_endav, 0, SvREFCNT_inc(enable_profile_sv));
5382 }
5383 537 av_extend(PL_endav, av_len(PL_endav)+20); /* see PL_endav in init_profiler() */
5384 537 ▶ if (trace_level >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) 0 537 537
5385 0 logwarn("~ INIT done\n");
5386  
5387 void
5388 _END()
5389 ALIAS:
5390 _CHECK = 1
5391 CODE:
5392 /* we want to END { finish_profile() } but we want it to be the last END
5393 * block run, so we don't push it into PL_endav until END phase has started,
5394 * so it's likely to be the last thing run. Do this once, else we could end
5395 * up in an infinite loop arms race with something else trying the same
5396 * strategy.
5397 */
5398 600 CV *finish_profile_cv = get_cv("DB::finish_profile", GV_ADDWARN);
5399 if (1) { /* defer */
5400 600 ▶ if (!PL_checkav) PL_checkav = newAV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_checkav) PL_checkav = newAV(); 0 600 600
5401 600 ▶ if (!PL_endav) PL_endav = newAV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_endav) PL_endav = newAV(); 0 600 600
5402 600 ▶ av_push((ix == 1 ? PL_checkav : PL_endav), SvREFCNT_inc(finish_profile_cv));
Branch: 1 branch
BranchTrueFalseTotal
av_push((ix == 1 ? PL_checkav : PL_endav), SvREFCNT_inc(finish_profile_cv)); 0 600 600
5403 }
5404 else { /* immediate */
5405 call_sv((SV *)finish_profile_cv, G_VOID);
5406 }
5407 600 ▶ if (trace_level >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level >= 1) 0 600 600
5408 0 ▶ logwarn("~ %s done\n", ix == 1 ? "CHECK" : "END");
Branch: 1 branch
BranchTrueFalseTotal
logwarn("~ %s done\n", ix == 1 ? "CHECK" : "END"); 0 0 0
5409  
5410  
5411  
5412 MODULE = Devel::NYTProf PACKAGE = Devel::NYTProf::Data
5413  
5414 PROTOTYPES: DISABLE
5415  
5416 HV*
5417 load_profile_data_from_file(file,cb=NULL)
5418 char *file;
5419 SV* cb;
5420 PREINIT:
5421 int result;
5422 NYTP_file in;
5423 CODE:
5424 1297 ▶ if (trace_level)
Branch: 1 branch
BranchTrueFalseTotal
if (trace_level) 0 1297 1297
5425 0 logwarn("reading profile data from file %s\n", file);
5426 1297 in = NYTP_open(file, "rb");
5427 1297 ▶ if (in == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (in == NULL) { 0 1297 1297
5428 0 croak("Failed to open input '%s': %s", file, strerror(errno));
5429 }
5430 1297 ▶ if (cb && SvROK(cb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cb && SvROK(cb)) { 1297 0 1297
if (cb && SvROK(cb)) { 349 948 1297
5431 349 load_profile_to_callback(aTHX_ in, SvRV(cb));
5432 349 RETVAL = (HV*) &PL_sv_undef;
5433 }
5434 else {
5435 948 RETVAL = load_profile_to_hv(aTHX_ in);
5436 }
5437  
5438 1297 ▶ if ((result = NYTP_close(in, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((result = NYTP_close(in, 0))) 0 1297 1297
5439 0 logwarn("Error closing profile data file: %s\n", strerror(result));
5440  
5441 OUTPUT:
5442 RETVAL
« FileHandle.xs ↑ Summary t/test40pmc.pm »