phpdbg_bp.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Felipe Pena <felipe@php.net> |
  16. | Authors: Joe Watkins <joe.watkins@live.co.uk> |
  17. | Authors: Bob Weinand <bwoebi@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include "zend.h"
  21. #include "zend_hash.h"
  22. #include "phpdbg.h"
  23. #include "phpdbg_bp.h"
  24. #include "phpdbg_utils.h"
  25. #include "phpdbg_opcode.h"
  26. #include "zend_globals.h"
  27. #include "ext/standard/php_string.h"
  28. ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
  29. /* {{{ private api functions */
  30. static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_file(zend_op_array*);
  31. static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_symbol(zend_function*);
  32. static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_method(zend_op_array*);
  33. static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_opline(phpdbg_opline_ptr_t);
  34. static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_opcode(zend_uchar);
  35. static inline phpdbg_breakbase_t *phpdbg_find_conditional_breakpoint(zend_execute_data *execute_data); /* }}} */
  36. /*
  37. * Note:
  38. * A break point must always set the correct id and type
  39. * A set breakpoint function must always map new points
  40. */
  41. static inline void _phpdbg_break_mapping(int id, HashTable *table) /* {{{ */
  42. {
  43. zend_hash_index_update_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP], id, table);
  44. }
  45. /* }}} */
  46. #define PHPDBG_BREAK_MAPPING(id, table) _phpdbg_break_mapping(id, table)
  47. #define PHPDBG_BREAK_UNMAPPING(id) \
  48. zend_hash_index_del(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP], (id))
  49. #define PHPDBG_BREAK_INIT(b, t) do {\
  50. memset(&b, 0, sizeof(b)); \
  51. b.id = PHPDBG_G(bp_count)++; \
  52. b.type = t; \
  53. b.disabled = 0;\
  54. b.hits = 0; \
  55. } while(0)
  56. static void phpdbg_file_breaks_dtor(zval *data) /* {{{ */
  57. {
  58. phpdbg_breakfile_t *bp = (phpdbg_breakfile_t*) Z_PTR_P(data);
  59. efree((char*)bp->filename);
  60. efree(bp);
  61. } /* }}} */
  62. static void phpdbg_class_breaks_dtor(zval *data) /* {{{ */
  63. {
  64. phpdbg_breakmethod_t *bp = (phpdbg_breakmethod_t *) Z_PTR_P(data);
  65. efree((char*)bp->class_name);
  66. efree((char*)bp->func_name);
  67. efree(bp);
  68. } /* }}} */
  69. static void phpdbg_opline_class_breaks_dtor(zval *data) /* {{{ */
  70. {
  71. zend_hash_destroy(Z_ARRVAL_P(data));
  72. efree(Z_ARRVAL_P(data));
  73. } /* }}} */
  74. static void phpdbg_opline_breaks_dtor(zval *data) /* {{{ */
  75. {
  76. phpdbg_breakopline_t *bp = (phpdbg_breakopline_t *) Z_PTR_P(data);
  77. if (bp->class_name) {
  78. efree((char*)bp->class_name);
  79. }
  80. if (bp->func_name) {
  81. efree((char*)bp->func_name);
  82. }
  83. efree(bp);
  84. } /* }}} */
  85. PHPDBG_API void phpdbg_reset_breakpoints(void) /* {{{ */
  86. {
  87. HashTable *table;
  88. ZEND_HASH_FOREACH_PTR(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP], table) {
  89. phpdbg_breakbase_t *brake;
  90. ZEND_HASH_FOREACH_PTR(table, brake) {
  91. brake->hits = 0;
  92. } ZEND_HASH_FOREACH_END();
  93. } ZEND_HASH_FOREACH_END();
  94. } /* }}} */
  95. PHPDBG_API void phpdbg_export_breakpoints(FILE *handle) /* {{{ */
  96. {
  97. char *string;
  98. phpdbg_export_breakpoints_to_string(&string);
  99. fputs(string, handle);
  100. }
  101. /* }}} */
  102. PHPDBG_API void phpdbg_export_breakpoints_to_string(char **str) /* {{{ */
  103. {
  104. HashTable *table;
  105. zend_ulong id = 0L;
  106. *str = "";
  107. if (zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP])) {
  108. phpdbg_notice("exportbreakpoint", "count=\"%d\"", "Exporting %d breakpoints", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP]));
  109. /* this only looks like magic, it isn't */
  110. ZEND_HASH_FOREACH_NUM_KEY_PTR(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP], id, table) {
  111. phpdbg_breakbase_t *brake;
  112. ZEND_HASH_FOREACH_PTR(table, brake) {
  113. if (brake->id == id) {
  114. char *new_str = NULL;
  115. switch (brake->type) {
  116. case PHPDBG_BREAK_FILE: {
  117. zend_string *filename = php_addcslashes_str(((phpdbg_breakfile_t*)brake)->filename, strlen(((phpdbg_breakfile_t*)brake)->filename), "\\\"\n", 3);
  118. phpdbg_asprintf(&new_str,
  119. "%sbreak \"%s\":%lu\n", *str,
  120. ZSTR_VAL(filename),
  121. ((phpdbg_breakfile_t*)brake)->line);
  122. zend_string_release(filename);
  123. } break;
  124. case PHPDBG_BREAK_SYM: {
  125. phpdbg_asprintf(&new_str,
  126. "%sbreak %s\n", *str,
  127. ((phpdbg_breaksymbol_t*)brake)->symbol);
  128. } break;
  129. case PHPDBG_BREAK_METHOD: {
  130. phpdbg_asprintf(&new_str,
  131. "%sbreak %s::%s\n", *str,
  132. ((phpdbg_breakmethod_t*)brake)->class_name,
  133. ((phpdbg_breakmethod_t*)brake)->func_name);
  134. } break;
  135. case PHPDBG_BREAK_METHOD_OPLINE: {
  136. phpdbg_asprintf(&new_str,
  137. "%sbreak %s::%s#%llu\n", *str,
  138. ((phpdbg_breakopline_t*)brake)->class_name,
  139. ((phpdbg_breakopline_t*)brake)->func_name,
  140. ((phpdbg_breakopline_t*)brake)->opline_num);
  141. } break;
  142. case PHPDBG_BREAK_FUNCTION_OPLINE: {
  143. phpdbg_asprintf(&new_str,
  144. "%sbreak %s#%llu\n", *str,
  145. ((phpdbg_breakopline_t*)brake)->func_name,
  146. ((phpdbg_breakopline_t*)brake)->opline_num);
  147. } break;
  148. case PHPDBG_BREAK_FILE_OPLINE: {
  149. zend_string *filename = php_addcslashes_str(((phpdbg_breakopline_t*)brake)->class_name, strlen(((phpdbg_breakopline_t*)brake)->class_name), "\\\"\n", 3);
  150. phpdbg_asprintf(&new_str,
  151. "%sbreak \"%s\":#%llu\n", *str,
  152. ZSTR_VAL(filename),
  153. ((phpdbg_breakopline_t*)brake)->opline_num);
  154. zend_string_release(filename);
  155. } break;
  156. case PHPDBG_BREAK_OPCODE: {
  157. phpdbg_asprintf(&new_str,
  158. "%sbreak %s\n", *str,
  159. ((phpdbg_breakop_t*)brake)->name);
  160. } break;
  161. case PHPDBG_BREAK_COND: {
  162. phpdbg_breakcond_t *conditional = (phpdbg_breakcond_t*) brake;
  163. if (conditional->paramed) {
  164. switch (conditional->param.type) {
  165. case NUMERIC_FUNCTION_PARAM:
  166. phpdbg_asprintf(&new_str,
  167. "%sbreak at %s#%ld if %s\n",
  168. *str, conditional->param.str, conditional->param.num, conditional->code);
  169. break;
  170. case NUMERIC_METHOD_PARAM:
  171. phpdbg_asprintf(&new_str,
  172. "%sbreak at %s::%s#%ld if %s\n",
  173. *str, conditional->param.method.class, conditional->param.method.name, conditional->param.num, conditional->code);
  174. break;
  175. case ADDR_PARAM:
  176. phpdbg_asprintf(&new_str,
  177. "%sbreak at 0X%lx if %s\n",
  178. *str, conditional->param.addr, conditional->code);
  179. break;
  180. case STR_PARAM:
  181. phpdbg_asprintf(&new_str,
  182. "%sbreak at %s if %s\n", *str, conditional->param.str, conditional->code);
  183. break;
  184. case METHOD_PARAM:
  185. phpdbg_asprintf(&new_str,
  186. "%sbreak at %s::%s if %s\n", *str,
  187. conditional->param.method.class, conditional->param.method.name,
  188. conditional->code);
  189. break;
  190. case FILE_PARAM: {
  191. zend_string *filename = php_addcslashes_str(conditional->param.file.name, strlen(conditional->param.file.name), "\\\"\n", 3);
  192. phpdbg_asprintf(&new_str,
  193. "%sbreak at \"%s\":%lu if %s\n", *str,
  194. ZSTR_VAL(filename), conditional->param.file.line,
  195. conditional->code);
  196. zend_string_release(filename);
  197. } break;
  198. default: { /* do nothing */ } break;
  199. }
  200. } else {
  201. phpdbg_asprintf(&new_str, "%sbreak if %s\n", str, conditional->code);
  202. }
  203. } break;
  204. default: continue;
  205. }
  206. if ((*str)[0]) {
  207. efree(*str);
  208. }
  209. *str = new_str;
  210. }
  211. } ZEND_HASH_FOREACH_END();
  212. } ZEND_HASH_FOREACH_END();
  213. }
  214. if ((*str) && !(*str)[0]) {
  215. *str = NULL;
  216. }
  217. } /* }}} */
  218. PHPDBG_API void phpdbg_set_breakpoint_file(const char *path, size_t path_len, long line_num) /* {{{ */
  219. {
  220. php_stream_statbuf ssb;
  221. char realpath[MAXPATHLEN];
  222. const char *original_path = path;
  223. zend_bool pending = 0;
  224. zend_string *path_str;
  225. HashTable *broken, *file_breaks = &PHPDBG_G(bp)[PHPDBG_BREAK_FILE];
  226. phpdbg_breakfile_t new_break;
  227. if (!path_len) {
  228. if (VCWD_REALPATH(path, realpath)) {
  229. path = realpath;
  230. }
  231. }
  232. path_len = strlen(path);
  233. phpdbg_debug("file path: %s, resolved path: %s, was compiled: %d\n", original_path, path, zend_hash_str_exists(&PHPDBG_G(file_sources), path, path_len));
  234. if (!zend_hash_str_exists(&PHPDBG_G(file_sources), path, path_len)) {
  235. if (php_stream_stat_path(path, &ssb) == FAILURE) {
  236. if (original_path[0] == '/') {
  237. phpdbg_error("breakpoint", "type=\"nofile\" add=\"fail\" file=\"%s\"", "Cannot stat %s, it does not exist", original_path);
  238. return;
  239. }
  240. file_breaks = &PHPDBG_G(bp)[PHPDBG_BREAK_FILE_PENDING];
  241. path = original_path;
  242. path_len = strlen(path);
  243. pending = 1;
  244. } else if (!(ssb.sb.st_mode & (S_IFREG|S_IFLNK))) {
  245. phpdbg_error("breakpoint", "type=\"notregular\" add=\"fail\" file=\"%s\"", "Cannot set breakpoint in %s, it is not a regular file", path);
  246. return;
  247. } else {
  248. phpdbg_debug("File exists, but not compiled\n");
  249. }
  250. }
  251. path_str = zend_string_init(path, path_len, 0);
  252. if (!(broken = zend_hash_find_ptr(file_breaks, path_str))) {
  253. HashTable breaks;
  254. zend_hash_init(&breaks, 8, NULL, phpdbg_file_breaks_dtor, 0);
  255. broken = zend_hash_add_mem(file_breaks, path_str, &breaks, sizeof(HashTable));
  256. }
  257. if (!zend_hash_index_exists(broken, line_num)) {
  258. PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_FILE);
  259. new_break.filename = estrndup(path, path_len);
  260. new_break.line = line_num;
  261. zend_hash_index_update_mem(broken, line_num, &new_break, sizeof(phpdbg_breakfile_t));
  262. PHPDBG_BREAK_MAPPING(new_break.id, broken);
  263. if (pending) {
  264. zend_string *file;
  265. ZEND_HASH_FOREACH_STR_KEY(&PHPDBG_G(file_sources), file) {
  266. HashTable *fileht;
  267. phpdbg_debug("Compare against loaded %s\n", file);
  268. if (!(pending = ((fileht = phpdbg_resolve_pending_file_break_ex(ZSTR_VAL(file), ZSTR_LEN(file), path_str, broken)) == NULL))) {
  269. new_break = *(phpdbg_breakfile_t *) zend_hash_index_find_ptr(fileht, line_num);
  270. break;
  271. }
  272. } ZEND_HASH_FOREACH_END();
  273. }
  274. if (pending) {
  275. PHPDBG_G(flags) |= PHPDBG_HAS_PENDING_FILE_BP;
  276. phpdbg_notice("breakpoint", "add=\"success\" id=\"%d\" file=\"%s\" line=\"%ld\" pending=\"pending\"", "Pending breakpoint #%d added at %s:%ld", new_break.id, new_break.filename, new_break.line);
  277. } else {
  278. PHPDBG_G(flags) |= PHPDBG_HAS_FILE_BP;
  279. phpdbg_notice("breakpoint", "add=\"success\" id=\"%d\" file=\"%s\" line=\"%ld\"", "Breakpoint #%d added at %s:%ld", new_break.id, new_break.filename, new_break.line);
  280. }
  281. } else {
  282. phpdbg_error("breakpoint", "type=\"exists\" add=\"fail\" file=\"%s\" line=\"%ld\"", "Breakpoint at %s:%ld exists", path, line_num);
  283. }
  284. zend_string_release(path_str);
  285. } /* }}} */
  286. PHPDBG_API HashTable *phpdbg_resolve_pending_file_break_ex(const char *file, uint32_t filelen, zend_string *cur, HashTable *fileht) /* {{{ */
  287. {
  288. phpdbg_debug("file: %s, filelen: %u, cur: %s, curlen %u, pos: %c, memcmp: %d\n", file, filelen, ZSTR_VAL(cur), ZSTR_LEN(cur), filelen > ZSTR_LEN(cur) ? file[filelen - ZSTR_LEN(cur) - 1] : '?', filelen > ZSTR_LEN(cur) ? memcmp(file + filelen - ZSTR_LEN(cur), ZSTR_VAL(cur), ZSTR_LEN(cur)) : 0);
  289. #ifdef _WIN32
  290. # define WIN32_PATH_CHECK file[filelen - ZSTR_LEN(cur) - 1] == '\\'
  291. #else
  292. # define WIN32_PATH_CHECK 0
  293. #endif
  294. if (((ZSTR_LEN(cur) < filelen && (file[filelen - ZSTR_LEN(cur) - 1] == '/' || WIN32_PATH_CHECK)) || filelen == ZSTR_LEN(cur)) && !memcmp(file + filelen - ZSTR_LEN(cur), ZSTR_VAL(cur), ZSTR_LEN(cur))) {
  295. phpdbg_breakfile_t *brake, new_brake;
  296. HashTable *master;
  297. PHPDBG_G(flags) |= PHPDBG_HAS_FILE_BP;
  298. if (!(master = zend_hash_str_find_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE], file, filelen))) {
  299. HashTable new_ht;
  300. zend_hash_init(&new_ht, 8, NULL, phpdbg_file_breaks_dtor, 0);
  301. master = zend_hash_str_add_mem(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE], file, filelen, &new_ht, sizeof(HashTable));
  302. }
  303. ZEND_HASH_FOREACH_PTR(fileht, brake) {
  304. new_brake = *brake;
  305. new_brake.filename = estrndup(file, filelen);
  306. PHPDBG_BREAK_UNMAPPING(brake->id);
  307. if (zend_hash_index_add_mem(master, brake->line, &new_brake, sizeof(phpdbg_breakfile_t))) {
  308. PHPDBG_BREAK_MAPPING(brake->id, master);
  309. }
  310. } ZEND_HASH_FOREACH_END();
  311. zend_hash_del(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_PENDING], cur);
  312. if (!zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_PENDING])) {
  313. PHPDBG_G(flags) &= ~PHPDBG_HAS_PENDING_FILE_BP;
  314. }
  315. phpdbg_debug("compiled file: %s, cur bp file: %s\n", file, cur);
  316. return master;
  317. }
  318. return NULL;
  319. } /* }}} */
  320. PHPDBG_API void phpdbg_resolve_pending_file_break(const char *file) /* {{{ */
  321. {
  322. HashTable *fileht;
  323. uint32_t filelen = strlen(file);
  324. zend_string *cur;
  325. phpdbg_debug("was compiled: %s\n", file);
  326. ZEND_HASH_FOREACH_STR_KEY_PTR(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_PENDING], cur, fileht) {
  327. phpdbg_debug("check bp: %s\n", cur);
  328. phpdbg_resolve_pending_file_break_ex(file, filelen, cur, fileht);
  329. } ZEND_HASH_FOREACH_END();
  330. } /* }}} */
  331. PHPDBG_API void phpdbg_set_breakpoint_symbol(const char *name, size_t name_len) /* {{{ */
  332. {
  333. char *lcname;
  334. if (*name == '\\') {
  335. name++;
  336. name_len--;
  337. }
  338. lcname = zend_str_tolower_dup(name, name_len);
  339. if (!zend_hash_str_exists(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM], name, name_len)) {
  340. phpdbg_breaksymbol_t new_break;
  341. PHPDBG_G(flags) |= PHPDBG_HAS_SYM_BP;
  342. PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_SYM);
  343. new_break.symbol = estrndup(name, name_len);
  344. zend_hash_str_update_mem(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM], lcname, name_len, &new_break, sizeof(phpdbg_breaksymbol_t));
  345. phpdbg_notice("breakpoint", "add=\"success\" id=\"%d\" function=\"%s\"", "Breakpoint #%d added at %s", new_break.id, new_break.symbol);
  346. PHPDBG_BREAK_MAPPING(new_break.id, &PHPDBG_G(bp)[PHPDBG_BREAK_SYM]);
  347. } else {
  348. phpdbg_error("breakpoint", "type=\"exists\" add=\"fail\" function=\"%s\"", "Breakpoint exists at %s", name);
  349. }
  350. efree(lcname);
  351. } /* }}} */
  352. PHPDBG_API void phpdbg_set_breakpoint_method(const char *class_name, const char *func_name) /* {{{ */
  353. {
  354. HashTable class_breaks, *class_table;
  355. size_t class_len = strlen(class_name);
  356. size_t func_len = strlen(func_name);
  357. char *func_lcname, *class_lcname;
  358. if (*class_name == '\\') {
  359. class_name++;
  360. class_len--;
  361. }
  362. func_lcname = zend_str_tolower_dup(func_name, func_len);
  363. class_lcname = zend_str_tolower_dup(class_name, class_len);
  364. if (!(class_table = zend_hash_str_find_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD], class_lcname, class_len))) {
  365. zend_hash_init(&class_breaks, 8, NULL, phpdbg_class_breaks_dtor, 0);
  366. class_table = zend_hash_str_update_mem(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD], class_lcname, class_len, &class_breaks, sizeof(HashTable));
  367. }
  368. if (!zend_hash_str_exists(class_table, func_lcname, func_len)) {
  369. phpdbg_breakmethod_t new_break;
  370. PHPDBG_G(flags) |= PHPDBG_HAS_METHOD_BP;
  371. PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_METHOD);
  372. new_break.class_name = estrndup(class_name, class_len);
  373. new_break.class_len = class_len;
  374. new_break.func_name = estrndup(func_name, func_len);
  375. new_break.func_len = func_len;
  376. zend_hash_str_update_mem(class_table, func_lcname, func_len, &new_break, sizeof(phpdbg_breakmethod_t));
  377. phpdbg_notice("breakpoint", "add=\"success\" id=\"%d\" method=\"%s::%s\"", "Breakpoint #%d added at %s::%s", new_break.id, class_name, func_name);
  378. PHPDBG_BREAK_MAPPING(new_break.id, class_table);
  379. } else {
  380. phpdbg_error("breakpoint", "type=\"exists\" add=\"fail\" method=\"%s::%s\"", "Breakpoint exists at %s::%s", class_name, func_name);
  381. }
  382. efree(func_lcname);
  383. efree(class_lcname);
  384. } /* }}} */
  385. PHPDBG_API void phpdbg_set_breakpoint_opline(zend_ulong opline) /* {{{ */
  386. {
  387. if (!zend_hash_index_exists(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], opline)) {
  388. phpdbg_breakline_t new_break;
  389. PHPDBG_G(flags) |= PHPDBG_HAS_OPLINE_BP;
  390. PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_OPLINE);
  391. new_break.name = NULL;
  392. new_break.opline = opline;
  393. new_break.base = NULL;
  394. zend_hash_index_update_mem(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], opline, &new_break, sizeof(phpdbg_breakline_t));
  395. phpdbg_notice("breakpoint", "add=\"success\" id=\"%d\" opline=\"%#lx\"", "Breakpoint #%d added at %#lx", new_break.id, new_break.opline);
  396. PHPDBG_BREAK_MAPPING(new_break.id, &PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE]);
  397. } else {
  398. phpdbg_error("breakpoint", "type=\"exists\" add=\"fail\" opline=\"%#lx\"", "Breakpoint exists at %#lx", opline);
  399. }
  400. } /* }}} */
  401. PHPDBG_API int phpdbg_resolve_op_array_break(phpdbg_breakopline_t *brake, zend_op_array *op_array) /* {{{ */
  402. {
  403. phpdbg_breakline_t opline_break;
  404. if (op_array->last <= brake->opline_num) {
  405. if (brake->class_name == NULL) {
  406. phpdbg_error("breakpoint", "type=\"maxoplines\" add=\"fail\" maxoplinenum=\"%d\" function=\"%s\" usedoplinenum=\"%ld\"", "There are only %d oplines in function %s (breaking at opline %ld impossible)", op_array->last, brake->func_name, brake->opline_num);
  407. } else if (brake->func_name == NULL) {
  408. phpdbg_error("breakpoint", "type=\"maxoplines\" add=\"fail\" maxoplinenum=\"%d\" file=\"%s\" usedoplinenum=\"%ld\"", "There are only %d oplines in file %s (breaking at opline %ld impossible)", op_array->last, brake->class_name, brake->opline_num);
  409. } else {
  410. phpdbg_error("breakpoint", "type=\"maxoplines\" add=\"fail\" maxoplinenum=\"%d\" method=\"%s::%s\" usedoplinenum=\"%ld\"", "There are only %d oplines in method %s::%s (breaking at opline %ld impossible)", op_array->last, brake->class_name, brake->func_name, brake->opline_num);
  411. }
  412. return FAILURE;
  413. }
  414. opline_break.disabled = 0;
  415. opline_break.hits = 0;
  416. opline_break.id = brake->id;
  417. opline_break.opline = brake->opline = (zend_ulong)(op_array->opcodes + brake->opline_num);
  418. opline_break.name = NULL;
  419. opline_break.base = brake;
  420. if (op_array->scope) {
  421. opline_break.type = PHPDBG_BREAK_METHOD_OPLINE;
  422. } else if (op_array->function_name) {
  423. opline_break.type = PHPDBG_BREAK_FUNCTION_OPLINE;
  424. } else {
  425. opline_break.type = PHPDBG_BREAK_FILE_OPLINE;
  426. }
  427. PHPDBG_G(flags) |= PHPDBG_HAS_OPLINE_BP;
  428. zend_hash_index_update_mem(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], opline_break.opline, &opline_break, sizeof(phpdbg_breakline_t));
  429. return SUCCESS;
  430. } /* }}} */
  431. PHPDBG_API void phpdbg_resolve_op_array_breaks(zend_op_array *op_array) /* {{{ */
  432. {
  433. HashTable *func_table = &PHPDBG_G(bp)[PHPDBG_BREAK_FUNCTION_OPLINE];
  434. HashTable *oplines_table;
  435. phpdbg_breakopline_t *brake;
  436. if (op_array->scope != NULL && !(func_table = zend_hash_find_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD_OPLINE], op_array->scope->name))) {
  437. return;
  438. }
  439. if (op_array->function_name == NULL) {
  440. if (!(oplines_table = zend_hash_find_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_OPLINE], op_array->filename))) {
  441. return;
  442. }
  443. } else if (!op_array->function_name || !(oplines_table = zend_hash_find_ptr(func_table, op_array->function_name))) {
  444. return;
  445. }
  446. ZEND_HASH_FOREACH_PTR(oplines_table, brake) {
  447. if (phpdbg_resolve_op_array_break(brake, op_array) == SUCCESS) {
  448. phpdbg_breakline_t *opline_break;
  449. zend_hash_internal_pointer_end(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE]);
  450. opline_break = zend_hash_get_current_data_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE]);
  451. phpdbg_notice("breakpoint", "add=\"success\" id=\"%d\" symbol=\"%s\" num=\"%ld\" opline=\"%#lx\"", "Breakpoint #%d resolved at %s%s%s#%ld (opline %#lx)",
  452. opline_break->id,
  453. brake->class_name ? brake->class_name : "",
  454. brake->class_name && brake->func_name ? "::" : "",
  455. brake->func_name ? brake->func_name : "",
  456. brake->opline_num,
  457. opline_break->opline);
  458. }
  459. } ZEND_HASH_FOREACH_END();
  460. } /* }}} */
  461. PHPDBG_API int phpdbg_resolve_opline_break(phpdbg_breakopline_t *new_break) /* {{{ */
  462. {
  463. HashTable *func_table = EG(function_table);
  464. zend_function *func;
  465. if (new_break->func_name == NULL) {
  466. if (EG(current_execute_data) == NULL) {
  467. if (PHPDBG_G(ops) != NULL && !memcmp(PHPDBG_G(ops)->filename, new_break->class_name, new_break->class_len)) {
  468. if (phpdbg_resolve_op_array_break(new_break, PHPDBG_G(ops)) == SUCCESS) {
  469. return SUCCESS;
  470. } else {
  471. return 2;
  472. }
  473. }
  474. return FAILURE;
  475. } else {
  476. zend_execute_data *execute_data = EG(current_execute_data);
  477. do {
  478. if (ZEND_USER_CODE(execute_data->func->common.type)) {
  479. zend_op_array *op_array = &execute_data->func->op_array;
  480. if (op_array->function_name == NULL && op_array->scope == NULL && new_break->class_len == ZSTR_LEN(op_array->filename) && !memcmp(ZSTR_VAL(op_array->filename), new_break->class_name, new_break->class_len)) {
  481. if (phpdbg_resolve_op_array_break(new_break, op_array) == SUCCESS) {
  482. return SUCCESS;
  483. } else {
  484. return 2;
  485. }
  486. }
  487. }
  488. } while ((execute_data = execute_data->prev_execute_data) != NULL);
  489. return FAILURE;
  490. }
  491. }
  492. if (new_break->class_name != NULL) {
  493. zend_class_entry *ce;
  494. if (!(ce = zend_hash_str_find_ptr(EG(class_table), zend_str_tolower_dup(new_break->class_name, new_break->class_len), new_break->class_len))) {
  495. return FAILURE;
  496. }
  497. func_table = &ce->function_table;
  498. }
  499. if (!(func = zend_hash_str_find_ptr(func_table, zend_str_tolower_dup(new_break->func_name, new_break->func_len), new_break->func_len))) {
  500. if (new_break->class_name != NULL && new_break->func_name != NULL) {
  501. phpdbg_error("breakpoint", "type=\"nomethod\" method=\"%s::%s\"", "Method %s doesn't exist in class %s", new_break->func_name, new_break->class_name);
  502. return 2;
  503. }
  504. return FAILURE;
  505. }
  506. if (func->type != ZEND_USER_FUNCTION) {
  507. if (new_break->class_name == NULL) {
  508. phpdbg_error("breakpoint", "type=\"internalfunction\" function=\"%s\"", "%s is not a user defined function, no oplines exist", new_break->func_name);
  509. } else {
  510. phpdbg_error("breakpoint", "type=\"internalfunction\" method=\"%s::%s\"", "%s::%s is not a user defined method, no oplines exist", new_break->class_name, new_break->func_name);
  511. }
  512. return 2;
  513. }
  514. if (phpdbg_resolve_op_array_break(new_break, &func->op_array) == FAILURE) {
  515. return 2;
  516. }
  517. return SUCCESS;
  518. } /* }}} */
  519. /* TODO ... method/function oplines need to be normalized (leading backslash, lowercase) and file oplines need to be resolved properly */
  520. PHPDBG_API void phpdbg_set_breakpoint_method_opline(const char *class, const char *method, zend_ulong opline) /* {{{ */
  521. {
  522. phpdbg_breakopline_t new_break;
  523. HashTable class_breaks, *class_table;
  524. HashTable method_breaks, *method_table;
  525. PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_METHOD_OPLINE);
  526. new_break.func_len = strlen(method);
  527. new_break.func_name = estrndup(method, new_break.func_len);
  528. new_break.class_len = strlen(class);
  529. new_break.class_name = estrndup(class, new_break.class_len);
  530. new_break.opline_num = opline;
  531. new_break.opline = 0;
  532. switch (phpdbg_resolve_opline_break(&new_break)) {
  533. case FAILURE:
  534. phpdbg_notice("breakpoint", "pending=\"pending\" id=\"%d\" method=\"%::%s\" num=\"%ld\"", "Pending breakpoint #%d at %s::%s#%ld", new_break.id, new_break.class_name, new_break.func_name, opline);
  535. break;
  536. case SUCCESS:
  537. phpdbg_notice("breakpoint", "id=\"%d\" method=\"%::%s\" num=\"%ld\"", "Breakpoint #%d added at %s::%s#%ld", new_break.id, new_break.class_name, new_break.func_name, opline);
  538. break;
  539. case 2:
  540. return;
  541. }
  542. if (!(class_table = zend_hash_str_find_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD_OPLINE], new_break.class_name, new_break.class_len))) {
  543. zend_hash_init(&class_breaks, 8, NULL, phpdbg_opline_class_breaks_dtor, 0);
  544. class_table = zend_hash_str_update_mem(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD_OPLINE], new_break.class_name, new_break.class_len, &class_breaks, sizeof(HashTable));
  545. }
  546. if (!(method_table = zend_hash_str_find_ptr(class_table, new_break.func_name, new_break.func_len))) {
  547. zend_hash_init(&method_breaks, 8, NULL, phpdbg_opline_breaks_dtor, 0);
  548. method_table = zend_hash_str_update_mem(class_table, new_break.func_name, new_break.func_len, &method_breaks, sizeof(HashTable));
  549. }
  550. if (zend_hash_index_exists(method_table, opline)) {
  551. phpdbg_error("breakpoint", "type=\"exists\" method=\"%s\" num=\"%ld\"", "Breakpoint already exists for %s::%s#%ld", new_break.class_name, new_break.func_name, opline);
  552. efree((char*)new_break.func_name);
  553. efree((char*)new_break.class_name);
  554. PHPDBG_G(bp_count)--;
  555. return;
  556. }
  557. PHPDBG_G(flags) |= PHPDBG_HAS_METHOD_OPLINE_BP;
  558. PHPDBG_BREAK_MAPPING(new_break.id, method_table);
  559. zend_hash_index_update_mem(method_table, opline, &new_break, sizeof(phpdbg_breakopline_t));
  560. }
  561. /* }}} */
  562. PHPDBG_API void phpdbg_set_breakpoint_function_opline(const char *function, zend_ulong opline) /* {{{ */
  563. {
  564. phpdbg_breakopline_t new_break;
  565. HashTable func_breaks, *func_table;
  566. PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_FUNCTION_OPLINE);
  567. new_break.func_len = strlen(function);
  568. new_break.func_name = estrndup(function, new_break.func_len);
  569. new_break.class_len = 0;
  570. new_break.class_name = NULL;
  571. new_break.opline_num = opline;
  572. new_break.opline = 0;
  573. switch (phpdbg_resolve_opline_break(&new_break)) {
  574. case FAILURE:
  575. phpdbg_notice("breakpoint", "pending=\"pending\" id=\"%d\" function=\"%s\" num=\"%ld\"", "Pending breakpoint #%d at %s#%ld", new_break.id, new_break.func_name, opline);
  576. break;
  577. case SUCCESS:
  578. phpdbg_notice("breakpoint", "id=\"%d\" function=\"%s\" num=\"%ld\"", "Breakpoint #%d added at %s#%ld", new_break.id, new_break.func_name, opline);
  579. break;
  580. case 2:
  581. return;
  582. }
  583. if (!(func_table = zend_hash_str_find_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_FUNCTION_OPLINE], new_break.func_name, new_break.func_len))) {
  584. zend_hash_init(&func_breaks, 8, NULL, phpdbg_opline_breaks_dtor, 0);
  585. func_table = zend_hash_str_update_mem(&PHPDBG_G(bp)[PHPDBG_BREAK_FUNCTION_OPLINE], new_break.func_name, new_break.func_len, &func_breaks, sizeof(HashTable));
  586. }
  587. if (zend_hash_index_exists(func_table, opline)) {
  588. phpdbg_error("breakpoint", "type=\"exists\" function=\"%s\" num=\"%ld\"", "Breakpoint already exists for %s#%ld", new_break.func_name, opline);
  589. efree((char*)new_break.func_name);
  590. PHPDBG_G(bp_count)--;
  591. return;
  592. }
  593. PHPDBG_BREAK_MAPPING(new_break.id, func_table);
  594. PHPDBG_G(flags) |= PHPDBG_HAS_FUNCTION_OPLINE_BP;
  595. zend_hash_index_update_mem(func_table, opline, &new_break, sizeof(phpdbg_breakopline_t));
  596. }
  597. /* }}} */
  598. PHPDBG_API void phpdbg_set_breakpoint_file_opline(const char *file, zend_ulong opline) /* {{{ */
  599. {
  600. phpdbg_breakopline_t new_break;
  601. HashTable file_breaks, *file_table;
  602. PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_FILE_OPLINE);
  603. new_break.func_len = 0;
  604. new_break.func_name = NULL;
  605. new_break.class_len = strlen(file);
  606. new_break.class_name = estrndup(file, new_break.class_len);
  607. new_break.opline_num = opline;
  608. new_break.opline = 0;
  609. switch (phpdbg_resolve_opline_break(&new_break)) {
  610. case FAILURE:
  611. phpdbg_notice("breakpoint", "pending=\"pending\" id=\"%d\" file=\"%s\" num=\"%ld\"", "Pending breakpoint #%d at %s:%ld", new_break.id, new_break.class_name, opline);
  612. break;
  613. case SUCCESS:
  614. phpdbg_notice("breakpoint", "id=\"%d\" file=\"%s\" num=\"%ld\"", "Breakpoint #%d added at %s:%ld", new_break.id, new_break.class_name, opline);
  615. break;
  616. case 2:
  617. return;
  618. }
  619. if (!(file_table = zend_hash_str_find_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_OPLINE], new_break.class_name, new_break.class_len))) {
  620. zend_hash_init(&file_breaks, 8, NULL, phpdbg_opline_breaks_dtor, 0);
  621. file_table = zend_hash_str_update_mem(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_OPLINE], new_break.class_name, new_break.class_len, &file_breaks, sizeof(HashTable));
  622. }
  623. if (zend_hash_index_exists(file_table, opline)) {
  624. phpdbg_error("breakpoint", "type=\"exists\" file=\"%s\" num=\"%d\"", "Breakpoint already exists for %s:%ld", new_break.class_name, opline);
  625. efree((char*)new_break.class_name);
  626. PHPDBG_G(bp_count)--;
  627. return;
  628. }
  629. PHPDBG_BREAK_MAPPING(new_break.id, file_table);
  630. PHPDBG_G(flags) |= PHPDBG_HAS_FILE_OPLINE_BP;
  631. zend_hash_index_update_mem(file_table, opline, &new_break, sizeof(phpdbg_breakopline_t));
  632. }
  633. /* }}} */
  634. PHPDBG_API void phpdbg_set_breakpoint_opcode(const char *name, size_t name_len) /* {{{ */
  635. {
  636. phpdbg_breakop_t new_break;
  637. zend_ulong hash = zend_hash_func(name, name_len);
  638. if (zend_hash_index_exists(&PHPDBG_G(bp)[PHPDBG_BREAK_OPCODE], hash)) {
  639. phpdbg_error("breakpoint", "type=\"exists\" opcode=\"%s\"", "Breakpoint exists for %s", name);
  640. return;
  641. }
  642. PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_OPCODE);
  643. new_break.hash = hash;
  644. new_break.name = estrndup(name, name_len);
  645. zend_hash_index_update_mem(&PHPDBG_G(bp)[PHPDBG_BREAK_OPCODE], hash, &new_break, sizeof(phpdbg_breakop_t));
  646. PHPDBG_G(flags) |= PHPDBG_HAS_OPCODE_BP;
  647. phpdbg_notice("breakpoint", "id=\"%d\" opcode=\"%s\"", "Breakpoint #%d added at %s", new_break.id, name);
  648. PHPDBG_BREAK_MAPPING(new_break.id, &PHPDBG_G(bp)[PHPDBG_BREAK_OPCODE]);
  649. } /* }}} */
  650. PHPDBG_API void phpdbg_set_breakpoint_opline_ex(phpdbg_opline_ptr_t opline) /* {{{ */
  651. {
  652. if (!zend_hash_index_exists(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], (zend_ulong) opline)) {
  653. phpdbg_breakline_t new_break;
  654. PHPDBG_G(flags) |= PHPDBG_HAS_OPLINE_BP;
  655. PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_OPLINE);
  656. new_break.opline = (zend_ulong) opline;
  657. new_break.base = NULL;
  658. zend_hash_index_update_mem(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], (zend_ulong) opline, &new_break, sizeof(phpdbg_breakline_t));
  659. phpdbg_notice("breakpoint", "id=\"%d\" opline=\"%#lx\"", "Breakpoint #%d added at %#lx", new_break.id, new_break.opline);
  660. PHPDBG_BREAK_MAPPING(new_break.id, &PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE]);
  661. } else {
  662. phpdbg_error("breakpoint", "type=\"exists\" opline=\"%#lx\"", "Breakpoint exists for opline %#lx", (zend_ulong) opline);
  663. }
  664. } /* }}} */
  665. static inline void phpdbg_create_conditional_break(phpdbg_breakcond_t *brake, const phpdbg_param_t *param, const char *expr, size_t expr_len, zend_ulong hash) /* {{{ */
  666. {
  667. phpdbg_breakcond_t new_break;
  668. uint32_t cops = CG(compiler_options);
  669. zval pv;
  670. switch (param->type) {
  671. case STR_PARAM:
  672. case NUMERIC_FUNCTION_PARAM:
  673. case METHOD_PARAM:
  674. case NUMERIC_METHOD_PARAM:
  675. case FILE_PARAM:
  676. case ADDR_PARAM:
  677. /* do nothing */
  678. break;
  679. default:
  680. phpdbg_error("eval", "type=\"invalidparameter\"", "Invalid parameter type for conditional breakpoint");
  681. return;
  682. }
  683. PHPDBG_BREAK_INIT(new_break, PHPDBG_BREAK_COND);
  684. new_break.hash = hash;
  685. if (param) {
  686. new_break.paramed = 1;
  687. phpdbg_copy_param(
  688. param, &new_break.param);
  689. if (new_break.param.type == FILE_PARAM ||
  690. new_break.param.type == NUMERIC_FILE_PARAM) {
  691. char realpath[MAXPATHLEN];
  692. if (VCWD_REALPATH(new_break.param.file.name, realpath)) {
  693. efree(new_break.param.file.name);
  694. new_break.param.file.name = estrdup(realpath);
  695. } else {
  696. phpdbg_error("eval", "type=\"invalidparameter\"", "Invalid file for conditional break %s", new_break.param.file.name);
  697. phpdbg_clear_param(&new_break.param);
  698. return;
  699. }
  700. }
  701. } else {
  702. new_break.paramed = 0;
  703. }
  704. cops = CG(compiler_options);
  705. CG(compiler_options) = ZEND_COMPILE_DEFAULT_FOR_EVAL;
  706. new_break.code = estrndup(expr, expr_len);
  707. new_break.code_len = expr_len;
  708. Z_STR(pv) = zend_string_alloc(expr_len + sizeof("return ;") - 1, 0);
  709. memcpy(Z_STRVAL(pv), "return ", sizeof("return ") - 1);
  710. memcpy(Z_STRVAL(pv) + sizeof("return ") - 1, expr, expr_len);
  711. Z_STRVAL(pv)[Z_STRLEN(pv) - 1] = ';';
  712. Z_STRVAL(pv)[Z_STRLEN(pv)] = '\0';
  713. Z_TYPE_INFO(pv) = IS_STRING;
  714. new_break.ops = zend_compile_string(&pv, "Conditional Breakpoint Code");
  715. zval_ptr_dtor_str(&pv);
  716. if (new_break.ops) {
  717. brake = zend_hash_index_update_mem(&PHPDBG_G(bp)[PHPDBG_BREAK_COND], hash, &new_break, sizeof(phpdbg_breakcond_t));
  718. phpdbg_notice("breakpoint", "id=\"%d\" expression=\"%s\" ptr=\"%p\"", "Conditional breakpoint #%d added %s/%p", brake->id, brake->code, brake->ops);
  719. PHPDBG_G(flags) |= PHPDBG_HAS_COND_BP;
  720. PHPDBG_BREAK_MAPPING(new_break.id, &PHPDBG_G(bp)[PHPDBG_BREAK_COND]);
  721. } else {
  722. phpdbg_error("compile", "expression=\"%s\"", "Failed to compile code for expression %s", expr);
  723. efree((char*)new_break.code);
  724. PHPDBG_G(bp_count)--;
  725. }
  726. CG(compiler_options) = cops;
  727. } /* }}} */
  728. PHPDBG_API void phpdbg_set_breakpoint_expression(const char *expr, size_t expr_len) /* {{{ */
  729. {
  730. zend_ulong expr_hash = zend_inline_hash_func(expr, expr_len);
  731. phpdbg_breakcond_t new_break;
  732. if (!zend_hash_index_exists(&PHPDBG_G(bp)[PHPDBG_BREAK_COND], expr_hash)) {
  733. phpdbg_create_conditional_break(
  734. &new_break, NULL, expr, expr_len, expr_hash);
  735. } else {
  736. phpdbg_error("breakpoint", "type=\"exists\" expression=\"%s\"", "Conditional break %s exists", expr);
  737. }
  738. } /* }}} */
  739. PHPDBG_API void phpdbg_set_breakpoint_at(const phpdbg_param_t *param) /* {{{ */
  740. {
  741. phpdbg_breakcond_t new_break;
  742. phpdbg_param_t *condition;
  743. zend_ulong hash = 0L;
  744. if (param->next) {
  745. condition = param->next;
  746. hash = zend_inline_hash_func(condition->str, condition->len);
  747. if (!zend_hash_index_exists(&PHPDBG_G(bp)[PHPDBG_BREAK_COND], hash)) {
  748. phpdbg_create_conditional_break(&new_break, param, condition->str, condition->len, hash);
  749. } else {
  750. phpdbg_notice("breakpoint", "type=\"exists\" arg=\"%s\"", "Conditional break %s exists at the specified location", condition->str);
  751. }
  752. }
  753. } /* }}} */
  754. static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_file(zend_op_array *op_array) /* {{{ */
  755. {
  756. HashTable *breaks;
  757. phpdbg_breakbase_t *brake;
  758. #if 0
  759. phpdbg_debug("Op at: %.*s %d\n", ZSTR_LEN(op_array->filename), ZSTR_VAL(op_array->filename), (*EG(opline_ptr))->lineno);
  760. #endif
  761. /* NOTE: realpath resolution should have happened at compile time - no reason to do it here again */
  762. if (!(breaks = zend_hash_find_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE], op_array->filename))) {
  763. return NULL;
  764. }
  765. if (EG(current_execute_data) && (brake = zend_hash_index_find_ptr(breaks, EG(current_execute_data)->opline->lineno))) {
  766. return brake;
  767. }
  768. return NULL;
  769. } /* }}} */
  770. static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_symbol(zend_function *fbc) /* {{{ */
  771. {
  772. zend_op_array *ops;
  773. if (fbc->type != ZEND_USER_FUNCTION) {
  774. return NULL;
  775. }
  776. ops = (zend_op_array *) fbc;
  777. if (ops->scope) {
  778. /* find method breaks here */
  779. return phpdbg_find_breakpoint_method(ops);
  780. }
  781. if (ops->function_name) {
  782. phpdbg_breakbase_t *brake;
  783. zend_string *fname = zend_string_tolower(ops->function_name);
  784. brake = zend_hash_find_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM], fname);
  785. zend_string_release(fname);
  786. return brake;
  787. } else {
  788. return zend_hash_str_find_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM], ZEND_STRL("main"));
  789. }
  790. } /* }}} */
  791. static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_method(zend_op_array *ops) /* {{{ */
  792. {
  793. HashTable *class_table;
  794. phpdbg_breakbase_t *brake = NULL;
  795. zend_string *class_lcname = zend_string_tolower(ops->scope->name);
  796. if ((class_table = zend_hash_find_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD], class_lcname))) {
  797. zend_string *lcname = zend_string_tolower(ops->function_name);
  798. brake = zend_hash_find_ptr(class_table, lcname);
  799. zend_string_release(lcname);
  800. }
  801. zend_string_release(class_lcname);
  802. return brake;
  803. } /* }}} */
  804. static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_opline(phpdbg_opline_ptr_t opline) /* {{{ */
  805. {
  806. phpdbg_breakline_t *brake;
  807. if ((brake = zend_hash_index_find_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], (zend_ulong) opline)) && brake->base) {
  808. return (phpdbg_breakbase_t *)brake->base;
  809. }
  810. return (phpdbg_breakbase_t *) brake;
  811. } /* }}} */
  812. static inline phpdbg_breakbase_t *phpdbg_find_breakpoint_opcode(zend_uchar opcode) /* {{{ */
  813. {
  814. const char *opname = zend_get_opcode_name(opcode);
  815. if (!opname) {
  816. return NULL;
  817. }
  818. return zend_hash_index_find_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_OPCODE], zend_hash_func(opname, strlen(opname)));
  819. } /* }}} */
  820. static inline zend_bool phpdbg_find_breakpoint_param(phpdbg_param_t *param, zend_execute_data *execute_data) /* {{{ */
  821. {
  822. zend_function *function = execute_data->func;
  823. switch (param->type) {
  824. case NUMERIC_FUNCTION_PARAM:
  825. case STR_PARAM: {
  826. /* function breakpoint */
  827. if (function->type != ZEND_USER_FUNCTION) {
  828. return 0;
  829. }
  830. {
  831. const char *str = NULL;
  832. size_t len = 0L;
  833. zend_op_array *ops = (zend_op_array*)function;
  834. str = ops->function_name ? ZSTR_VAL(ops->function_name) : "main";
  835. len = ops->function_name ? ZSTR_LEN(ops->function_name) : strlen(str);
  836. if (len == param->len && memcmp(param->str, str, len) == SUCCESS) {
  837. return param->type == STR_PARAM || execute_data->opline - ops->opcodes == param->num;
  838. }
  839. }
  840. } break;
  841. case FILE_PARAM: {
  842. if (param->file.line == zend_get_executed_lineno()) {
  843. const char *str = zend_get_executed_filename();
  844. size_t lengths[2] = {strlen(param->file.name), strlen(str)};
  845. if (lengths[0] == lengths[1]) {
  846. return (memcmp(
  847. param->file.name, str, lengths[0]) == SUCCESS);
  848. }
  849. }
  850. } break;
  851. case NUMERIC_METHOD_PARAM:
  852. case METHOD_PARAM: {
  853. if (function->type != ZEND_USER_FUNCTION) {
  854. return 0;
  855. }
  856. {
  857. zend_op_array *ops = (zend_op_array*) function;
  858. if (ops->scope) {
  859. size_t lengths[2] = { strlen(param->method.class), ZSTR_LEN(ops->scope->name) };
  860. if (lengths[0] == lengths[1] && memcmp(param->method.class, ops->scope->name, lengths[0]) == SUCCESS) {
  861. lengths[0] = strlen(param->method.name);
  862. lengths[1] = ZSTR_LEN(ops->function_name);
  863. if (lengths[0] == lengths[1] && memcmp(param->method.name, ops->function_name, lengths[0]) == SUCCESS) {
  864. return param->type == METHOD_PARAM || (execute_data->opline - ops->opcodes) == param->num;
  865. }
  866. }
  867. }
  868. }
  869. } break;
  870. case ADDR_PARAM: {
  871. return ((zend_ulong)(phpdbg_opline_ptr_t)execute_data->opline == param->addr);
  872. } break;
  873. default: {
  874. /* do nothing */
  875. } break;
  876. }
  877. return 0;
  878. } /* }}} */
  879. static inline phpdbg_breakbase_t *phpdbg_find_conditional_breakpoint(zend_execute_data *execute_data) /* {{{ */
  880. {
  881. phpdbg_breakcond_t *bp;
  882. int breakpoint = FAILURE;
  883. ZEND_HASH_FOREACH_PTR(&PHPDBG_G(bp)[PHPDBG_BREAK_COND], bp) {
  884. zval retval;
  885. const zend_op *orig_opline = EG(current_execute_data)->opline;
  886. zend_function *orig_func = EG(current_execute_data)->func;
  887. zval *orig_retval = EG(current_execute_data)->return_value;
  888. if (((phpdbg_breakbase_t*)bp)->disabled) {
  889. continue;
  890. }
  891. if (bp->paramed) {
  892. if (!phpdbg_find_breakpoint_param(&bp->param, execute_data)) {
  893. continue;
  894. }
  895. }
  896. EG(no_extensions) = 1;
  897. zend_rebuild_symbol_table();
  898. zend_try {
  899. PHPDBG_G(flags) |= PHPDBG_IN_COND_BP;
  900. zend_execute(bp->ops, &retval);
  901. if (zend_is_true(&retval)) {
  902. breakpoint = SUCCESS;
  903. }
  904. } zend_end_try();
  905. EG(no_extensions) = 1;
  906. EG(current_execute_data)->opline = orig_opline;
  907. EG(current_execute_data)->func = orig_func;
  908. EG(current_execute_data)->return_value = orig_retval;
  909. PHPDBG_G(flags) &= ~PHPDBG_IN_COND_BP;
  910. if (breakpoint == SUCCESS) {
  911. break;
  912. }
  913. } ZEND_HASH_FOREACH_END();
  914. return (breakpoint == SUCCESS) ? ((phpdbg_breakbase_t *) bp) : NULL;
  915. } /* }}} */
  916. PHPDBG_API phpdbg_breakbase_t *phpdbg_find_breakpoint(zend_execute_data *execute_data) /* {{{ */
  917. {
  918. phpdbg_breakbase_t *base = NULL;
  919. if (!(PHPDBG_G(flags) & PHPDBG_IS_BP_ENABLED)) {
  920. return NULL;
  921. }
  922. /* conditions cannot be executed by eval()'d code */
  923. if (!(PHPDBG_G(flags) & PHPDBG_IN_EVAL) &&
  924. (PHPDBG_G(flags) & PHPDBG_HAS_COND_BP) &&
  925. (base = phpdbg_find_conditional_breakpoint(execute_data))) {
  926. goto result;
  927. }
  928. if ((PHPDBG_G(flags) & PHPDBG_HAS_FILE_BP) && (base = phpdbg_find_breakpoint_file(&execute_data->func->op_array))) {
  929. goto result;
  930. }
  931. if (PHPDBG_G(flags) & (PHPDBG_HAS_METHOD_BP|PHPDBG_HAS_SYM_BP)) {
  932. zend_op_array *op_array = &execute_data->func->op_array;
  933. /* check we are at the beginning of the stack, but after argument RECV */
  934. if (execute_data->opline == op_array->opcodes + op_array->num_args + !!(op_array->fn_flags & ZEND_ACC_VARIADIC)) {
  935. if ((base = phpdbg_find_breakpoint_symbol(execute_data->func))) {
  936. goto result;
  937. }
  938. }
  939. }
  940. if ((PHPDBG_G(flags) & PHPDBG_HAS_OPLINE_BP) && (base = phpdbg_find_breakpoint_opline((phpdbg_opline_ptr_t) execute_data->opline))) {
  941. goto result;
  942. }
  943. if ((PHPDBG_G(flags) & PHPDBG_HAS_OPCODE_BP) && (base = phpdbg_find_breakpoint_opcode(execute_data->opline->opcode))) {
  944. goto result;
  945. }
  946. return NULL;
  947. result:
  948. /* we return nothing for disable breakpoints */
  949. if (base->disabled) {
  950. return NULL;
  951. }
  952. return base;
  953. } /* }}} */
  954. PHPDBG_API void phpdbg_delete_breakpoint(zend_ulong num) /* {{{ */
  955. {
  956. HashTable *table;
  957. phpdbg_breakbase_t *brake;
  958. zend_string *strkey;
  959. zend_ulong numkey;
  960. if ((brake = phpdbg_find_breakbase_ex(num, &table, &numkey, &strkey))) {
  961. int type = brake->type;
  962. char *name = NULL;
  963. size_t name_len = 0L;
  964. switch (type) {
  965. case PHPDBG_BREAK_FILE:
  966. case PHPDBG_BREAK_METHOD:
  967. if (zend_hash_num_elements(table) == 1) {
  968. name = estrdup(brake->name);
  969. name_len = strlen(name);
  970. if (zend_hash_num_elements(&PHPDBG_G(bp)[type]) == 1) {
  971. PHPDBG_G(flags) &= ~(1<<(brake->type+1));
  972. }
  973. }
  974. break;
  975. default: {
  976. if (zend_hash_num_elements(table) == 1) {
  977. PHPDBG_G(flags) &= ~(1<<(brake->type+1));
  978. }
  979. }
  980. }
  981. switch (type) {
  982. case PHPDBG_BREAK_FILE_OPLINE:
  983. case PHPDBG_BREAK_FUNCTION_OPLINE:
  984. case PHPDBG_BREAK_METHOD_OPLINE:
  985. if (zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE]) == 1) {
  986. PHPDBG_G(flags) &= PHPDBG_HAS_OPLINE_BP;
  987. }
  988. zend_hash_index_del(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], ((phpdbg_breakopline_t *) brake)->opline);
  989. }
  990. if (strkey) {
  991. zend_hash_del(table, strkey);
  992. } else {
  993. zend_hash_index_del(table, numkey);
  994. }
  995. switch (type) {
  996. case PHPDBG_BREAK_FILE:
  997. case PHPDBG_BREAK_METHOD:
  998. if (name) {
  999. zend_hash_str_del(&PHPDBG_G(bp)[type], name, name_len);
  1000. efree(name);
  1001. }
  1002. break;
  1003. }
  1004. phpdbg_notice("breakpoint", "deleted=\"success\" id=\"%ld\"", "Deleted breakpoint #%ld", num);
  1005. PHPDBG_BREAK_UNMAPPING(num);
  1006. } else {
  1007. phpdbg_error("breakpoint", "type=\"nobreakpoint\" deleted=\"fail\" id=\"%ld\"", "Failed to find breakpoint #%ld", num);
  1008. }
  1009. } /* }}} */
  1010. PHPDBG_API void phpdbg_clear_breakpoints(void) /* {{{ */
  1011. {
  1012. zend_hash_clean(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE]);
  1013. zend_hash_clean(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_PENDING]);
  1014. zend_hash_clean(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM]);
  1015. zend_hash_clean(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE]);
  1016. zend_hash_clean(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD_OPLINE]);
  1017. zend_hash_clean(&PHPDBG_G(bp)[PHPDBG_BREAK_FUNCTION_OPLINE]);
  1018. zend_hash_clean(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_OPLINE]);
  1019. zend_hash_clean(&PHPDBG_G(bp)[PHPDBG_BREAK_OPCODE]);
  1020. zend_hash_clean(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD]);
  1021. zend_hash_clean(&PHPDBG_G(bp)[PHPDBG_BREAK_COND]);
  1022. zend_hash_clean(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP]);
  1023. PHPDBG_G(flags) &= ~PHPDBG_BP_MASK;
  1024. PHPDBG_G(bp_count) = 0;
  1025. } /* }}} */
  1026. PHPDBG_API void phpdbg_hit_breakpoint(phpdbg_breakbase_t *brake, zend_bool output) /* {{{ */
  1027. {
  1028. brake->hits++;
  1029. if (output) {
  1030. phpdbg_print_breakpoint(brake);
  1031. }
  1032. } /* }}} */
  1033. PHPDBG_API void phpdbg_print_breakpoint(phpdbg_breakbase_t *brake) /* {{{ */
  1034. {
  1035. if (!brake)
  1036. goto unknown;
  1037. switch (brake->type) {
  1038. case PHPDBG_BREAK_FILE: {
  1039. phpdbg_notice("breakpoint", "id=\"%d\" file=\"%s\" line=\"%ld\" hits=\"%lu\"", "Breakpoint #%d at %s:%ld, hits: %lu",
  1040. ((phpdbg_breakfile_t*)brake)->id,
  1041. ((phpdbg_breakfile_t*)brake)->filename,
  1042. ((phpdbg_breakfile_t*)brake)->line,
  1043. ((phpdbg_breakfile_t*)brake)->hits);
  1044. } break;
  1045. case PHPDBG_BREAK_SYM: {
  1046. phpdbg_notice("breakpoint", "id=\"%d\" function=\"%s\" file=\"%s\" line=\"%ld\" hits=\"%lu\"", "Breakpoint #%d in %s() at %s:%u, hits: %lu",
  1047. ((phpdbg_breaksymbol_t*)brake)->id,
  1048. ((phpdbg_breaksymbol_t*)brake)->symbol,
  1049. zend_get_executed_filename(),
  1050. zend_get_executed_lineno(),
  1051. ((phpdbg_breakfile_t*)brake)->hits);
  1052. } break;
  1053. case PHPDBG_BREAK_OPLINE: {
  1054. phpdbg_notice("breakpoint", "id=\"%d\" opline=\"%#lx\" file=\"%s\" line=\"%ld\" hits=\"%lu\"", "Breakpoint #%d in %#lx at %s:%u, hits: %lu",
  1055. ((phpdbg_breakline_t*)brake)->id,
  1056. ((phpdbg_breakline_t*)brake)->opline,
  1057. zend_get_executed_filename(),
  1058. zend_get_executed_lineno(),
  1059. ((phpdbg_breakline_t*)brake)->hits);
  1060. } break;
  1061. case PHPDBG_BREAK_METHOD_OPLINE: {
  1062. phpdbg_notice("breakpoint", "id=\"%d\" method=\"%s::%s\" num=\"%lu\" file=\"%s\" line=\"%ld\" hits=\"%lu\"", "Breakpoint #%d in %s::%s()#%lu at %s:%u, hits: %lu",
  1063. ((phpdbg_breakopline_t*)brake)->id,
  1064. ((phpdbg_breakopline_t*)brake)->class_name,
  1065. ((phpdbg_breakopline_t*)brake)->func_name,
  1066. ((phpdbg_breakopline_t*)brake)->opline_num,
  1067. zend_get_executed_filename(),
  1068. zend_get_executed_lineno(),
  1069. ((phpdbg_breakopline_t*)brake)->hits);
  1070. } break;
  1071. case PHPDBG_BREAK_FUNCTION_OPLINE: {
  1072. phpdbg_notice("breakpoint", "id=\"%d\" num=\"%lu\" function=\"%s\" file=\"%s\" line=\"%ld\" hits=\"%lu\"", "Breakpoint #%d in %s()#%lu at %s:%u, hits: %lu",
  1073. ((phpdbg_breakopline_t*)brake)->id,
  1074. ((phpdbg_breakopline_t*)brake)->func_name,
  1075. ((phpdbg_breakopline_t*)brake)->opline_num,
  1076. zend_get_executed_filename(),
  1077. zend_get_executed_lineno(),
  1078. ((phpdbg_breakopline_t*)brake)->hits);
  1079. } break;
  1080. case PHPDBG_BREAK_FILE_OPLINE: {
  1081. phpdbg_notice("breakpoint", "id=\"%d\" num=\"%lu\" file=\"%s\" line=\"%ld\" hits=\"%lu\"", "Breakpoint #%d in #%lu at %s:%u, hits: %lu",
  1082. ((phpdbg_breakopline_t*)brake)->id,
  1083. ((phpdbg_breakopline_t*)brake)->opline_num,
  1084. zend_get_executed_filename(),
  1085. zend_get_executed_lineno(),
  1086. ((phpdbg_breakopline_t*)brake)->hits);
  1087. } break;
  1088. case PHPDBG_BREAK_OPCODE: {
  1089. phpdbg_notice("breakpoint", "id=\"%d\" opcode=\"%s\" file=\"%s\" line=\"%ld\" hits=\"%lu\"", "Breakpoint #%d in %s at %s:%u, hits: %lu",
  1090. ((phpdbg_breakop_t*)brake)->id,
  1091. ((phpdbg_breakop_t*)brake)->name,
  1092. zend_get_executed_filename(),
  1093. zend_get_executed_lineno(),
  1094. ((phpdbg_breakop_t*)brake)->hits);
  1095. } break;
  1096. case PHPDBG_BREAK_METHOD: {
  1097. phpdbg_notice("breakpoint", "id=\"%d\" method=\"%s::%s\" file=\"%s\" line=\"%ld\" hits=\"%lu\"", "Breakpoint #%d in %s::%s() at %s:%u, hits: %lu",
  1098. ((phpdbg_breakmethod_t*)brake)->id,
  1099. ((phpdbg_breakmethod_t*)brake)->class_name,
  1100. ((phpdbg_breakmethod_t*)brake)->func_name,
  1101. zend_get_executed_filename(),
  1102. zend_get_executed_lineno(),
  1103. ((phpdbg_breakmethod_t*)brake)->hits);
  1104. } break;
  1105. case PHPDBG_BREAK_COND: {
  1106. if (((phpdbg_breakcond_t*)brake)->paramed) {
  1107. char *param;
  1108. phpdbg_notice("breakpoint", "id=\"%d\" location=\"%s\" eval=\"%s\" file=\"%s\" line=\"%ld\" hits=\"%lu\"", "Conditional breakpoint #%d: at %s if %s at %s:%u, hits: %lu",
  1109. ((phpdbg_breakcond_t*)brake)->id,
  1110. phpdbg_param_tostring(&((phpdbg_breakcond_t*)brake)->param, &param),
  1111. ((phpdbg_breakcond_t*)brake)->code,
  1112. zend_get_executed_filename(),
  1113. zend_get_executed_lineno(),
  1114. ((phpdbg_breakcond_t*)brake)->hits);
  1115. if (param)
  1116. free(param);
  1117. } else {
  1118. phpdbg_notice("breakpoint", "id=\"%d\" eval=\"%s\" file=\"%s\" line=\"%ld\" hits=\"%lu\"", "Conditional breakpoint #%d: on %s == true at %s:%u, hits: %lu",
  1119. ((phpdbg_breakcond_t*)brake)->id,
  1120. ((phpdbg_breakcond_t*)brake)->code,
  1121. zend_get_executed_filename(),
  1122. zend_get_executed_lineno(),
  1123. ((phpdbg_breakcond_t*)brake)->hits);
  1124. }
  1125. } break;
  1126. default: {
  1127. unknown:
  1128. phpdbg_notice("breakpoint", "id=\"\" file=\"%s\" line=\"%ld\" hits=\"%lu\"", "Unknown breakpoint at %s:%u",
  1129. zend_get_executed_filename(),
  1130. zend_get_executed_lineno());
  1131. }
  1132. }
  1133. } /* }}} */
  1134. PHPDBG_API void phpdbg_enable_breakpoint(zend_ulong id) /* {{{ */
  1135. {
  1136. phpdbg_breakbase_t *brake = phpdbg_find_breakbase(id);
  1137. if (brake) {
  1138. brake->disabled = 0;
  1139. }
  1140. } /* }}} */
  1141. PHPDBG_API void phpdbg_disable_breakpoint(zend_ulong id) /* {{{ */
  1142. {
  1143. phpdbg_breakbase_t *brake = phpdbg_find_breakbase(id);
  1144. if (brake) {
  1145. brake->disabled = 1;
  1146. }
  1147. } /* }}} */
  1148. PHPDBG_API void phpdbg_enable_breakpoints(void) /* {{{ */
  1149. {
  1150. PHPDBG_G(flags) |= PHPDBG_IS_BP_ENABLED;
  1151. } /* }}} */
  1152. PHPDBG_API void phpdbg_disable_breakpoints(void) { /* {{{ */
  1153. PHPDBG_G(flags) &= ~PHPDBG_IS_BP_ENABLED;
  1154. } /* }}} */
  1155. PHPDBG_API phpdbg_breakbase_t *phpdbg_find_breakbase(zend_ulong id) /* {{{ */
  1156. {
  1157. HashTable *table;
  1158. zend_string *strkey;
  1159. zend_ulong numkey;
  1160. return phpdbg_find_breakbase_ex(id, &table, &numkey, &strkey);
  1161. } /* }}} */
  1162. PHPDBG_API phpdbg_breakbase_t *phpdbg_find_breakbase_ex(zend_ulong id, HashTable **table, zend_ulong *numkey, zend_string **strkey) /* {{{ */
  1163. {
  1164. if ((*table = zend_hash_index_find_ptr(&PHPDBG_G(bp)[PHPDBG_BREAK_MAP], id))) {
  1165. phpdbg_breakbase_t *brake;
  1166. ZEND_HASH_FOREACH_KEY_PTR(*table, *numkey, *strkey, brake) {
  1167. if (brake->id == id) {
  1168. return brake;
  1169. }
  1170. } ZEND_HASH_FOREACH_END();
  1171. }
  1172. return NULL;
  1173. } /* }}} */
  1174. PHPDBG_API void phpdbg_print_breakpoints(zend_ulong type) /* {{{ */
  1175. {
  1176. phpdbg_xml("<breakpoints %r>");
  1177. switch (type) {
  1178. case PHPDBG_BREAK_SYM: if ((PHPDBG_G(flags) & PHPDBG_HAS_SYM_BP)) {
  1179. phpdbg_breaksymbol_t *brake;
  1180. phpdbg_out(SEPARATE "\n");
  1181. phpdbg_out("Function Breakpoints:\n");
  1182. ZEND_HASH_FOREACH_PTR(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM], brake) {
  1183. phpdbg_writeln("function", "id=\"%d\" name=\"%s\" disabled=\"%s\"", "#%d\t\t%s%s",
  1184. brake->id, brake->symbol,
  1185. ((phpdbg_breakbase_t *) brake)->disabled ? " [disabled]" : "");
  1186. } ZEND_HASH_FOREACH_END();
  1187. } break;
  1188. case PHPDBG_BREAK_METHOD: if ((PHPDBG_G(flags) & PHPDBG_HAS_METHOD_BP)) {
  1189. HashTable *class_table;
  1190. phpdbg_out(SEPARATE "\n");
  1191. phpdbg_out("Method Breakpoints:\n");
  1192. ZEND_HASH_FOREACH_PTR(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD], class_table) {
  1193. phpdbg_breakmethod_t *brake;
  1194. ZEND_HASH_FOREACH_PTR(class_table, brake) {
  1195. phpdbg_writeln("method", "id=\"%d\" name=\"%s::%s\" disabled=\"%s\"", "#%d\t\t%s::%s%s",
  1196. brake->id, brake->class_name, brake->func_name,
  1197. ((phpdbg_breakbase_t *) brake)->disabled ? " [disabled]" : "");
  1198. } ZEND_HASH_FOREACH_END();
  1199. } ZEND_HASH_FOREACH_END();
  1200. } break;
  1201. case PHPDBG_BREAK_FILE: if ((PHPDBG_G(flags) & PHPDBG_HAS_FILE_BP)) {
  1202. HashTable *points;
  1203. phpdbg_out(SEPARATE "\n");
  1204. phpdbg_out("File Breakpoints:\n");
  1205. ZEND_HASH_FOREACH_PTR(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE], points) {
  1206. phpdbg_breakfile_t *brake;
  1207. ZEND_HASH_FOREACH_PTR(points, brake) {
  1208. phpdbg_writeln("file", "id=\"%d\" name=\"%s\" line=\"%lu\" disabled=\"%s\"", "#%d\t\t%s:%lu%s",
  1209. brake->id, brake->filename, brake->line,
  1210. ((phpdbg_breakbase_t *) brake)->disabled ? " [disabled]" : "");
  1211. } ZEND_HASH_FOREACH_END();
  1212. } ZEND_HASH_FOREACH_END();
  1213. } if ((PHPDBG_G(flags) & PHPDBG_HAS_PENDING_FILE_BP)) {
  1214. HashTable *points;
  1215. phpdbg_out(SEPARATE "\n");
  1216. phpdbg_out("Pending File Breakpoints:\n");
  1217. ZEND_HASH_FOREACH_PTR(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_PENDING], points) {
  1218. phpdbg_breakfile_t *brake;
  1219. ZEND_HASH_FOREACH_PTR(points, brake) {
  1220. phpdbg_writeln("file", "id=\"%d\" name=\"%s\" line=\"%lu\" disabled=\"%s\" pending=\"pending\"", "#%d\t\t%s:%lu%s",
  1221. brake->id, brake->filename, brake->line,
  1222. ((phpdbg_breakbase_t *) brake)->disabled ? " [disabled]" : "");
  1223. } ZEND_HASH_FOREACH_END();
  1224. } ZEND_HASH_FOREACH_END();
  1225. } break;
  1226. case PHPDBG_BREAK_OPLINE: if ((PHPDBG_G(flags) & PHPDBG_HAS_OPLINE_BP)) {
  1227. phpdbg_breakline_t *brake;
  1228. phpdbg_out(SEPARATE "\n");
  1229. phpdbg_out("Opline Breakpoints:\n");
  1230. ZEND_HASH_FOREACH_PTR(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE], brake) {
  1231. const char *type;
  1232. switch (brake->type) {
  1233. case PHPDBG_BREAK_METHOD_OPLINE:
  1234. type = "method";
  1235. goto print_opline;
  1236. case PHPDBG_BREAK_FUNCTION_OPLINE:
  1237. type = "function";
  1238. goto print_opline;
  1239. case PHPDBG_BREAK_FILE_OPLINE:
  1240. type = "method";
  1241. print_opline: {
  1242. if (brake->type == PHPDBG_BREAK_METHOD_OPLINE) {
  1243. type = "method";
  1244. } else if (brake->type == PHPDBG_BREAK_FUNCTION_OPLINE) {
  1245. type = "function";
  1246. } else if (brake->type == PHPDBG_BREAK_FILE_OPLINE) {
  1247. type = "file";
  1248. }
  1249. phpdbg_writeln("opline", "id=\"%d\" num=\"%#lx\" type=\"%s\" disabled=\"%s\"", "#%d\t\t%#lx\t\t(%s breakpoint)%s",
  1250. brake->id, brake->opline, type,
  1251. ((phpdbg_breakbase_t *) brake)->disabled ? " [disabled]" : "");
  1252. } break;
  1253. default:
  1254. phpdbg_writeln("opline", "id=\"%d\" num=\"%#lx\" disabled=\"%s\"", "#%d\t\t%#lx%s",
  1255. brake->id, brake->opline,
  1256. ((phpdbg_breakbase_t *) brake)->disabled ? " [disabled]" : "");
  1257. break;
  1258. }
  1259. } ZEND_HASH_FOREACH_END();
  1260. } break;
  1261. case PHPDBG_BREAK_METHOD_OPLINE: if ((PHPDBG_G(flags) & PHPDBG_HAS_METHOD_OPLINE_BP)) {
  1262. HashTable *class_table, *method_table;
  1263. phpdbg_out(SEPARATE "\n");
  1264. phpdbg_out("Method opline Breakpoints:\n");
  1265. ZEND_HASH_FOREACH_PTR(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD_OPLINE], class_table) {
  1266. ZEND_HASH_FOREACH_PTR(class_table, method_table) {
  1267. phpdbg_breakopline_t *brake;
  1268. ZEND_HASH_FOREACH_PTR(method_table, brake) {
  1269. phpdbg_writeln("methodopline", "id=\"%d\" name=\"%s::%s\" num=\"%ld\" disabled=\"%s\"", "#%d\t\t%s::%s opline %ld%s",
  1270. brake->id, brake->class_name, brake->func_name, brake->opline_num,
  1271. ((phpdbg_breakbase_t *) brake)->disabled ? " [disabled]" : "");
  1272. } ZEND_HASH_FOREACH_END();
  1273. } ZEND_HASH_FOREACH_END();
  1274. } ZEND_HASH_FOREACH_END();
  1275. } break;
  1276. case PHPDBG_BREAK_FUNCTION_OPLINE: if ((PHPDBG_G(flags) & PHPDBG_HAS_FUNCTION_OPLINE_BP)) {
  1277. HashTable *function_table;
  1278. phpdbg_out(SEPARATE "\n");
  1279. phpdbg_out("Function opline Breakpoints:\n");
  1280. ZEND_HASH_FOREACH_PTR(&PHPDBG_G(bp)[PHPDBG_BREAK_FUNCTION_OPLINE], function_table) {
  1281. phpdbg_breakopline_t *brake;
  1282. ZEND_HASH_FOREACH_PTR(function_table, brake) {
  1283. phpdbg_writeln("functionopline", "id=\"%d\" name=\"%s\" num=\"%ld\" disabled=\"%s\"", "#%d\t\t%s opline %ld%s",
  1284. brake->id, brake->func_name, brake->opline_num,
  1285. ((phpdbg_breakbase_t *) brake)->disabled ? " [disabled]" : "");
  1286. } ZEND_HASH_FOREACH_END();
  1287. } ZEND_HASH_FOREACH_END();
  1288. } break;
  1289. case PHPDBG_BREAK_FILE_OPLINE: if ((PHPDBG_G(flags) & PHPDBG_HAS_FILE_OPLINE_BP)) {
  1290. HashTable *file_table;
  1291. phpdbg_out(SEPARATE "\n");
  1292. phpdbg_out("File opline Breakpoints:\n");
  1293. ZEND_HASH_FOREACH_PTR(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_OPLINE], file_table) {
  1294. phpdbg_breakopline_t *brake;
  1295. ZEND_HASH_FOREACH_PTR(file_table, brake) {
  1296. phpdbg_writeln("fileopline", "id=\"%d\" name=\"%s\" num=\"%ld\" disabled=\"%s\"", "#%d\t\t%s opline %ld%s",
  1297. brake->id, brake->class_name, brake->opline_num,
  1298. ((phpdbg_breakbase_t *) brake)->disabled ? " [disabled]" : "");
  1299. } ZEND_HASH_FOREACH_END();
  1300. } ZEND_HASH_FOREACH_END();
  1301. } break;
  1302. case PHPDBG_BREAK_COND: if ((PHPDBG_G(flags) & PHPDBG_HAS_COND_BP)) {
  1303. phpdbg_breakcond_t *brake;
  1304. phpdbg_out(SEPARATE "\n");
  1305. phpdbg_out("Conditional Breakpoints:\n");
  1306. ZEND_HASH_FOREACH_PTR(&PHPDBG_G(bp)[PHPDBG_BREAK_COND], brake) {
  1307. if (brake->paramed) {
  1308. switch (brake->param.type) {
  1309. case STR_PARAM:
  1310. phpdbg_writeln("evalfunction", "id=\"%d\" name=\"%s\" eval=\"%s\" disabled=\"%s\"", "#%d\t\tat %s if %s%s",
  1311. brake->id, brake->param.str, brake->code,
  1312. ((phpdbg_breakbase_t *) brake)->disabled ? " [disabled]" : "");
  1313. break;
  1314. case NUMERIC_FUNCTION_PARAM:
  1315. phpdbg_writeln("evalfunctionopline", "id=\"%d\" name=\"%s\" num=\"%ld\" eval=\"%s\" disabled=\"%s\"", "#%d\t\tat %s#%ld if %s%s",
  1316. brake->id, brake->param.str, brake->param.num, brake->code,
  1317. ((phpdbg_breakbase_t *) brake)->disabled ? " [disabled]" : "");
  1318. break;
  1319. case METHOD_PARAM:
  1320. phpdbg_writeln("evalmethod", "id=\"%d\" name=\"%s::%s\" eval=\"%s\" disabled=\"%s\"", "#%d\t\tat %s::%s if %s%s",
  1321. brake->id, brake->param.method.class, brake->param.method.name, brake->code,
  1322. ((phpdbg_breakbase_t*)brake)->disabled ? " [disabled]" : "");
  1323. break;
  1324. case NUMERIC_METHOD_PARAM:
  1325. phpdbg_writeln("evalmethodopline", "id=\"%d\" name=\"%s::%s\" num=\"%d\" eval=\"%s\" disabled=\"%s\"", "#%d\t\tat %s::%s#%ld if %s%s",
  1326. brake->id, brake->param.method.class, brake->param.method.name, brake->param.num, brake->code,
  1327. ((phpdbg_breakbase_t *) brake)->disabled ? " [disabled]" : "");
  1328. break;
  1329. case FILE_PARAM:
  1330. phpdbg_writeln("evalfile", "id=\"%d\" name=\"%s\" line=\"%d\" eval=\"%s\" disabled=\"%s\"", "#%d\t\tat %s:%lu if %s%s",
  1331. brake->id, brake->param.file.name, brake->param.file.line, brake->code,
  1332. ((phpdbg_breakbase_t *) brake)->disabled ? " [disabled]" : "");
  1333. break;
  1334. case ADDR_PARAM:
  1335. phpdbg_writeln("evalopline", "id=\"%d\" opline=\"%#lx\" eval=\"%s\" disabled=\"%s\"", "#%d\t\tat #%lx if %s%s",
  1336. brake->id, brake->param.addr, brake->code,
  1337. ((phpdbg_breakbase_t *) brake)->disabled ? " [disabled]" : "");
  1338. break;
  1339. default:
  1340. phpdbg_error("eval", "type=\"invalidparameter\"", "Invalid parameter type for conditional breakpoint");
  1341. return;
  1342. }
  1343. } else {
  1344. phpdbg_writeln("eval", "id=\"%d\" eval=\"%s\" disabled=\"%s\"", "#%d\t\tif %s%s",
  1345. brake->id, brake->code,
  1346. ((phpdbg_breakbase_t *) brake)->disabled ? " [disabled]" : "");
  1347. }
  1348. } ZEND_HASH_FOREACH_END();
  1349. } break;
  1350. case PHPDBG_BREAK_OPCODE: if (PHPDBG_G(flags) & PHPDBG_HAS_OPCODE_BP) {
  1351. phpdbg_breakop_t *brake;
  1352. phpdbg_out(SEPARATE "\n");
  1353. phpdbg_out("Opcode Breakpoints:\n");
  1354. ZEND_HASH_FOREACH_PTR(&PHPDBG_G(bp)[PHPDBG_BREAK_OPCODE], brake) {
  1355. phpdbg_writeln("opcode", "id=\"%d\" name=\"%s\" disabled=\"%s\"", "#%d\t\t%s%s",
  1356. brake->id, brake->name,
  1357. ((phpdbg_breakbase_t *) brake)->disabled ? " [disabled]" : "");
  1358. } ZEND_HASH_FOREACH_END();
  1359. } break;
  1360. }
  1361. phpdbg_xml("</breakpoints>");
  1362. } /* }}} */