5 Consistent formatting of this file is achieved with:
6 perl ./Porting/podtidy pod/perlhacktips.pod
10 perlhacktips - Tips for Perl core C code hacking
14 This document will help you learn the best way to go about hacking on
15 the Perl core C code. It covers common problems, debugging, profiling,
18 If you haven't read L<perlhack> and L<perlhacktut> yet, you might want
21 =head1 COMMON PROBLEMS
23 Perl source now permits some specific C99 features which we know are
24 supported by all platforms, but mostly plays by ANSI C89 rules.
25 You don't care about some particular platform having broken Perl? I
26 hear there is still a strong demand for J2EE programmers.
28 =head2 Perl environment problems
34 Not compiling with threading
36 Compiling with threading (-Duseithreads) completely rewrites the
37 function prototypes of Perl. You better try your changes with that.
38 Related to this is the difference between "Perl_-less" and "Perl_-ly"
41 Perl_sv_setiv(aTHX_ ...);
44 The first one explicitly passes in the context, which is needed for
45 e.g. threaded builds. The second one does that implicitly; do not get
46 them mixed. If you are not passing in a aTHX_, you will need to do a
47 dTHX as the first thing in the function.
49 See L<perlguts/"How multiple interpreters and concurrency are
50 supported"> for further discussion about context.
54 Not compiling with -DDEBUGGING
56 The DEBUGGING define exposes more code to the compiler, therefore more
57 ways for things to go wrong. You should try it.
61 Introducing (non-read-only) globals
63 Do not introduce any modifiable globals, truly global or file static.
64 They are bad form and complicate multithreading and other forms of
65 concurrency. The right way is to introduce them as new interpreter
66 variables, see F<intrpvar.h> (at the very end for binary
69 Introducing read-only (const) globals is okay, as long as you verify
70 with e.g. C<nm libperl.a|egrep -v ' [TURtr] '> (if your C<nm> has
71 BSD-style output) that the data you added really is read-only. (If it
72 is, it shouldn't show up in the output of that command.)
74 If you want to have static strings, make them constant:
76 static const char etc[] = "...";
78 If you want to have arrays of constant strings, note carefully the
79 right combination of C<const>s:
81 static const char * const yippee[] =
82 {"hi", "ho", "silver"};
86 Not exporting your new function
88 Some platforms (Win32, AIX, VMS, OS/2, to name a few) require any
89 function that is part of the public API (the shared Perl library) to be
90 explicitly marked as exported. See the discussion about F<embed.pl> in
95 Exporting your new function
97 The new shiny result of either genuine new functionality or your
98 arduous refactoring is now ready and correctly exported. So what could
101 Maybe simply that your function did not need to be exported in the
102 first place. Perl has a long and not so glorious history of exporting
103 functions that it should not have.
105 If the function is used only inside one source code file, make it
106 static. See the discussion about F<embed.pl> in L<perlguts>.
108 If the function is used across several files, but intended only for
109 Perl's internal use (and this should be the common case), do not export
110 it to the public API. See the discussion about F<embed.pl> in
117 Starting from 5.35.5 we now permit some C99 features in the core C source.
118 However, code in dual life extensions still needs to be C89 only, because it
119 needs to compile against earlier version of Perl running on older platforms.
120 Also note that our headers need to also be valid as C++, because XS extensions
121 written in C++ need to include them, hence I<member structure initialisers>
122 can't be used in headers.
124 C99 support is still far from complete on all platforms we currently support.
125 As a baseline we can only assume C89 semantics with the specific C99 features
126 described below, which we've verified work everywhere. It's fine to probe for
127 additional C99 features and use them where available, providing there is also a
128 fallback for compilers that don't support the feature. For example, we use C11
129 thread local storage when available, but fall back to POSIX thread specific
130 APIs otherwise, and we use C<char> for booleans if C<< <stdbool.h> >> isn't
133 Code can use (and rely on) the following C99 features being present
139 mixed declarations and code
145 For consistency with the existing source code, use the typedefs C<I64> and
146 C<U64>, instead of using C<long long> and C<unsigned long long> directly.
152 void greet(char *file, unsigned int line, char *format, ...);
153 #define logged_greet(...) greet(__FILE__, __LINE__, __VA_ARGS__);
155 Note that C<__VA_OPT__> is a gcc extension not yet in any published standard.
159 declarations in for loops
161 for (const char *p = message; *p; ++p) {
167 member structure initialisers
169 But not in headers, as support was only added to C++ relatively recently.
171 Hence this is fine in C and XS code, but not headers:
178 struct message mcguffin = {
179 .target = "member structure initialisers",
185 flexible array members
187 This is standards conformant:
194 However, the source code already uses the "unwarranted chumminess with the
195 compiler" hack in many places:
202 Strictly it B<is> undefined behaviour accessing beyond C<message[0]>, but this
203 has been a commonly used hack since K&R times, and using it hasn't been a
204 practical issue anywhere (in the perl source or any other common C code).
205 Hence it's unclear what we would gain from actively changing to the C99
212 All compilers we tested support their use. Not all humans we tested support
217 Code explicitly should not use any other C99 features. For example
223 variable length arrays
225 Not supported by B<any> MSVC, and this is not going to change.
227 Even "variable" length arrays where the variable is a constant expression
228 are syntax errors under MSVC.
232 C99 types in C<< <stdint.h> >>
234 Use C<PERL_INT_FAST8_T> etc as defined in F<handy.h>
238 C99 format strings in C<< <inttypes> >>
240 C<snprintf> in the VMS libc only added support for C<PRIdN> etc very recently,
241 meaning that there are live supported installations without this, or formats
244 (perl's C<sv_catpvf> etc use parser code code in C<sv.c>, which supports the
245 C<z> modifier, along with perl-specific formats such as C<SVf>.)
249 If you want to use a C99 feature not listed above then you need to do one of
255 Probe for it in F<Configure>, set a variable in F<config.sh>, and add fallback logic in the headers for platforms which don't have it.
259 Write test code and verify that it works on platforms we need to support, before relying on it unconditionally.
263 Likely you want to repeat the same plan as we used to get the current C99
264 feature set. See the message at https://markmail.org/thread/odr4fjrn72u2fkpz
265 for the C99 probes we used before. Note that the two most "fussy" compilers
266 appear to be MSVC and the vendor compiler on VMS. To date all the *nix
267 compilers have been far more flexible in what they support.
269 On *nix platforms, F<Configure> attempts to set compiler flags appropriately.
270 All vendor compilers that we tested defaulted to C99 (or C11) support.
271 However, older versions of gcc default to C89, or permit I<most> C99 (with
272 warnings), but forbid I<declarations in for loops> unless C<-std=gnu99> is
273 added. The alternative C<-std=c99> B<might> seem better, but using it on some
274 platforms can prevent C<< <unistd.h> >> declaring some prototypes being
275 declared, which breaks the build. gcc's C<-ansi> flag implies C<-std=c89> so we
276 can no longer set that, hence the Configure option C<-gccansipedantic> now only
279 The Perl core source code files (the ones at the top level of the source code
280 distribution) are automatically compiled with as many as possible of the
281 C<-std=gnu99>, C<-pedantic>, and a selection of C<-W> flags (see
282 cflags.SH). Files in F<ext/> F<dist/> F<cpan/> etc are compiled with the same
283 flags as the installed perl would use to compile XS extensions.
285 Basically, it's safe to assume that F<Configure> and F<cflags.SH> have
286 picked the best combination of flags for the version of gcc on the platform,
287 and attempting to add more flags related to enforcing a C dialect will
288 cause problems either locally, or on other systems that the code is shipped
291 We believe that the C99 support in gcc 3.1 is good enough for us, but we don't
292 have a 19 year old gcc handy to check this :-)
293 If you have ancient vendor compilers that don't default to C99, the flags
294 you might want to try are
312 =head2 Portability problems
314 The following are common causes of compilation and/or execution
315 failures, not common to Perl as such. The C FAQ is good bedtime
316 reading. Please test your changes with as many C compilers and
317 platforms as possible; we will, anyway, and it's nice to save oneself
318 from public embarrassment.
320 Also study L<perlport> carefully to avoid any bad assumptions about the
321 operating system, filesystems, character set, and so forth.
323 Do not assume an operating system indicates a certain compiler.
329 Casting pointers to integers or casting integers to pointers
341 Both are bad, and broken, and unportable. Use the PTR2IV() macro that
342 does it right. (Likewise, there are PTR2UV(), PTR2NV(), INT2PTR(), and
347 Casting between function pointers and data pointers
349 Technically speaking casting between function pointers and data
350 pointers is unportable and undefined, but practically speaking it seems
351 to work, but you should use the FPTR2DPTR() and DPTR2FPTR() macros.
352 Sometimes you can also play games with unions.
356 Assuming sizeof(int) == sizeof(long)
358 There are platforms where longs are 64 bits, and platforms where ints
359 are 64 bits, and while we are out to shock you, even platforms where
360 shorts are 64 bits. This is all legal according to the C standard. (In
361 other words, "long long" is not a portable way to specify 64 bits, and
362 "long long" is not even guaranteed to be any wider than "long".)
364 Instead, use the definitions IV, UV, IVSIZE, I32SIZE, and so forth.
365 Avoid things like I32 because they are B<not> guaranteed to be
366 I<exactly> 32 bits, they are I<at least> 32 bits, nor are they
367 guaranteed to be B<int> or B<long>. If you explicitly need
368 64-bit variables, use I64 and U64.
372 Assuming one can dereference any type of pointer for any type of data
375 long pony = *(long *)p; /* BAD */
377 Many platforms, quite rightly so, will give you a core dump instead of
378 a pony if the p happens not to be correctly aligned.
384 (int)*p = ...; /* BAD */
386 Simply not portable. Get your lvalue to be of the right type, or maybe
387 use temporary variables, or dirty tricks with unions.
391 Assume B<anything> about structs (especially the ones you don't
392 control, like the ones coming from the system headers)
398 That a certain field exists in a struct
402 That no other fields exist besides the ones you know of
406 That a field is of certain signedness, sizeof, or type
410 That the fields are in a certain order
416 While C guarantees the ordering specified in the struct definition,
417 between different platforms the definitions might differ
423 That the sizeof(struct) or the alignments are the same everywhere
429 There might be padding bytes between the fields to align the fields -
430 the bytes can be anything
434 Structs are required to be aligned to the maximum alignment required by
435 the fields - which for native types is for usually equivalent to
436 sizeof() of the field
444 Assuming the character set is ASCIIish
446 Perl can compile and run under EBCDIC platforms. See L<perlebcdic>.
447 This is transparent for the most part, but because the character sets
448 differ, you shouldn't use numeric (decimal, octal, nor hex) constants
449 to refer to characters. You can safely say C<'A'>, but not C<0x41>.
450 You can safely say C<'\n'>, but not C<\012>. However, you can use
451 macros defined in F<utf8.h> to specify any code point portably.
452 C<LATIN1_TO_NATIVE(0xDF)> is going to be the code point that means
453 LATIN SMALL LETTER SHARP S on whatever platform you are running on (on
454 ASCII platforms it compiles without adding any extra code, so there is
455 zero performance hit on those). The acceptable inputs to
456 C<LATIN1_TO_NATIVE> are from C<0x00> through C<0xFF>. If your input
457 isn't guaranteed to be in that range, use C<UNICODE_TO_NATIVE> instead.
458 C<NATIVE_TO_LATIN1> and C<NATIVE_TO_UNICODE> translate the opposite
461 If you need the string representation of a character that doesn't have a
462 mnemonic name in C, you should add it to the list in
463 F<regen/unicode_constants.pl>, and have Perl create C<#define>'s for you,
464 based on the current platform.
466 Note that the C<isI<FOO>> and C<toI<FOO>> macros in F<handy.h> work
467 properly on native code points and strings.
469 Also, the range 'A' - 'Z' in ASCII is an unbroken sequence of 26 upper
470 case alphabetic characters. That is not true in EBCDIC. Nor for 'a' to
471 'z'. But '0' - '9' is an unbroken range in both systems. Don't assume
472 anything about other ranges. (Note that special handling of ranges in
473 regular expression patterns and transliterations makes it appear to Perl
474 code that the aforementioned ranges are all unbroken.)
476 Many of the comments in the existing code ignore the possibility of
477 EBCDIC, and may be wrong therefore, even if the code works. This is
478 actually a tribute to the successful transparent insertion of being
479 able to handle EBCDIC without having to change pre-existing code.
481 UTF-8 and UTF-EBCDIC are two different encodings used to represent
482 Unicode code points as sequences of bytes. Macros with the same names
483 (but different definitions) in F<utf8.h> and F<utfebcdic.h> are used to
484 allow the calling code to think that there is only one such encoding.
485 This is almost always referred to as C<utf8>, but it means the EBCDIC
486 version as well. Again, comments in the code may well be wrong even if
487 the code itself is right. For example, the concept of UTF-8 C<invariant
488 characters> differs between ASCII and EBCDIC. On ASCII platforms, only
489 characters that do not have the high-order bit set (i.e. whose ordinals
490 are strict ASCII, 0 - 127) are invariant, and the documentation and
491 comments in the code may assume that, often referring to something
492 like, say, C<hibit>. The situation differs and is not so simple on
493 EBCDIC machines, but as long as the code itself uses the
494 C<NATIVE_IS_INVARIANT()> macro appropriately, it works, even if the
497 As noted in L<perlhack/TESTING>, when writing test scripts, the file
498 F<t/charset_tools.pl> contains some helpful functions for writing tests
499 valid on both ASCII and EBCDIC platforms. Sometimes, though, a test
500 can't use a function and it's inconvenient to have different test
501 versions depending on the platform. There are 20 code points that are
502 the same in all 4 character sets currently recognized by Perl (the 3
503 EBCDIC code pages plus ISO 8859-1 (ASCII/Latin1)). These can be used in
504 such tests, though there is a small possibility that Perl will become
505 available in yet another character set, breaking your test. All but one
506 of these code points are C0 control characters. The most significant
507 controls that are the same are C<\0>, C<\r>, and C<\N{VT}> (also
508 specifiable as C<\cK>, C<\x0B>, C<\N{U+0B}>, or C<\013>). The single
509 non-control is U+00B6 PILCROW SIGN. The controls that are the same have
510 the same bit pattern in all 4 character sets, regardless of the UTF8ness
511 of the string containing them. The bit pattern for U+B6 is the same in
512 all 4 for non-UTF8 strings, but differs in each when its containing
513 string is UTF-8 encoded. The only other code points that have some sort
514 of sameness across all 4 character sets are the pair 0xDC and 0xFC.
515 Together these represent upper- and lowercase LATIN LETTER U WITH
516 DIAERESIS, but which is upper and which is lower may be reversed: 0xDC
517 is the capital in Latin1 and 0xFC is the small letter, while 0xFC is the
518 capital in EBCDIC and 0xDC is the small one. This factoid may be
519 exploited in writing case insensitive tests that are the same across all
524 Assuming the character set is just ASCII
526 ASCII is a 7 bit encoding, but bytes have 8 bits in them. The 128 extra
527 characters have different meanings depending on the locale. Absent a
528 locale, currently these extra characters are generally considered to be
529 unassigned, and this has presented some problems. This has being
530 changed starting in 5.12 so that these characters can be considered to
531 be Latin-1 (ISO-8859-1).
535 Mixing #define and #ifdef
537 #define BURGLE(x) ... \
538 #ifdef BURGLE_OLD_STYLE /* BAD */
539 ... do it the old way ... \
541 ... do it the new way ... \
544 You cannot portably "stack" cpp directives. For example in the above
545 you need two separate BURGLE() #defines, one for each #ifdef branch.
549 Adding non-comment stuff after #endif or #else
553 #else !SNOSH /* BAD */
555 #endif SNOSH /* BAD */
557 The #endif and #else cannot portably have anything non-comment after
558 them. If you want to document what is going (which is a good idea
559 especially if the branches are long), use (C) comments:
567 The gcc option C<-Wendif-labels> warns about the bad variant (by
568 default on starting from Perl 5.9.4).
572 Having a comma after the last element of an enum list
580 is not portable. Leave out the last comma.
582 Also note that whether enums are implicitly morphable to ints varies
583 between compilers, you might need to (int).
587 Mixing signed char pointers with unsigned char pointers
589 int foo(char *s) { ... }
591 unsigned char *t = ...; /* Or U8* t = ... */
594 While this is legal practice, it is certainly dubious, and downright
595 fatal in at least one platform: for example VMS cc considers this a
596 fatal error. One cause for people often making this mistake is that a
597 "naked char" and therefore dereferencing a "naked char pointer" have an
598 undefined signedness: it depends on the compiler and the flags of the
599 compiler and the underlying platform whether the result is signed or
600 unsigned. For this very same reason using a 'char' as an array index is
605 Macros that have string constants and their arguments as substrings of
608 #define FOO(n) printf("number = %d\n", n) /* BAD */
611 Pre-ANSI semantics for that was equivalent to
613 printf("10umber = %d\10");
615 which is probably not what you were expecting. Unfortunately at least
616 one reasonably common and modern C compiler does "real backward
617 compatibility" here, in AIX that is what still happens even though the
618 rest of the AIX compiler is very happily C89.
622 Using printf formats for non-basic C types
625 printf("i = %d\n", i); /* BAD */
627 While this might by accident work in some platform (where IV happens to
628 be an C<int>), in general it cannot. IV might be something larger. Even
629 worse the situation is with more specific types (defined by Perl's
630 configuration step in F<config.h>):
633 printf("who = %d\n", who); /* BAD */
635 The problem here is that Uid_t might be not only not C<int>-wide but it
636 might also be unsigned, in which case large uids would be printed as
639 There is no simple solution to this because of printf()'s limited
640 intelligence, but for many types the right format is available as with
641 either 'f' or '_f' suffix, for example:
643 IVdf /* IV in decimal */
644 UVxf /* UV is hexadecimal */
646 printf("i = %"IVdf"\n", i); /* The IVdf is a string constant. */
648 Uid_t_f /* Uid_t in decimal */
650 printf("who = %"Uid_t_f"\n", who);
652 Or you can try casting to a "wide enough" type:
654 printf("i = %"IVdf"\n", (IV)something_very_small_and_signed);
656 See L<perlguts/Formatted Printing of Size_t and SSize_t> for how to
659 Also remember that the C<%p> format really does require a void pointer:
662 printf("p = %p\n", (void*)p);
664 The gcc option C<-Wformat> scans for such problems.
668 Blindly passing va_list
670 Not all platforms support passing va_list to further varargs (stdarg)
671 functions. The right thing to do is to copy the va_list using the
672 Perl_va_copy() if the NEED_VA_COPY is defined.
676 Using gcc statement expressions
678 val = ({...;...;...}); /* BAD */
680 While a nice extension, it's not portable. Historically, Perl used
681 them in macros if available to gain some extra speed (essentially
682 as a funky form of inlining), but we now support (or emulate) C99
683 C<static inline> functions, so use them instead. Declare functions as
684 C<PERL_STATIC_INLINE> to transparently fall back to emulation where needed.
688 Binding together several statements in a macro
690 Use the macros STMT_START and STMT_END.
698 Testing for operating systems or versions when should be testing for
701 #ifdef __FOONIX__ /* BAD */
705 Unless you know with 100% certainty that quux() is only ever available
706 for the "Foonix" operating system B<and> that is available B<and>
707 correctly working for B<all> past, present, B<and> future versions of
708 "Foonix", the above is very wrong. This is more correct (though still
709 not perfect, because the below is a compile-time check):
715 How does the HAS_QUUX become defined where it needs to be? Well, if
716 Foonix happens to be Unixy enough to be able to run the Configure
717 script, and Configure has been taught about detecting and testing
718 quux(), the HAS_QUUX will be correctly defined. In other platforms, the
719 corresponding configuration step will hopefully do the same.
721 In a pinch, if you cannot wait for Configure to be educated, or if you
722 have a good hunch of where quux() might be available, you can
723 temporarily try the following:
725 #if (defined(__FOONIX__) || defined(__BARNIX__))
735 But in any case, try to keep the features and operating systems
738 A good resource on the predefined macros for various operating
739 systems, compilers, and so forth is
740 L<http://sourceforge.net/p/predef/wiki/Home/>
744 Assuming the contents of static memory pointed to by the return values
745 of Perl wrappers for C library functions doesn't change. Many C library
746 functions return pointers to static storage that can be overwritten by
747 subsequent calls to the same or related functions. Perl has
748 light-weight wrappers for some of these functions, and which don't make
749 copies of the static memory. A good example is the interface to the
750 environment variables that are in effect for the program. Perl has
751 C<PerlEnv_getenv> to get values from the environment. But the return is
752 a pointer to static memory in the C library. If you are using the value
753 to immediately test for something, that's fine, but if you save the
754 value and expect it to be unchanged by later processing, you would be
755 wrong, but perhaps you wouldn't know it because different C library
756 implementations behave differently, and the one on the platform you're
757 testing on might work for your situation. But on some platforms, a
758 subsequent call to C<PerlEnv_getenv> or related function WILL overwrite
759 the memory that your first call points to. This has led to some
760 hard-to-debug problems. Do a L<perlapi/savepv> to make a copy, thus
761 avoiding these problems. You will have to free the copy when you're
762 done to avoid memory leaks. If you don't have control over when it gets
763 freed, you'll need to make the copy in a mortal scalar, like so:
765 if ((s = PerlEnv_getenv("foo") == NULL) {
766 ... /* handle NULL case */
769 s = SvPVX(sv_2mortal(newSVpv(s, 0)));
772 The above example works only if C<"s"> is C<NUL>-terminated; otherwise
773 you have to pass its length to C<newSVpv>.
777 =head2 Problematic System Interfaces
783 Perl strings are NOT the same as C strings: They may contain C<NUL>
784 characters, whereas a C string is terminated by the first C<NUL>.
785 That is why Perl API functions that deal with strings generally take a
786 pointer to the first byte and either a length or a pointer to the byte
787 just beyond the final one.
789 And this is the reason that many of the C library string handling
790 functions should not be used. They don't cope with the full generality
791 of Perl strings. It may be that your test cases don't have embedded
792 C<NUL>s, and so the tests pass, whereas there may well eventually arise
793 real-world cases where they fail. A lesson here is to include C<NUL>s
794 in your tests. Now it's fairly rare in most real world cases to get
795 C<NUL>s, so your code may seem to work, until one day a C<NUL> comes
798 Here's an example. It used to be a common paradigm, for decades, in the
799 perl core to use S<C<strchr("list", c)>> to see if the character C<c> is
800 any of the ones given in C<"list">, a double-quote-enclosed string of
801 the set of characters that we are seeing if C<c> is one of. As long as
802 C<c> isn't a C<NUL>, it works. But when C<c> is a C<NUL>, C<strchr>
803 returns a pointer to the terminating C<NUL> in C<"list">. This likely
804 will result in a segfault or a security issue when the caller uses that
805 end pointer as the starting point to read from.
807 A solution to this and many similar issues is to use the C<mem>I<-foo> C
808 library functions instead. In this case C<memchr> can be used to see if
809 C<c> is in C<"list"> and works even if C<c> is C<NUL>. These functions
810 need an additional parameter to give the string length.
811 In the case of literal string parameters, perl has defined macros that
812 calculate the length for you. See L<perlapi/String Handling>.
816 malloc(0), realloc(0), calloc(0, 0) are non-portable. To be portable
817 allocate at least one byte. (In general you should rarely need to work
818 at this low level, but instead use the various malloc wrappers.)
822 snprintf() - the return type is unportable. Use my_snprintf() instead.
826 =head2 Security problems
828 Last but not least, here are various tips for safer coding.
829 See also L<perlclib> for libc/stdio replacements one should use.
837 Or we will publicly ridicule you. Seriously.
843 Use mkstemp() instead.
847 Do not use strcpy() or strcat() or strncpy() or strncat()
849 Use my_strlcpy() and my_strlcat() instead: they either use the native
850 implementation, or Perl's own implementation (borrowed from the public
851 domain implementation of INN).
855 Do not use sprintf() or vsprintf()
857 If you really want just plain byte strings, use my_snprintf() and
858 my_vsnprintf() instead, which will try to use snprintf() and
859 vsnprintf() if those safer APIs are available. If you want something
860 fancier than a plain byte string, use
861 L<C<Perl_form>()|perlapi/form> or SVs and
862 L<C<Perl_sv_catpvf()>|perlapi/sv_catpvf>.
864 Note that glibc C<printf()>, C<sprintf()>, etc. are buggy before glibc
865 version 2.17. They won't allow a C<%.s> format with a precision to
866 create a string that isn't valid UTF-8 if the current underlying locale
867 of the program is UTF-8. What happens is that the C<%s> and its operand are
868 simply skipped without any notice.
869 L<https://sourceware.org/bugzilla/show_bug.cgi?id=6530>.
875 Use grok_atoUV() instead. atoi() has ill-defined behavior on overflows,
876 and cannot be used for incremental parsing. It is also affected by locale,
881 Do not use strtol() or strtoul()
883 Use grok_atoUV() instead. strtol() or strtoul() (or their IV/UV-friendly
884 macro disguises, Strtol() and Strtoul(), or Atol() and Atoul() are
885 affected by locale, which is bad.
891 You can compile a special debugging version of Perl, which allows you
892 to use the C<-D> option of Perl to tell more about what Perl is doing.
893 But sometimes there is no alternative than to dive in with a debugger,
894 either to see the stack trace of a core dump (very useful in a bug
895 report), or trying to figure out what went wrong before the core dump
896 happened, or how did we end up having wrong or unexpected results.
898 =head2 Poking at Perl
900 To really poke around with Perl, you'll probably want to build Perl for
901 debugging, like this:
903 ./Configure -d -DDEBUGGING
906 C<-DDEBUGGING> turns on the C compiler's C<-g> flag to have it produce
907 debugging information which will allow us to step through a running
908 program, and to see in which C function we are at (without the debugging
909 information we might see only the numerical addresses of the functions,
910 which is not very helpful). It will also turn on the C<DEBUGGING>
911 compilation symbol which enables all the internal debugging code in Perl.
912 There are a whole bunch of things you can debug with this:
913 L<perlrun|perlrun/-Dletters> lists them all, and the best way to find out
914 about them is to play about with them. The most useful options are
917 l Context (loop) stack processing
918 s Stack snapshots (with v, displays all stacks)
920 o Method and overloading resolution
921 c String/numeric conversions
925 $ perl -Dst -e '$a + 1'
935 Some of the functionality of the debugging code can be achieved with a
936 non-debugging perl by using XS modules:
938 -Dr => use re 'debug'
941 =head2 Using a source-level debugger
943 If the debugging output of C<-D> doesn't help you, it's time to step
944 through perl's execution with a source-level debugger.
950 We'll use C<gdb> for our examples here; the principles will apply to
951 any debugger (many vendors call their debugger C<dbx>), but check the
952 manual of the one you're using.
956 To fire up the debugger, type
960 Or if you have a core dump:
964 You'll want to do that in your Perl source tree so the debugger can
965 read the source code. You should see the copyright message, followed by
970 C<help> will get you into the documentation, but here are the most
977 Run the program with the given arguments.
979 =item * break function_name
981 =item * break source.c:xxx
983 Tells the debugger that we'll want to pause execution when we reach
984 either the named function (but see L<perlguts/Internal Functions>!) or
985 the given line in the named source file.
989 Steps through the program a line at a time.
993 Steps through the program a line at a time, without descending into
998 Run until the next breakpoint.
1002 Run until the end of the current function, then stop again.
1006 Just pressing Enter will do the most recent operation again - it's a
1007 blessing when stepping through miles of source code.
1011 Prints the C definition of the argument given.
1017 OP *(*op_ppaddr)(void);
1019 unsigned int op_type : 9;
1020 unsigned int op_opt : 1;
1021 unsigned int op_slabbed : 1;
1022 unsigned int op_savefree : 1;
1023 unsigned int op_static : 1;
1024 unsigned int op_folded : 1;
1025 unsigned int op_spare : 2;
1032 Execute the given C code and print its results. B<WARNING>: Perl makes
1033 heavy use of macros, and F<gdb> does not necessarily support macros
1034 (see later L</"gdb macro support">). You'll have to substitute them
1035 yourself, or to invoke cpp on the source code files (see L</"The .i
1036 Targets">) So, for instance, you can't say
1038 print SvPV_nolen(sv)
1042 print Perl_sv_2pv_nolen(sv)
1046 You may find it helpful to have a "macro dictionary", which you can
1047 produce by saying C<cpp -dM perl.c | sort>. Even then, F<cpp> won't
1048 recursively apply those macros for you.
1050 =head2 gdb macro support
1052 Recent versions of F<gdb> have fairly good macro support, but in order
1053 to use it you'll need to compile perl with macro definitions included
1054 in the debugging information. Using F<gcc> version 3.1, this means
1055 configuring with C<-Doptimize=-g3>. Other compilers might use a
1056 different switch (if they support debugging macros at all).
1058 =head2 Dumping Perl Data Structures
1060 One way to get around this macro hell is to use the dumping functions
1061 in F<dump.c>; these work a little like an internal
1062 L<Devel::Peek|Devel::Peek>, but they also cover OPs and other
1063 structures that you can't get at from Perl. Let's take an example.
1064 We'll use the C<$a = $b + $c> we used before, but give it a bit of
1065 context: C<$b = "6XXXX"; $c = 2.3;>. Where's a good place to stop and
1068 What about C<pp_add>, the function we examined earlier to implement the
1071 (gdb) break Perl_pp_add
1072 Breakpoint 1 at 0x46249f: file pp_hot.c, line 309.
1074 Notice we use C<Perl_pp_add> and not C<pp_add> - see
1075 L<perlguts/Internal Functions>. With the breakpoint in place, we can
1078 (gdb) run -e '$b = "6XXXX"; $c = 2.3; $a = $b + $c'
1080 Lots of junk will go past as gdb reads in the relevant source files and
1081 libraries, and then:
1083 Breakpoint 1, Perl_pp_add () at pp_hot.c:309
1084 1396 dSP; dATARGET; bool useleft; SV *svl, *svr;
1089 We looked at this bit of code before, and we said that
1090 C<dPOPTOPnnrl_ul> arranges for two C<NV>s to be placed into C<left> and
1091 C<right> - let's slightly expand it:
1093 #define dPOPTOPnnrl_ul NV right = POPn; \
1094 SV *leftsv = TOPs; \
1095 NV left = USE_LEFT(leftsv) ? SvNV(leftsv) : 0.0
1097 C<POPn> takes the SV from the top of the stack and obtains its NV
1098 either directly (if C<SvNOK> is set) or by calling the C<sv_2nv>
1099 function. C<TOPs> takes the next SV from the top of the stack - yes,
1100 C<POPn> uses C<TOPs> - but doesn't remove it. We then use C<SvNV> to
1101 get the NV from C<leftsv> in the same way as before - yes, C<POPn> uses
1104 Since we don't have an NV for C<$b>, we'll have to use C<sv_2nv> to
1105 convert it. If we step again, we'll find ourselves there:
1108 Perl_sv_2nv (sv=0xa0675d0) at sv.c:1669
1112 We can now use C<Perl_sv_dump> to investigate the SV:
1114 (gdb) print Perl_sv_dump(sv)
1115 SV = PV(0xa057cc0) at 0xa0675d0
1118 PV = 0xa06a510 "6XXXX"\0
1123 We know we're going to get C<6> from this, so let's finish the
1127 Run till exit from #0 Perl_sv_2nv (sv=0xa0675d0) at sv.c:1671
1128 0x462669 in Perl_pp_add () at pp_hot.c:311
1131 We can also dump out this op: the current op is always stored in
1132 C<PL_op>, and we can dump it with C<Perl_op_dump>. This'll give us
1133 similar output to CPAN module B::Debug.
1135 (gdb) print Perl_op_dump(PL_op)
1137 13 TYPE = add ===> 14
1139 FLAGS = (SCALAR,KIDS)
1141 TYPE = null ===> (12)
1143 FLAGS = (SCALAR,KIDS)
1145 11 TYPE = gvsv ===> 12
1151 # finish this later #
1153 =head2 Using gdb to look at specific parts of a program
1155 With the example above, you knew to look for C<Perl_pp_add>, but what if
1156 there were multiple calls to it all over the place, or you didn't know what
1157 the op was you were looking for?
1159 One way to do this is to inject a rare call somewhere near what you're looking
1160 for. For example, you could add C<study> before your method:
1166 (gdb) break Perl_pp_study
1168 And then step until you hit what you're
1169 looking for. This works well in a loop
1170 if you want to only break at certain iterations:
1172 for my $c (1..100) {
1176 =head2 Using gdb to look at what the parser/lexer are doing
1178 If you want to see what perl is doing when parsing/lexing your code, you can
1187 (gdb) break Perl_pp_study
1189 If you want to see what the parser/lexer is doing inside of C<if> blocks and
1190 the like you need to be a little trickier:
1192 if ($a && $b && do { BEGIN { study } 1 } && $c) { ... }
1194 =head1 SOURCE CODE STATIC ANALYSIS
1196 Various tools exist for analysing C source code B<statically>, as
1197 opposed to B<dynamically>, that is, without executing the code. It is
1198 possible to detect resource leaks, undefined behaviour, type
1199 mismatches, portability problems, code paths that would cause illegal
1200 memory accesses, and other similar problems by just parsing the C code
1201 and looking at the resulting graph, what does it tell about the
1202 execution and data flows. As a matter of fact, this is exactly how C
1203 compilers know to give warnings about dubious code.
1207 The good old C code quality inspector, C<lint>, is available in several
1208 platforms, but please be aware that there are several different
1209 implementations of it by different vendors, which means that the flags
1210 are not identical across different platforms.
1212 There is a C<lint> target in Makefile, but you may have to
1213 diddle with the flags (see above).
1217 Coverity (L<http://www.coverity.com/>) is a product similar to lint and as
1218 a testbed for their product they periodically check several open source
1219 projects, and they give out accounts to open source developers to the
1222 There is Coverity setup for the perl5 project:
1223 L<https://scan.coverity.com/projects/perl5>
1225 =head2 HP-UX cadvise (Code Advisor)
1227 HP has a C/C++ static analyzer product for HP-UX caller Code Advisor.
1228 (Link not given here because the URL is horribly long and seems horribly
1229 unstable; use the search engine of your choice to find it.) The use of
1230 the C<cadvise_cc> recipe with C<Configure ... -Dcc=./cadvise_cc>
1231 (see cadvise "User Guide") is recommended; as is the use of C<+wall>.
1233 =head2 cpd (cut-and-paste detector)
1235 The cpd tool detects cut-and-paste coding. If one instance of the
1236 cut-and-pasted code changes, all the other spots should probably be
1237 changed, too. Therefore such code should probably be turned into a
1238 subroutine or a macro.
1240 cpd (L<https://pmd.github.io/latest/pmd_userdocs_cpd.html>) is part of the pmd project
1241 (L<https://pmd.github.io/>). pmd was originally written for static
1242 analysis of Java code, but later the cpd part of it was extended to
1243 parse also C and C++.
1245 Download the pmd-bin-X.Y.zip () from the SourceForge site, extract the
1246 pmd-X.Y.jar from it, and then run that on source code thusly:
1248 java -cp pmd-X.Y.jar net.sourceforge.pmd.cpd.CPD \
1249 --minimum-tokens 100 --files /some/where/src --language c > cpd.txt
1251 You may run into memory limits, in which case you should use the -Xmx
1258 Though much can be written about the inconsistency and coverage
1259 problems of gcc warnings (like C<-Wall> not meaning "all the warnings",
1260 or some common portability problems not being covered by C<-Wall>, or
1261 C<-ansi> and C<-pedantic> both being a poorly defined collection of
1262 warnings, and so forth), gcc is still a useful tool in keeping our
1265 The C<-Wall> is by default on.
1267 It would be nice for C<-pedantic>) to be on always, but unfortunately it is not
1268 safe on all platforms - for example fatal conflicts with the system headers
1269 (Solaris being a prime example). If Configure C<-Dgccansipedantic> is used,
1270 the C<cflags> frontend selects C<-pedantic> for the platforms where it is known
1273 The following extra flags are added:
1295 C<-Werror=pointer-arith>
1303 The following flags would be nice to have but they would first need
1304 their own Augean stablemaster:
1314 C<-Wstrict-prototypes>
1318 The C<-Wtraditional> is another example of the annoying tendency of gcc
1319 to bundle a lot of warnings under one switch (it would be impossible to
1320 deploy in practice because it would complain a lot) but it does contain
1321 some warnings that would be beneficial to have available on their own,
1322 such as the warning about string constants inside macros containing the
1323 macro arguments: this behaved differently pre-ANSI than it does in
1324 ANSI, and some C compilers are still in transition, AIX being an
1327 =head2 Warnings of other C compilers
1329 Other C compilers (yes, there B<are> other C compilers than gcc) often
1330 have their "strict ANSI" or "strict ANSI with some portability
1331 extensions" modes on, like for example the Sun Workshop has its C<-Xa>
1332 mode on (though implicitly), or the DEC (these days, HP...) has its
1335 =head1 MEMORY DEBUGGERS
1337 B<NOTE 1>: Running under older memory debuggers such as Purify,
1338 valgrind or Third Degree greatly slows down the execution: seconds
1339 become minutes, minutes become hours. For example as of Perl 5.8.1, the
1340 ext/Encode/t/Unicode.t takes extraordinarily long to complete under
1341 e.g. Purify, Third Degree, and valgrind. Under valgrind it takes more
1342 than six hours, even on a snappy computer. The said test must be doing
1343 something that is quite unfriendly for memory debuggers. If you don't
1344 feel like waiting, that you can simply kill away the perl process.
1345 Roughly valgrind slows down execution by factor 10, AddressSanitizer by
1348 B<NOTE 2>: To minimize the number of memory leak false alarms (see
1349 L</PERL_DESTRUCT_LEVEL> for more information), you have to set the
1350 environment variable PERL_DESTRUCT_LEVEL to 2. For example, like this:
1352 env PERL_DESTRUCT_LEVEL=2 valgrind ./perl -Ilib ...
1354 B<NOTE 3>: There are known memory leaks when there are compile-time
1355 errors within eval or require, seeing C<S_doeval> in the call stack is
1356 a good sign of these. Fixing these leaks is non-trivial, unfortunately,
1357 but they must be fixed eventually.
1359 B<NOTE 4>: L<DynaLoader> will not clean up after itself completely
1360 unless Perl is built with the Configure option
1361 C<-Accflags=-DDL_UNLOAD_ALL_AT_EXIT>.
1365 The valgrind tool can be used to find out both memory leaks and illegal
1366 heap memory accesses. As of version 3.3.0, Valgrind only supports Linux
1367 on x86, x86-64 and PowerPC and Darwin (OS X) on x86 and x86-64. The
1368 special "test.valgrind" target can be used to run the tests under
1369 valgrind. Found errors and memory leaks are logged in files named
1370 F<testfile.valgrind> and by default output is displayed inline.
1376 Since valgrind adds significant overhead, tests will take much longer to
1377 run. The valgrind tests support being run in parallel to help with this:
1379 TEST_JOBS=9 make test.valgrind
1381 Note that the above two invocations will be very verbose as reachable
1382 memory and leak-checking is enabled by default. If you want to just see
1385 VG_OPTS='-q --leak-check=no --show-reachable=no' TEST_JOBS=9 \
1388 Valgrind also provides a cachegrind tool, invoked on perl as:
1390 VG_OPTS=--tool=cachegrind make test.valgrind
1392 As system libraries (most notably glibc) are also triggering errors,
1393 valgrind allows to suppress such errors using suppression files. The
1394 default suppression file that comes with valgrind already catches a lot
1395 of them. Some additional suppressions are defined in F<t/perl.supp>.
1397 To get valgrind and for more information see
1399 http://valgrind.org/
1401 =head2 AddressSanitizer
1403 AddressSanitizer ("ASan") consists of a compiler instrumentation module
1404 and a run-time C<malloc> library. ASan is available for a variety of
1405 architectures, operating systems, and compilers (see project link below).
1406 It checks for unsafe memory usage, such as use after free and buffer
1407 overflow conditions, and is fast enough that you can easily compile your
1408 debugging or optimized perl with it. Modern versions of ASan check for
1409 memory leaks by default on most platforms, otherwise (e.g. x86_64 OS X)
1410 this feature can be enabled via C<ASAN_OPTIONS=detect_leaks=1>.
1413 To build perl with AddressSanitizer, your Configure invocation should
1416 sh Configure -des -Dcc=clang \
1417 -Accflags=-fsanitize=address -Aldflags=-fsanitize=address \
1418 -Alddlflags=-shared\ -fsanitize=address \
1419 -fsanitize-blacklist=`pwd`/asan_ignore
1421 where these arguments mean:
1427 This should be replaced by the full path to your clang executable if it
1428 is not in your path.
1430 =item * -Accflags=-fsanitize=address
1432 Compile perl and extensions sources with AddressSanitizer.
1434 =item * -Aldflags=-fsanitize=address
1436 Link the perl executable with AddressSanitizer.
1438 =item * -Alddlflags=-shared\ -fsanitize=address
1440 Link dynamic extensions with AddressSanitizer. You must manually
1441 specify C<-shared> because using C<-Alddlflags=-shared> will prevent
1442 Configure from setting a default value for C<lddlflags>, which usually
1443 contains C<-shared> (at least on Linux).
1445 =item * -fsanitize-blacklist=`pwd`/asan_ignore
1447 AddressSanitizer will ignore functions listed in the C<asan_ignore>
1448 file. (This file should contain a short explanation of why each of
1449 the functions is listed.)
1454 L<https://github.com/google/sanitizers/wiki/AddressSanitizer>.
1459 Depending on your platform there are various ways of profiling Perl.
1461 There are two commonly used techniques of profiling executables:
1462 I<statistical time-sampling> and I<basic-block counting>.
1464 The first method takes periodically samples of the CPU program counter,
1465 and since the program counter can be correlated with the code generated
1466 for functions, we get a statistical view of in which functions the
1467 program is spending its time. The caveats are that very small/fast
1468 functions have lower probability of showing up in the profile, and that
1469 periodically interrupting the program (this is usually done rather
1470 frequently, in the scale of milliseconds) imposes an additional
1471 overhead that may skew the results. The first problem can be alleviated
1472 by running the code for longer (in general this is a good idea for
1473 profiling), the second problem is usually kept in guard by the
1474 profiling tools themselves.
1476 The second method divides up the generated code into I<basic blocks>.
1477 Basic blocks are sections of code that are entered only in the
1478 beginning and exited only at the end. For example, a conditional jump
1479 starts a basic block. Basic block profiling usually works by
1480 I<instrumenting> the code by adding I<enter basic block #nnnn>
1481 book-keeping code to the generated code. During the execution of the
1482 code the basic block counters are then updated appropriately. The
1483 caveat is that the added extra code can skew the results: again, the
1484 profiling tools usually try to factor their own effects out of the
1487 =head2 Gprof Profiling
1489 I<gprof> is a profiling tool available in many Unix platforms which
1490 uses I<statistical time-sampling>. You can build a profiled version of
1491 F<perl> by compiling using gcc with the flag C<-pg>. Either edit
1492 F<config.sh> or re-run F<Configure>. Running the profiled version of
1493 Perl will create an output file called F<gmon.out> which contains the
1494 profiling data collected during the execution.
1498 $ sh Configure -des -Dusedevel -Accflags='-pg' \
1499 -Aldflags='-pg' -Alddlflags='-pg -shared' \
1501 $ ./perl ... # creates gmon.out in current directory
1502 $ gprof ./perl > out
1505 (you probably need to add C<-shared> to the <-Alddlflags> line until RT
1506 #118199 is resolved)
1508 The F<gprof> tool can then display the collected data in various ways.
1509 Usually F<gprof> understands the following options:
1515 Suppress statically defined functions from the profile.
1519 Suppress the verbose descriptions in the profile.
1523 Exclude the given routine and its descendants from the profile.
1527 Display only the given routine and its descendants in the profile.
1531 Generate a summary file called F<gmon.sum> which then may be given to
1532 subsequent gprof runs to accumulate data over several runs.
1536 Display routines that have zero usage.
1540 For more detailed explanation of the available commands and output
1541 formats, see your own local documentation of F<gprof>.
1543 =head2 GCC gcov Profiling
1545 I<basic block profiling> is officially available in gcc 3.0 and later.
1546 You can build a profiled version of F<perl> by compiling using gcc with
1547 the flags C<-fprofile-arcs -ftest-coverage>. Either edit F<config.sh>
1548 or re-run F<Configure>.
1552 $ sh Configure -des -Dusedevel -Doptimize='-g' \
1553 -Accflags='-fprofile-arcs -ftest-coverage' \
1554 -Aldflags='-fprofile-arcs -ftest-coverage' \
1555 -Alddlflags='-fprofile-arcs -ftest-coverage -shared' \
1557 $ rm -f regexec.c.gcov regexec.gcda
1560 $ less regexec.c.gcov
1562 (you probably need to add C<-shared> to the <-Alddlflags> line until RT
1563 #118199 is resolved)
1565 Running the profiled version of Perl will cause profile output to be
1566 generated. For each source file an accompanying F<.gcda> file will be
1569 To display the results you use the I<gcov> utility (which should be
1570 installed if you have gcc 3.0 or newer installed). F<gcov> is run on
1571 source code files, like this
1575 which will cause F<sv.c.gcov> to be created. The F<.gcov> files contain
1576 the source code annotated with relative frequencies of execution
1577 indicated by "#" markers. If you want to generate F<.gcov> files for
1578 all profiled object files, you can run something like this:
1580 for file in `find . -name \*.gcno`
1581 do sh -c "cd `dirname $file` && gcov `basename $file .gcno`"
1584 Useful options of F<gcov> include C<-b> which will summarise the basic
1585 block, branch, and function call coverage, and C<-c> which instead of
1586 relative frequencies will use the actual counts. For more information
1587 on the use of F<gcov> and basic block profiling with gcc, see the
1588 latest GNU CC manual. As of gcc 4.8, this is at
1589 L<http://gcc.gnu.org/onlinedocs/gcc/Gcov-Intro.html#Gcov-Intro>
1591 =head2 callgrind profiling
1593 callgrind is a valgrind tool for profiling source code. Paired
1594 with kcachegrind (a Qt based UI), it gives you an overview of
1595 where code is taking up time, as well as the ability
1596 to examine callers, call trees, and more. One of its benefits
1597 is you can use it on perl and XS modules that have not been
1598 compiled with debugging symbols.
1600 If perl is compiled with debugging symbols (C<-g>), you can view
1601 the annotated source and click around, much like Devel::NYTProf's
1606 valgrind --tool=callgrind ./perl ...
1608 By default it will write output to F<callgrind.out.PID>, but you
1609 can change that with C<--callgrind-out-file=...>
1611 To view the data, do:
1613 kcachegrind callgrind.out.PID
1615 If you'd prefer to view the data in a terminal, you can use
1616 F<callgrind_annotate>. In it's basic form:
1618 callgrind_annotate callgrind.out.PID | less
1620 Some useful options are:
1626 Percentage of counts (of primary sort event) we are interested in.
1627 The default is 99%, 100% might show things that seem to be missing.
1631 Annotate all source files containing functions that helped reach
1632 the event count threshold.
1636 =head1 MISCELLANEOUS TRICKS
1638 =head2 PERL_DESTRUCT_LEVEL
1640 If you want to run any of the tests yourself manually using e.g.
1641 valgrind, please note that by default perl B<does not> explicitly
1642 cleanup all the memory it has allocated (such as global memory arenas)
1643 but instead lets the exit() of the whole program "take care" of such
1644 allocations, also known as "global destruction of objects".
1646 There is a way to tell perl to do complete cleanup: set the environment
1647 variable PERL_DESTRUCT_LEVEL to a non-zero value. The t/TEST wrapper
1648 does set this to 2, and this is what you need to do too, if you don't
1649 want to see the "global leaks": For example, for running under valgrind
1651 env PERL_DESTRUCT_LEVEL=2 valgrind ./perl -Ilib t/foo/bar.t
1653 (Note: the mod_perl apache module uses also this environment variable
1654 for its own purposes and extended its semantics. Refer to the mod_perl
1655 documentation for more information. Also, spawned threads do the
1656 equivalent of setting this variable to the value 1.)
1658 If, at the end of a run you get the message I<N scalars leaked>, you
1659 can recompile with C<-DDEBUG_LEAKING_SCALARS>,
1660 (C<Configure -Accflags=-DDEBUG_LEAKING_SCALARS>), which will cause the
1661 addresses of all those leaked SVs to be dumped along with details as to
1662 where each SV was originally allocated. This information is also
1663 displayed by Devel::Peek. Note that the extra details recorded with
1664 each SV increases memory usage, so it shouldn't be used in production
1665 environments. It also converts C<new_SV()> from a macro into a real
1666 function, so you can use your favourite debugger to discover where
1667 those pesky SVs were allocated.
1669 If you see that you're leaking memory at runtime, but neither valgrind
1670 nor C<-DDEBUG_LEAKING_SCALARS> will find anything, you're probably
1671 leaking SVs that are still reachable and will be properly cleaned up
1672 during destruction of the interpreter. In such cases, using the C<-Dm>
1673 switch can point you to the source of the leak. If the executable was
1674 built with C<-DDEBUG_LEAKING_SCALARS>, C<-Dm> will output SV
1675 allocations in addition to memory allocations. Each SV allocation has a
1676 distinct serial number that will be written on creation and destruction
1677 of the SV. So if you're executing the leaking code in a loop, you need
1678 to look for SVs that are created, but never destroyed between each
1679 cycle. If such an SV is found, set a conditional breakpoint within
1680 C<new_SV()> and make it break only when C<PL_sv_serial> is equal to the
1681 serial number of the leaking SV. Then you will catch the interpreter in
1682 exactly the state where the leaking SV is allocated, which is
1683 sufficient in many cases to find the source of the leak.
1685 As C<-Dm> is using the PerlIO layer for output, it will by itself
1686 allocate quite a bunch of SVs, which are hidden to avoid recursion. You
1687 can bypass the PerlIO layer if you use the SV logging provided by
1688 C<-DPERL_MEM_LOG> instead.
1692 If compiled with C<-DPERL_MEM_LOG> (C<-Accflags=-DPERL_MEM_LOG>), both
1693 memory and SV allocations go through logging functions, which is
1694 handy for breakpoint setting.
1696 Unless C<-DPERL_MEM_LOG_NOIMPL> (C<-Accflags=-DPERL_MEM_LOG_NOIMPL>) is
1697 also compiled, the logging functions read $ENV{PERL_MEM_LOG} to
1698 determine whether to log the event, and if so how:
1700 $ENV{PERL_MEM_LOG} =~ /m/ Log all memory ops
1701 $ENV{PERL_MEM_LOG} =~ /s/ Log all SV ops
1702 $ENV{PERL_MEM_LOG} =~ /t/ include timestamp in Log
1703 $ENV{PERL_MEM_LOG} =~ /^(\d+)/ write to FD given (default is 2)
1705 Memory logging is somewhat similar to C<-Dm> but is independent of
1706 C<-DDEBUGGING>, and at a higher level; all uses of Newx(), Renew(), and
1707 Safefree() are logged with the caller's source code file and line
1708 number (and C function name, if supported by the C compiler). In
1709 contrast, C<-Dm> is directly at the point of C<malloc()>. SV logging is
1712 Since the logging doesn't use PerlIO, all SV allocations are logged and
1713 no extra SV allocations are introduced by enabling the logging. If
1714 compiled with C<-DDEBUG_LEAKING_SCALARS>, the serial number for each SV
1715 allocation is also logged.
1719 Those debugging perl with the DDD frontend over gdb may find the
1722 You can extend the data conversion shortcuts menu, so for example you
1723 can display an SV's IV value with one click, without doing any typing.
1724 To do that simply edit ~/.ddd/init file and add after:
1726 ! Display shortcuts.
1727 Ddd*gdbDisplayShortcuts: \
1728 /t () // Convert to Bin\n\
1729 /d () // Convert to Dec\n\
1730 /x () // Convert to Hex\n\
1731 /o () // Convert to Oct(\n\
1733 the following two lines:
1735 ((XPV*) (())->sv_any )->xpv_pv // 2pvx\n\
1736 ((XPVIV*) (())->sv_any )->xiv_iv // 2ivx
1738 so now you can do ivx and pvx lookups or you can plug there the sv_peek
1741 Perl_sv_peek(my_perl, (SV*)()) // sv_peek
1743 (The my_perl is for threaded builds.) Just remember that every line,
1744 but the last one, should end with \n\
1746 Alternatively edit the init file interactively via: 3rd mouse button ->
1747 New Display -> Edit Menu
1749 Note: you can define up to 20 conversion shortcuts in the gdb section.
1753 On some platforms Perl supports retrieving the C level backtrace
1754 (similar to what symbolic debuggers like gdb do).
1756 The backtrace returns the stack trace of the C call frames,
1757 with the symbol names (function names), the object names (like "perl"),
1758 and if it can, also the source code locations (file:line).
1760 The supported platforms are Linux, and OS X (some *BSD might
1761 work at least partly, but they have not yet been tested).
1763 This feature hasn't been tested with multiple threads, but it will
1764 only show the backtrace of the thread doing the backtracing.
1766 The feature needs to be enabled with C<Configure -Dusecbacktrace>.
1768 The C<-Dusecbacktrace> also enables keeping the debug information when
1769 compiling/linking (often: C<-g>). Many compilers/linkers do support
1770 having both optimization and keeping the debug information. The debug
1771 information is needed for the symbol names and the source locations.
1773 Static functions might not be visible for the backtrace.
1775 Source code locations, even if available, can often be missing or
1776 misleading if the compiler has e.g. inlined code. Optimizer can
1777 make matching the source code and the object code quite challenging.
1783 You B<must> have the BFD (-lbfd) library installed, otherwise C<perl> will
1784 fail to link. The BFD is usually distributed as part of the GNU binutils.
1786 Summary: C<Configure ... -Dusecbacktrace>
1787 and you need C<-lbfd>.
1791 The source code locations are supported B<only> if you have
1792 the Developer Tools installed. (BFD is B<not> needed.)
1794 Summary: C<Configure ... -Dusecbacktrace>
1795 and installing the Developer Tools would be good.
1799 Optionally, for trying out the feature, you may want to enable
1800 automatic dumping of the backtrace just before a warning or croak (die)
1801 message is emitted, by adding C<-Accflags=-DUSE_C_BACKTRACE_ON_ERROR>
1804 Unless the above additional feature is enabled, nothing about the
1805 backtrace functionality is visible, except for the Perl/XS level.
1807 Furthermore, even if you have enabled this feature to be compiled,
1808 you need to enable it in runtime with an environment variable:
1809 C<PERL_C_BACKTRACE_ON_ERROR=10>. It must be an integer higher
1810 than zero, telling the desired frame count.
1812 Retrieving the backtrace from Perl level (using for example an XS
1813 extension) would be much less exciting than one would hope: normally
1814 you would see C<runops>, C<entersub>, and not much else. This API is
1815 intended to be called B<from within> the Perl implementation, not from
1816 Perl level execution.
1818 The C API for the backtrace is as follows:
1822 =item get_c_backtrace
1824 =item free_c_backtrace
1826 =item get_c_backtrace_dump
1828 =item dump_c_backtrace
1834 If you see in a debugger a memory area mysteriously full of 0xABABABAB
1835 or 0xEFEFEFEF, you may be seeing the effect of the Poison() macros, see
1838 =head2 Read-only optrees
1840 Under ithreads the optree is read only. If you want to enforce this, to
1841 check for write accesses from buggy code, compile with
1842 C<-Accflags=-DPERL_DEBUG_READONLY_OPS>
1843 to enable code that allocates op memory
1844 via C<mmap>, and sets it read-only when it is attached to a subroutine.
1845 Any write access to an op results in a C<SIGBUS> and abort.
1847 This code is intended for development only, and may not be portable
1848 even to all Unix variants. Also, it is an 80% solution, in that it
1849 isn't able to make all ops read only. Specifically it does not apply to
1850 op slabs belonging to C<BEGIN> blocks.
1852 However, as an 80% solution it is still effective, as it has caught
1855 =head2 When is a bool not a bool?
1857 There wasn't necessarily a standard C<bool> type on compilers prior to
1858 C99, and so some workarounds were created. The C<TRUE> and C<FALSE>
1859 macros are still available as alternatives for C<true> and C<false>.
1860 And the C<cBOOL> macro was created to correctly cast to a true/false
1861 value in all circumstances, but should no longer be necessary.
1862 Using S<C<(bool)> I<expr>>> should now always work.
1864 There are no plans to remove any of C<TRUE>, C<FALSE>, nor C<cBOOL>.
1866 =head2 Finding unsafe truncations
1868 You may wish to run C<Configure> with something like
1870 -Accflags='-Wconversion -Wno-sign-conversion -Wno-shorten-64-to-32'
1872 or your compiler's equivalent to make it easier to spot any unsafe truncations
1875 =head2 The .i Targets
1877 You can expand the macros in a F<foo.c> file by saying
1881 which will expand the macros using cpp. Don't be scared by the
1886 This document was originally written by Nathan Torkington, and is
1887 maintained by the perl5-porters mailing list.