Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

925 lines
34 KiB

  1. =head1 NAME
  2. perldebguts - Guts of Perl debugging
  3. =head1 DESCRIPTION
  4. This is not the perldebug(1) manpage, which tells you how to use
  5. the debugger. This manpage describes low-level details ranging
  6. between difficult and impossible for anyone who isn't incredibly
  7. intimate with Perl's guts to understand. Caveat lector.
  8. =head1 Debugger Internals
  9. Perl has special debugging hooks at compile-time and run-time used
  10. to create debugging environments. These hooks are not to be confused
  11. with the I<perl -Dxxx> command described in L<perlrun>, which is
  12. usable only if a special Perl is built per the instructions in the
  13. F<INSTALL> podpage in the Perl source tree.
  14. For example, whenever you call Perl's built-in C<caller> function
  15. from the package DB, the arguments that the corresponding stack
  16. frame was called with are copied to the @DB::args array. The
  17. general mechanisms is enabled by calling Perl with the B<-d> switch, the
  18. following additional features are enabled (cf. L<perlvar/$^P>):
  19. =over 4
  20. =item *
  21. Perl inserts the contents of C<$ENV{PERL5DB}> (or C<BEGIN {require
  22. 'perl5db.pl'}> if not present) before the first line of your program.
  23. =item *
  24. Each array C<@{"_<$filename"}> holds the lines of $filename for a
  25. file compiled by Perl. The same for C<eval>ed strings that contain
  26. subroutines, or which are currently being executed. The $filename
  27. for C<eval>ed strings looks like C<(eval 34)>. Code assertions
  28. in regexes look like C<(re_eval 19)>.
  29. Values in this array are magical in numeric context: they compare
  30. equal to zero only if the line is not breakable.
  31. =item *
  32. Each hash C<%{"_<$filename"}> contains breakpoints and actions keyed
  33. by line number. Individual entries (as opposed to the whole hash)
  34. are settable. Perl only cares about Boolean true here, although
  35. the values used by F<perl5db.pl> have the form
  36. C<"$break_condition\0$action">.
  37. The same holds for evaluated strings that contain subroutines, or
  38. which are currently being executed. The $filename for C<eval>ed strings
  39. looks like C<(eval 34)> or C<(re_eval 19)>.
  40. =item *
  41. Each scalar C<${"_<$filename"}> contains C<"_<$filename">. This is
  42. also the case for evaluated strings that contain subroutines, or
  43. which are currently being executed. The $filename for C<eval>ed
  44. strings looks like C<(eval 34)> or C<(re_eval 19)>.
  45. =item *
  46. After each C<require>d file is compiled, but before it is executed,
  47. C<DB::postponed(*{"_<$filename"})> is called if the subroutine
  48. C<DB::postponed> exists. Here, the $filename is the expanded name of
  49. the C<require>d file, as found in the values of %INC.
  50. =item *
  51. After each subroutine C<subname> is compiled, the existence of
  52. C<$DB::postponed{subname}> is checked. If this key exists,
  53. C<DB::postponed(subname)> is called if the C<DB::postponed> subroutine
  54. also exists.
  55. =item *
  56. A hash C<%DB::sub> is maintained, whose keys are subroutine names
  57. and whose values have the form C<filename:startline-endline>.
  58. C<filename> has the form C<(eval 34)> for subroutines defined inside
  59. C<eval>s, or C<(re_eval 19)> for those within regex code assertions.
  60. =item *
  61. When the execution of your program reaches a point that can hold a
  62. breakpoint, the C<DB::DB()> subroutine is called any of the variables
  63. $DB::trace, $DB::single, or $DB::signal is true. These variables
  64. are not C<local>izable. This feature is disabled when executing
  65. inside C<DB::DB()>, including functions called from it
  66. unless C<< $^D & (1<<30) >> is true.
  67. =item *
  68. When execution of the program reaches a subroutine call, a call to
  69. C<&DB::sub>(I<args>) is made instead, with C<$DB::sub> holding the
  70. name of the called subroutine. This doesn't happen if the subroutine
  71. was compiled in the C<DB> package.)
  72. =back
  73. Note that if C<&DB::sub> needs external data for it to work, no
  74. subroutine call is possible until this is done. For the standard
  75. debugger, the C<$DB::deep> variable (how many levels of recursion
  76. deep into the debugger you can go before a mandatory break) gives
  77. an example of such a dependency.
  78. =head2 Writing Your Own Debugger
  79. The minimal working debugger consists of one line
  80. sub DB::DB {}
  81. which is quite handy as contents of C<PERL5DB> environment
  82. variable:
  83. $ PERL5DB="sub DB::DB {}" perl -d your-script
  84. Another brief debugger, slightly more useful, could be created
  85. with only the line:
  86. sub DB::DB {print ++$i; scalar <STDIN>}
  87. This debugger would print the sequential number of encountered
  88. statement, and would wait for you to hit a newline before continuing.
  89. The following debugger is quite functional:
  90. {
  91. package DB;
  92. sub DB {}
  93. sub sub {print ++$i, " $sub\n"; &$sub}
  94. }
  95. It prints the sequential number of subroutine call and the name of the
  96. called subroutine. Note that C<&DB::sub> should be compiled into the
  97. package C<DB>.
  98. At the start, the debugger reads your rc file (F<./.perldb> or
  99. F<~/.perldb> under Unix), which can set important options. This file may
  100. define a subroutine C<&afterinit> to be executed after the debugger is
  101. initialized.
  102. After the rc file is read, the debugger reads the PERLDB_OPTS
  103. environment variable and parses this as the remainder of a C<O ...>
  104. line as one might enter at the debugger prompt.
  105. The debugger also maintains magical internal variables, such as
  106. C<@DB::dbline>, C<%DB::dbline>, which are aliases for
  107. C<@{"::_<current_file"}> C<%{"::_<current_file"}>. Here C<current_file>
  108. is the currently selected file, either explicitly chosen with the
  109. debugger's C<f> command, or implicitly by flow of execution.
  110. Some functions are provided to simplify customization. See
  111. L<perldebug/"Options"> for description of options parsed by
  112. C<DB::parse_options(string)>. The function C<DB::dump_trace(skip[,
  113. count])> skips the specified number of frames and returns a list
  114. containing information about the calling frames (all of them, if
  115. C<count> is missing). Each entry is reference to a hash with
  116. keys C<context> (either C<.>, C<$>, or C<@>), C<sub> (subroutine
  117. name, or info about C<eval>), C<args> (C<undef> or a reference to
  118. an array), C<file>, and C<line>.
  119. The function C<DB::print_trace(FH, skip[, count[, short]])> prints
  120. formatted info about caller frames. The last two functions may be
  121. convenient as arguments to C<< < >>, C<< << >> commands.
  122. Note that any variables and functions that are not documented in
  123. this manpages (or in L<perldebug>) are considered for internal
  124. use only, and as such are subject to change without notice.
  125. =head1 Frame Listing Output Examples
  126. The C<frame> option can be used to control the output of frame
  127. information. For example, contrast this expression trace:
  128. $ perl -de 42
  129. Stack dump during die enabled outside of evals.
  130. Loading DB routines from perl5db.pl patch level 0.94
  131. Emacs support available.
  132. Enter h or `h h' for help.
  133. main::(-e:1): 0
  134. DB<1> sub foo { 14 }
  135. DB<2> sub bar { 3 }
  136. DB<3> t print foo() * bar()
  137. main::((eval 172):3): print foo() + bar();
  138. main::foo((eval 168):2):
  139. main::bar((eval 170):2):
  140. 42
  141. with this one, once the C<O>ption C<frame=2> has been set:
  142. DB<4> O f=2
  143. frame = '2'
  144. DB<5> t print foo() * bar()
  145. 3: foo() * bar()
  146. entering main::foo
  147. 2: sub foo { 14 };
  148. exited main::foo
  149. entering main::bar
  150. 2: sub bar { 3 };
  151. exited main::bar
  152. 42
  153. By way of demonstration, we present below a laborious listing
  154. resulting from setting your C<PERLDB_OPTS> environment variable to
  155. the value C<f=n N>, and running I<perl -d -V> from the command line.
  156. Examples use various values of C<n> are shown to give you a feel
  157. for the difference between settings. Long those it may be, this
  158. is not a complete listing, but only excerpts.
  159. =over 4
  160. =item 1
  161. entering main::BEGIN
  162. entering Config::BEGIN
  163. Package lib/Exporter.pm.
  164. Package lib/Carp.pm.
  165. Package lib/Config.pm.
  166. entering Config::TIEHASH
  167. entering Exporter::import
  168. entering Exporter::export
  169. entering Config::myconfig
  170. entering Config::FETCH
  171. entering Config::FETCH
  172. entering Config::FETCH
  173. entering Config::FETCH
  174. =item 2
  175. entering main::BEGIN
  176. entering Config::BEGIN
  177. Package lib/Exporter.pm.
  178. Package lib/Carp.pm.
  179. exited Config::BEGIN
  180. Package lib/Config.pm.
  181. entering Config::TIEHASH
  182. exited Config::TIEHASH
  183. entering Exporter::import
  184. entering Exporter::export
  185. exited Exporter::export
  186. exited Exporter::import
  187. exited main::BEGIN
  188. entering Config::myconfig
  189. entering Config::FETCH
  190. exited Config::FETCH
  191. entering Config::FETCH
  192. exited Config::FETCH
  193. entering Config::FETCH
  194. =item 4
  195. in $=main::BEGIN() from /dev/null:0
  196. in $=Config::BEGIN() from lib/Config.pm:2
  197. Package lib/Exporter.pm.
  198. Package lib/Carp.pm.
  199. Package lib/Config.pm.
  200. in $=Config::TIEHASH('Config') from lib/Config.pm:644
  201. in $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
  202. in $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from li
  203. in @=Config::myconfig() from /dev/null:0
  204. in $=Config::FETCH(ref(Config), 'package') from lib/Config.pm:574
  205. in $=Config::FETCH(ref(Config), 'baserev') from lib/Config.pm:574
  206. in $=Config::FETCH(ref(Config), 'PERL_VERSION') from lib/Config.pm:574
  207. in $=Config::FETCH(ref(Config), 'PERL_SUBVERSION') from lib/Config.pm:574
  208. in $=Config::FETCH(ref(Config), 'osname') from lib/Config.pm:574
  209. in $=Config::FETCH(ref(Config), 'osvers') from lib/Config.pm:574
  210. =item 6
  211. in $=main::BEGIN() from /dev/null:0
  212. in $=Config::BEGIN() from lib/Config.pm:2
  213. Package lib/Exporter.pm.
  214. Package lib/Carp.pm.
  215. out $=Config::BEGIN() from lib/Config.pm:0
  216. Package lib/Config.pm.
  217. in $=Config::TIEHASH('Config') from lib/Config.pm:644
  218. out $=Config::TIEHASH('Config') from lib/Config.pm:644
  219. in $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
  220. in $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/
  221. out $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/
  222. out $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
  223. out $=main::BEGIN() from /dev/null:0
  224. in @=Config::myconfig() from /dev/null:0
  225. in $=Config::FETCH(ref(Config), 'package') from lib/Config.pm:574
  226. out $=Config::FETCH(ref(Config), 'package') from lib/Config.pm:574
  227. in $=Config::FETCH(ref(Config), 'baserev') from lib/Config.pm:574
  228. out $=Config::FETCH(ref(Config), 'baserev') from lib/Config.pm:574
  229. in $=Config::FETCH(ref(Config), 'PERL_VERSION') from lib/Config.pm:574
  230. out $=Config::FETCH(ref(Config), 'PERL_VERSION') from lib/Config.pm:574
  231. in $=Config::FETCH(ref(Config), 'PERL_SUBVERSION') from lib/Config.pm:574
  232. =item 14
  233. in $=main::BEGIN() from /dev/null:0
  234. in $=Config::BEGIN() from lib/Config.pm:2
  235. Package lib/Exporter.pm.
  236. Package lib/Carp.pm.
  237. out $=Config::BEGIN() from lib/Config.pm:0
  238. Package lib/Config.pm.
  239. in $=Config::TIEHASH('Config') from lib/Config.pm:644
  240. out $=Config::TIEHASH('Config') from lib/Config.pm:644
  241. in $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
  242. in $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/E
  243. out $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/E
  244. out $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
  245. out $=main::BEGIN() from /dev/null:0
  246. in @=Config::myconfig() from /dev/null:0
  247. in $=Config::FETCH('Config=HASH(0x1aa444)', 'package') from lib/Config.pm:574
  248. out $=Config::FETCH('Config=HASH(0x1aa444)', 'package') from lib/Config.pm:574
  249. in $=Config::FETCH('Config=HASH(0x1aa444)', 'baserev') from lib/Config.pm:574
  250. out $=Config::FETCH('Config=HASH(0x1aa444)', 'baserev') from lib/Config.pm:574
  251. =item 30
  252. in $=CODE(0x15eca4)() from /dev/null:0
  253. in $=CODE(0x182528)() from lib/Config.pm:2
  254. Package lib/Exporter.pm.
  255. out $=CODE(0x182528)() from lib/Config.pm:0
  256. scalar context return from CODE(0x182528): undef
  257. Package lib/Config.pm.
  258. in $=Config::TIEHASH('Config') from lib/Config.pm:628
  259. out $=Config::TIEHASH('Config') from lib/Config.pm:628
  260. scalar context return from Config::TIEHASH: empty hash
  261. in $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
  262. in $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/Exporter.pm:171
  263. out $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/Exporter.pm:171
  264. scalar context return from Exporter::export: ''
  265. out $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
  266. scalar context return from Exporter::import: ''
  267. =back
  268. In all cases shown above, the line indentation shows the call tree.
  269. If bit 2 of C<frame> is set, a line is printed on exit from a
  270. subroutine as well. If bit 4 is set, the arguments are printed
  271. along with the caller info. If bit 8 is set, the arguments are
  272. printed even if they are tied or references. If bit 16 is set, the
  273. return value is printed, too.
  274. When a package is compiled, a line like this
  275. Package lib/Carp.pm.
  276. is printed with proper indentation.
  277. =head1 Debugging regular expressions
  278. There are two ways to enable debugging output for regular expressions.
  279. If your perl is compiled with C<-DDEBUGGING>, you may use the
  280. B<-Dr> flag on the command line.
  281. Otherwise, one can C<use re 'debug'>, which has effects at
  282. compile time and run time. It is not lexically scoped.
  283. =head2 Compile-time output
  284. The debugging output at compile time looks like this:
  285. compiling RE `[bc]d(ef*g)+h[ij]k$'
  286. size 43 first at 1
  287. 1: ANYOF(11)
  288. 11: EXACT <d>(13)
  289. 13: CURLYX {1,32767}(27)
  290. 15: OPEN1(17)
  291. 17: EXACT <e>(19)
  292. 19: STAR(22)
  293. 20: EXACT <f>(0)
  294. 22: EXACT <g>(24)
  295. 24: CLOSE1(26)
  296. 26: WHILEM(0)
  297. 27: NOTHING(28)
  298. 28: EXACT <h>(30)
  299. 30: ANYOF(40)
  300. 40: EXACT <k>(42)
  301. 42: EOL(43)
  302. 43: END(0)
  303. anchored `de' at 1 floating `gh' at 3..2147483647 (checking floating)
  304. stclass `ANYOF' minlen 7
  305. The first line shows the pre-compiled form of the regex. The second
  306. shows the size of the compiled form (in arbitrary units, usually
  307. 4-byte words) and the label I<id> of the first node that does a
  308. match.
  309. The last line (split into two lines above) contains optimizer
  310. information. In the example shown, the optimizer found that the match
  311. should contain a substring C<de> at offset 1, plus substring C<gh>
  312. at some offset between 3 and infinity. Moreover, when checking for
  313. these substrings (to abandon impossible matches quickly), Perl will check
  314. for the substring C<gh> before checking for the substring C<de>. The
  315. optimizer may also use the knowledge that the match starts (at the
  316. C<first> I<id>) with a character class, and the match cannot be
  317. shorter than 7 chars.
  318. The fields of interest which may appear in the last line are
  319. =over 4
  320. =item C<anchored> I<STRING> C<at> I<POS>
  321. =item C<floating> I<STRING> C<at> I<POS1..POS2>
  322. See above.
  323. =item C<matching floating/anchored>
  324. Which substring to check first.
  325. =item C<minlen>
  326. The minimal length of the match.
  327. =item C<stclass> I<TYPE>
  328. Type of first matching node.
  329. =item C<noscan>
  330. Don't scan for the found substrings.
  331. =item C<isall>
  332. Means that the optimizer info is all that the regular
  333. expression contains, and thus one does not need to enter the regex engine at
  334. all.
  335. =item C<GPOS>
  336. Set if the pattern contains C<\G>.
  337. =item C<plus>
  338. Set if the pattern starts with a repeated char (as in C<x+y>).
  339. =item C<implicit>
  340. Set if the pattern starts with C<.*>.
  341. =item C<with eval>
  342. Set if the pattern contain eval-groups, such as C<(?{ code })> and
  343. C<(??{ code })>.
  344. =item C<anchored(TYPE)>
  345. If the pattern may match only at a handful of places, (with C<TYPE>
  346. being C<BOL>, C<MBOL>, or C<GPOS>. See the table below.
  347. =back
  348. If a substring is known to match at end-of-line only, it may be
  349. followed by C<$>, as in C<floating `k'$>.
  350. The optimizer-specific info is used to avoid entering (a slow) regex
  351. engine on strings that will not definitely match. If C<isall> flag
  352. is set, a call to the regex engine may be avoided even when the optimizer
  353. found an appropriate place for the match.
  354. The rest of the output contains the list of I<nodes> of the compiled
  355. form of the regex. Each line has format
  356. C< >I<id>: I<TYPE> I<OPTIONAL-INFO> (I<next-id>)
  357. =head2 Types of nodes
  358. Here are the possible types, with short descriptions:
  359. # TYPE arg-description [num-args] [longjump-len] DESCRIPTION
  360. # Exit points
  361. END no End of program.
  362. SUCCEED no Return from a subroutine, basically.
  363. # Anchors:
  364. BOL no Match "" at beginning of line.
  365. MBOL no Same, assuming multiline.
  366. SBOL no Same, assuming singleline.
  367. EOS no Match "" at end of string.
  368. EOL no Match "" at end of line.
  369. MEOL no Same, assuming multiline.
  370. SEOL no Same, assuming singleline.
  371. BOUND no Match "" at any word boundary
  372. BOUNDL no Match "" at any word boundary
  373. NBOUND no Match "" at any word non-boundary
  374. NBOUNDL no Match "" at any word non-boundary
  375. GPOS no Matches where last m//g left off.
  376. # [Special] alternatives
  377. ANY no Match any one character (except newline).
  378. SANY no Match any one character.
  379. ANYOF sv Match character in (or not in) this class.
  380. ALNUM no Match any alphanumeric character
  381. ALNUML no Match any alphanumeric char in locale
  382. NALNUM no Match any non-alphanumeric character
  383. NALNUML no Match any non-alphanumeric char in locale
  384. SPACE no Match any whitespace character
  385. SPACEL no Match any whitespace char in locale
  386. NSPACE no Match any non-whitespace character
  387. NSPACEL no Match any non-whitespace char in locale
  388. DIGIT no Match any numeric character
  389. NDIGIT no Match any non-numeric character
  390. # BRANCH The set of branches constituting a single choice are hooked
  391. # together with their "next" pointers, since precedence prevents
  392. # anything being concatenated to any individual branch. The
  393. # "next" pointer of the last BRANCH in a choice points to the
  394. # thing following the whole choice. This is also where the
  395. # final "next" pointer of each individual branch points; each
  396. # branch starts with the operand node of a BRANCH node.
  397. #
  398. BRANCH node Match this alternative, or the next...
  399. # BACK Normal "next" pointers all implicitly point forward; BACK
  400. # exists to make loop structures possible.
  401. # not used
  402. BACK no Match "", "next" ptr points backward.
  403. # Literals
  404. EXACT sv Match this string (preceded by length).
  405. EXACTF sv Match this string, folded (prec. by length).
  406. EXACTFL sv Match this string, folded in locale (w/len).
  407. # Do nothing
  408. NOTHING no Match empty string.
  409. # A variant of above which delimits a group, thus stops optimizations
  410. TAIL no Match empty string. Can jump here from outside.
  411. # STAR,PLUS '?', and complex '*' and '+', are implemented as circular
  412. # BRANCH structures using BACK. Simple cases (one character
  413. # per match) are implemented with STAR and PLUS for speed
  414. # and to minimize recursive plunges.
  415. #
  416. STAR node Match this (simple) thing 0 or more times.
  417. PLUS node Match this (simple) thing 1 or more times.
  418. CURLY sv 2 Match this simple thing {n,m} times.
  419. CURLYN no 2 Match next-after-this simple thing
  420. # {n,m} times, set parens.
  421. CURLYM no 2 Match this medium-complex thing {n,m} times.
  422. CURLYX sv 2 Match this complex thing {n,m} times.
  423. # This terminator creates a loop structure for CURLYX
  424. WHILEM no Do curly processing and see if rest matches.
  425. # OPEN,CLOSE,GROUPP ...are numbered at compile time.
  426. OPEN num 1 Mark this point in input as start of #n.
  427. CLOSE num 1 Analogous to OPEN.
  428. REF num 1 Match some already matched string
  429. REFF num 1 Match already matched string, folded
  430. REFFL num 1 Match already matched string, folded in loc.
  431. # grouping assertions
  432. IFMATCH off 1 2 Succeeds if the following matches.
  433. UNLESSM off 1 2 Fails if the following matches.
  434. SUSPEND off 1 1 "Independent" sub-regex.
  435. IFTHEN off 1 1 Switch, should be preceded by switcher .
  436. GROUPP num 1 Whether the group matched.
  437. # Support for long regex
  438. LONGJMP off 1 1 Jump far away.
  439. BRANCHJ off 1 1 BRANCH with long offset.
  440. # The heavy worker
  441. EVAL evl 1 Execute some Perl code.
  442. # Modifiers
  443. MINMOD no Next operator is not greedy.
  444. LOGICAL no Next opcode should set the flag only.
  445. # This is not used yet
  446. RENUM off 1 1 Group with independently numbered parens.
  447. # This is not really a node, but an optimized away piece of a "long" node.
  448. # To simplify debugging output, we mark it as if it were a node
  449. OPTIMIZED off Placeholder for dump.
  450. =head2 Run-time output
  451. First of all, when doing a match, one may get no run-time output even
  452. if debugging is enabled. This means that the regex engine was never
  453. entered and that all of the job was therefore done by the optimizer.
  454. If the regex engine was entered, the output may look like this:
  455. Matching `[bc]d(ef*g)+h[ij]k$' against `abcdefg__gh__'
  456. Setting an EVAL scope, savestack=3
  457. 2 <ab> <cdefg__gh_> | 1: ANYOF
  458. 3 <abc> <defg__gh_> | 11: EXACT <d>
  459. 4 <abcd> <efg__gh_> | 13: CURLYX {1,32767}
  460. 4 <abcd> <efg__gh_> | 26: WHILEM
  461. 0 out of 1..32767 cc=effff31c
  462. 4 <abcd> <efg__gh_> | 15: OPEN1
  463. 4 <abcd> <efg__gh_> | 17: EXACT <e>
  464. 5 <abcde> <fg__gh_> | 19: STAR
  465. EXACT <f> can match 1 times out of 32767...
  466. Setting an EVAL scope, savestack=3
  467. 6 <bcdef> <g__gh__> | 22: EXACT <g>
  468. 7 <bcdefg> <__gh__> | 24: CLOSE1
  469. 7 <bcdefg> <__gh__> | 26: WHILEM
  470. 1 out of 1..32767 cc=effff31c
  471. Setting an EVAL scope, savestack=12
  472. 7 <bcdefg> <__gh__> | 15: OPEN1
  473. 7 <bcdefg> <__gh__> | 17: EXACT <e>
  474. restoring \1 to 4(4)..7
  475. failed, try continuation...
  476. 7 <bcdefg> <__gh__> | 27: NOTHING
  477. 7 <bcdefg> <__gh__> | 28: EXACT <h>
  478. failed...
  479. failed...
  480. The most significant information in the output is about the particular I<node>
  481. of the compiled regex that is currently being tested against the target string.
  482. The format of these lines is
  483. C< >I<STRING-OFFSET> <I<PRE-STRING>> <I<POST-STRING>> |I<ID>: I<TYPE>
  484. The I<TYPE> info is indented with respect to the backtracking level.
  485. Other incidental information appears interspersed within.
  486. =head1 Debugging Perl memory usage
  487. Perl is a profligate wastrel when it comes to memory use. There
  488. is a saying that to estimate memory usage of Perl, assume a reasonable
  489. algorithm for memory allocation, multiply that estimate by 10, and
  490. while you still may miss the mark, at least you won't be quite so
  491. astonished. This is not absolutely true, but may provide a good
  492. grasp of what happens.
  493. Assume that an integer cannot take less than 20 bytes of memory, a
  494. float cannot take less than 24 bytes, a string cannot take less
  495. than 32 bytes (all these examples assume 32-bit architectures, the
  496. result are quite a bit worse on 64-bit architectures). If a variable
  497. is accessed in two of three different ways (which require an integer,
  498. a float, or a string), the memory footprint may increase yet another
  499. 20 bytes. A sloppy malloc(3) implementation can inflate these
  500. numbers dramatically.
  501. On the opposite end of the scale, a declaration like
  502. sub foo;
  503. may take up to 500 bytes of memory, depending on which release of Perl
  504. you're running.
  505. Anecdotal estimates of source-to-compiled code bloat suggest an
  506. eightfold increase. This means that the compiled form of reasonable
  507. (normally commented, properly indented etc.) code will take
  508. about eight times more space in memory than the code took
  509. on disk.
  510. There are two Perl-specific ways to analyze memory usage:
  511. $ENV{PERL_DEBUG_MSTATS} and B<-DL> command-line switch. The first
  512. is available only if Perl is compiled with Perl's malloc(); the
  513. second only if Perl was built with C<-DDEBUGGING>. See the
  514. instructions for how to do this in the F<INSTALL> podpage at
  515. the top level of the Perl source tree.
  516. =head2 Using C<$ENV{PERL_DEBUG_MSTATS}>
  517. If your perl is using Perl's malloc() and was compiled with the
  518. necessary switches (this is the default), then it will print memory
  519. usage statistics after compiling your code when C<< $ENV{PERL_DEBUG_MSTATS}
  520. > 1 >>, and before termination of the program when C<<
  521. $ENV{PERL_DEBUG_MSTATS} >= 1 >>. The report format is similar to
  522. the following example:
  523. $ PERL_DEBUG_MSTATS=2 perl -e "require Carp"
  524. Memory allocation statistics after compilation: (buckets 4(4)..8188(8192)
  525. 14216 free: 130 117 28 7 9 0 2 2 1 0 0
  526. 437 61 36 0 5
  527. 60924 used: 125 137 161 55 7 8 6 16 2 0 1
  528. 74 109 304 84 20
  529. Total sbrk(): 77824/21:119. Odd ends: pad+heads+chain+tail: 0+636+0+2048.
  530. Memory allocation statistics after execution: (buckets 4(4)..8188(8192)
  531. 30888 free: 245 78 85 13 6 2 1 3 2 0 1
  532. 315 162 39 42 11
  533. 175816 used: 265 176 1112 111 26 22 11 27 2 1 1
  534. 196 178 1066 798 39
  535. Total sbrk(): 215040/47:145. Odd ends: pad+heads+chain+tail: 0+2192+0+6144.
  536. It is possible to ask for such a statistic at arbitrary points in
  537. your execution using the mstat() function out of the standard
  538. Devel::Peek module.
  539. Here is some explanation of that format:
  540. =over 4
  541. =item C<buckets SMALLEST(APPROX)..GREATEST(APPROX)>
  542. Perl's malloc() uses bucketed allocations. Every request is rounded
  543. up to the closest bucket size available, and a bucket is taken from
  544. the pool of buckets of that size.
  545. The line above describes the limits of buckets currently in use.
  546. Each bucket has two sizes: memory footprint and the maximal size
  547. of user data that can fit into this bucket. Suppose in the above
  548. example that the smallest bucket were size 4. The biggest bucket
  549. would have usable size 8188, and the memory footprint would be 8192.
  550. In a Perl built for debugging, some buckets may have negative usable
  551. size. This means that these buckets cannot (and will not) be used.
  552. For larger buckets, the memory footprint may be one page greater
  553. than a power of 2. If so, case the corresponding power of two is
  554. printed in the C<APPROX> field above.
  555. =item Free/Used
  556. The 1 or 2 rows of numbers following that correspond to the number
  557. of buckets of each size between C<SMALLEST> and C<GREATEST>. In
  558. the first row, the sizes (memory footprints) of buckets are powers
  559. of two--or possibly one page greater. In the second row, if present,
  560. the memory footprints of the buckets are between the memory footprints
  561. of two buckets "above".
  562. For example, suppose under the previous example, the memory footprints
  563. were
  564. free: 8 16 32 64 128 256 512 1024 2048 4096 8192
  565. 4 12 24 48 80
  566. With non-C<DEBUGGING> perl, the buckets starting from C<128> have
  567. a 4-byte overhead, and thus a 8192-long bucket may take up to
  568. 8188-byte allocations.
  569. =item C<Total sbrk(): SBRKed/SBRKs:CONTINUOUS>
  570. The first two fields give the total amount of memory perl sbrk(2)ed
  571. (ess-broken? :-) and number of sbrk(2)s used. The third number is
  572. what perl thinks about continuity of returned chunks. So long as
  573. this number is positive, malloc() will assume that it is probable
  574. that sbrk(2) will provide continuous memory.
  575. Memory allocated by external libraries is not counted.
  576. =item C<pad: 0>
  577. The amount of sbrk(2)ed memory needed to keep buckets aligned.
  578. =item C<heads: 2192>
  579. Although memory overhead of bigger buckets is kept inside the bucket, for
  580. smaller buckets, it is kept in separate areas. This field gives the
  581. total size of these areas.
  582. =item C<chain: 0>
  583. malloc() may want to subdivide a bigger bucket into smaller buckets.
  584. If only a part of the deceased bucket is left unsubdivided, the rest
  585. is kept as an element of a linked list. This field gives the total
  586. size of these chunks.
  587. =item C<tail: 6144>
  588. To minimize the number of sbrk(2)s, malloc() asks for more memory. This
  589. field gives the size of the yet unused part, which is sbrk(2)ed, but
  590. never touched.
  591. =back
  592. =head2 Example of using B<-DL> switch
  593. Below we show how to analyse memory usage by
  594. do 'lib/auto/POSIX/autosplit.ix';
  595. The file in question contains a header and 146 lines similar to
  596. sub getcwd;
  597. B<WARNING>: The discussion below supposes 32-bit architecture. In
  598. newer releases of Perl, memory usage of the constructs discussed
  599. here is greatly improved, but the story discussed below is a real-life
  600. story. This story is mercilessly terse, and assumes rather more than cursory
  601. knowledge of Perl internals. Type space to continue, `q' to quit.
  602. (Actually, you just want to skip to the next section.)
  603. Here is the itemized list of Perl allocations performed during parsing
  604. of this file:
  605. !!! "after" at test.pl line 3.
  606. Id subtot 4 8 12 16 20 24 28 32 36 40 48 56 64 72 80 80+
  607. 0 02 13752 . . . . 294 . . . . . . . . . . 4
  608. 0 54 5545 . . 8 124 16 . . . 1 1 . . . . . 3
  609. 5 05 32 . . . . . . . 1 . . . . . . . .
  610. 6 02 7152 . . . . . . . . . . 149 . . . . .
  611. 7 02 3600 . . . . . 150 . . . . . . . . . .
  612. 7 03 64 . -1 . 1 . . 2 . . . . . . . . .
  613. 7 04 7056 . . . . . . . . . . . . . . . 7
  614. 7 17 38404 . . . . . . . 1 . . 442 149 . . 147 .
  615. 9 03 2078 17 249 32 . . . . 2 . . . . . . . .
  616. To see this list, insert two C<warn('!...')> statements around the call:
  617. warn('!');
  618. do 'lib/auto/POSIX/autosplit.ix';
  619. warn('!!! "after"');
  620. and run it with Perl's B<-DL> option. The first warn() will print
  621. memory allocation info before parsing the file and will memorize
  622. the statistics at this point (we ignore what it prints). The second
  623. warn() prints increments with respect to these memorized data. This
  624. is the printout shown above.
  625. Different I<Id>s on the left correspond to different subsystems of
  626. the perl interpreter. They are just the first argument given to
  627. the perl memory allocation API named New(). To find what C<9 03>
  628. means, just B<grep> the perl source for C<903>. You'll find it in
  629. F<util.c>, function savepvn(). (I know, you wonder why we told you
  630. to B<grep> and then gave away the answer. That's because grepping
  631. the source is good for the soul.) This function is used to store
  632. a copy of an existing chunk of memory. Using a C debugger, one can
  633. see that the function was called either directly from gv_init() or
  634. via sv_magic(), and that gv_init() is called from gv_fetchpv()--which
  635. was itself called from newSUB(). Please stop to catch your breath now.
  636. B<NOTE>: To reach this point in the debugger and skip the calls to
  637. savepvn() during the compilation of the main program, you should
  638. set a C breakpoint
  639. in Perl_warn(), continue until this point is reached, and I<then> set
  640. a C breakpoint in Perl_savepvn(). Note that you may need to skip a
  641. handful of Perl_savepvn() calls that do not correspond to mass production
  642. of CVs (there are more C<903> allocations than 146 similar lines of
  643. F<lib/auto/POSIX/autosplit.ix>). Note also that C<Perl_> prefixes are
  644. added by macroization code in perl header files to avoid conflicts
  645. with external libraries.
  646. Anyway, we see that C<903> ids correspond to creation of globs, twice
  647. per glob - for glob name, and glob stringification magic.
  648. Here are explanations for other I<Id>s above:
  649. =over 4
  650. =item C<717>
  651. Creates bigger C<XPV*> structures. In the case above, it
  652. creates 3 C<AV>s per subroutine, one for a list of lexical variable
  653. names, one for a scratchpad (which contains lexical variables and
  654. C<targets>), and one for the array of scratchpads needed for
  655. recursion.
  656. It also creates a C<GV> and a C<CV> per subroutine, all called from
  657. start_subparse().
  658. =item C<002>
  659. Creates a C array corresponding to the C<AV> of scratchpads and the
  660. scratchpad itself. The first fake entry of this scratchpad is
  661. created though the subroutine itself is not defined yet.
  662. It also creates C arrays to keep data for the stash. This is one HV,
  663. but it grows; thus, there are 4 big allocations: the big chunks are not
  664. freed, but are kept as additional arenas for C<SV> allocations.
  665. =item C<054>
  666. Creates a C<HEK> for the name of the glob for the subroutine. This
  667. name is a key in a I<stash>.
  668. Big allocations with this I<Id> correspond to allocations of new
  669. arenas to keep C<HE>.
  670. =item C<602>
  671. Creates a C<GP> for the glob for the subroutine.
  672. =item C<702>
  673. Creates the C<MAGIC> for the glob for the subroutine.
  674. =item C<704>
  675. Creates I<arenas> which keep SVs.
  676. =back
  677. =head2 B<-DL> details
  678. If Perl is run with B<-DL> option, then warn()s that start with `!'
  679. behave specially. They print a list of I<categories> of memory
  680. allocations, and statistics of allocations of different sizes for
  681. these categories.
  682. If warn() string starts with
  683. =over 4
  684. =item C<!!!>
  685. print changed categories only, print the differences in counts of allocations.
  686. =item C<!!>
  687. print grown categories only; print the absolute values of counts, and totals.
  688. =item C<!>
  689. print nonempty categories, print the absolute values of counts and totals.
  690. =back
  691. =head2 Limitations of B<-DL> statistics
  692. If an extension or external library does not use the Perl API to
  693. allocate memory, such allocations are not counted.
  694. =head1 SEE ALSO
  695. L<perldebug>,
  696. L<perlguts>,
  697. L<perlrun>
  698. L<re>,
  699. and
  700. L<Devel::Dprof>.