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.

494 lines
14 KiB

  1. # Devel::Peek - A data debugging tool for the XS programmer
  2. # The documentation is after the __END__
  3. package Devel::Peek;
  4. # Underscore to allow older Perls to access older version from CPAN
  5. $VERSION = '1.00_01';
  6. require Exporter;
  7. use XSLoader ();
  8. @ISA = qw(Exporter);
  9. @EXPORT = qw(Dump mstat DeadCode DumpArray DumpWithOP DumpProg
  10. fill_mstats mstats_fillhash mstats2hash);
  11. @EXPORT_OK = qw(SvREFCNT SvREFCNT_inc SvREFCNT_dec CvGV);
  12. %EXPORT_TAGS = ('ALL' => [@EXPORT, @EXPORT_OK]);
  13. XSLoader::load 'Devel::Peek';
  14. sub DumpWithOP ($;$) {
  15. local($Devel::Peek::dump_ops)=1;
  16. my $depth = @_ > 1 ? $_[1] : 4 ;
  17. Dump($_[0],$depth);
  18. }
  19. 1;
  20. __END__
  21. =head1 NAME
  22. Devel::Peek - A data debugging tool for the XS programmer
  23. =head1 SYNOPSIS
  24. use Devel::Peek;
  25. Dump( $a );
  26. Dump( $a, 5 );
  27. DumpArray( 5, $a, $b, ... );
  28. mstat "Point 5";
  29. =head1 DESCRIPTION
  30. Devel::Peek contains functions which allows raw Perl datatypes to be
  31. manipulated from a Perl script. This is used by those who do XS programming
  32. to check that the data they are sending from C to Perl looks as they think
  33. it should look. The trick, then, is to know what the raw datatype is
  34. supposed to look like when it gets to Perl. This document offers some tips
  35. and hints to describe good and bad raw data.
  36. It is very possible that this document will fall far short of being useful
  37. to the casual reader. The reader is expected to understand the material in
  38. the first few sections of L<perlguts>.
  39. Devel::Peek supplies a C<Dump()> function which can dump a raw Perl
  40. datatype, and C<mstat("marker")> function to report on memory usage
  41. (if perl is compiled with corresponding option). The function
  42. DeadCode() provides statistics on the data "frozen" into inactive
  43. C<CV>. Devel::Peek also supplies C<SvREFCNT()>, C<SvREFCNT_inc()>, and
  44. C<SvREFCNT_dec()> which can query, increment, and decrement reference
  45. counts on SVs. This document will take a passive, and safe, approach
  46. to data debugging and for that it will describe only the C<Dump()>
  47. function.
  48. Function C<DumpArray()> allows dumping of multiple values (useful when you
  49. need to analyze returns of functions).
  50. The global variable $Devel::Peek::pv_limit can be set to limit the
  51. number of character printed in various string values. Setting it to 0
  52. means no limit.
  53. =head2 Memory footprint debugging
  54. When perl is compiled with support for memory footprint debugging
  55. (default with Perl's malloc()), Devel::Peek provides an access to this API.
  56. Use mstat() function to emit a memory state statistic to the terminal.
  57. For more information on the format of output of mstat() see
  58. L<perldebug/Using C<$ENV{PERL_DEBUG_MSTATS}>>.
  59. Three additional functions allow access to this statistic from Perl.
  60. First, use C<mstats_fillhash(%hash)> to get the information contained
  61. in the output of mstat() into %hash. The field of this hash are
  62. minbucket nbuckets sbrk_good sbrk_slack sbrked_remains sbrks start_slack
  63. topbucket topbucket_ev topbucket_odd total total_chain total_sbrk totfree
  64. Two additional fields C<free>, C<used> contain array references which
  65. provide per-bucket count of free and used chunks. Two other fields
  66. C<mem_size>, C<available_size> contain array references which provide
  67. the information about the allocated size and usable size of chunks in
  68. each bucket. Again, see L<perldebug/Using C<$ENV{PERL_DEBUG_MSTATS}>>
  69. for details.
  70. Keep in mind that only the first several "odd-numbered" buckets are
  71. used, so the information on size of the "odd-numbered" buckets which are
  72. not used is probably meaningless.
  73. The information in
  74. mem_size available_size minbucket nbuckets
  75. is the property of a particular build of perl, and does not depend on
  76. the current process. If you do not provide the optional argument to
  77. the functions mstats_fillhash(), fill_mstats(), mstats2hash(), then
  78. the information in fields C<mem_size>, C<available_size> is not
  79. updated.
  80. C<fill_mstats($buf)> is a much cheaper call (both speedwise and
  81. memory-wise) which collects the statistic into $buf in
  82. machine-readable form. At a later moment you may need to call
  83. C<mstats2hash($buf, %hash)> to use this information to fill %hash.
  84. All three APIs C<fill_mstats($buf)>, C<mstats_fillhash(%hash)>, and
  85. C<mstats2hash($buf, %hash)> are designed to allocate no memory if used
  86. I<the second time> on the same $buf and/or %hash.
  87. So, if you want to collect memory info in a cycle, you may call
  88. $#buf = 999;
  89. fill_mstats($_) for @buf;
  90. mstats_fillhash(%report, 1); # Static info too
  91. foreach (@buf) {
  92. # Do something...
  93. fill_mstats $_; # Collect statistic
  94. }
  95. foreach (@buf) {
  96. mstats2hash($_, %report); # Preserve static info
  97. # Do something with %report
  98. }
  99. =head1 EXAMPLES
  100. The following examples don't attempt to show everything as that would be a
  101. monumental task, and, frankly, we don't want this manpage to be an internals
  102. document for Perl. The examples do demonstrate some basics of the raw Perl
  103. datatypes, and should suffice to get most determined people on their way.
  104. There are no guidewires or safety nets, nor blazed trails, so be prepared to
  105. travel alone from this point and on and, if at all possible, don't fall into
  106. the quicksand (it's bad for business).
  107. Oh, one final bit of advice: take L<perlguts> with you. When you return we
  108. expect to see it well-thumbed.
  109. =head2 A simple scalar string
  110. Let's begin by looking a simple scalar which is holding a string.
  111. use Devel::Peek;
  112. $a = "hello";
  113. Dump $a;
  114. The output:
  115. SV = PVIV(0xbc288)
  116. REFCNT = 1
  117. FLAGS = (POK,pPOK)
  118. IV = 0
  119. PV = 0xb2048 "hello"\0
  120. CUR = 5
  121. LEN = 6
  122. This says C<$a> is an SV, a scalar. The scalar is a PVIV, a string.
  123. Its reference count is 1. It has the C<POK> flag set, meaning its
  124. current PV field is valid. Because POK is set we look at the PV item
  125. to see what is in the scalar. The \0 at the end indicate that this
  126. PV is properly NUL-terminated.
  127. If the FLAGS had been IOK we would look
  128. at the IV item. CUR indicates the number of characters in the PV.
  129. LEN indicates the number of bytes requested for the PV (one more than
  130. CUR, in this case, because LEN includes an extra byte for the
  131. end-of-string marker).
  132. =head2 A simple scalar number
  133. If the scalar contains a number the raw SV will be leaner.
  134. use Devel::Peek;
  135. $a = 42;
  136. Dump $a;
  137. The output:
  138. SV = IV(0xbc818)
  139. REFCNT = 1
  140. FLAGS = (IOK,pIOK)
  141. IV = 42
  142. This says C<$a> is an SV, a scalar. The scalar is an IV, a number. Its
  143. reference count is 1. It has the C<IOK> flag set, meaning it is currently
  144. being evaluated as a number. Because IOK is set we look at the IV item to
  145. see what is in the scalar.
  146. =head2 A simple scalar with an extra reference
  147. If the scalar from the previous example had an extra reference:
  148. use Devel::Peek;
  149. $a = 42;
  150. $b = \$a;
  151. Dump $a;
  152. The output:
  153. SV = IV(0xbe860)
  154. REFCNT = 2
  155. FLAGS = (IOK,pIOK)
  156. IV = 42
  157. Notice that this example differs from the previous example only in its
  158. reference count. Compare this to the next example, where we dump C<$b>
  159. instead of C<$a>.
  160. =head2 A reference to a simple scalar
  161. This shows what a reference looks like when it references a simple scalar.
  162. use Devel::Peek;
  163. $a = 42;
  164. $b = \$a;
  165. Dump $b;
  166. The output:
  167. SV = RV(0xf041c)
  168. REFCNT = 1
  169. FLAGS = (ROK)
  170. RV = 0xbab08
  171. SV = IV(0xbe860)
  172. REFCNT = 2
  173. FLAGS = (IOK,pIOK)
  174. IV = 42
  175. Starting from the top, this says C<$b> is an SV. The scalar is an RV, a
  176. reference. It has the C<ROK> flag set, meaning it is a reference. Because
  177. ROK is set we have an RV item rather than an IV or PV. Notice that Dump
  178. follows the reference and shows us what C<$b> was referencing. We see the
  179. same C<$a> that we found in the previous example.
  180. Note that the value of C<RV> coincides with the numbers we see when we
  181. stringify $b. The addresses inside RV() and IV() are addresses of
  182. C<X***> structure which holds the current state of an C<SV>. This
  183. address may change during lifetime of an SV.
  184. =head2 A reference to an array
  185. This shows what a reference to an array looks like.
  186. use Devel::Peek;
  187. $a = [42];
  188. Dump $a;
  189. The output:
  190. SV = RV(0xf041c)
  191. REFCNT = 1
  192. FLAGS = (ROK)
  193. RV = 0xb2850
  194. SV = PVAV(0xbd448)
  195. REFCNT = 1
  196. FLAGS = ()
  197. IV = 0
  198. NV = 0
  199. ARRAY = 0xb2048
  200. ALLOC = 0xb2048
  201. FILL = 0
  202. MAX = 0
  203. ARYLEN = 0x0
  204. FLAGS = (REAL)
  205. Elt No. 0 0xb5658
  206. SV = IV(0xbe860)
  207. REFCNT = 1
  208. FLAGS = (IOK,pIOK)
  209. IV = 42
  210. This says C<$a> is an SV and that it is an RV. That RV points to
  211. another SV which is a PVAV, an array. The array has one element,
  212. element zero, which is another SV. The field C<FILL> above indicates
  213. the last element in the array, similar to C<$#$a>.
  214. If C<$a> pointed to an array of two elements then we would see the
  215. following.
  216. use Devel::Peek 'Dump';
  217. $a = [42,24];
  218. Dump $a;
  219. The output:
  220. SV = RV(0xf041c)
  221. REFCNT = 1
  222. FLAGS = (ROK)
  223. RV = 0xb2850
  224. SV = PVAV(0xbd448)
  225. REFCNT = 1
  226. FLAGS = ()
  227. IV = 0
  228. NV = 0
  229. ARRAY = 0xb2048
  230. ALLOC = 0xb2048
  231. FILL = 0
  232. MAX = 0
  233. ARYLEN = 0x0
  234. FLAGS = (REAL)
  235. Elt No. 0 0xb5658
  236. SV = IV(0xbe860)
  237. REFCNT = 1
  238. FLAGS = (IOK,pIOK)
  239. IV = 42
  240. Elt No. 1 0xb5680
  241. SV = IV(0xbe818)
  242. REFCNT = 1
  243. FLAGS = (IOK,pIOK)
  244. IV = 24
  245. Note that C<Dump> will not report I<all> the elements in the array,
  246. only several first (depending on how deep it already went into the
  247. report tree).
  248. =head2 A reference to a hash
  249. The following shows the raw form of a reference to a hash.
  250. use Devel::Peek;
  251. $a = {hello=>42};
  252. Dump $a;
  253. The output:
  254. SV = RV(0xf041c)
  255. REFCNT = 1
  256. FLAGS = (ROK)
  257. RV = 0xb2850
  258. SV = PVHV(0xbd448)
  259. REFCNT = 1
  260. FLAGS = ()
  261. NV = 0
  262. ARRAY = 0xbd748
  263. KEYS = 1
  264. FILL = 1
  265. MAX = 7
  266. RITER = -1
  267. EITER = 0x0
  268. Elt "hello" => 0xbaaf0
  269. SV = IV(0xbe860)
  270. REFCNT = 1
  271. FLAGS = (IOK,pIOK)
  272. IV = 42
  273. This shows C<$a> is a reference pointing to an SV. That SV is a PVHV, a
  274. hash. Fields RITER and EITER are used by C<L<each>>.
  275. =head2 Dumping a large array or hash
  276. The C<Dump()> function, by default, dumps up to 4 elements from a
  277. toplevel array or hash. This number can be increased by supplying a
  278. second argument to the function.
  279. use Devel::Peek;
  280. $a = [10,11,12,13,14];
  281. Dump $a;
  282. Notice that C<Dump()> prints only elements 10 through 13 in the above code.
  283. The following code will print all of the elements.
  284. use Devel::Peek 'Dump';
  285. $a = [10,11,12,13,14];
  286. Dump $a, 5;
  287. =head2 A reference to an SV which holds a C pointer
  288. This is what you really need to know as an XS programmer, of course. When
  289. an XSUB returns a pointer to a C structure that pointer is stored in an SV
  290. and a reference to that SV is placed on the XSUB stack. So the output from
  291. an XSUB which uses something like the T_PTROBJ map might look something like
  292. this:
  293. SV = RV(0xf381c)
  294. REFCNT = 1
  295. FLAGS = (ROK)
  296. RV = 0xb8ad8
  297. SV = PVMG(0xbb3c8)
  298. REFCNT = 1
  299. FLAGS = (OBJECT,IOK,pIOK)
  300. IV = 729160
  301. NV = 0
  302. PV = 0
  303. STASH = 0xc1d10 "CookBookB::Opaque"
  304. This shows that we have an SV which is an RV. That RV points at another
  305. SV. In this case that second SV is a PVMG, a blessed scalar. Because it is
  306. blessed it has the C<OBJECT> flag set. Note that an SV which holds a C
  307. pointer also has the C<IOK> flag set. The C<STASH> is set to the package
  308. name which this SV was blessed into.
  309. The output from an XSUB which uses something like the T_PTRREF map, which
  310. doesn't bless the object, might look something like this:
  311. SV = RV(0xf381c)
  312. REFCNT = 1
  313. FLAGS = (ROK)
  314. RV = 0xb8ad8
  315. SV = PVMG(0xbb3c8)
  316. REFCNT = 1
  317. FLAGS = (IOK,pIOK)
  318. IV = 729160
  319. NV = 0
  320. PV = 0
  321. =head2 A reference to a subroutine
  322. Looks like this:
  323. SV = RV(0x798ec)
  324. REFCNT = 1
  325. FLAGS = (TEMP,ROK)
  326. RV = 0x1d453c
  327. SV = PVCV(0x1c768c)
  328. REFCNT = 2
  329. FLAGS = ()
  330. IV = 0
  331. NV = 0
  332. COMP_STASH = 0x31068 "main"
  333. START = 0xb20e0
  334. ROOT = 0xbece0
  335. XSUB = 0x0
  336. XSUBANY = 0
  337. GVGV::GV = 0x1d44e8 "MY" :: "top_targets"
  338. FILE = "(eval 5)"
  339. DEPTH = 0
  340. PADLIST = 0x1c9338
  341. This shows that
  342. =over
  343. =item *
  344. the subroutine is not an XSUB (since C<START> and C<ROOT> are
  345. non-zero, and C<XSUB> is zero);
  346. =item *
  347. that it was compiled in the package C<main>;
  348. =item *
  349. under the name C<MY::top_targets>;
  350. =item *
  351. inside a 5th eval in the program;
  352. =item *
  353. it is not currently executed (see C<DEPTH>);
  354. =item *
  355. it has no prototype (C<PROTOTYPE> field is missing).
  356. =back
  357. =head1 EXPORTS
  358. C<Dump>, C<mstat>, C<DeadCode>, C<DumpArray>, C<DumpWithOP> and
  359. C<DumpProg>, C<fill_mstats>, C<mstats_fillhash>, C<mstats2hash> by
  360. default. Additionally available C<SvREFCNT>, C<SvREFCNT_inc> and
  361. C<SvREFCNT_dec>.
  362. =head1 BUGS
  363. Readers have been known to skip important parts of L<perlguts>, causing much
  364. frustration for all.
  365. =head1 AUTHOR
  366. Ilya Zakharevich ilya@math.ohio-state.edu
  367. Copyright (c) 1995-98 Ilya Zakharevich. All rights reserved.
  368. This program is free software; you can redistribute it and/or
  369. modify it under the same terms as Perl itself.
  370. Author of this software makes no claim whatsoever about suitability,
  371. reliability, edability, editability or usability of this product, and
  372. should not be kept liable for any damage resulting from the use of
  373. it. If you can use it, you are in luck, if not, I should not be kept
  374. responsible. Keep a handy copy of your backup tape at hand.
  375. =head1 SEE ALSO
  376. L<perlguts>, and L<perlguts>, again.
  377. =cut