1PERLHACKTIPS(1) Perl Programmers Reference Guide PERLHACKTIPS(1)
2
3
4
6 perlhacktips - Tips for Perl core C code hacking
7
9 This document will help you learn the best way to go about hacking on
10 the Perl core C code. It covers common problems, debugging, profiling,
11 and more.
12
13 If you haven't read perlhack and perlhacktut yet, you might want to do
14 that first.
15
17 Perl source plays by ANSI C89 rules: no C99 (or C++) extensions. You
18 don't care about some particular platform having broken Perl? I hear
19 there is still a strong demand for J2EE programmers.
20
21 Perl environment problems
22 · Not compiling with threading
23
24 Compiling with threading (-Duseithreads) completely rewrites the
25 function prototypes of Perl. You better try your changes with
26 that. Related to this is the difference between "Perl_-less" and
27 "Perl_-ly" APIs, for example:
28
29 Perl_sv_setiv(aTHX_ ...);
30 sv_setiv(...);
31
32 The first one explicitly passes in the context, which is needed for
33 e.g. threaded builds. The second one does that implicitly; do not
34 get them mixed. If you are not passing in a aTHX_, you will need
35 to do a dTHX (or a dVAR) as the first thing in the function.
36
37 See "How multiple interpreters and concurrency are supported" in
38 perlguts for further discussion about context.
39
40 · Not compiling with -DDEBUGGING
41
42 The DEBUGGING define exposes more code to the compiler, therefore
43 more ways for things to go wrong. You should try it.
44
45 · Introducing (non-read-only) globals
46
47 Do not introduce any modifiable globals, truly global or file
48 static. They are bad form and complicate multithreading and other
49 forms of concurrency. The right way is to introduce them as new
50 interpreter variables, see intrpvar.h (at the very end for binary
51 compatibility).
52
53 Introducing read-only (const) globals is okay, as long as you
54 verify with e.g. "nm libperl.a|egrep -v ' [TURtr] '" (if your "nm"
55 has BSD-style output) that the data you added really is read-only.
56 (If it is, it shouldn't show up in the output of that command.)
57
58 If you want to have static strings, make them constant:
59
60 static const char etc[] = "...";
61
62 If you want to have arrays of constant strings, note carefully the
63 right combination of "const"s:
64
65 static const char * const yippee[] =
66 {"hi", "ho", "silver"};
67
68 There is a way to completely hide any modifiable globals (they are
69 all moved to heap), the compilation setting
70 "-DPERL_GLOBAL_STRUCT_PRIVATE". It is not normally used, but can
71 be used for testing, read more about it in "Background and
72 PERL_IMPLICIT_CONTEXT" in perlguts.
73
74 · Not exporting your new function
75
76 Some platforms (Win32, AIX, VMS, OS/2, to name a few) require any
77 function that is part of the public API (the shared Perl library)
78 to be explicitly marked as exported. See the discussion about
79 embed.pl in perlguts.
80
81 · Exporting your new function
82
83 The new shiny result of either genuine new functionality or your
84 arduous refactoring is now ready and correctly exported. So what
85 could possibly go wrong?
86
87 Maybe simply that your function did not need to be exported in the
88 first place. Perl has a long and not so glorious history of
89 exporting functions that it should not have.
90
91 If the function is used only inside one source code file, make it
92 static. See the discussion about embed.pl in perlguts.
93
94 If the function is used across several files, but intended only for
95 Perl's internal use (and this should be the common case), do not
96 export it to the public API. See the discussion about embed.pl in
97 perlguts.
98
99 Portability problems
100 The following are common causes of compilation and/or execution
101 failures, not common to Perl as such. The C FAQ is good bedtime
102 reading. Please test your changes with as many C compilers and
103 platforms as possible; we will, anyway, and it's nice to save oneself
104 from public embarrassment.
105
106 If using gcc, you can add the "-std=c89" option which will hopefully
107 catch most of these unportabilities. (However it might also catch
108 incompatibilities in your system's header files.)
109
110 Use the Configure "-Dgccansipedantic" flag to enable the gcc "-ansi
111 -pedantic" flags which enforce stricter ANSI rules.
112
113 If using the "gcc -Wall" note that not all the possible warnings (like
114 "-Wuninitialized") are given unless you also compile with "-O".
115
116 Note that if using gcc, starting from Perl 5.9.5 the Perl core source
117 code files (the ones at the top level of the source code distribution,
118 but not e.g. the extensions under ext/) are automatically compiled with
119 as many as possible of the "-std=c89", "-ansi", "-pedantic", and a
120 selection of "-W" flags (see cflags.SH).
121
122 Also study perlport carefully to avoid any bad assumptions about the
123 operating system, filesystems, character set, and so forth.
124
125 You may once in a while try a "make microperl" to see whether we can
126 still compile Perl with just the bare minimum of interfaces. (See
127 README.micro.)
128
129 Do not assume an operating system indicates a certain compiler.
130
131 · Casting pointers to integers or casting integers to pointers
132
133 void castaway(U8* p)
134 {
135 IV i = p;
136
137 or
138
139 void castaway(U8* p)
140 {
141 IV i = (IV)p;
142
143 Both are bad, and broken, and unportable. Use the PTR2IV() macro
144 that does it right. (Likewise, there are PTR2UV(), PTR2NV(),
145 INT2PTR(), and NUM2PTR().)
146
147 · Casting between function pointers and data pointers
148
149 Technically speaking casting between function pointers and data
150 pointers is unportable and undefined, but practically speaking it
151 seems to work, but you should use the FPTR2DPTR() and DPTR2FPTR()
152 macros. Sometimes you can also play games with unions.
153
154 · Assuming sizeof(int) == sizeof(long)
155
156 There are platforms where longs are 64 bits, and platforms where
157 ints are 64 bits, and while we are out to shock you, even platforms
158 where shorts are 64 bits. This is all legal according to the C
159 standard. (In other words, "long long" is not a portable way to
160 specify 64 bits, and "long long" is not even guaranteed to be any
161 wider than "long".)
162
163 Instead, use the definitions IV, UV, IVSIZE, I32SIZE, and so forth.
164 Avoid things like I32 because they are not guaranteed to be exactly
165 32 bits, they are at least 32 bits, nor are they guaranteed to be
166 int or long. If you really explicitly need 64-bit variables, use
167 I64 and U64, but only if guarded by HAS_QUAD.
168
169 · Assuming one can dereference any type of pointer for any type of
170 data
171
172 char *p = ...;
173 long pony = *(long *)p; /* BAD */
174
175 Many platforms, quite rightly so, will give you a core dump instead
176 of a pony if the p happens not to be correctly aligned.
177
178 · Lvalue casts
179
180 (int)*p = ...; /* BAD */
181
182 Simply not portable. Get your lvalue to be of the right type, or
183 maybe use temporary variables, or dirty tricks with unions.
184
185 · Assume anything about structs (especially the ones you don't
186 control, like the ones coming from the system headers)
187
188 · That a certain field exists in a struct
189
190 · That no other fields exist besides the ones you know of
191
192 · That a field is of certain signedness, sizeof, or type
193
194 · That the fields are in a certain order
195
196 · While C guarantees the ordering specified in the
197 struct definition, between different platforms the
198 definitions might differ
199
200 · That the sizeof(struct) or the alignments are the same
201 everywhere
202
203 · There might be padding bytes between the fields to
204 align the fields - the bytes can be anything
205
206 · Structs are required to be aligned to the maximum
207 alignment required by the fields - which for native
208 types is for usually equivalent to sizeof() of the
209 field
210
211 · Assuming the character set is ASCIIish
212
213 Perl can compile and run under EBCDIC platforms. See perlebcdic.
214 This is transparent for the most part, but because the character
215 sets differ, you shouldn't use numeric (decimal, octal, nor hex)
216 constants to refer to characters. You can safely say 'A', but not
217 0x41. You can safely say '\n', but not "\012". However, you can
218 use macros defined in utf8.h to specify any code point portably.
219 "LATIN1_TO_NATIVE(0xDF)" is going to be the code point that means
220 LATIN SMALL LETTER SHARP S on whatever platform you are running on
221 (on ASCII platforms it compiles without adding any extra code, so
222 there is zero performance hit on those). The acceptable inputs to
223 "LATIN1_TO_NATIVE" are from 0x00 through 0xFF. If your input isn't
224 guaranteed to be in that range, use "UNICODE_TO_NATIVE" instead.
225 "NATIVE_TO_LATIN1" and "NATIVE_TO_UNICODE" translate the opposite
226 direction.
227
228 If you need the string representation of a character that doesn't
229 have a mnemonic name in C, you should add it to the list in
230 regen/unicode_constants.pl, and have Perl create "#define"'s for
231 you, based on the current platform.
232
233 Note that the "isFOO" and "toFOO" macros in handy.h work properly
234 on native code points and strings.
235
236 Also, the range 'A' - 'Z' in ASCII is an unbroken sequence of 26
237 upper case alphabetic characters. That is not true in EBCDIC. Nor
238 for 'a' to 'z'. But '0' - '9' is an unbroken range in both
239 systems. Don't assume anything about other ranges. (Note that
240 special handling of ranges in regular expression patterns and
241 transliterations makes it appear to Perl code that the
242 aforementioned ranges are all unbroken.)
243
244 Many of the comments in the existing code ignore the possibility of
245 EBCDIC, and may be wrong therefore, even if the code works. This
246 is actually a tribute to the successful transparent insertion of
247 being able to handle EBCDIC without having to change pre-existing
248 code.
249
250 UTF-8 and UTF-EBCDIC are two different encodings used to represent
251 Unicode code points as sequences of bytes. Macros with the same
252 names (but different definitions) in utf8.h and utfebcdic.h are
253 used to allow the calling code to think that there is only one such
254 encoding. This is almost always referred to as "utf8", but it
255 means the EBCDIC version as well. Again, comments in the code may
256 well be wrong even if the code itself is right. For example, the
257 concept of UTF-8 "invariant characters" differs between ASCII and
258 EBCDIC. On ASCII platforms, only characters that do not have the
259 high-order bit set (i.e. whose ordinals are strict ASCII, 0 - 127)
260 are invariant, and the documentation and comments in the code may
261 assume that, often referring to something like, say, "hibit". The
262 situation differs and is not so simple on EBCDIC machines, but as
263 long as the code itself uses the "NATIVE_IS_INVARIANT()" macro
264 appropriately, it works, even if the comments are wrong.
265
266 As noted in "TESTING" in perlhack, when writing test scripts, the
267 file t/charset_tools.pl contains some helpful functions for writing
268 tests valid on both ASCII and EBCDIC platforms. Sometimes, though,
269 a test can't use a function and it's inconvenient to have different
270 test versions depending on the platform. There are 20 code points
271 that are the same in all 4 character sets currently recognized by
272 Perl (the 3 EBCDIC code pages plus ISO 8859-1 (ASCII/Latin1)).
273 These can be used in such tests, though there is a small
274 possibility that Perl will become available in yet another
275 character set, breaking your test. All but one of these code
276 points are C0 control characters. The most significant controls
277 that are the same are "\0", "\r", and "\N{VT}" (also specifiable as
278 "\cK", "\x0B", "\N{U+0B}", or "\013"). The single non-control is
279 U+00B6 PILCROW SIGN. The controls that are the same have the same
280 bit pattern in all 4 character sets, regardless of the UTF8ness of
281 the string containing them. The bit pattern for U+B6 is the same
282 in all 4 for non-UTF8 strings, but differs in each when its
283 containing string is UTF-8 encoded. The only other code points
284 that have some sort of sameness across all 4 character sets are the
285 pair 0xDC and 0xFC. Together these represent upper- and lowercase
286 LATIN LETTER U WITH DIAERESIS, but which is upper and which is
287 lower may be reversed: 0xDC is the capital in Latin1 and 0xFC is
288 the small letter, while 0xFC is the capital in EBCDIC and 0xDC is
289 the small one. This factoid may be exploited in writing case
290 insensitive tests that are the same across all 4 character sets.
291
292 · Assuming the character set is just ASCII
293
294 ASCII is a 7 bit encoding, but bytes have 8 bits in them. The 128
295 extra characters have different meanings depending on the locale.
296 Absent a locale, currently these extra characters are generally
297 considered to be unassigned, and this has presented some problems.
298 This has being changed starting in 5.12 so that these characters
299 can be considered to be Latin-1 (ISO-8859-1).
300
301 · Mixing #define and #ifdef
302
303 #define BURGLE(x) ... \
304 #ifdef BURGLE_OLD_STYLE /* BAD */
305 ... do it the old way ... \
306 #else
307 ... do it the new way ... \
308 #endif
309
310 You cannot portably "stack" cpp directives. For example in the
311 above you need two separate BURGLE() #defines, one for each #ifdef
312 branch.
313
314 · Adding non-comment stuff after #endif or #else
315
316 #ifdef SNOSH
317 ...
318 #else !SNOSH /* BAD */
319 ...
320 #endif SNOSH /* BAD */
321
322 The #endif and #else cannot portably have anything non-comment
323 after them. If you want to document what is going (which is a good
324 idea especially if the branches are long), use (C) comments:
325
326 #ifdef SNOSH
327 ...
328 #else /* !SNOSH */
329 ...
330 #endif /* SNOSH */
331
332 The gcc option "-Wendif-labels" warns about the bad variant (by
333 default on starting from Perl 5.9.4).
334
335 · Having a comma after the last element of an enum list
336
337 enum color {
338 CERULEAN,
339 CHARTREUSE,
340 CINNABAR, /* BAD */
341 };
342
343 is not portable. Leave out the last comma.
344
345 Also note that whether enums are implicitly morphable to ints
346 varies between compilers, you might need to (int).
347
348 · Using //-comments
349
350 // This function bamfoodles the zorklator. /* BAD */
351
352 That is C99 or C++. Perl is C89. Using the //-comments is
353 silently allowed by many C compilers but cranking up the ANSI C89
354 strictness (which we like to do) causes the compilation to fail.
355
356 · Mixing declarations and code
357
358 void zorklator()
359 {
360 int n = 3;
361 set_zorkmids(n); /* BAD */
362 int q = 4;
363
364 That is C99 or C++. Some C compilers allow that, but you
365 shouldn't.
366
367 The gcc option "-Wdeclaration-after-statements" scans for such
368 problems (by default on starting from Perl 5.9.4).
369
370 · Introducing variables inside for()
371
372 for(int i = ...; ...; ...) { /* BAD */
373
374 That is C99 or C++. While it would indeed be awfully nice to have
375 that also in C89, to limit the scope of the loop variable, alas, we
376 cannot.
377
378 · Mixing signed char pointers with unsigned char pointers
379
380 int foo(char *s) { ... }
381 ...
382 unsigned char *t = ...; /* Or U8* t = ... */
383 foo(t); /* BAD */
384
385 While this is legal practice, it is certainly dubious, and
386 downright fatal in at least one platform: for example VMS cc
387 considers this a fatal error. One cause for people often making
388 this mistake is that a "naked char" and therefore dereferencing a
389 "naked char pointer" have an undefined signedness: it depends on
390 the compiler and the flags of the compiler and the underlying
391 platform whether the result is signed or unsigned. For this very
392 same reason using a 'char' as an array index is bad.
393
394 · Macros that have string constants and their arguments as substrings
395 of the string constants
396
397 #define FOO(n) printf("number = %d\n", n) /* BAD */
398 FOO(10);
399
400 Pre-ANSI semantics for that was equivalent to
401
402 printf("10umber = %d\10");
403
404 which is probably not what you were expecting. Unfortunately at
405 least one reasonably common and modern C compiler does "real
406 backward compatibility" here, in AIX that is what still happens
407 even though the rest of the AIX compiler is very happily C89.
408
409 · Using printf formats for non-basic C types
410
411 IV i = ...;
412 printf("i = %d\n", i); /* BAD */
413
414 While this might by accident work in some platform (where IV
415 happens to be an "int"), in general it cannot. IV might be
416 something larger. Even worse the situation is with more specific
417 types (defined by Perl's configuration step in config.h):
418
419 Uid_t who = ...;
420 printf("who = %d\n", who); /* BAD */
421
422 The problem here is that Uid_t might be not only not "int"-wide but
423 it might also be unsigned, in which case large uids would be
424 printed as negative values.
425
426 There is no simple solution to this because of printf()'s limited
427 intelligence, but for many types the right format is available as
428 with either 'f' or '_f' suffix, for example:
429
430 IVdf /* IV in decimal */
431 UVxf /* UV is hexadecimal */
432
433 printf("i = %"IVdf"\n", i); /* The IVdf is a string constant. */
434
435 Uid_t_f /* Uid_t in decimal */
436
437 printf("who = %"Uid_t_f"\n", who);
438
439 Or you can try casting to a "wide enough" type:
440
441 printf("i = %"IVdf"\n", (IV)something_very_small_and_signed);
442
443 See "Formatted Printing of Size_t and SSize_t" in perlguts for how
444 to print those.
445
446 Also remember that the %p format really does require a void
447 pointer:
448
449 U8* p = ...;
450 printf("p = %p\n", (void*)p);
451
452 The gcc option "-Wformat" scans for such problems.
453
454 · Blindly using variadic macros
455
456 gcc has had them for a while with its own syntax, and C99 brought
457 them with a standardized syntax. Don't use the former, and use the
458 latter only if the HAS_C99_VARIADIC_MACROS is defined.
459
460 · Blindly passing va_list
461
462 Not all platforms support passing va_list to further varargs
463 (stdarg) functions. The right thing to do is to copy the va_list
464 using the Perl_va_copy() if the NEED_VA_COPY is defined.
465
466 · Using gcc statement expressions
467
468 val = ({...;...;...}); /* BAD */
469
470 While a nice extension, it's not portable. The Perl code does
471 admittedly use them if available to gain some extra speed
472 (essentially as a funky form of inlining), but you shouldn't.
473
474 · Binding together several statements in a macro
475
476 Use the macros STMT_START and STMT_END.
477
478 STMT_START {
479 ...
480 } STMT_END
481
482 · Testing for operating systems or versions when should be testing
483 for features
484
485 #ifdef __FOONIX__ /* BAD */
486 foo = quux();
487 #endif
488
489 Unless you know with 100% certainty that quux() is only ever
490 available for the "Foonix" operating system and that is available
491 and correctly working for all past, present, and future versions of
492 "Foonix", the above is very wrong. This is more correct (though
493 still not perfect, because the below is a compile-time check):
494
495 #ifdef HAS_QUUX
496 foo = quux();
497 #endif
498
499 How does the HAS_QUUX become defined where it needs to be? Well,
500 if Foonix happens to be Unixy enough to be able to run the
501 Configure script, and Configure has been taught about detecting and
502 testing quux(), the HAS_QUUX will be correctly defined. In other
503 platforms, the corresponding configuration step will hopefully do
504 the same.
505
506 In a pinch, if you cannot wait for Configure to be educated, or if
507 you have a good hunch of where quux() might be available, you can
508 temporarily try the following:
509
510 #if (defined(__FOONIX__) || defined(__BARNIX__))
511 # define HAS_QUUX
512 #endif
513
514 ...
515
516 #ifdef HAS_QUUX
517 foo = quux();
518 #endif
519
520 But in any case, try to keep the features and operating systems
521 separate.
522
523 A good resource on the predefined macros for various operating
524 systems, compilers, and so forth is
525 <http://sourceforge.net/p/predef/wiki/Home/>
526
527 · Assuming the contents of static memory pointed to by the return
528 values of Perl wrappers for C library functions doesn't change.
529 Many C library functions return pointers to static storage that can
530 be overwritten by subsequent calls to the same or related
531 functions. Perl has light-weight wrappers for some of these
532 functions, and which don't make copies of the static memory. A
533 good example is the interface to the environment variables that are
534 in effect for the program. Perl has "PerlEnv_getenv" to get values
535 from the environment. But the return is a pointer to static memory
536 in the C library. If you are using the value to immediately test
537 for something, that's fine, but if you save the value and expect it
538 to be unchanged by later processing, you would be wrong, but
539 perhaps you wouldn't know it because different C library
540 implementations behave differently, and the one on the platform
541 you're testing on might work for your situation. But on some
542 platforms, a subsequent call to "PerlEnv_getenv" or related
543 function WILL overwrite the memory that your first call points to.
544 This has led to some hard-to-debug problems. Do a "savepv" in
545 perlapi to make a copy, thus avoiding these problems. You will
546 have to free the copy when you're done to avoid memory leaks. If
547 you don't have control over when it gets freed, you'll need to make
548 the copy in a mortal scalar, like so:
549
550 if ((s = PerlEnv_getenv("foo") == NULL) {
551 ... /* handle NULL case */
552 }
553 else {
554 s = SvPVX(sv_2mortal(newSVpv(s, 0)));
555 }
556
557 The above example works only if "s" is "NUL"-terminated; otherwise
558 you have to pass its length to "newSVpv".
559
560 Problematic System Interfaces
561 · malloc(0), realloc(0), calloc(0, 0) are non-portable. To be
562 portable allocate at least one byte. (In general you should rarely
563 need to work at this low level, but instead use the various malloc
564 wrappers.)
565
566 · snprintf() - the return type is unportable. Use my_snprintf()
567 instead.
568
569 Security problems
570 Last but not least, here are various tips for safer coding. See also
571 perlclib for libc/stdio replacements one should use.
572
573 · Do not use gets()
574
575 Or we will publicly ridicule you. Seriously.
576
577 · Do not use tmpfile()
578
579 Use mkstemp() instead.
580
581 · Do not use strcpy() or strcat() or strncpy() or strncat()
582
583 Use my_strlcpy() and my_strlcat() instead: they either use the
584 native implementation, or Perl's own implementation (borrowed from
585 the public domain implementation of INN).
586
587 · Do not use sprintf() or vsprintf()
588
589 If you really want just plain byte strings, use my_snprintf() and
590 my_vsnprintf() instead, which will try to use snprintf() and
591 vsnprintf() if those safer APIs are available. If you want
592 something fancier than a plain byte string, use "Perl_form"() or
593 SVs and "Perl_sv_catpvf()".
594
595 Note that glibc "printf()", "sprintf()", etc. are buggy before
596 glibc version 2.17. They won't allow a "%.s" format with a
597 precision to create a string that isn't valid UTF-8 if the current
598 underlying locale of the program is UTF-8. What happens is that
599 the %s and its operand are simply skipped without any notice.
600 <https://sourceware.org/bugzilla/show_bug.cgi?id=6530>.
601
602 · Do not use atoi()
603
604 Use grok_atoUV() instead. atoi() has ill-defined behavior on
605 overflows, and cannot be used for incremental parsing. It is also
606 affected by locale, which is bad.
607
608 · Do not use strtol() or strtoul()
609
610 Use grok_atoUV() instead. strtol() or strtoul() (or their
611 IV/UV-friendly macro disguises, Strtol() and Strtoul(), or Atol()
612 and Atoul() are affected by locale, which is bad.
613
615 You can compile a special debugging version of Perl, which allows you
616 to use the "-D" option of Perl to tell more about what Perl is doing.
617 But sometimes there is no alternative than to dive in with a debugger,
618 either to see the stack trace of a core dump (very useful in a bug
619 report), or trying to figure out what went wrong before the core dump
620 happened, or how did we end up having wrong or unexpected results.
621
622 Poking at Perl
623 To really poke around with Perl, you'll probably want to build Perl for
624 debugging, like this:
625
626 ./Configure -d -DDEBUGGING
627 make
628
629 "-DDEBUGGING" turns on the C compiler's "-g" flag to have it produce
630 debugging information which will allow us to step through a running
631 program, and to see in which C function we are at (without the
632 debugging information we might see only the numerical addresses of the
633 functions, which is not very helpful). It will also turn on the
634 "DEBUGGING" compilation symbol which enables all the internal debugging
635 code in Perl. There are a whole bunch of things you can debug with
636 this: perlrun lists them all, and the best way to find out about them
637 is to play about with them. The most useful options are probably
638
639 l Context (loop) stack processing
640 s Stack snapshots (with v, displays all stacks)
641 t Trace execution
642 o Method and overloading resolution
643 c String/numeric conversions
644
645 For example
646
647 $ perl -Dst -e '$a + 1'
648 ....
649 (-e:1) gvsv(main::a)
650 => UNDEF
651 (-e:1) const(IV(1))
652 => UNDEF IV(1)
653 (-e:1) add
654 => NV(1)
655
656 Some of the functionality of the debugging code can be achieved with a
657 non-debugging perl by using XS modules:
658
659 -Dr => use re 'debug'
660 -Dx => use O 'Debug'
661
662 Using a source-level debugger
663 If the debugging output of "-D" doesn't help you, it's time to step
664 through perl's execution with a source-level debugger.
665
666 · We'll use "gdb" for our examples here; the principles will apply to
667 any debugger (many vendors call their debugger "dbx"), but check the
668 manual of the one you're using.
669
670 To fire up the debugger, type
671
672 gdb ./perl
673
674 Or if you have a core dump:
675
676 gdb ./perl core
677
678 You'll want to do that in your Perl source tree so the debugger can
679 read the source code. You should see the copyright message, followed
680 by the prompt.
681
682 (gdb)
683
684 "help" will get you into the documentation, but here are the most
685 useful commands:
686
687 · run [args]
688
689 Run the program with the given arguments.
690
691 · break function_name
692
693 · break source.c:xxx
694
695 Tells the debugger that we'll want to pause execution when we reach
696 either the named function (but see "Internal Functions" in
697 perlguts!) or the given line in the named source file.
698
699 · step
700
701 Steps through the program a line at a time.
702
703 · next
704
705 Steps through the program a line at a time, without descending into
706 functions.
707
708 · continue
709
710 Run until the next breakpoint.
711
712 · finish
713
714 Run until the end of the current function, then stop again.
715
716 · 'enter'
717
718 Just pressing Enter will do the most recent operation again - it's a
719 blessing when stepping through miles of source code.
720
721 · ptype
722
723 Prints the C definition of the argument given.
724
725 (gdb) ptype PL_op
726 type = struct op {
727 OP *op_next;
728 OP *op_sibparent;
729 OP *(*op_ppaddr)(void);
730 PADOFFSET op_targ;
731 unsigned int op_type : 9;
732 unsigned int op_opt : 1;
733 unsigned int op_slabbed : 1;
734 unsigned int op_savefree : 1;
735 unsigned int op_static : 1;
736 unsigned int op_folded : 1;
737 unsigned int op_spare : 2;
738 U8 op_flags;
739 U8 op_private;
740 } *
741
742 · print
743
744 Execute the given C code and print its results. WARNING: Perl makes
745 heavy use of macros, and gdb does not necessarily support macros
746 (see later "gdb macro support"). You'll have to substitute them
747 yourself, or to invoke cpp on the source code files (see "The .i
748 Targets") So, for instance, you can't say
749
750 print SvPV_nolen(sv)
751
752 but you have to say
753
754 print Perl_sv_2pv_nolen(sv)
755
756 You may find it helpful to have a "macro dictionary", which you can
757 produce by saying "cpp -dM perl.c | sort". Even then, cpp won't
758 recursively apply those macros for you.
759
760 gdb macro support
761 Recent versions of gdb have fairly good macro support, but in order to
762 use it you'll need to compile perl with macro definitions included in
763 the debugging information. Using gcc version 3.1, this means
764 configuring with "-Doptimize=-g3". Other compilers might use a
765 different switch (if they support debugging macros at all).
766
767 Dumping Perl Data Structures
768 One way to get around this macro hell is to use the dumping functions
769 in dump.c; these work a little like an internal Devel::Peek, but they
770 also cover OPs and other structures that you can't get at from Perl.
771 Let's take an example. We'll use the "$a = $b + $c" we used before,
772 but give it a bit of context: "$b = "6XXXX"; $c = 2.3;". Where's a
773 good place to stop and poke around?
774
775 What about "pp_add", the function we examined earlier to implement the
776 "+" operator:
777
778 (gdb) break Perl_pp_add
779 Breakpoint 1 at 0x46249f: file pp_hot.c, line 309.
780
781 Notice we use "Perl_pp_add" and not "pp_add" - see "Internal Functions"
782 in perlguts. With the breakpoint in place, we can run our program:
783
784 (gdb) run -e '$b = "6XXXX"; $c = 2.3; $a = $b + $c'
785
786 Lots of junk will go past as gdb reads in the relevant source files and
787 libraries, and then:
788
789 Breakpoint 1, Perl_pp_add () at pp_hot.c:309
790 1396 dSP; dATARGET; bool useleft; SV *svl, *svr;
791 (gdb) step
792 311 dPOPTOPnnrl_ul;
793 (gdb)
794
795 We looked at this bit of code before, and we said that "dPOPTOPnnrl_ul"
796 arranges for two "NV"s to be placed into "left" and "right" - let's
797 slightly expand it:
798
799 #define dPOPTOPnnrl_ul NV right = POPn; \
800 SV *leftsv = TOPs; \
801 NV left = USE_LEFT(leftsv) ? SvNV(leftsv) : 0.0
802
803 "POPn" takes the SV from the top of the stack and obtains its NV either
804 directly (if "SvNOK" is set) or by calling the "sv_2nv" function.
805 "TOPs" takes the next SV from the top of the stack - yes, "POPn" uses
806 "TOPs" - but doesn't remove it. We then use "SvNV" to get the NV from
807 "leftsv" in the same way as before - yes, "POPn" uses "SvNV".
808
809 Since we don't have an NV for $b, we'll have to use "sv_2nv" to convert
810 it. If we step again, we'll find ourselves there:
811
812 (gdb) step
813 Perl_sv_2nv (sv=0xa0675d0) at sv.c:1669
814 1669 if (!sv)
815 (gdb)
816
817 We can now use "Perl_sv_dump" to investigate the SV:
818
819 (gdb) print Perl_sv_dump(sv)
820 SV = PV(0xa057cc0) at 0xa0675d0
821 REFCNT = 1
822 FLAGS = (POK,pPOK)
823 PV = 0xa06a510 "6XXXX"\0
824 CUR = 5
825 LEN = 6
826 $1 = void
827
828 We know we're going to get 6 from this, so let's finish the subroutine:
829
830 (gdb) finish
831 Run till exit from #0 Perl_sv_2nv (sv=0xa0675d0) at sv.c:1671
832 0x462669 in Perl_pp_add () at pp_hot.c:311
833 311 dPOPTOPnnrl_ul;
834
835 We can also dump out this op: the current op is always stored in
836 "PL_op", and we can dump it with "Perl_op_dump". This'll give us
837 similar output to CPAN module B::Debug.
838
839 (gdb) print Perl_op_dump(PL_op)
840 {
841 13 TYPE = add ===> 14
842 TARG = 1
843 FLAGS = (SCALAR,KIDS)
844 {
845 TYPE = null ===> (12)
846 (was rv2sv)
847 FLAGS = (SCALAR,KIDS)
848 {
849 11 TYPE = gvsv ===> 12
850 FLAGS = (SCALAR)
851 GV = main::b
852 }
853 }
854
855 # finish this later #
856
857 Using gdb to look at specific parts of a program
858 With the example above, you knew to look for "Perl_pp_add", but what if
859 there were multiple calls to it all over the place, or you didn't know
860 what the op was you were looking for?
861
862 One way to do this is to inject a rare call somewhere near what you're
863 looking for. For example, you could add "study" before your method:
864
865 study;
866
867 And in gdb do:
868
869 (gdb) break Perl_pp_study
870
871 And then step until you hit what you're looking for. This works well
872 in a loop if you want to only break at certain iterations:
873
874 for my $c (1..100) {
875 study if $c == 50;
876 }
877
878 Using gdb to look at what the parser/lexer are doing
879 If you want to see what perl is doing when parsing/lexing your code,
880 you can use "BEGIN {}":
881
882 print "Before\n";
883 BEGIN { study; }
884 print "After\n";
885
886 And in gdb:
887
888 (gdb) break Perl_pp_study
889
890 If you want to see what the parser/lexer is doing inside of "if" blocks
891 and the like you need to be a little trickier:
892
893 if ($a && $b && do { BEGIN { study } 1 } && $c) { ... }
894
896 Various tools exist for analysing C source code statically, as opposed
897 to dynamically, that is, without executing the code. It is possible to
898 detect resource leaks, undefined behaviour, type mismatches,
899 portability problems, code paths that would cause illegal memory
900 accesses, and other similar problems by just parsing the C code and
901 looking at the resulting graph, what does it tell about the execution
902 and data flows. As a matter of fact, this is exactly how C compilers
903 know to give warnings about dubious code.
904
905 lint
906 The good old C code quality inspector, "lint", is available in several
907 platforms, but please be aware that there are several different
908 implementations of it by different vendors, which means that the flags
909 are not identical across different platforms.
910
911 There is a "lint" target in Makefile, but you may have to diddle with
912 the flags (see above).
913
914 Coverity
915 Coverity (<http://www.coverity.com/>) is a product similar to lint and
916 as a testbed for their product they periodically check several open
917 source projects, and they give out accounts to open source developers
918 to the defect databases.
919
920 There is Coverity setup for the perl5 project:
921 <https://scan.coverity.com/projects/perl5>
922
923 HP-UX cadvise (Code Advisor)
924 HP has a C/C++ static analyzer product for HP-UX caller Code Advisor.
925 (Link not given here because the URL is horribly long and seems
926 horribly unstable; use the search engine of your choice to find it.)
927 The use of the "cadvise_cc" recipe with "Configure ...
928 -Dcc=./cadvise_cc" (see cadvise "User Guide") is recommended; as is the
929 use of "+wall".
930
931 cpd (cut-and-paste detector)
932 The cpd tool detects cut-and-paste coding. If one instance of the cut-
933 and-pasted code changes, all the other spots should probably be
934 changed, too. Therefore such code should probably be turned into a
935 subroutine or a macro.
936
937 cpd (<http://pmd.sourceforge.net/cpd.html>) is part of the pmd project
938 (<http://pmd.sourceforge.net/>). pmd was originally written for static
939 analysis of Java code, but later the cpd part of it was extended to
940 parse also C and C++.
941
942 Download the pmd-bin-X.Y.zip () from the SourceForge site, extract the
943 pmd-X.Y.jar from it, and then run that on source code thusly:
944
945 java -cp pmd-X.Y.jar net.sourceforge.pmd.cpd.CPD \
946 --minimum-tokens 100 --files /some/where/src --language c > cpd.txt
947
948 You may run into memory limits, in which case you should use the -Xmx
949 option:
950
951 java -Xmx512M ...
952
953 gcc warnings
954 Though much can be written about the inconsistency and coverage
955 problems of gcc warnings (like "-Wall" not meaning "all the warnings",
956 or some common portability problems not being covered by "-Wall", or
957 "-ansi" and "-pedantic" both being a poorly defined collection of
958 warnings, and so forth), gcc is still a useful tool in keeping our
959 coding nose clean.
960
961 The "-Wall" is by default on.
962
963 The "-ansi" (and its sidekick, "-pedantic") would be nice to be on
964 always, but unfortunately they are not safe on all platforms, they can
965 for example cause fatal conflicts with the system headers (Solaris
966 being a prime example). If Configure "-Dgccansipedantic" is used, the
967 "cflags" frontend selects "-ansi -pedantic" for the platforms where
968 they are known to be safe.
969
970 The following extra flags are added:
971
972 · "-Wendif-labels"
973
974 · "-Wextra"
975
976 · "-Wc++-compat"
977
978 · "-Wwrite-strings"
979
980 · "-Werror=declaration-after-statement"
981
982 · "-Werror=pointer-arith"
983
984 The following flags would be nice to have but they would first need
985 their own Augean stablemaster:
986
987 · "-Wshadow"
988
989 · "-Wstrict-prototypes"
990
991 The "-Wtraditional" is another example of the annoying tendency of gcc
992 to bundle a lot of warnings under one switch (it would be impossible to
993 deploy in practice because it would complain a lot) but it does contain
994 some warnings that would be beneficial to have available on their own,
995 such as the warning about string constants inside macros containing the
996 macro arguments: this behaved differently pre-ANSI than it does in
997 ANSI, and some C compilers are still in transition, AIX being an
998 example.
999
1000 Warnings of other C compilers
1001 Other C compilers (yes, there are other C compilers than gcc) often
1002 have their "strict ANSI" or "strict ANSI with some portability
1003 extensions" modes on, like for example the Sun Workshop has its "-Xa"
1004 mode on (though implicitly), or the DEC (these days, HP...) has its
1005 "-std1" mode on.
1006
1008 NOTE 1: Running under older memory debuggers such as Purify, valgrind
1009 or Third Degree greatly slows down the execution: seconds become
1010 minutes, minutes become hours. For example as of Perl 5.8.1, the
1011 ext/Encode/t/Unicode.t takes extraordinarily long to complete under
1012 e.g. Purify, Third Degree, and valgrind. Under valgrind it takes more
1013 than six hours, even on a snappy computer. The said test must be doing
1014 something that is quite unfriendly for memory debuggers. If you don't
1015 feel like waiting, that you can simply kill away the perl process.
1016 Roughly valgrind slows down execution by factor 10, AddressSanitizer by
1017 factor 2.
1018
1019 NOTE 2: To minimize the number of memory leak false alarms (see
1020 "PERL_DESTRUCT_LEVEL" for more information), you have to set the
1021 environment variable PERL_DESTRUCT_LEVEL to 2. For example, like this:
1022
1023 env PERL_DESTRUCT_LEVEL=2 valgrind ./perl -Ilib ...
1024
1025 NOTE 3: There are known memory leaks when there are compile-time errors
1026 within eval or require, seeing "S_doeval" in the call stack is a good
1027 sign of these. Fixing these leaks is non-trivial, unfortunately, but
1028 they must be fixed eventually.
1029
1030 NOTE 4: DynaLoader will not clean up after itself completely unless
1031 Perl is built with the Configure option
1032 "-Accflags=-DDL_UNLOAD_ALL_AT_EXIT".
1033
1034 valgrind
1035 The valgrind tool can be used to find out both memory leaks and illegal
1036 heap memory accesses. As of version 3.3.0, Valgrind only supports
1037 Linux on x86, x86-64 and PowerPC and Darwin (OS X) on x86 and x86-64.
1038 The special "test.valgrind" target can be used to run the tests under
1039 valgrind. Found errors and memory leaks are logged in files named
1040 testfile.valgrind and by default output is displayed inline.
1041
1042 Example usage:
1043
1044 make test.valgrind
1045
1046 Since valgrind adds significant overhead, tests will take much longer
1047 to run. The valgrind tests support being run in parallel to help with
1048 this:
1049
1050 TEST_JOBS=9 make test.valgrind
1051
1052 Note that the above two invocations will be very verbose as reachable
1053 memory and leak-checking is enabled by default. If you want to just
1054 see pure errors, try:
1055
1056 VG_OPTS='-q --leak-check=no --show-reachable=no' TEST_JOBS=9 \
1057 make test.valgrind
1058
1059 Valgrind also provides a cachegrind tool, invoked on perl as:
1060
1061 VG_OPTS=--tool=cachegrind make test.valgrind
1062
1063 As system libraries (most notably glibc) are also triggering errors,
1064 valgrind allows to suppress such errors using suppression files. The
1065 default suppression file that comes with valgrind already catches a lot
1066 of them. Some additional suppressions are defined in t/perl.supp.
1067
1068 To get valgrind and for more information see
1069
1070 http://valgrind.org/
1071
1072 AddressSanitizer
1073 AddressSanitizer is a clang and gcc extension, included in clang since
1074 v3.1 and gcc since v4.8. It checks illegal heap pointers, global
1075 pointers, stack pointers and use after free errors, and is fast enough
1076 that you can easily compile your debugging or optimized perl with it.
1077 It does not check memory leaks though. AddressSanitizer is available
1078 for Linux, Mac OS X and soon on Windows.
1079
1080 To build perl with AddressSanitizer, your Configure invocation should
1081 look like:
1082
1083 sh Configure -des -Dcc=clang \
1084 -Accflags=-faddress-sanitizer -Aldflags=-faddress-sanitizer \
1085 -Alddlflags=-shared\ -faddress-sanitizer
1086
1087 where these arguments mean:
1088
1089 · -Dcc=clang
1090
1091 This should be replaced by the full path to your clang executable
1092 if it is not in your path.
1093
1094 · -Accflags=-faddress-sanitizer
1095
1096 Compile perl and extensions sources with AddressSanitizer.
1097
1098 · -Aldflags=-faddress-sanitizer
1099
1100 Link the perl executable with AddressSanitizer.
1101
1102 · -Alddlflags=-shared\ -faddress-sanitizer
1103
1104 Link dynamic extensions with AddressSanitizer. You must manually
1105 specify "-shared" because using "-Alddlflags=-shared" will prevent
1106 Configure from setting a default value for "lddlflags", which
1107 usually contains "-shared" (at least on Linux).
1108
1109 See also
1110 <http://code.google.com/p/address-sanitizer/wiki/AddressSanitizer>.
1111
1113 Depending on your platform there are various ways of profiling Perl.
1114
1115 There are two commonly used techniques of profiling executables:
1116 statistical time-sampling and basic-block counting.
1117
1118 The first method takes periodically samples of the CPU program counter,
1119 and since the program counter can be correlated with the code generated
1120 for functions, we get a statistical view of in which functions the
1121 program is spending its time. The caveats are that very small/fast
1122 functions have lower probability of showing up in the profile, and that
1123 periodically interrupting the program (this is usually done rather
1124 frequently, in the scale of milliseconds) imposes an additional
1125 overhead that may skew the results. The first problem can be
1126 alleviated by running the code for longer (in general this is a good
1127 idea for profiling), the second problem is usually kept in guard by the
1128 profiling tools themselves.
1129
1130 The second method divides up the generated code into basic blocks.
1131 Basic blocks are sections of code that are entered only in the
1132 beginning and exited only at the end. For example, a conditional jump
1133 starts a basic block. Basic block profiling usually works by
1134 instrumenting the code by adding enter basic block #nnnn book-keeping
1135 code to the generated code. During the execution of the code the basic
1136 block counters are then updated appropriately. The caveat is that the
1137 added extra code can skew the results: again, the profiling tools
1138 usually try to factor their own effects out of the results.
1139
1140 Gprof Profiling
1141 gprof is a profiling tool available in many Unix platforms which uses
1142 statistical time-sampling. You can build a profiled version of perl by
1143 compiling using gcc with the flag "-pg". Either edit config.sh or re-
1144 run Configure. Running the profiled version of Perl will create an
1145 output file called gmon.out which contains the profiling data collected
1146 during the execution.
1147
1148 quick hint:
1149
1150 $ sh Configure -des -Dusedevel -Accflags='-pg' \
1151 -Aldflags='-pg' -Alddlflags='-pg -shared' \
1152 && make perl
1153 $ ./perl ... # creates gmon.out in current directory
1154 $ gprof ./perl > out
1155 $ less out
1156
1157 (you probably need to add "-shared" to the <-Alddlflags> line until RT
1158 #118199 is resolved)
1159
1160 The gprof tool can then display the collected data in various ways.
1161 Usually gprof understands the following options:
1162
1163 · -a
1164
1165 Suppress statically defined functions from the profile.
1166
1167 · -b
1168
1169 Suppress the verbose descriptions in the profile.
1170
1171 · -e routine
1172
1173 Exclude the given routine and its descendants from the profile.
1174
1175 · -f routine
1176
1177 Display only the given routine and its descendants in the profile.
1178
1179 · -s
1180
1181 Generate a summary file called gmon.sum which then may be given to
1182 subsequent gprof runs to accumulate data over several runs.
1183
1184 · -z
1185
1186 Display routines that have zero usage.
1187
1188 For more detailed explanation of the available commands and output
1189 formats, see your own local documentation of gprof.
1190
1191 GCC gcov Profiling
1192 basic block profiling is officially available in gcc 3.0 and later.
1193 You can build a profiled version of perl by compiling using gcc with
1194 the flags "-fprofile-arcs -ftest-coverage". Either edit config.sh or
1195 re-run Configure.
1196
1197 quick hint:
1198
1199 $ sh Configure -des -Dusedevel -Doptimize='-g' \
1200 -Accflags='-fprofile-arcs -ftest-coverage' \
1201 -Aldflags='-fprofile-arcs -ftest-coverage' \
1202 -Alddlflags='-fprofile-arcs -ftest-coverage -shared' \
1203 && make perl
1204 $ rm -f regexec.c.gcov regexec.gcda
1205 $ ./perl ...
1206 $ gcov regexec.c
1207 $ less regexec.c.gcov
1208
1209 (you probably need to add "-shared" to the <-Alddlflags> line until RT
1210 #118199 is resolved)
1211
1212 Running the profiled version of Perl will cause profile output to be
1213 generated. For each source file an accompanying .gcda file will be
1214 created.
1215
1216 To display the results you use the gcov utility (which should be
1217 installed if you have gcc 3.0 or newer installed). gcov is run on
1218 source code files, like this
1219
1220 gcov sv.c
1221
1222 which will cause sv.c.gcov to be created. The .gcov files contain the
1223 source code annotated with relative frequencies of execution indicated
1224 by "#" markers. If you want to generate .gcov files for all profiled
1225 object files, you can run something like this:
1226
1227 for file in `find . -name \*.gcno`
1228 do sh -c "cd `dirname $file` && gcov `basename $file .gcno`"
1229 done
1230
1231 Useful options of gcov include "-b" which will summarise the basic
1232 block, branch, and function call coverage, and "-c" which instead of
1233 relative frequencies will use the actual counts. For more information
1234 on the use of gcov and basic block profiling with gcc, see the latest
1235 GNU CC manual. As of gcc 4.8, this is at
1236 <http://gcc.gnu.org/onlinedocs/gcc/Gcov-Intro.html#Gcov-Intro>
1237
1239 PERL_DESTRUCT_LEVEL
1240 If you want to run any of the tests yourself manually using e.g.
1241 valgrind, please note that by default perl does not explicitly cleanup
1242 all the memory it has allocated (such as global memory arenas) but
1243 instead lets the exit() of the whole program "take care" of such
1244 allocations, also known as "global destruction of objects".
1245
1246 There is a way to tell perl to do complete cleanup: set the environment
1247 variable PERL_DESTRUCT_LEVEL to a non-zero value. The t/TEST wrapper
1248 does set this to 2, and this is what you need to do too, if you don't
1249 want to see the "global leaks": For example, for running under valgrind
1250
1251 env PERL_DESTRUCT_LEVEL=2 valgrind ./perl -Ilib t/foo/bar.t
1252
1253 (Note: the mod_perl apache module uses also this environment variable
1254 for its own purposes and extended its semantics. Refer to the mod_perl
1255 documentation for more information. Also, spawned threads do the
1256 equivalent of setting this variable to the value 1.)
1257
1258 If, at the end of a run you get the message N scalars leaked, you can
1259 recompile with "-DDEBUG_LEAKING_SCALARS", ("Configure
1260 -Accflags=-DDEBUG_LEAKING_SCALARS"), which will cause the addresses of
1261 all those leaked SVs to be dumped along with details as to where each
1262 SV was originally allocated. This information is also displayed by
1263 Devel::Peek. Note that the extra details recorded with each SV
1264 increases memory usage, so it shouldn't be used in production
1265 environments. It also converts "new_SV()" from a macro into a real
1266 function, so you can use your favourite debugger to discover where
1267 those pesky SVs were allocated.
1268
1269 If you see that you're leaking memory at runtime, but neither valgrind
1270 nor "-DDEBUG_LEAKING_SCALARS" will find anything, you're probably
1271 leaking SVs that are still reachable and will be properly cleaned up
1272 during destruction of the interpreter. In such cases, using the "-Dm"
1273 switch can point you to the source of the leak. If the executable was
1274 built with "-DDEBUG_LEAKING_SCALARS", "-Dm" will output SV allocations
1275 in addition to memory allocations. Each SV allocation has a distinct
1276 serial number that will be written on creation and destruction of the
1277 SV. So if you're executing the leaking code in a loop, you need to
1278 look for SVs that are created, but never destroyed between each cycle.
1279 If such an SV is found, set a conditional breakpoint within "new_SV()"
1280 and make it break only when "PL_sv_serial" is equal to the serial
1281 number of the leaking SV. Then you will catch the interpreter in
1282 exactly the state where the leaking SV is allocated, which is
1283 sufficient in many cases to find the source of the leak.
1284
1285 As "-Dm" is using the PerlIO layer for output, it will by itself
1286 allocate quite a bunch of SVs, which are hidden to avoid recursion.
1287 You can bypass the PerlIO layer if you use the SV logging provided by
1288 "-DPERL_MEM_LOG" instead.
1289
1290 PERL_MEM_LOG
1291 If compiled with "-DPERL_MEM_LOG" ("-Accflags=-DPERL_MEM_LOG"), both
1292 memory and SV allocations go through logging functions, which is handy
1293 for breakpoint setting.
1294
1295 Unless "-DPERL_MEM_LOG_NOIMPL" ("-Accflags=-DPERL_MEM_LOG_NOIMPL") is
1296 also compiled, the logging functions read $ENV{PERL_MEM_LOG} to
1297 determine whether to log the event, and if so how:
1298
1299 $ENV{PERL_MEM_LOG} =~ /m/ Log all memory ops
1300 $ENV{PERL_MEM_LOG} =~ /s/ Log all SV ops
1301 $ENV{PERL_MEM_LOG} =~ /t/ include timestamp in Log
1302 $ENV{PERL_MEM_LOG} =~ /^(\d+)/ write to FD given (default is 2)
1303
1304 Memory logging is somewhat similar to "-Dm" but is independent of
1305 "-DDEBUGGING", and at a higher level; all uses of Newx(), Renew(), and
1306 Safefree() are logged with the caller's source code file and line
1307 number (and C function name, if supported by the C compiler). In
1308 contrast, "-Dm" is directly at the point of "malloc()". SV logging is
1309 similar.
1310
1311 Since the logging doesn't use PerlIO, all SV allocations are logged and
1312 no extra SV allocations are introduced by enabling the logging. If
1313 compiled with "-DDEBUG_LEAKING_SCALARS", the serial number for each SV
1314 allocation is also logged.
1315
1316 DDD over gdb
1317 Those debugging perl with the DDD frontend over gdb may find the
1318 following useful:
1319
1320 You can extend the data conversion shortcuts menu, so for example you
1321 can display an SV's IV value with one click, without doing any typing.
1322 To do that simply edit ~/.ddd/init file and add after:
1323
1324 ! Display shortcuts.
1325 Ddd*gdbDisplayShortcuts: \
1326 /t () // Convert to Bin\n\
1327 /d () // Convert to Dec\n\
1328 /x () // Convert to Hex\n\
1329 /o () // Convert to Oct(\n\
1330
1331 the following two lines:
1332
1333 ((XPV*) (())->sv_any )->xpv_pv // 2pvx\n\
1334 ((XPVIV*) (())->sv_any )->xiv_iv // 2ivx
1335
1336 so now you can do ivx and pvx lookups or you can plug there the sv_peek
1337 "conversion":
1338
1339 Perl_sv_peek(my_perl, (SV*)()) // sv_peek
1340
1341 (The my_perl is for threaded builds.) Just remember that every line,
1342 but the last one, should end with \n\
1343
1344 Alternatively edit the init file interactively via: 3rd mouse button ->
1345 New Display -> Edit Menu
1346
1347 Note: you can define up to 20 conversion shortcuts in the gdb section.
1348
1349 C backtrace
1350 On some platforms Perl supports retrieving the C level backtrace
1351 (similar to what symbolic debuggers like gdb do).
1352
1353 The backtrace returns the stack trace of the C call frames, with the
1354 symbol names (function names), the object names (like "perl"), and if
1355 it can, also the source code locations (file:line).
1356
1357 The supported platforms are Linux, and OS X (some *BSD might work at
1358 least partly, but they have not yet been tested).
1359
1360 This feature hasn't been tested with multiple threads, but it will only
1361 show the backtrace of the thread doing the backtracing.
1362
1363 The feature needs to be enabled with "Configure -Dusecbacktrace".
1364
1365 The "-Dusecbacktrace" also enables keeping the debug information when
1366 compiling/linking (often: "-g"). Many compilers/linkers do support
1367 having both optimization and keeping the debug information. The debug
1368 information is needed for the symbol names and the source locations.
1369
1370 Static functions might not be visible for the backtrace.
1371
1372 Source code locations, even if available, can often be missing or
1373 misleading if the compiler has e.g. inlined code. Optimizer can make
1374 matching the source code and the object code quite challenging.
1375
1376 Linux
1377 You must have the BFD (-lbfd) library installed, otherwise "perl"
1378 will fail to link. The BFD is usually distributed as part of the
1379 GNU binutils.
1380
1381 Summary: "Configure ... -Dusecbacktrace" and you need "-lbfd".
1382
1383 OS X
1384 The source code locations are supported only if you have the
1385 Developer Tools installed. (BFD is not needed.)
1386
1387 Summary: "Configure ... -Dusecbacktrace" and installing the
1388 Developer Tools would be good.
1389
1390 Optionally, for trying out the feature, you may want to enable
1391 automatic dumping of the backtrace just before a warning or croak (die)
1392 message is emitted, by adding "-Accflags=-DUSE_C_BACKTRACE_ON_ERROR"
1393 for Configure.
1394
1395 Unless the above additional feature is enabled, nothing about the
1396 backtrace functionality is visible, except for the Perl/XS level.
1397
1398 Furthermore, even if you have enabled this feature to be compiled, you
1399 need to enable it in runtime with an environment variable:
1400 "PERL_C_BACKTRACE_ON_ERROR=10". It must be an integer higher than
1401 zero, telling the desired frame count.
1402
1403 Retrieving the backtrace from Perl level (using for example an XS
1404 extension) would be much less exciting than one would hope: normally
1405 you would see "runops", "entersub", and not much else. This API is
1406 intended to be called from within the Perl implementation, not from
1407 Perl level execution.
1408
1409 The C API for the backtrace is as follows:
1410
1411 get_c_backtrace
1412 free_c_backtrace
1413 get_c_backtrace_dump
1414 dump_c_backtrace
1415
1416 Poison
1417 If you see in a debugger a memory area mysteriously full of 0xABABABAB
1418 or 0xEFEFEFEF, you may be seeing the effect of the Poison() macros, see
1419 perlclib.
1420
1421 Read-only optrees
1422 Under ithreads the optree is read only. If you want to enforce this,
1423 to check for write accesses from buggy code, compile with
1424 "-Accflags=-DPERL_DEBUG_READONLY_OPS" to enable code that allocates op
1425 memory via "mmap", and sets it read-only when it is attached to a
1426 subroutine. Any write access to an op results in a "SIGBUS" and abort.
1427
1428 This code is intended for development only, and may not be portable
1429 even to all Unix variants. Also, it is an 80% solution, in that it
1430 isn't able to make all ops read only. Specifically it does not apply
1431 to op slabs belonging to "BEGIN" blocks.
1432
1433 However, as an 80% solution it is still effective, as it has caught
1434 bugs in the past.
1435
1436 When is a bool not a bool?
1437 On pre-C99 compilers, "bool" is defined as equivalent to "char".
1438 Consequently assignment of any larger type to a "bool" is unsafe and
1439 may be truncated. The "cBOOL" macro exists to cast it correctly; you
1440 may also find that using it is shorter and clearer than writing out the
1441 equivalent conditional expression longhand.
1442
1443 On those platforms and compilers where "bool" really is a boolean (C++,
1444 C99), it is easy to forget the cast. You can force "bool" to be a
1445 "char" by compiling with "-Accflags=-DPERL_BOOL_AS_CHAR". You may also
1446 wish to run "Configure" with something like
1447
1448 -Accflags='-Wconversion -Wno-sign-conversion -Wno-shorten-64-to-32'
1449
1450 or your compiler's equivalent to make it easier to spot any unsafe
1451 truncations that show up.
1452
1453 The "TRUE" and "FALSE" macros are available for situations where using
1454 them would clarify intent. (But they always just mean the same as the
1455 integers 1 and 0 regardless, so using them isn't compulsory.)
1456
1457 The .i Targets
1458 You can expand the macros in a foo.c file by saying
1459
1460 make foo.i
1461
1462 which will expand the macros using cpp. Don't be scared by the
1463 results.
1464
1466 This document was originally written by Nathan Torkington, and is
1467 maintained by the perl5-porters mailing list.
1468
1469
1470
1471perl v5.30.2 2020-03-27 PERLHACKTIPS(1)