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