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