.gdbinit 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. define set_ts
  2. set $tsrm_ls = $arg0
  3. end
  4. document set_ts
  5. set the ts resource, it is impossible for gdb to
  6. call ts_resource_ex while no process is running,
  7. but we could get the resource from the argument
  8. of frame info.
  9. end
  10. define ____executor_globals
  11. if basic_functions_module.zts
  12. if !$tsrm_ls
  13. set $tsrm_ls = ts_resource_ex(0, 0)
  14. end
  15. set $eg = ((zend_executor_globals*) (*((void ***) $tsrm_ls))[executor_globals_id-1])
  16. set $cg = ((zend_compiler_globals*) (*((void ***) $tsrm_ls))[compiler_globals_id-1])
  17. set $eg_ptr = $eg
  18. else
  19. set $eg = executor_globals
  20. set $cg = compiler_globals
  21. set $eg_ptr = (zend_executor_globals*) &executor_globals
  22. end
  23. end
  24. document ____executor_globals
  25. portable way of accessing executor_globals, set $eg
  26. this also sets compiler_globals to $cg
  27. ZTS detection is automatically based on ext/standard module struct
  28. end
  29. define print_cvs
  30. if $argc == 0
  31. ____executor_globals
  32. set $cv_ex_ptr = $eg.current_execute_data
  33. else
  34. set $cv_ex_ptr = (zend_execute_data *)$arg0
  35. end
  36. set $cv_count = $cv_ex_ptr.func.op_array.last_var
  37. set $cv = $cv_ex_ptr.func.op_array.vars
  38. set $cv_idx = 0
  39. set $callFrameSize = (sizeof(zend_execute_data) + sizeof(zval) - 1) / sizeof(zval)
  40. printf "Compiled variables count: %d\n\n", $cv_count
  41. while $cv_idx < $cv_count
  42. printf "[%d] '%s'\n", $cv_idx, $cv[$cv_idx].val
  43. set $zvalue = ((zval *) $cv_ex_ptr) + $callFrameSize + $cv_idx
  44. printzv $zvalue
  45. set $cv_idx = $cv_idx + 1
  46. end
  47. end
  48. document print_cvs
  49. Prints the compiled variables and their values.
  50. If a zend_execute_data pointer is set this will print the compiled
  51. variables of that scope. If no parameter is used it will use
  52. current_execute_data for scope.
  53. usage: print_cvs [zend_execute_data *]
  54. end
  55. define dump_bt
  56. set $ex = $arg0
  57. while $ex
  58. printf "[%p] ", $ex
  59. set $func = $ex->func
  60. if $func
  61. if $ex->This->value.obj
  62. if $func->common.scope
  63. printf "%s->", $func->common.scope->name->val
  64. else
  65. printf "%s->", $ex->This->value.obj->ce.name->val
  66. end
  67. else
  68. if $func->common.scope
  69. printf "%s::", $func->common.scope->name->val
  70. end
  71. end
  72. if $func->common.function_name
  73. printf "%s(", $func->common.function_name->val
  74. else
  75. printf "(main"
  76. end
  77. set $callFrameSize = (sizeof(zend_execute_data) + sizeof(zval) - 1) / sizeof(zval)
  78. set $count = $ex->This.u2.num_args
  79. set $arg = 0
  80. while $arg < $count
  81. if $arg > 0
  82. printf ", "
  83. end
  84. set $zvalue = (zval *) $ex + $callFrameSize + $arg
  85. set $type = $zvalue->u1.v.type
  86. if $type == 1
  87. printf "NULL"
  88. end
  89. if $type == 2
  90. printf "false"
  91. end
  92. if $type == 3
  93. printf "true"
  94. end
  95. if $type == 4
  96. printf "%ld", $zvalue->value.lval
  97. end
  98. if $type == 5
  99. printf "%f", $zvalue->value.dval
  100. end
  101. if $type == 6
  102. ____print_str $zvalue->value.str->val $zvalue->value.str->len
  103. end
  104. if $type == 7
  105. printf "array(%d)[%p]", $zvalue->value.arr->nNumOfElements, $zvalue
  106. end
  107. if $type == 8
  108. printf "object[%p]", $zvalue
  109. end
  110. if $type == 9
  111. printf "resource(#%d)", $zvalue->value.lval
  112. end
  113. if $type == 10
  114. printf "reference"
  115. end
  116. if $type > 10
  117. printf "unknown type %d", $type
  118. end
  119. set $arg = $arg + 1
  120. end
  121. printf ") "
  122. else
  123. printf "??? "
  124. end
  125. if $func != 0
  126. if $func->type == 2
  127. printf "%s:%d ", $func->op_array.filename->val, $ex->opline->lineno
  128. else
  129. printf "[internal function]"
  130. end
  131. end
  132. set $ex = $ex->prev_execute_data
  133. printf "\n"
  134. end
  135. end
  136. document dump_bt
  137. dumps the current execution stack. usage: dump_bt executor_globals.current_execute_data
  138. end
  139. define printzv
  140. set $ind = 1
  141. ____printzv $arg0 0
  142. end
  143. document printzv
  144. prints zval contents
  145. end
  146. define ____printzv_contents
  147. set $zvalue = $arg0
  148. set $type = $zvalue->u1.v.type
  149. # 15 == IS_INDIRECT
  150. if $type > 5 && $type != 15
  151. printf "(refcount=%d) ", $zvalue->value.counted->gc.refcount
  152. end
  153. if $type == 0
  154. printf "UNDEF"
  155. end
  156. if $type == 1
  157. printf "NULL"
  158. end
  159. if $type == 2
  160. printf "bool: false"
  161. end
  162. if $type == 3
  163. printf "bool: true"
  164. end
  165. if $type == 4
  166. printf "long: %ld", $zvalue->value.lval
  167. end
  168. if $type == 5
  169. printf "double: %f", $zvalue->value.dval
  170. end
  171. if $type == 6
  172. printf "string: %s", $zvalue->value.str->val
  173. end
  174. if $type == 7
  175. printf "array: "
  176. if ! $arg1
  177. set $ind = $ind + 1
  178. ____print_ht $zvalue->value.arr 1
  179. set $ind = $ind - 1
  180. set $i = $ind
  181. while $i > 0
  182. printf " "
  183. set $i = $i - 1
  184. end
  185. end
  186. set $type = 0
  187. end
  188. if $type == 8
  189. printf "object"
  190. ____executor_globals
  191. set $handle = $zvalue->value.obj.handle
  192. set $handlers = $zvalue->value.obj.handlers
  193. set $zobj = $zvalue->value.obj
  194. set $cname = $zobj->ce->name->val
  195. printf "(%s) #%d", $cname, $handle
  196. if ! $arg1
  197. if $handlers->get_properties == &zend_std_get_properties
  198. if $zobj->properties
  199. set $ht = $zobj->properties
  200. else
  201. set $ht = &$zobj->ce->properties_info
  202. end
  203. printf "\nProperties "
  204. if $ht
  205. set $ind = $ind + 1
  206. ____print_ht $ht 1
  207. set $ind = $ind - 1
  208. set $i = $ind
  209. while $i > 0
  210. printf " "
  211. set $i = $i - 1
  212. end
  213. else
  214. echo "not found"
  215. end
  216. end
  217. end
  218. set $type = 0
  219. end
  220. if $type == 9
  221. printf "resource: #%d", $zvalue->value.res->handle
  222. end
  223. if $type == 10
  224. printf "reference: "
  225. ____printzv &$zvalue->value.ref->val $arg1
  226. end
  227. if $type == 11
  228. printf "const: %s", $zvalue->value.str->val
  229. end
  230. if $type == 12
  231. printf "CONSTANT_AST"
  232. end
  233. if $type == 13
  234. printf "_BOOL"
  235. end
  236. if $type == 14
  237. printf "CALLABLE"
  238. end
  239. if $type == 15
  240. printf "indirect: "
  241. ____printzv $zvalue->value.zv $arg1
  242. end
  243. if $type == 17
  244. printf "pointer: %p", $zvalue->value.ptr
  245. end
  246. if $type == 18
  247. printf "ITERABLE"
  248. end
  249. if $type == 19
  250. printf "VOID"
  251. end
  252. if $type == 20
  253. printf "_ERROR"
  254. end
  255. if $type == 16 || $type > 20
  256. printf "unknown type %d", $type
  257. end
  258. printf "\n"
  259. end
  260. define ____printzv
  261. ____executor_globals
  262. set $zvalue = $arg0
  263. printf "[%p] ", $zvalue
  264. set $zcontents = (zval*) $zvalue
  265. if $arg1
  266. ____printzv_contents $zcontents $arg1
  267. else
  268. ____printzv_contents $zcontents 0
  269. end
  270. end
  271. define print_global_vars
  272. ____executor_globals
  273. set $symtable = ((HashTable *)&($eg_ptr->symbol_table))
  274. print_ht $symtable
  275. end
  276. document print_global_vars
  277. Prints the global variables
  278. end
  279. define print_const_table
  280. set $ind = 1
  281. printf "[%p] {\n", $arg0
  282. ____print_ht $arg0 4
  283. printf "}\n"
  284. end
  285. document print_const_table
  286. Dumps elements of Constants HashTable
  287. Example: print_const_table executor_globals.zend_constants
  288. end
  289. define ____print_ht
  290. set $ht = (HashTable*)$arg0
  291. set $n = $ind
  292. while $n > 0
  293. printf " "
  294. set $n = $n - 1
  295. end
  296. if $ht->u.v.flags & 4
  297. printf "Packed"
  298. else
  299. printf "Hash"
  300. end
  301. printf "(%d)[%p]: {\n", $ht->nNumOfElements, $ht
  302. set $num = $ht->nNumUsed
  303. set $i = 0
  304. set $ind = $ind + 1
  305. while $i < $num
  306. set $p = (Bucket*)($ht->arData + $i)
  307. set $n = $ind
  308. if $p->val.u1.v.type > 0
  309. while $n > 0
  310. printf " "
  311. set $n = $n - 1
  312. end
  313. printf "[%d] ", $i
  314. if $p->key
  315. printf "%s => ", $p->key->val
  316. else
  317. printf "%d => ", $p->h
  318. end
  319. if $arg1 == 0
  320. printf "%p\n", (zval *)&$p->val
  321. end
  322. if $arg1 == 1
  323. set $zval = (zval *)&$p->val
  324. ____printzv $zval 1
  325. end
  326. if $arg1 == 2
  327. printf "%s\n", (char*)$p->val.value.ptr
  328. end
  329. if $arg1 == 3
  330. set $func = (zend_function*)$p->val.value.ptr
  331. printf "\"%s\"\n", $func->common.function_name->val
  332. end
  333. if $arg1 == 4
  334. set $const = (zend_constant *)$p->val.value.ptr
  335. ____printzv $const 1
  336. end
  337. end
  338. set $i = $i + 1
  339. end
  340. set $ind = $ind - 1
  341. printf "}\n"
  342. end
  343. define print_ht
  344. set $ind = 0
  345. ____print_ht $arg0 1
  346. end
  347. document print_ht
  348. dumps elements of HashTable made of zval
  349. end
  350. define print_htptr
  351. set $ind = 0
  352. ____print_ht $arg0 0
  353. end
  354. document print_htptr
  355. dumps elements of HashTable made of pointers
  356. end
  357. define print_htstr
  358. set $ind = 0
  359. ____print_ht $arg0 2
  360. end
  361. document print_htstr
  362. dumps elements of HashTable made of strings
  363. end
  364. define print_ft
  365. set $ind = 0
  366. ____print_ht $arg0 3
  367. end
  368. document print_ft
  369. dumps a function table (HashTable)
  370. end
  371. define ____print_inh_class
  372. set $ce = $arg0
  373. if $ce->ce_flags & 0x10 || $ce->ce_flags & 0x20
  374. printf "abstract "
  375. else
  376. if $ce->ce_flags & 0x40
  377. printf "final "
  378. end
  379. end
  380. printf "class %s", $ce->name->val
  381. if $ce->parent != 0
  382. printf " extends %s", $ce->parent->name->val
  383. end
  384. if $ce->num_interfaces != 0
  385. printf " implements"
  386. set $tmp = 0
  387. while $tmp < $ce->num_interfaces
  388. printf " %s", $ce->interfaces[$tmp]->name->val
  389. set $tmp = $tmp + 1
  390. if $tmp < $ce->num_interfaces
  391. printf ","
  392. end
  393. end
  394. end
  395. set $ce = $ce->parent
  396. end
  397. define ____print_inh_iface
  398. set $ce = $arg0
  399. printf "interface %s", $ce->name->val
  400. if $ce->num_interfaces != 0
  401. set $ce = $ce->interfaces[0]
  402. printf " extends %s", $ce->name->val
  403. else
  404. set $ce = 0
  405. end
  406. end
  407. define print_inh
  408. set $ce = $arg0
  409. set $depth = 0
  410. while $ce != 0
  411. set $tmp = $depth
  412. while $tmp != 0
  413. printf " "
  414. set $tmp = $tmp - 1
  415. end
  416. set $depth = $depth + 1
  417. if $ce->ce_flags & 0x80
  418. ____print_inh_iface $ce
  419. else
  420. ____print_inh_class $ce
  421. end
  422. printf " {\n"
  423. end
  424. while $depth != 0
  425. set $tmp = $depth
  426. while $tmp != 1
  427. printf " "
  428. set $tmp = $tmp - 1
  429. end
  430. printf "}\n"
  431. set $depth = $depth - 1
  432. end
  433. end
  434. define print_pi
  435. set $pi = (zend_property_info *)$arg0
  436. set $initial_offset = ((uint32_t)(zend_uintptr_t)(&((zend_object*)0)->properties_table[(0)]))
  437. set $ptr_to_val = (zval*)((char*)$pi->ce->default_properties_table + $pi->offset - $initial_offset)
  438. printf "[%p] {\n", $pi
  439. printf " offset = %p\n", $pi->offset
  440. printf " ce = [%p] %s\n", $pi->ce, $pi->ce->name->val
  441. printf " flags = 0x%x (", $pi->flags
  442. if $pi->flags & 0x100
  443. printf "ZEND_ACC_PUBLIC"
  444. else
  445. if $pi->flags & 0x200
  446. printf "ZEND_ACC_PROTECTED"
  447. else
  448. if $pi->flags & 0x400
  449. printf "ZEND_ACC_PRIVATE"
  450. else
  451. if $pi->flags & 0x800
  452. printf "ZEND_ACC_EARLY_BINDING"
  453. else
  454. if $pi->flags & 0x20000
  455. printf "ZEND_ACC_SHADOW"
  456. end
  457. end
  458. end
  459. end
  460. end
  461. printf ")\n"
  462. printf " name = "
  463. print_zstr $pi->name
  464. printf " default value: "
  465. printzv $ptr_to_val
  466. printf "}\n"
  467. end
  468. document print_pi
  469. Takes a pointer to an object's property and prints the property information
  470. usage: print_pi <ptr>
  471. end
  472. define ____print_str
  473. set $tmp = 0
  474. set $str = $arg0
  475. if $argc > 2
  476. set $maxlen = $arg2
  477. else
  478. set $maxlen = 256
  479. end
  480. printf "\""
  481. while $tmp < $arg1 && $tmp < $maxlen
  482. if $str[$tmp] > 31 && $str[$tmp] < 127
  483. printf "%c", $str[$tmp]
  484. else
  485. printf "\\%o", $str[$tmp]
  486. end
  487. set $tmp = $tmp + 1
  488. end
  489. if $tmp != $arg1
  490. printf "..."
  491. end
  492. printf "\""
  493. end
  494. define printzn
  495. ____executor_globals
  496. set $ind = 0
  497. set $znode = $arg0
  498. if $znode->op_type == 1
  499. set $optype = "IS_CONST"
  500. end
  501. if $znode->op_type == 2
  502. set $optype = "IS_TMP_VAR"
  503. end
  504. if $znode->op_type == 4
  505. set $optype = "IS_VAR"
  506. end
  507. if $znode->op_type == 8
  508. set $optype = "IS_UNUSED"
  509. end
  510. printf "[%p] %s", $znode, $optype
  511. if $znode->op_type == 1
  512. printf ": "
  513. ____printzv &$znode->u.constant 0
  514. end
  515. if $znode->op_type == 2
  516. printf ": "
  517. set $tvar = (union _temp_variable *)((char *)$eg.current_execute_data->Ts + $znode->u.var)
  518. ____printzv ((union _temp_variable *)$tvar)->tmp_var 0
  519. end
  520. if $znode->op_type == 4
  521. printf ": "
  522. set $tvar = (union _temp_variable *)((char *)$eg.current_execute_data->Ts + $znode->u.var)
  523. ____printzv *$tvar->var.ptr_ptr 0
  524. end
  525. if $znode->op_type == 8
  526. printf "\n"
  527. end
  528. end
  529. document printzn
  530. print type and content of znode.
  531. usage: printzn &opline->op1
  532. end
  533. define printzops
  534. printf "op1 => "
  535. printzn &execute_data->opline.op1
  536. printf "op2 => "
  537. printzn &execute_data->opline.op2
  538. printf "result => "
  539. printzn &execute_data->opline.result
  540. end
  541. document printzops
  542. dump operands of the current opline
  543. end
  544. define print_zstr
  545. set $zstr = (zend_string *)$arg0
  546. if $argc == 2
  547. set $maxlen = $arg1
  548. else
  549. set $maxlen = $zstr->len
  550. end
  551. printf "string(%d) ", $zstr->len
  552. ____print_str $zstr->val $zstr->len $maxlen
  553. printf "\n"
  554. end
  555. document print_zstr
  556. print the length and contents of a zend string
  557. usage: print_zstr <ptr> [max length]
  558. end
  559. define zbacktrace
  560. ____executor_globals
  561. dump_bt $eg.current_execute_data
  562. end
  563. document zbacktrace
  564. prints backtrace.
  565. This command is almost a short cut for
  566. > (gdb) ____executor_globals
  567. > (gdb) dump_bt $eg.current_execute_data
  568. end
  569. define lookup_root
  570. set $found = 0
  571. if gc_globals->roots
  572. set $current = gc_globals->roots->next
  573. printf "looking ref %p in roots\n", $arg0
  574. while $current != &gc_globals->roots
  575. if $current->ref == $arg0
  576. set $found = $current
  577. break
  578. end
  579. set $current = $current->next
  580. end
  581. if $found != 0
  582. printf "found root %p\n", $found
  583. else
  584. printf "not found\n"
  585. end
  586. end
  587. end
  588. document lookup_root
  589. lookup a refcounted in root
  590. usage: lookup_root [ptr].
  591. end