blogs-debugging-article2.pod 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. In the first B::C debugging article we saw how to use the perl level debugger
  2. stepping into compiling code with <b>Od</b>.
  3. However we found no error in the compiler at a first glance.
  4. Inspecting the generated c code we see for the perl code
  5. <tt>
  6. 'package dummy;sub meth{print "ok"};package main;dummy->meth'
  7. </tt>
  8. parts of the optree as
  9. <pre>
  10. listop_list[0].op_ppaddr = PL_ppaddr[OP_LEAVE];
  11. op_list[0].op_ppaddr = PL_ppaddr[OP_ENTER];
  12. cop_list[0].op_ppaddr = PL_ppaddr[OP_NEXTSTATE];
  13. cop_list[0].cop_warnings = pWARN_STD;
  14. unop_list[0].op_ppaddr = PL_ppaddr[OP_ENTERSUB];
  15. op_list[1].op_ppaddr = PL_ppaddr[OP_PUSHMARK];
  16. sv_list[0].sv_u.svu_pv = savepvn("dummy", 5);
  17. svop_list[0].op_ppaddr = PL_ppaddr[OP_CONST];
  18. sv_list[1].sv_u.svu_pv = savepvn("meth", 4);
  19. svop_list[1].op_ppaddr = PL_ppaddr[OP_METHOD_NAMED];
  20. ..
  21. </pre>
  22. Check the optree with Concise:
  23. <pre>
  24. $ perl -MO=Concise -e'package dummy;sub meth{print "ok"};
  25. package main;dummy->meth'
  26. 7 <@> leave[1 ref] vKP/REFC ->(end)
  27. 1 <0> enter ->2
  28. 2 <;> nextstate(main 2 -e:1) v:{ ->3
  29. 6 <1> entersub[t1] vKS/TARG ->7
  30. 3 <0> pushmark s ->4
  31. 4 <$> const[PV "dummy"] sM/BARE ->5
  32. 5 <$> method_named[PV "meth"] ->6
  33. </pre>
  34. Lets debug into that live. Maybe method_named is wrong. You need a DEBUGGING perl,
  35. and I always prefer -g3 to expand macros.
  36. <em>I generated that as testcase 35 with <tt>t/testc.sh 35</tt>,
  37. so I get a ccode35.c and exe.</em>
  38. <pre>
  39. $ gdb ccode35
  40. (gdb) start
  41. main (argc=1, argv=0x1499a60, env=0x14880e0) at ccode35.c:240
  42. 240 {
  43. (gdb) b Perl_pp_method_named
  44. Breakpoint 2 at 0x5212b7b9: file pp_hot.c, line 3023.
  45. (gdb) c
  46. Breakpoint 2, Perl_pp_method_named () at pp_hot.c:3023
  47. 3023 dVAR; dSP;
  48. (gdb) bt
  49. #0 Perl_pp_method_named () at pp_hot.c:3023
  50. #1 0x520d95d1 in Perl_runops_debug () at dump.c:1968
  51. #2 0x52027747 in S_run_body (oldscope=1) at perl.c:2431
  52. #3 0x52026ce7 in perl_run (my_perl=0x1499b60) at perl.c:2349
  53. #4 0x00401e00 in _fu25__PL_compcv () at ccode35.c:307
  54. (gdb) n
  55. 3024 SV* const sv = cSVOP_sv;
  56. (gdb) n
  57. 3025 U32 hash = SvSHARED_HASH(sv);
  58. (gdb) p *sv
  59. $1 = {sv_any = 0x403390, sv_refcnt = 1, sv_flags = 151012357, sv_u = {
  60. svu_iv = 21808872, svu_uv = 21808872, svu_rv = 0x14cc6e8,
  61. svu_pv = 0x14cc6e8 "meth", svu_array = 0x14cc6e8, svu_hash = 0x14cc6e8,
  62. svu_gp = 0x14cc6e8}}
  63. </pre>
  64. So we are in the method named "meth", which should be in the "dummy" package.
  65. Since the error is
  66. Can't locate object method "meth" via package "dummy" (perhaps you forgot
  67. to load "dummy"?) at ccode35 line 1.
  68. we trust that the stash for meth is correctly assigned as "dummy", but we believe
  69. that the cv optree for <b>dummy::meth</b> is not stored.
  70. Indeed inspecting the c code shows us:
  71. <pre>
  72. svop_list[1].op_ppaddr = PL_ppaddr[OP_METHOD_NAMED];
  73. /* done main optree, extra subs which might be unused */
  74. /* done extras */
  75. </pre>
  76. There's only the optree for main, but no further subs.
  77. we miss &dummy::meth.
  78. Back to the B::Debugger <b>Od</b>.
  79. why is this sub not stored?