.gdbinit 13 KB

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