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