phpdbg_prompt.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Felipe Pena <felipe@php.net> |
  14. | Authors: Joe Watkins <joe.watkins@live.co.uk> |
  15. | Authors: Bob Weinand <bwoebi@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include "zend.h"
  21. #include "zend_compile.h"
  22. #include "zend_exceptions.h"
  23. #include "zend_vm.h"
  24. #include "zend_generators.h"
  25. #include "zend_interfaces.h"
  26. #include "zend_smart_str.h"
  27. #include "phpdbg.h"
  28. #include "phpdbg_io.h"
  29. #include "phpdbg_help.h"
  30. #include "phpdbg_print.h"
  31. #include "phpdbg_info.h"
  32. #include "phpdbg_break.h"
  33. #include "phpdbg_list.h"
  34. #include "phpdbg_utils.h"
  35. #include "phpdbg_prompt.h"
  36. #include "phpdbg_cmd.h"
  37. #include "phpdbg_set.h"
  38. #include "phpdbg_frame.h"
  39. #include "phpdbg_lexer.h"
  40. #include "phpdbg_parser.h"
  41. #if ZEND_VM_KIND != ZEND_VM_KIND_CALL && ZEND_VM_KIND != ZEND_VM_KIND_HYBRID
  42. #error "phpdbg can only be built with CALL zend vm kind"
  43. #endif
  44. ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
  45. extern int phpdbg_startup_run;
  46. #ifdef HAVE_LIBDL
  47. #ifdef PHP_WIN32
  48. #include "win32/param.h"
  49. #include "win32/winutil.h"
  50. #define GET_DL_ERROR() php_win_err()
  51. #else
  52. #include <sys/param.h>
  53. #define GET_DL_ERROR() DL_ERROR()
  54. #endif
  55. #endif
  56. /* {{{ command declarations */
  57. const phpdbg_command_t phpdbg_prompt_commands[] = {
  58. PHPDBG_COMMAND_D(exec, "set execution context", 'e', NULL, "s", 0),
  59. PHPDBG_COMMAND_D(stdin, "read script from stdin", 0 , NULL, "s", 0),
  60. PHPDBG_COMMAND_D(step, "step through execution", 's', NULL, 0, PHPDBG_ASYNC_SAFE),
  61. PHPDBG_COMMAND_D(continue, "continue execution", 'c', NULL, 0, PHPDBG_ASYNC_SAFE),
  62. PHPDBG_COMMAND_D(run, "attempt execution", 'r', NULL, "|s", 0),
  63. PHPDBG_COMMAND_D(ev, "evaluate some code", 0 , NULL, "i", PHPDBG_ASYNC_SAFE), /* restricted ASYNC_SAFE */
  64. PHPDBG_COMMAND_D(until, "continue past the current line", 'u', NULL, 0, 0),
  65. PHPDBG_COMMAND_D(finish, "continue past the end of the stack", 'F', NULL, 0, 0),
  66. PHPDBG_COMMAND_D(leave, "continue until the end of the stack", 'L', NULL, 0, 0),
  67. PHPDBG_COMMAND_D(generator, "inspect or switch to a generator", 'g', NULL, "|n", 0),
  68. PHPDBG_COMMAND_D(print, "print something", 'p', phpdbg_print_commands, "|*c", 0),
  69. PHPDBG_COMMAND_D(break, "set breakpoint", 'b', phpdbg_break_commands, "|*c", 0),
  70. PHPDBG_COMMAND_D(back, "show trace", 't', NULL, "|n", PHPDBG_ASYNC_SAFE),
  71. PHPDBG_COMMAND_D(frame, "switch to a frame", 'f', NULL, "|n", PHPDBG_ASYNC_SAFE),
  72. PHPDBG_COMMAND_D(list, "lists some code", 'l', phpdbg_list_commands, "*", PHPDBG_ASYNC_SAFE),
  73. PHPDBG_COMMAND_D(info, "displays some information", 'i', phpdbg_info_commands, "|s", PHPDBG_ASYNC_SAFE),
  74. PHPDBG_COMMAND_D(clean, "clean the execution environment", 'X', NULL, 0, 0),
  75. PHPDBG_COMMAND_D(clear, "clear breakpoints", 'C', NULL, 0, 0),
  76. PHPDBG_COMMAND_D(help, "show help menu", 'h', phpdbg_help_commands, "|s", PHPDBG_ASYNC_SAFE),
  77. PHPDBG_COMMAND_D(set, "set phpdbg configuration", 'S', phpdbg_set_commands, "s", PHPDBG_ASYNC_SAFE),
  78. PHPDBG_COMMAND_D(register, "register a function", 'R', NULL, "s", 0),
  79. PHPDBG_COMMAND_D(source, "execute a phpdbginit", '<', NULL, "s", 0),
  80. PHPDBG_COMMAND_D(export, "export breaks to a .phpdbginit script", '>', NULL, "s", PHPDBG_ASYNC_SAFE),
  81. PHPDBG_COMMAND_D(sh, "shell a command", 0 , NULL, "i", 0),
  82. PHPDBG_COMMAND_D(quit, "exit phpdbg", 'q', NULL, 0, PHPDBG_ASYNC_SAFE),
  83. PHPDBG_COMMAND_D(watch, "set watchpoint", 'w', phpdbg_watch_commands, "|ss", 0),
  84. PHPDBG_COMMAND_D(next, "step over next line", 'n', NULL, 0, PHPDBG_ASYNC_SAFE),
  85. PHPDBG_END_COMMAND
  86. }; /* }}} */
  87. static inline int phpdbg_call_register(phpdbg_param_t *stack) /* {{{ */
  88. {
  89. phpdbg_param_t *name = NULL;
  90. if (stack->type == STACK_PARAM) {
  91. char *lc_name;
  92. name = stack->next;
  93. if (!name || name->type != STR_PARAM) {
  94. return FAILURE;
  95. }
  96. lc_name = zend_str_tolower_dup(name->str, name->len);
  97. if (zend_hash_str_exists(&PHPDBG_G(registered), lc_name, name->len)) {
  98. zval fretval;
  99. zend_fcall_info fci;
  100. memset(&fci, 0, sizeof(zend_fcall_info));
  101. ZVAL_STRINGL(&fci.function_name, lc_name, name->len);
  102. fci.size = sizeof(zend_fcall_info);
  103. //???fci.symbol_table = zend_rebuild_symbol_table();
  104. fci.object = NULL;
  105. fci.retval = &fretval;
  106. if (name->next) {
  107. zval params;
  108. phpdbg_param_t *next = name->next;
  109. array_init(&params);
  110. while (next) {
  111. char *buffered = NULL;
  112. switch (next->type) {
  113. case OP_PARAM:
  114. case COND_PARAM:
  115. case STR_PARAM:
  116. add_next_index_stringl(&params, next->str, next->len);
  117. break;
  118. case NUMERIC_PARAM:
  119. add_next_index_long(&params, next->num);
  120. break;
  121. case METHOD_PARAM:
  122. spprintf(&buffered, 0, "%s::%s", next->method.class, next->method.name);
  123. add_next_index_string(&params, buffered);
  124. break;
  125. case NUMERIC_METHOD_PARAM:
  126. spprintf(&buffered, 0, "%s::%s#"ZEND_LONG_FMT, next->method.class, next->method.name, next->num);
  127. add_next_index_string(&params, buffered);
  128. break;
  129. case NUMERIC_FUNCTION_PARAM:
  130. spprintf(&buffered, 0, "%s#"ZEND_LONG_FMT, next->str, next->num);
  131. add_next_index_string(&params, buffered);
  132. break;
  133. case FILE_PARAM:
  134. spprintf(&buffered, 0, "%s:"ZEND_ULONG_FMT, next->file.name, next->file.line);
  135. add_next_index_string(&params, buffered);
  136. break;
  137. case NUMERIC_FILE_PARAM:
  138. spprintf(&buffered, 0, "%s:#"ZEND_ULONG_FMT, next->file.name, next->file.line);
  139. add_next_index_string(&params, buffered);
  140. break;
  141. default: {
  142. /* not yet */
  143. }
  144. }
  145. next = next->next;
  146. }
  147. zend_fcall_info_args(&fci, &params);
  148. } else {
  149. fci.params = NULL;
  150. fci.param_count = 0;
  151. }
  152. phpdbg_activate_err_buf(0);
  153. phpdbg_free_err_buf();
  154. phpdbg_debug("created %d params from arguments", fci.param_count);
  155. if (zend_call_function(&fci, NULL) == SUCCESS) {
  156. zend_print_zval_r(&fretval, 0);
  157. phpdbg_out("\n");
  158. zval_ptr_dtor(&fretval);
  159. }
  160. zval_ptr_dtor_str(&fci.function_name);
  161. efree(lc_name);
  162. return SUCCESS;
  163. }
  164. efree(lc_name);
  165. }
  166. return FAILURE;
  167. } /* }}} */
  168. struct phpdbg_init_state {
  169. int line;
  170. bool in_code;
  171. char *code;
  172. size_t code_len;
  173. const char *init_file;
  174. };
  175. static void phpdbg_line_init(char *cmd, struct phpdbg_init_state *state) {
  176. size_t cmd_len = strlen(cmd);
  177. state->line++;
  178. while (cmd_len > 0L && isspace(cmd[cmd_len-1])) {
  179. cmd_len--;
  180. }
  181. cmd[cmd_len] = '\0';
  182. if (*cmd && cmd_len > 0L && cmd[0] != '#') {
  183. if (cmd_len == 2) {
  184. if (memcmp(cmd, "<:", sizeof("<:")-1) == SUCCESS) {
  185. state->in_code = 1;
  186. return;
  187. } else {
  188. if (memcmp(cmd, ":>", sizeof(":>")-1) == SUCCESS) {
  189. state->in_code = 0;
  190. state->code[state->code_len] = '\0';
  191. zend_eval_stringl(state->code, state->code_len, NULL, "phpdbginit code");
  192. free(state->code);
  193. state->code = NULL;
  194. return;
  195. }
  196. }
  197. }
  198. if (state->in_code) {
  199. if (state->code == NULL) {
  200. state->code = malloc(cmd_len + 1);
  201. } else {
  202. state->code = realloc(state->code, state->code_len + cmd_len + 1);
  203. }
  204. if (state->code) {
  205. memcpy(&state->code[state->code_len], cmd, cmd_len);
  206. state->code_len += cmd_len;
  207. }
  208. return;
  209. }
  210. zend_try {
  211. char *input = phpdbg_read_input(cmd);
  212. phpdbg_param_t stack;
  213. phpdbg_init_param(&stack, STACK_PARAM);
  214. phpdbg_activate_err_buf(1);
  215. if (phpdbg_do_parse(&stack, input) <= 0) {
  216. switch (phpdbg_stack_execute(&stack, 1 /* allow_async_unsafe == 1 */)) {
  217. case FAILURE:
  218. phpdbg_activate_err_buf(0);
  219. if (phpdbg_call_register(&stack) == FAILURE) {
  220. if (state->init_file) {
  221. phpdbg_output_err_buf("Unrecognized command in %s:%d: %s, %s!", state->init_file, state->line, input, PHPDBG_G(err_buf).msg);
  222. } else {
  223. phpdbg_output_err_buf("Unrecognized command on line %d: %s, %s!", state->line, input, PHPDBG_G(err_buf).msg);
  224. }
  225. }
  226. break;
  227. }
  228. }
  229. phpdbg_activate_err_buf(0);
  230. phpdbg_free_err_buf();
  231. phpdbg_stack_free(&stack);
  232. phpdbg_destroy_input(&input);
  233. } zend_catch {
  234. PHPDBG_G(flags) &= ~(PHPDBG_IS_RUNNING | PHPDBG_IS_CLEANING);
  235. if (PHPDBG_G(flags) & PHPDBG_IS_QUITTING) {
  236. zend_bailout();
  237. }
  238. } zend_end_try();
  239. }
  240. }
  241. void phpdbg_string_init(char *buffer) {
  242. struct phpdbg_init_state state = {0};
  243. char *str = strtok(buffer, "\n");
  244. while (str) {
  245. phpdbg_line_init(str, &state);
  246. str = strtok(NULL, "\n");
  247. }
  248. if (state.code) {
  249. free(state.code);
  250. }
  251. }
  252. void phpdbg_try_file_init(char *init_file, size_t init_file_len, bool free_init) /* {{{ */
  253. {
  254. zend_stat_t sb;
  255. if (init_file && VCWD_STAT(init_file, &sb) != -1) {
  256. FILE *fp = fopen(init_file, "r");
  257. if (fp) {
  258. char cmd[PHPDBG_MAX_CMD];
  259. struct phpdbg_init_state state = {0};
  260. state.init_file = init_file;
  261. while (fgets(cmd, PHPDBG_MAX_CMD, fp) != NULL) {
  262. phpdbg_line_init(cmd, &state);
  263. }
  264. if (state.code) {
  265. free(state.code);
  266. }
  267. fclose(fp);
  268. } else {
  269. phpdbg_error("Failed to open %s for initialization", init_file);
  270. }
  271. if (free_init) {
  272. free(init_file);
  273. }
  274. }
  275. } /* }}} */
  276. void phpdbg_init(char *init_file, size_t init_file_len, bool use_default) /* {{{ */
  277. {
  278. if (init_file) {
  279. phpdbg_try_file_init(init_file, init_file_len, 1);
  280. } else if (use_default) {
  281. char *scan_dir = getenv("PHP_INI_SCAN_DIR");
  282. char *sys_ini;
  283. int i;
  284. ZEND_IGNORE_VALUE(asprintf(&sys_ini, "%s/" PHPDBG_INIT_FILENAME, PHP_CONFIG_FILE_PATH));
  285. phpdbg_try_file_init(sys_ini, strlen(sys_ini), 0);
  286. free(sys_ini);
  287. if (!scan_dir) {
  288. scan_dir = PHP_CONFIG_FILE_SCAN_DIR;
  289. }
  290. while (*scan_dir != 0) {
  291. i = 0;
  292. while (scan_dir[i] != ':') {
  293. if (scan_dir[i++] == 0) {
  294. i = -1;
  295. break;
  296. }
  297. }
  298. if (i != -1) {
  299. scan_dir[i] = 0;
  300. }
  301. ZEND_IGNORE_VALUE(asprintf(&init_file, "%s/%s", scan_dir, PHPDBG_INIT_FILENAME));
  302. phpdbg_try_file_init(init_file, strlen(init_file), 1);
  303. free(init_file);
  304. if (i == -1) {
  305. break;
  306. }
  307. scan_dir += i + 1;
  308. }
  309. phpdbg_try_file_init(PHPDBG_STRL(PHPDBG_INIT_FILENAME), 0);
  310. }
  311. }
  312. /* }}} */
  313. void phpdbg_clean(bool full, bool resubmit) /* {{{ */
  314. {
  315. /* this is implicitly required */
  316. if (PHPDBG_G(ops)) {
  317. destroy_op_array(PHPDBG_G(ops));
  318. efree(PHPDBG_G(ops));
  319. PHPDBG_G(ops) = NULL;
  320. }
  321. if (!resubmit && PHPDBG_G(cur_command)) {
  322. free(PHPDBG_G(cur_command));
  323. PHPDBG_G(cur_command) = NULL;
  324. }
  325. if (full) {
  326. PHPDBG_G(flags) |= PHPDBG_IS_CLEANING;
  327. }
  328. } /* }}} */
  329. PHPDBG_COMMAND(exec) /* {{{ */
  330. {
  331. zend_stat_t sb;
  332. if (VCWD_STAT(param->str, &sb) != FAILURE) {
  333. if (sb.st_mode & (S_IFREG|S_IFLNK)) {
  334. char *res = phpdbg_resolve_path(param->str);
  335. size_t res_len = strlen(res);
  336. if ((res_len != PHPDBG_G(exec_len)) || (memcmp(res, PHPDBG_G(exec), res_len) != SUCCESS)) {
  337. if (PHPDBG_G(in_execution)) {
  338. if (phpdbg_ask_user_permission("Do you really want to stop execution to set a new execution context?") == FAILURE) {
  339. return FAILURE;
  340. }
  341. }
  342. if (PHPDBG_G(exec)) {
  343. phpdbg_notice("Unsetting old execution context: %s", PHPDBG_G(exec));
  344. free(PHPDBG_G(exec));
  345. PHPDBG_G(exec) = NULL;
  346. PHPDBG_G(exec_len) = 0L;
  347. }
  348. if (PHPDBG_G(ops)) {
  349. phpdbg_notice("Destroying compiled opcodes");
  350. phpdbg_clean(0, 0);
  351. }
  352. PHPDBG_G(exec) = res;
  353. PHPDBG_G(exec_len) = res_len;
  354. VCWD_CHDIR_FILE(res);
  355. *SG(request_info).argv = estrndup(PHPDBG_G(exec), PHPDBG_G(exec_len));
  356. php_build_argv(NULL, &PG(http_globals)[TRACK_VARS_SERVER]);
  357. phpdbg_notice("Set execution context: %s", PHPDBG_G(exec));
  358. if (PHPDBG_G(in_execution)) {
  359. phpdbg_clean(1, 0);
  360. return SUCCESS;
  361. }
  362. phpdbg_compile();
  363. } else {
  364. phpdbg_notice("Execution context not changed");
  365. }
  366. } else {
  367. phpdbg_error("Cannot use %s as execution context, not a valid file or symlink", param->str);
  368. }
  369. } else {
  370. phpdbg_error("Cannot stat %s, ensure the file exists", param->str);
  371. }
  372. return SUCCESS;
  373. } /* }}} */
  374. PHPDBG_COMMAND(stdin)
  375. {
  376. smart_str code = {0};
  377. char *buf;
  378. char *sep = param->str;
  379. int seplen = param->len;
  380. int bytes = 0;
  381. smart_str_appends(&code, "?>");
  382. do {
  383. PHPDBG_G(input_buflen) += bytes;
  384. if (PHPDBG_G(input_buflen) <= 0) {
  385. continue;
  386. }
  387. if (sep && seplen) {
  388. char *nl = buf = PHPDBG_G(input_buffer);
  389. do {
  390. if (buf == nl + seplen) {
  391. if (!memcmp(sep, nl, seplen) && (*buf == '\n' || (*buf == '\r' && buf[1] == '\n'))) {
  392. smart_str_appendl(&code, PHPDBG_G(input_buffer), nl - PHPDBG_G(input_buffer));
  393. memmove(PHPDBG_G(input_buffer), ++buf, --PHPDBG_G(input_buflen));
  394. goto exec_code;
  395. }
  396. }
  397. if (*buf == '\n') {
  398. nl = buf + 1;
  399. }
  400. buf++;
  401. } while (--PHPDBG_G(input_buflen));
  402. if (buf != nl && buf <= nl + seplen) {
  403. smart_str_appendl(&code, PHPDBG_G(input_buffer), nl - PHPDBG_G(input_buffer));
  404. PHPDBG_G(input_buflen) = buf - nl;
  405. memmove(PHPDBG_G(input_buffer), nl, PHPDBG_G(input_buflen));
  406. } else {
  407. PHPDBG_G(input_buflen) = 0;
  408. smart_str_appendl(&code, PHPDBG_G(input_buffer), buf - PHPDBG_G(input_buffer));
  409. }
  410. } else {
  411. smart_str_appendl(&code, PHPDBG_G(input_buffer), PHPDBG_G(input_buflen));
  412. PHPDBG_G(input_buflen) = 0;
  413. }
  414. } while ((bytes = phpdbg_mixed_read(PHPDBG_G(io)[PHPDBG_STDIN].fd, PHPDBG_G(input_buffer) + PHPDBG_G(input_buflen), PHPDBG_MAX_CMD - PHPDBG_G(input_buflen), -1)) > 0);
  415. if (bytes < 0) {
  416. PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
  417. zend_bailout();
  418. }
  419. exec_code:
  420. smart_str_0(&code);
  421. if (phpdbg_compile_stdin(code.s) == FAILURE) {
  422. zend_exception_error(EG(exception), E_ERROR);
  423. zend_bailout();
  424. }
  425. return SUCCESS;
  426. } /* }}} */
  427. int phpdbg_compile_stdin(zend_string *code) {
  428. PHPDBG_G(ops) = zend_compile_string(code, "Standard input code");
  429. zend_string_release(code);
  430. if (EG(exception)) {
  431. return FAILURE;
  432. }
  433. if (PHPDBG_G(exec)) {
  434. free(PHPDBG_G(exec));
  435. }
  436. PHPDBG_G(exec) = strdup("Standard input code");
  437. PHPDBG_G(exec_len) = sizeof("Standard input code") - 1;
  438. { /* remove leading ?> from source */
  439. int i;
  440. /* remove trailing data after zero byte, used for avoiding conflicts in eval()'ed code snippets */
  441. zend_string *source_path = strpprintf(0, "Standard input code%c%p", 0, PHPDBG_G(ops)->opcodes);
  442. phpdbg_file_source *data = zend_hash_find_ptr(&PHPDBG_G(file_sources), source_path);
  443. dtor_func_t dtor = PHPDBG_G(file_sources).pDestructor;
  444. PHPDBG_G(file_sources).pDestructor = NULL;
  445. zend_hash_del(&PHPDBG_G(file_sources), source_path);
  446. PHPDBG_G(file_sources).pDestructor = dtor;
  447. zend_hash_str_update_ptr(&PHPDBG_G(file_sources), "Standard input code", sizeof("Standard input code")-1, data);
  448. zend_string_release(source_path);
  449. for (i = 1; i <= data->lines; i++) {
  450. data->line[i] -= 2;
  451. }
  452. data->len -= 2;
  453. memmove(data->buf, data->buf + 2, data->len);
  454. }
  455. phpdbg_notice("Successful compilation of stdin input");
  456. return SUCCESS;
  457. }
  458. int phpdbg_compile(void) /* {{{ */
  459. {
  460. zend_file_handle fh;
  461. char *buf;
  462. size_t len;
  463. if (!PHPDBG_G(exec)) {
  464. phpdbg_error("No execution context");
  465. return FAILURE;
  466. }
  467. zend_stream_init_filename(&fh, PHPDBG_G(exec));
  468. if (php_stream_open_for_zend_ex(&fh, USE_PATH|STREAM_OPEN_FOR_INCLUDE) == SUCCESS && zend_stream_fixup(&fh, &buf, &len) == SUCCESS) {
  469. CG(skip_shebang) = 1;
  470. PHPDBG_G(ops) = zend_compile_file(&fh, ZEND_INCLUDE);
  471. zend_destroy_file_handle(&fh);
  472. if (EG(exception)) {
  473. zend_exception_error(EG(exception), E_ERROR);
  474. zend_bailout();
  475. }
  476. phpdbg_notice("Successful compilation of %s", PHPDBG_G(exec));
  477. return SUCCESS;
  478. } else {
  479. phpdbg_error("Could not open file %s", PHPDBG_G(exec));
  480. }
  481. zend_destroy_file_handle(&fh);
  482. return FAILURE;
  483. } /* }}} */
  484. PHPDBG_COMMAND(step) /* {{{ */
  485. {
  486. if (PHPDBG_G(in_execution)) {
  487. PHPDBG_G(flags) |= PHPDBG_IS_STEPPING;
  488. }
  489. return PHPDBG_NEXT;
  490. } /* }}} */
  491. PHPDBG_COMMAND(continue) /* {{{ */
  492. {
  493. return PHPDBG_NEXT;
  494. } /* }}} */
  495. int phpdbg_skip_line_helper(void) /* {{{ */ {
  496. zend_execute_data *ex = phpdbg_user_execute_data(EG(current_execute_data));
  497. const zend_op_array *op_array = &ex->func->op_array;
  498. const zend_op *opline = op_array->opcodes;
  499. PHPDBG_G(flags) |= PHPDBG_IN_UNTIL;
  500. PHPDBG_G(seek_ex) = ex;
  501. do {
  502. if (opline->lineno != ex->opline->lineno
  503. || opline->opcode == ZEND_RETURN
  504. || opline->opcode == ZEND_FAST_RET
  505. || opline->opcode == ZEND_GENERATOR_RETURN
  506. || opline->opcode == ZEND_EXIT
  507. || opline->opcode == ZEND_YIELD
  508. || opline->opcode == ZEND_YIELD_FROM
  509. ) {
  510. zend_hash_index_update_ptr(&PHPDBG_G(seek), (zend_ulong) opline, (void *) opline);
  511. }
  512. } while (++opline < op_array->opcodes + op_array->last);
  513. return PHPDBG_UNTIL;
  514. }
  515. /* }}} */
  516. PHPDBG_COMMAND(until) /* {{{ */
  517. {
  518. if (!PHPDBG_G(in_execution)) {
  519. phpdbg_error("Not executing");
  520. return SUCCESS;
  521. }
  522. return phpdbg_skip_line_helper();
  523. } /* }}} */
  524. PHPDBG_COMMAND(next) /* {{{ */
  525. {
  526. if (!PHPDBG_G(in_execution)) {
  527. phpdbg_error("Not executing");
  528. return SUCCESS;
  529. }
  530. PHPDBG_G(flags) |= PHPDBG_IS_STEPPING;
  531. return phpdbg_skip_line_helper();
  532. } /* }}} */
  533. static void phpdbg_seek_to_end(void) /* {{{ */ {
  534. zend_execute_data *ex = phpdbg_user_execute_data(EG(current_execute_data));
  535. const zend_op_array *op_array = &ex->func->op_array;
  536. const zend_op *opline = op_array->opcodes;
  537. PHPDBG_G(seek_ex) = ex;
  538. do {
  539. switch (opline->opcode) {
  540. case ZEND_RETURN:
  541. case ZEND_FAST_RET:
  542. case ZEND_GENERATOR_RETURN:
  543. case ZEND_EXIT:
  544. case ZEND_YIELD:
  545. case ZEND_YIELD_FROM:
  546. zend_hash_index_update_ptr(&PHPDBG_G(seek), (zend_ulong) opline, (void *) opline);
  547. }
  548. } while (++opline < op_array->opcodes + op_array->last);
  549. }
  550. /* }}} */
  551. PHPDBG_COMMAND(finish) /* {{{ */
  552. {
  553. if (!PHPDBG_G(in_execution)) {
  554. phpdbg_error("Not executing");
  555. return SUCCESS;
  556. }
  557. phpdbg_seek_to_end();
  558. if (zend_hash_index_exists(&PHPDBG_G(seek), (zend_ulong) phpdbg_user_execute_data(EG(current_execute_data))->opline)) {
  559. zend_hash_clean(&PHPDBG_G(seek));
  560. } else {
  561. PHPDBG_G(flags) |= PHPDBG_IN_FINISH;
  562. }
  563. return PHPDBG_FINISH;
  564. } /* }}} */
  565. PHPDBG_COMMAND(leave) /* {{{ */
  566. {
  567. if (!PHPDBG_G(in_execution)) {
  568. phpdbg_error("Not executing");
  569. return SUCCESS;
  570. }
  571. phpdbg_seek_to_end();
  572. if (zend_hash_index_exists(&PHPDBG_G(seek), (zend_ulong) phpdbg_user_execute_data(EG(current_execute_data))->opline)) {
  573. zend_hash_clean(&PHPDBG_G(seek));
  574. phpdbg_notice("Already at the end of the function");
  575. return SUCCESS;
  576. } else {
  577. PHPDBG_G(flags) |= PHPDBG_IN_LEAVE;
  578. return PHPDBG_LEAVE;
  579. }
  580. } /* }}} */
  581. PHPDBG_COMMAND(frame) /* {{{ */
  582. {
  583. if (!param) {
  584. phpdbg_notice("Currently in frame #%d", PHPDBG_G(frame).num);
  585. } else {
  586. phpdbg_switch_frame(param->num);
  587. }
  588. return SUCCESS;
  589. } /* }}} */
  590. static inline void phpdbg_handle_exception(void) /* {{{ */
  591. {
  592. zend_object *ex = EG(exception);
  593. zend_string *msg, *file;
  594. zend_long line;
  595. zval rv, tmp;
  596. EG(exception) = NULL;
  597. zend_call_known_instance_method_with_0_params(ex->ce->__tostring, ex, &tmp);
  598. file = zval_get_string(zend_read_property(zend_get_exception_base(ex), ex, ZEND_STRL("file"), 1, &rv));
  599. line = zval_get_long(zend_read_property(zend_get_exception_base(ex), ex, ZEND_STRL("line"), 1, &rv));
  600. if (EG(exception)) {
  601. EG(exception) = NULL;
  602. msg = ZSTR_EMPTY_ALLOC();
  603. } else {
  604. zend_update_property_string(zend_get_exception_base(ex), ex, ZEND_STRL("string"), Z_STRVAL(tmp));
  605. zval_ptr_dtor(&tmp);
  606. msg = zval_get_string(zend_read_property(zend_get_exception_base(ex), ex, ZEND_STRL("string"), 1, &rv));
  607. }
  608. phpdbg_error("Uncaught %s in %s on line " ZEND_LONG_FMT, ZSTR_VAL(ex->ce->name), ZSTR_VAL(file), line);
  609. zend_string_release(file);
  610. phpdbg_writeln("%s", ZSTR_VAL(msg));
  611. zend_string_release(msg);
  612. if (EG(prev_exception)) {
  613. OBJ_RELEASE(EG(prev_exception));
  614. EG(prev_exception) = 0;
  615. }
  616. OBJ_RELEASE(ex);
  617. EG(opline_before_exception) = NULL;
  618. EG(exit_status) = 255;
  619. } /* }}} */
  620. PHPDBG_COMMAND(run) /* {{{ */
  621. {
  622. if (PHPDBG_G(ops) || PHPDBG_G(exec)) {
  623. zend_execute_data *ex = EG(current_execute_data);
  624. bool restore = 1;
  625. if (PHPDBG_G(in_execution)) {
  626. if (phpdbg_ask_user_permission("Do you really want to restart execution?") == SUCCESS) {
  627. phpdbg_startup_run++;
  628. phpdbg_clean(1, 1);
  629. }
  630. return SUCCESS;
  631. }
  632. if (!PHPDBG_G(ops)) {
  633. if (phpdbg_compile() == FAILURE) {
  634. phpdbg_error("Failed to compile %s, cannot run", PHPDBG_G(exec));
  635. EG(exit_status) = FAILURE;
  636. goto out;
  637. }
  638. }
  639. if (param && param->type != EMPTY_PARAM && param->len != 0) {
  640. char **argv = emalloc(5 * sizeof(char *));
  641. char *end = param->str + param->len, *p = param->str;
  642. char last_byte;
  643. int argc = 0;
  644. int i;
  645. while (*end == '\r' || *end == '\n') *(end--) = 0;
  646. last_byte = end[1];
  647. end[1] = 0;
  648. while (*p == ' ') p++;
  649. while (*p) {
  650. char sep = ' ';
  651. char *buf = emalloc(end - p + 2), *q = buf;
  652. if (*p == '<') {
  653. /* use as STDIN */
  654. do p++; while (*p == ' ');
  655. if (*p == '\'' || *p == '"') {
  656. sep = *(p++);
  657. }
  658. while (*p && *p != sep) {
  659. if (*p == '\\' && (p[1] == sep || p[1] == '\\')) {
  660. p++;
  661. }
  662. *(q++) = *(p++);
  663. }
  664. *(q++) = 0;
  665. if (*p) {
  666. do p++; while (*p == ' ');
  667. }
  668. if (*p) {
  669. phpdbg_error("Invalid run command, cannot put further arguments after stdin");
  670. goto free_cmd;
  671. }
  672. PHPDBG_G(stdin_file) = fopen(buf, "r");
  673. if (PHPDBG_G(stdin_file) == NULL) {
  674. phpdbg_error("Could not open '%s' for reading from stdin", buf);
  675. goto free_cmd;
  676. }
  677. efree(buf);
  678. phpdbg_register_file_handles();
  679. break;
  680. }
  681. if (argc >= 4 && argc == (argc & -argc)) {
  682. argv = erealloc(argv, (argc * 2 + 1) * sizeof(char *));
  683. }
  684. if (*p == '\'' || *p == '"') {
  685. sep = *(p++);
  686. }
  687. if (*p == '\\' && (p[1] == '<' || p[1] == '\'' || p[1] == '"')) {
  688. p++;
  689. }
  690. while (*p && *p != sep) {
  691. if (*p == '\\' && (p[1] == sep || p[1] == '\\' || (p[1] == '#' && sep == ' '))) {
  692. p++;
  693. }
  694. *(q++) = *(p++);
  695. }
  696. if (!*p && sep != ' ') {
  697. phpdbg_error("Invalid run command, unterminated escape sequence");
  698. free_cmd:
  699. efree(buf);
  700. for (i = 0; i < argc; i++) {
  701. efree(argv[i]);
  702. }
  703. efree(argv);
  704. end[1] = last_byte;
  705. return SUCCESS;
  706. }
  707. *(q++) = 0;
  708. argv[++argc] = erealloc(buf, q - buf);
  709. if (*p) {
  710. do p++; while (*p == ' ');
  711. }
  712. }
  713. end[1] = last_byte;
  714. argv[0] = SG(request_info).argv[0];
  715. for (i = SG(request_info).argc; --i;) {
  716. efree(SG(request_info).argv[i]);
  717. }
  718. efree(SG(request_info).argv);
  719. SG(request_info).argv = erealloc(argv, ++argc * sizeof(char *));
  720. SG(request_info).argc = argc;
  721. php_build_argv(NULL, &PG(http_globals)[TRACK_VARS_SERVER]);
  722. }
  723. /* clean up from last execution */
  724. if (ex && (ZEND_CALL_INFO(ex) & ZEND_CALL_HAS_SYMBOL_TABLE)) {
  725. zend_hash_clean(ex->symbol_table);
  726. } else {
  727. zend_rebuild_symbol_table();
  728. }
  729. PHPDBG_G(handled_exception) = NULL;
  730. /* clean seek state */
  731. PHPDBG_G(flags) &= ~PHPDBG_SEEK_MASK;
  732. zend_hash_clean(&PHPDBG_G(seek));
  733. /* reset hit counters */
  734. phpdbg_reset_breakpoints();
  735. zend_try {
  736. PHPDBG_G(flags) ^= PHPDBG_IS_INTERACTIVE;
  737. PHPDBG_G(flags) |= PHPDBG_IS_RUNNING;
  738. zend_execute(PHPDBG_G(ops), &PHPDBG_G(retval));
  739. PHPDBG_G(flags) ^= PHPDBG_IS_INTERACTIVE;
  740. } zend_catch {
  741. PHPDBG_G(in_execution) = 0;
  742. if (!(PHPDBG_G(flags) & PHPDBG_IS_STOPPING)) {
  743. restore = 0;
  744. } else {
  745. zend_bailout();
  746. }
  747. } zend_end_try();
  748. if (restore) {
  749. zend_exception_restore();
  750. zend_try {
  751. zend_try_exception_handler();
  752. PHPDBG_G(in_execution) = 1;
  753. } zend_catch {
  754. PHPDBG_G(in_execution) = 0;
  755. if (PHPDBG_G(flags) & PHPDBG_IS_STOPPING) {
  756. zend_bailout();
  757. }
  758. } zend_end_try();
  759. if (EG(exception)) {
  760. phpdbg_handle_exception();
  761. }
  762. }
  763. PHPDBG_G(flags) &= ~PHPDBG_IS_RUNNING;
  764. phpdbg_clean(1, 0);
  765. } else {
  766. phpdbg_error("Nothing to execute!");
  767. }
  768. out:
  769. PHPDBG_FRAME(num) = 0;
  770. return SUCCESS;
  771. } /* }}} */
  772. int phpdbg_output_ev_variable(char *name, size_t len, char *keyname, size_t keylen, HashTable *parent, zval *zv) /* {{{ */ {
  773. phpdbg_notice("Printing variable %.*s", (int) len, name);
  774. zend_print_zval_r(zv, 0);
  775. phpdbg_out("\n");
  776. efree(name);
  777. efree(keyname);
  778. return SUCCESS;
  779. }
  780. /* }}} */
  781. PHPDBG_COMMAND(ev) /* {{{ */
  782. {
  783. bool stepping = ((PHPDBG_G(flags) & PHPDBG_IS_STEPPING) == PHPDBG_IS_STEPPING);
  784. zval retval;
  785. zend_execute_data *original_execute_data = EG(current_execute_data);
  786. zend_vm_stack original_stack = EG(vm_stack);
  787. zend_object *ex = NULL;
  788. PHPDBG_OUTPUT_BACKUP();
  789. original_stack->top = EG(vm_stack_top);
  790. if (PHPDBG_G(flags) & PHPDBG_IN_SIGNAL_HANDLER) {
  791. phpdbg_try_access {
  792. phpdbg_parse_variable(param->str, param->len, &EG(symbol_table), 0, phpdbg_output_ev_variable, 0);
  793. } phpdbg_catch_access {
  794. phpdbg_error("Could not fetch data, invalid data source");
  795. } phpdbg_end_try_access();
  796. PHPDBG_OUTPUT_BACKUP_RESTORE();
  797. return SUCCESS;
  798. }
  799. if (!(PHPDBG_G(flags) & PHPDBG_IS_STEPONEVAL)) {
  800. PHPDBG_G(flags) &= ~PHPDBG_IS_STEPPING;
  801. }
  802. /* disable stepping while eval() in progress */
  803. PHPDBG_G(flags) |= PHPDBG_IN_EVAL;
  804. zend_try {
  805. if (zend_eval_stringl(param->str, param->len, &retval, "eval()'d code") == SUCCESS) {
  806. if (EG(exception)) {
  807. ex = EG(exception);
  808. zend_exception_error(EG(exception), E_ERROR);
  809. } else {
  810. zend_print_zval_r(&retval, 0);
  811. phpdbg_out("\n");
  812. zval_ptr_dtor(&retval);
  813. }
  814. }
  815. } zend_catch {
  816. PHPDBG_G(unclean_eval) = 1;
  817. if (ex) {
  818. OBJ_RELEASE(ex);
  819. }
  820. EG(current_execute_data) = original_execute_data;
  821. EG(vm_stack_top) = original_stack->top;
  822. EG(vm_stack_end) = original_stack->end;
  823. EG(vm_stack) = original_stack;
  824. EG(exit_status) = 0;
  825. } zend_end_try();
  826. PHPDBG_G(flags) &= ~PHPDBG_IN_EVAL;
  827. /* switch stepping back on */
  828. if (stepping && !(PHPDBG_G(flags) & PHPDBG_IS_STEPONEVAL)) {
  829. PHPDBG_G(flags) |= PHPDBG_IS_STEPPING;
  830. }
  831. CG(unclean_shutdown) = 0;
  832. PHPDBG_OUTPUT_BACKUP_RESTORE();
  833. return SUCCESS;
  834. } /* }}} */
  835. PHPDBG_COMMAND(back) /* {{{ */
  836. {
  837. if (!PHPDBG_G(in_execution)) {
  838. phpdbg_error("Not executing!");
  839. return SUCCESS;
  840. }
  841. if (!param) {
  842. phpdbg_dump_backtrace(0);
  843. } else {
  844. phpdbg_dump_backtrace(param->num);
  845. }
  846. return SUCCESS;
  847. } /* }}} */
  848. PHPDBG_COMMAND(generator) /* {{{ */
  849. {
  850. int i;
  851. if (!PHPDBG_G(in_execution)) {
  852. phpdbg_error("Not executing!");
  853. return SUCCESS;
  854. }
  855. if (param) {
  856. i = param->num;
  857. zend_object **obj = EG(objects_store).object_buckets + i;
  858. if (i < EG(objects_store).top && *obj && IS_OBJ_VALID(*obj) && (*obj)->ce == zend_ce_generator) {
  859. zend_generator *gen = (zend_generator *) *obj;
  860. if (gen->execute_data) {
  861. if (zend_generator_get_current(gen)->flags & ZEND_GENERATOR_CURRENTLY_RUNNING) {
  862. phpdbg_error("Generator currently running");
  863. } else {
  864. phpdbg_open_generator_frame(gen);
  865. }
  866. } else {
  867. phpdbg_error("Generator already closed");
  868. }
  869. } else {
  870. phpdbg_error("Invalid object handle");
  871. }
  872. } else {
  873. for (i = 0; i < EG(objects_store).top; i++) {
  874. zend_object *obj = EG(objects_store).object_buckets[i];
  875. if (obj && IS_OBJ_VALID(obj) && obj->ce == zend_ce_generator) {
  876. zend_generator *gen = (zend_generator *) obj, *current = zend_generator_get_current(gen);
  877. if (gen->execute_data) {
  878. zend_string *s = phpdbg_compile_stackframe(gen->execute_data);
  879. phpdbg_out("#%d: %.*s", i, (int) ZSTR_LEN(s), ZSTR_VAL(s));
  880. zend_string_release(s);
  881. if (gen != current) {
  882. if (gen->node.parent != current) {
  883. phpdbg_out(" with direct parent #%d and", gen->node.parent->std.handle);
  884. }
  885. phpdbg_out(" executing #%d currently", current->std.handle);
  886. }
  887. phpdbg_out("\n");
  888. }
  889. }
  890. }
  891. }
  892. return SUCCESS;
  893. } /* }}} */
  894. PHPDBG_COMMAND(print) /* {{{ */
  895. {
  896. if (!param || param->type == EMPTY_PARAM) {
  897. return phpdbg_do_print_stack(param);
  898. } else switch (param->type) {
  899. case STR_PARAM:
  900. return phpdbg_do_print_func(param);
  901. case METHOD_PARAM:
  902. return phpdbg_do_print_method(param);
  903. default:
  904. phpdbg_error("Invalid arguments to print, expected nothing, function name or method name");
  905. return SUCCESS;
  906. }
  907. } /* }}} */
  908. PHPDBG_COMMAND(info) /* {{{ */
  909. {
  910. phpdbg_out("Execution Context Information\n\n");
  911. #ifdef HAVE_PHPDBG_READLINE
  912. # ifdef HAVE_LIBREADLINE
  913. phpdbg_writeln( "Readline yes");
  914. # else
  915. phpdbg_writeln("Readline no");
  916. # endif
  917. # ifdef HAVE_LIBEDIT
  918. phpdbg_writeln("Libedit yes");
  919. # else
  920. phpdbg_writeln("Libedit no");
  921. # endif
  922. #else
  923. phpdbg_writeln("Readline unavailable");
  924. #endif
  925. phpdbg_writeln("Exec %s", PHPDBG_G(exec) ? PHPDBG_G(exec) : "none");
  926. phpdbg_writeln("Compiled %s", PHPDBG_G(ops) ? "yes" : "no");
  927. phpdbg_writeln("Stepping %s", (PHPDBG_G(flags) & PHPDBG_IS_STEPPING) ? "on" : "off");
  928. phpdbg_writeln("Quietness %s", (PHPDBG_G(flags) & PHPDBG_IS_QUIET) ? "on" : "off");
  929. if (PHPDBG_G(ops)) {
  930. phpdbg_writeln("Opcodes %d", PHPDBG_G(ops)->last);
  931. phpdbg_writeln("Variables %d", PHPDBG_G(ops)->last_var ? PHPDBG_G(ops)->last_var - 1 : 0);
  932. }
  933. phpdbg_writeln("Executing %s", PHPDBG_G(in_execution) ? "yes" : "no");
  934. if (PHPDBG_G(in_execution)) {
  935. phpdbg_writeln("VM Return %d", PHPDBG_G(vmret));
  936. }
  937. phpdbg_writeln("Classes %d", zend_hash_num_elements(EG(class_table)));
  938. phpdbg_writeln("Functions %d", zend_hash_num_elements(EG(function_table)));
  939. phpdbg_writeln("Constants %d", zend_hash_num_elements(EG(zend_constants)));
  940. phpdbg_writeln("Included %d", zend_hash_num_elements(&EG(included_files)));
  941. return SUCCESS;
  942. } /* }}} */
  943. PHPDBG_COMMAND(set) /* {{{ */
  944. {
  945. phpdbg_error("No set command selected!");
  946. return SUCCESS;
  947. } /* }}} */
  948. PHPDBG_COMMAND(break) /* {{{ */
  949. {
  950. if (!param) {
  951. if (PHPDBG_G(exec)) {
  952. phpdbg_set_breakpoint_file(
  953. zend_get_executed_filename(),
  954. strlen(zend_get_executed_filename()),
  955. zend_get_executed_lineno());
  956. } else {
  957. phpdbg_error("Execution context not set!");
  958. }
  959. } else switch (param->type) {
  960. case ADDR_PARAM:
  961. phpdbg_set_breakpoint_opline(param->addr);
  962. break;
  963. case NUMERIC_PARAM:
  964. if (PHPDBG_G(exec)) {
  965. phpdbg_set_breakpoint_file(phpdbg_current_file(), strlen(phpdbg_current_file()), param->num);
  966. } else {
  967. phpdbg_error("Execution context not set!");
  968. }
  969. break;
  970. case METHOD_PARAM:
  971. phpdbg_set_breakpoint_method(param->method.class, param->method.name);
  972. break;
  973. case NUMERIC_METHOD_PARAM:
  974. phpdbg_set_breakpoint_method_opline(param->method.class, param->method.name, param->num);
  975. break;
  976. case NUMERIC_FUNCTION_PARAM:
  977. phpdbg_set_breakpoint_function_opline(param->str, param->num);
  978. break;
  979. case FILE_PARAM:
  980. phpdbg_set_breakpoint_file(param->file.name, 0, param->file.line);
  981. break;
  982. case NUMERIC_FILE_PARAM:
  983. phpdbg_set_breakpoint_file_opline(param->file.name, param->file.line);
  984. break;
  985. case COND_PARAM:
  986. phpdbg_set_breakpoint_expression(param->str, param->len);
  987. break;
  988. case STR_PARAM:
  989. phpdbg_set_breakpoint_symbol(param->str, param->len);
  990. break;
  991. case OP_PARAM:
  992. phpdbg_set_breakpoint_opcode(param->str, param->len);
  993. break;
  994. phpdbg_default_switch_case();
  995. }
  996. return SUCCESS;
  997. } /* }}} */
  998. PHPDBG_COMMAND(sh) /* {{{ */
  999. {
  1000. FILE *fd = NULL;
  1001. if ((fd=VCWD_POPEN((char*)param->str, "w"))) {
  1002. /* TODO: do something perhaps ?? do we want input ?? */
  1003. pclose(fd);
  1004. } else {
  1005. phpdbg_error("Failed to execute %s", param->str);
  1006. }
  1007. return SUCCESS;
  1008. } /* }}} */
  1009. static int add_module_info(zend_module_entry *module) /* {{{ */ {
  1010. phpdbg_write("%s\n", module->name);
  1011. return 0;
  1012. }
  1013. /* }}} */
  1014. static void add_zendext_info(zend_extension *ext) /* {{{ */ {
  1015. phpdbg_write("%s\n", ext->name);
  1016. }
  1017. /* }}} */
  1018. #ifdef HAVE_LIBDL
  1019. PHPDBG_API const char *phpdbg_load_module_or_extension(char **path, const char **name) /* {{{ */ {
  1020. DL_HANDLE handle;
  1021. char *extension_dir;
  1022. extension_dir = INI_STR("extension_dir");
  1023. if (strchr(*path, '/') != NULL || strchr(*path, DEFAULT_SLASH) != NULL) {
  1024. /* path is fine */
  1025. } else if (extension_dir && extension_dir[0]) {
  1026. char *libpath;
  1027. int extension_dir_len = strlen(extension_dir);
  1028. if (IS_SLASH(extension_dir[extension_dir_len-1])) {
  1029. spprintf(&libpath, 0, "%s%s", extension_dir, *path); /* SAFE */
  1030. } else {
  1031. spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, *path); /* SAFE */
  1032. }
  1033. efree(*path);
  1034. *path = libpath;
  1035. } else {
  1036. phpdbg_error("Not a full path given or extension_dir ini setting is not set");
  1037. return NULL;
  1038. }
  1039. handle = DL_LOAD(*path);
  1040. if (!handle) {
  1041. #ifdef PHP_WIN32
  1042. char *err = GET_DL_ERROR();
  1043. if (err && err[0]) {
  1044. phpdbg_error("%s", err);
  1045. php_win32_error_msg_free(err);
  1046. } else {
  1047. phpdbg_error("Unknown reason");
  1048. }
  1049. #else
  1050. phpdbg_error("%s", GET_DL_ERROR());
  1051. #endif
  1052. return NULL;
  1053. }
  1054. #if ZEND_EXTENSIONS_SUPPORT
  1055. do {
  1056. zend_extension *new_extension;
  1057. zend_extension_version_info *extension_version_info;
  1058. extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "extension_version_info");
  1059. if (!extension_version_info) {
  1060. extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "_extension_version_info");
  1061. }
  1062. new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "zend_extension_entry");
  1063. if (!new_extension) {
  1064. new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "_zend_extension_entry");
  1065. }
  1066. if (!extension_version_info || !new_extension) {
  1067. break;
  1068. }
  1069. if (extension_version_info->zend_extension_api_no != ZEND_EXTENSION_API_NO &&(!new_extension->api_no_check || new_extension->api_no_check(ZEND_EXTENSION_API_NO) != SUCCESS)) {
  1070. phpdbg_error("%s requires Zend Engine API version %d, which does not match the installed Zend Engine API version %d", new_extension->name, extension_version_info->zend_extension_api_no, ZEND_EXTENSION_API_NO);
  1071. goto quit;
  1072. } else if (strcmp(ZEND_EXTENSION_BUILD_ID, extension_version_info->build_id) && (!new_extension->build_id_check || new_extension->build_id_check(ZEND_EXTENSION_BUILD_ID) != SUCCESS)) {
  1073. phpdbg_error("%s was built with configuration %s, whereas running engine is %s", new_extension->name, extension_version_info->build_id, ZEND_EXTENSION_BUILD_ID);
  1074. goto quit;
  1075. }
  1076. *name = new_extension->name;
  1077. zend_register_extension(new_extension, handle);
  1078. if (new_extension->startup) {
  1079. if (new_extension->startup(new_extension) != SUCCESS) {
  1080. phpdbg_error("Unable to startup Zend extension %s", new_extension->name);
  1081. goto quit;
  1082. }
  1083. zend_append_version_info(new_extension);
  1084. }
  1085. return "Zend extension";
  1086. } while (0);
  1087. #endif
  1088. do {
  1089. zend_module_entry *module_entry;
  1090. zend_module_entry *(*get_module)(void);
  1091. get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "get_module");
  1092. if (!get_module) {
  1093. get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "_get_module");
  1094. }
  1095. if (!get_module) {
  1096. break;
  1097. }
  1098. module_entry = get_module();
  1099. *name = module_entry->name;
  1100. if (strcmp(ZEND_EXTENSION_BUILD_ID, module_entry->build_id)) {
  1101. phpdbg_error("%s was built with configuration %s, whereas running engine is %s", module_entry->name, module_entry->build_id, ZEND_EXTENSION_BUILD_ID);
  1102. goto quit;
  1103. }
  1104. module_entry->type = MODULE_PERSISTENT;
  1105. module_entry->module_number = zend_next_free_module();
  1106. module_entry->handle = handle;
  1107. if ((module_entry = zend_register_module_ex(module_entry)) == NULL) {
  1108. phpdbg_error("Unable to register module %s", module_entry->name);
  1109. goto quit;
  1110. }
  1111. if (zend_startup_module_ex(module_entry) == FAILURE) {
  1112. phpdbg_error("Unable to startup module %s", module_entry->name);
  1113. goto quit;
  1114. }
  1115. if (module_entry->request_startup_func) {
  1116. if (module_entry->request_startup_func(MODULE_PERSISTENT, module_entry->module_number) == FAILURE) {
  1117. phpdbg_error("Unable to initialize module %s", module_entry->name);
  1118. goto quit;
  1119. }
  1120. }
  1121. return "module";
  1122. } while (0);
  1123. phpdbg_error("This shared object is nor a Zend extension nor a module");
  1124. quit:
  1125. DL_UNLOAD(handle);
  1126. return NULL;
  1127. }
  1128. /* }}} */
  1129. #endif
  1130. PHPDBG_COMMAND(dl) /* {{{ */
  1131. {
  1132. const char *type, *name;
  1133. char *path;
  1134. if (!param || param->type == EMPTY_PARAM) {
  1135. phpdbg_notice("Zend extensions");
  1136. zend_llist_apply(&zend_extensions, (llist_apply_func_t) add_zendext_info);
  1137. phpdbg_out("\n");
  1138. phpdbg_notice("Modules");
  1139. zend_hash_apply(&module_registry, (apply_func_t) add_module_info);
  1140. } else switch (param->type) {
  1141. case STR_PARAM:
  1142. #ifdef HAVE_LIBDL
  1143. path = estrndup(param->str, param->len);
  1144. phpdbg_activate_err_buf(1);
  1145. if ((type = phpdbg_load_module_or_extension(&path, &name)) == NULL) {
  1146. phpdbg_error("Could not load %s, not found or invalid zend extension / module: %s", path, PHPDBG_G(err_buf).msg);
  1147. } else {
  1148. phpdbg_notice("Successfully loaded the %s %s at path %s", type, name, path);
  1149. }
  1150. phpdbg_activate_err_buf(0);
  1151. phpdbg_free_err_buf();
  1152. efree(path);
  1153. #else
  1154. phpdbg_error("Cannot dynamically load %.*s - dynamic modules are not supported", (int) param->len, param->str);
  1155. #endif
  1156. break;
  1157. phpdbg_default_switch_case();
  1158. }
  1159. return SUCCESS;
  1160. } /* }}} */
  1161. PHPDBG_COMMAND(source) /* {{{ */
  1162. {
  1163. zend_stat_t sb;
  1164. if (VCWD_STAT(param->str, &sb) != -1) {
  1165. phpdbg_try_file_init(param->str, param->len, 0);
  1166. } else {
  1167. phpdbg_error("Failed to stat %s, file does not exist", param->str);
  1168. }
  1169. return SUCCESS;
  1170. } /* }}} */
  1171. PHPDBG_COMMAND(export) /* {{{ */
  1172. {
  1173. FILE *handle = VCWD_FOPEN(param->str, "w+");
  1174. if (handle) {
  1175. phpdbg_export_breakpoints(handle);
  1176. fclose(handle);
  1177. } else {
  1178. phpdbg_error("Failed to open or create %s, check path and permissions", param->str);
  1179. }
  1180. return SUCCESS;
  1181. } /* }}} */
  1182. PHPDBG_COMMAND(register) /* {{{ */
  1183. {
  1184. zend_function *function;
  1185. char *lcname = zend_str_tolower_dup(param->str, param->len);
  1186. size_t lcname_len = strlen(lcname);
  1187. if (!zend_hash_str_exists(&PHPDBG_G(registered), lcname, lcname_len)) {
  1188. if ((function = zend_hash_str_find_ptr(EG(function_table), lcname, lcname_len))) {
  1189. zend_hash_str_update_ptr(&PHPDBG_G(registered), lcname, lcname_len, function);
  1190. function_add_ref(function);
  1191. phpdbg_notice("Registered %s", lcname);
  1192. } else {
  1193. phpdbg_error("The requested function (%s) could not be found", param->str);
  1194. }
  1195. } else {
  1196. phpdbg_error("The requested name (%s) is already in use", lcname);
  1197. }
  1198. efree(lcname);
  1199. return SUCCESS;
  1200. } /* }}} */
  1201. PHPDBG_COMMAND(quit) /* {{{ */
  1202. {
  1203. PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
  1204. PHPDBG_G(flags) &= ~PHPDBG_IS_CLEANING;
  1205. return SUCCESS;
  1206. } /* }}} */
  1207. PHPDBG_COMMAND(clean) /* {{{ */
  1208. {
  1209. if (PHPDBG_G(in_execution)) {
  1210. if (phpdbg_ask_user_permission("Do you really want to clean your current environment?") == FAILURE) {
  1211. return SUCCESS;
  1212. }
  1213. }
  1214. phpdbg_out("Cleaning Execution Environment\n");
  1215. phpdbg_writeln("Classes %d", zend_hash_num_elements(EG(class_table)));
  1216. phpdbg_writeln("Functions %d", zend_hash_num_elements(EG(function_table)));
  1217. phpdbg_writeln("Constants %d", zend_hash_num_elements(EG(zend_constants)));
  1218. phpdbg_writeln("Includes %d", zend_hash_num_elements(&EG(included_files)));
  1219. phpdbg_clean(1, 0);
  1220. return SUCCESS;
  1221. } /* }}} */
  1222. PHPDBG_COMMAND(clear) /* {{{ */
  1223. {
  1224. phpdbg_out("Clearing Breakpoints\n");
  1225. phpdbg_writeln("File %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE]));
  1226. phpdbg_writeln("Functions %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM]));
  1227. phpdbg_writeln("Methods %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD]));
  1228. phpdbg_writeln("Oplines %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_OPLINE]));
  1229. phpdbg_writeln("File oplines %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE_OPLINE]));
  1230. phpdbg_writeln("Function oplines %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_FUNCTION_OPLINE]));
  1231. phpdbg_writeln("Method oplines %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD_OPLINE]));
  1232. phpdbg_writeln("Conditionals %d", zend_hash_num_elements(&PHPDBG_G(bp)[PHPDBG_BREAK_COND]));
  1233. phpdbg_clear_breakpoints();
  1234. return SUCCESS;
  1235. } /* }}} */
  1236. PHPDBG_COMMAND(list) /* {{{ */
  1237. {
  1238. if (!param) {
  1239. return PHPDBG_LIST_HANDLER(lines)(PHPDBG_COMMAND_ARGS);
  1240. } else switch (param->type) {
  1241. case NUMERIC_PARAM:
  1242. return PHPDBG_LIST_HANDLER(lines)(PHPDBG_COMMAND_ARGS);
  1243. case FILE_PARAM:
  1244. return PHPDBG_LIST_HANDLER(lines)(PHPDBG_COMMAND_ARGS);
  1245. case STR_PARAM:
  1246. phpdbg_list_function_byname(param->str, param->len);
  1247. break;
  1248. case METHOD_PARAM:
  1249. return PHPDBG_LIST_HANDLER(method)(PHPDBG_COMMAND_ARGS);
  1250. phpdbg_default_switch_case();
  1251. }
  1252. return SUCCESS;
  1253. } /* }}} */
  1254. PHPDBG_COMMAND(watch) /* {{{ */
  1255. {
  1256. if (!param || param->type == EMPTY_PARAM) {
  1257. phpdbg_list_watchpoints();
  1258. } else switch (param->type) {
  1259. case STR_PARAM:
  1260. phpdbg_create_var_watchpoint(param->str, param->len);
  1261. break;
  1262. phpdbg_default_switch_case();
  1263. }
  1264. return SUCCESS;
  1265. } /* }}} */
  1266. int phpdbg_interactive(bool allow_async_unsafe, char *input) /* {{{ */
  1267. {
  1268. int ret = SUCCESS;
  1269. phpdbg_param_t stack;
  1270. PHPDBG_G(flags) |= PHPDBG_IS_INTERACTIVE;
  1271. while (ret == SUCCESS || ret == FAILURE) {
  1272. if (PHPDBG_G(flags) & PHPDBG_IS_STOPPING) {
  1273. zend_bailout();
  1274. }
  1275. if (!input && !(input = phpdbg_read_input(NULL))) {
  1276. break;
  1277. }
  1278. phpdbg_init_param(&stack, STACK_PARAM);
  1279. if (phpdbg_do_parse(&stack, input) <= 0) {
  1280. phpdbg_activate_err_buf(1);
  1281. zend_try {
  1282. ret = phpdbg_stack_execute(&stack, allow_async_unsafe);
  1283. } zend_catch {
  1284. phpdbg_stack_free(&stack);
  1285. zend_bailout();
  1286. } zend_end_try();
  1287. switch (ret) {
  1288. case FAILURE:
  1289. if (!(PHPDBG_G(flags) & PHPDBG_IS_STOPPING)) {
  1290. if (!allow_async_unsafe || phpdbg_call_register(&stack) == FAILURE) {
  1291. if (PHPDBG_G(err_buf).active) {
  1292. phpdbg_output_err_buf("%s", PHPDBG_G(err_buf).msg);
  1293. }
  1294. }
  1295. }
  1296. break;
  1297. case PHPDBG_LEAVE:
  1298. case PHPDBG_FINISH:
  1299. case PHPDBG_UNTIL:
  1300. case PHPDBG_NEXT: {
  1301. phpdbg_activate_err_buf(0);
  1302. phpdbg_free_err_buf();
  1303. if (!PHPDBG_G(in_execution) && !(PHPDBG_G(flags) & PHPDBG_IS_STOPPING)) {
  1304. phpdbg_error("Not running");
  1305. }
  1306. break;
  1307. }
  1308. }
  1309. phpdbg_activate_err_buf(0);
  1310. phpdbg_free_err_buf();
  1311. }
  1312. phpdbg_stack_free(&stack);
  1313. phpdbg_destroy_input(&input);
  1314. PHPDBG_G(req_id) = 0;
  1315. input = NULL;
  1316. }
  1317. if (input) {
  1318. phpdbg_stack_free(&stack);
  1319. phpdbg_destroy_input(&input);
  1320. PHPDBG_G(req_id) = 0;
  1321. }
  1322. if (PHPDBG_G(in_execution)) {
  1323. phpdbg_restore_frame();
  1324. }
  1325. PHPDBG_G(flags) &= ~PHPDBG_IS_INTERACTIVE;
  1326. phpdbg_print_changed_zvals();
  1327. return ret;
  1328. } /* }}} */
  1329. static inline void list_code(void) {
  1330. if (!(PHPDBG_G(flags) & PHPDBG_IN_EVAL)) {
  1331. const char *file_char = zend_get_executed_filename();
  1332. zend_string *file = zend_string_init(file_char, strlen(file_char), 0);
  1333. phpdbg_list_file(file, 3, zend_get_executed_lineno()-1, zend_get_executed_lineno());
  1334. efree(file);
  1335. }
  1336. }
  1337. /* code may behave weirdly if EG(exception) is set; thus backup it */
  1338. #define DO_INTERACTIVE(allow_async_unsafe) do { \
  1339. if (exception) { \
  1340. const zend_op *before_ex = EG(opline_before_exception); \
  1341. const zend_op *backup_opline = NULL; \
  1342. if (EG(current_execute_data) && EG(current_execute_data)->func && ZEND_USER_CODE(EG(current_execute_data)->func->common.type)) { \
  1343. backup_opline = EG(current_execute_data)->opline; \
  1344. } \
  1345. GC_ADDREF(exception); \
  1346. zend_clear_exception(); \
  1347. list_code(); \
  1348. switch (phpdbg_interactive(allow_async_unsafe, NULL)) { \
  1349. case PHPDBG_LEAVE: \
  1350. case PHPDBG_FINISH: \
  1351. case PHPDBG_UNTIL: \
  1352. case PHPDBG_NEXT: \
  1353. if (backup_opline \
  1354. && (backup_opline->opcode == ZEND_HANDLE_EXCEPTION || backup_opline->opcode == ZEND_CATCH)) { \
  1355. EG(current_execute_data)->opline = backup_opline; \
  1356. EG(exception) = exception; \
  1357. } else { \
  1358. zend_throw_exception_internal(exception); \
  1359. } \
  1360. EG(opline_before_exception) = before_ex; \
  1361. } \
  1362. } else { \
  1363. list_code(); \
  1364. phpdbg_interactive(allow_async_unsafe, NULL); \
  1365. } \
  1366. goto next; \
  1367. } while (0)
  1368. void phpdbg_execute_ex(zend_execute_data *execute_data) /* {{{ */
  1369. {
  1370. bool original_in_execution = PHPDBG_G(in_execution);
  1371. if ((PHPDBG_G(flags) & PHPDBG_IS_STOPPING) && !(PHPDBG_G(flags) & PHPDBG_IS_RUNNING)) {
  1372. zend_bailout();
  1373. }
  1374. PHPDBG_G(in_execution) = 1;
  1375. while (1) {
  1376. zend_object *exception = EG(exception);
  1377. if ((PHPDBG_G(flags) & PHPDBG_BP_RESOLVE_MASK)) {
  1378. /* resolve nth opline breakpoints */
  1379. phpdbg_resolve_op_array_breaks(&execute_data->func->op_array);
  1380. }
  1381. #ifdef ZEND_WIN32
  1382. if (EG(timed_out)) {
  1383. zend_timeout();
  1384. }
  1385. #endif
  1386. if (exception && zend_is_unwind_exit(exception)) {
  1387. /* Restore bailout based exit. */
  1388. zend_bailout();
  1389. }
  1390. if (PHPDBG_G(flags) & PHPDBG_PREVENT_INTERACTIVE) {
  1391. phpdbg_print_opline(execute_data, 0);
  1392. goto next;
  1393. }
  1394. /* check for uncaught exceptions */
  1395. if (exception && PHPDBG_G(handled_exception) != exception && !(PHPDBG_G(flags) & PHPDBG_IN_EVAL)) {
  1396. zend_execute_data *prev_ex = execute_data;
  1397. do {
  1398. prev_ex = zend_generator_check_placeholder_frame(prev_ex);
  1399. /* assuming that no internal functions will silently swallow exceptions ... */
  1400. if (!prev_ex->func || !ZEND_USER_CODE(prev_ex->func->common.type)) {
  1401. continue;
  1402. }
  1403. if (phpdbg_check_caught_ex(prev_ex, exception)) {
  1404. goto ex_is_caught;
  1405. }
  1406. } while ((prev_ex = prev_ex->prev_execute_data));
  1407. PHPDBG_G(handled_exception) = exception;
  1408. zval rv;
  1409. zend_string *file = zval_get_string(zend_read_property(zend_get_exception_base(exception), exception, ZEND_STRL("file"), 1, &rv));
  1410. zend_long line = zval_get_long(zend_read_property(zend_get_exception_base(exception), exception, ZEND_STRL("line"), 1, &rv));
  1411. zend_string *msg = zval_get_string(zend_read_property(zend_get_exception_base(exception), exception, ZEND_STRL("message"), 1, &rv));
  1412. phpdbg_error("Uncaught %s in %s on line " ZEND_LONG_FMT ": %.*s",
  1413. ZSTR_VAL(exception->ce->name), ZSTR_VAL(file), line,
  1414. ZSTR_LEN(msg) < 80 ? (int) ZSTR_LEN(msg) : 80, ZSTR_VAL(msg));
  1415. zend_string_release(msg);
  1416. zend_string_release(file);
  1417. DO_INTERACTIVE(1);
  1418. }
  1419. ex_is_caught:
  1420. /* allow conditional breakpoints and initialization to access the vm uninterrupted */
  1421. if (PHPDBG_G(flags) & (PHPDBG_IN_COND_BP | PHPDBG_IS_INITIALIZING)) {
  1422. /* skip possible breakpoints */
  1423. goto next;
  1424. }
  1425. /* not while in conditionals */
  1426. phpdbg_print_opline(execute_data, 0);
  1427. /* perform seek operation */
  1428. if ((PHPDBG_G(flags) & PHPDBG_SEEK_MASK) && !(PHPDBG_G(flags) & PHPDBG_IN_EVAL)) {
  1429. /* current address */
  1430. zend_ulong address = (zend_ulong) execute_data->opline;
  1431. if (PHPDBG_G(seek_ex) != execute_data) {
  1432. if (PHPDBG_G(flags) & PHPDBG_IS_STEPPING) {
  1433. goto stepping;
  1434. }
  1435. goto next;
  1436. }
  1437. #define INDEX_EXISTS_CHECK (zend_hash_index_exists(&PHPDBG_G(seek), address) || (exception && phpdbg_check_caught_ex(execute_data, exception) == 0))
  1438. /* run to next line */
  1439. if (PHPDBG_G(flags) & PHPDBG_IN_UNTIL) {
  1440. if (INDEX_EXISTS_CHECK) {
  1441. PHPDBG_G(flags) &= ~PHPDBG_IN_UNTIL;
  1442. zend_hash_clean(&PHPDBG_G(seek));
  1443. } else {
  1444. /* skip possible breakpoints */
  1445. goto next;
  1446. }
  1447. }
  1448. /* run to finish */
  1449. if (PHPDBG_G(flags) & PHPDBG_IN_FINISH) {
  1450. if (INDEX_EXISTS_CHECK) {
  1451. PHPDBG_G(flags) &= ~PHPDBG_IN_FINISH;
  1452. zend_hash_clean(&PHPDBG_G(seek));
  1453. }
  1454. /* skip possible breakpoints */
  1455. goto next;
  1456. }
  1457. /* break for leave */
  1458. if (PHPDBG_G(flags) & PHPDBG_IN_LEAVE) {
  1459. if (INDEX_EXISTS_CHECK) {
  1460. PHPDBG_G(flags) &= ~PHPDBG_IN_LEAVE;
  1461. zend_hash_clean(&PHPDBG_G(seek));
  1462. phpdbg_notice("Breaking for leave at %s:%u",
  1463. zend_get_executed_filename(),
  1464. zend_get_executed_lineno()
  1465. );
  1466. DO_INTERACTIVE(1);
  1467. } else {
  1468. /* skip possible breakpoints */
  1469. goto next;
  1470. }
  1471. }
  1472. }
  1473. if (PHPDBG_G(flags) & PHPDBG_IS_STEPPING && (PHPDBG_G(flags) & PHPDBG_STEP_OPCODE || execute_data->opline->lineno != PHPDBG_G(last_line))) {
  1474. stepping:
  1475. PHPDBG_G(flags) &= ~PHPDBG_IS_STEPPING;
  1476. DO_INTERACTIVE(1);
  1477. }
  1478. /* check if some watchpoint was hit */
  1479. {
  1480. if (phpdbg_print_changed_zvals() == SUCCESS) {
  1481. DO_INTERACTIVE(1);
  1482. }
  1483. }
  1484. /* search for breakpoints */
  1485. {
  1486. phpdbg_breakbase_t *brake;
  1487. if ((PHPDBG_G(flags) & PHPDBG_BP_MASK)
  1488. && (brake = phpdbg_find_breakpoint(execute_data))
  1489. && (brake->type != PHPDBG_BREAK_FILE || execute_data->opline->lineno != PHPDBG_G(last_line))) {
  1490. phpdbg_hit_breakpoint(brake, 1);
  1491. DO_INTERACTIVE(1);
  1492. }
  1493. }
  1494. if (PHPDBG_G(flags) & PHPDBG_IS_SIGNALED) {
  1495. PHPDBG_G(flags) &= ~PHPDBG_IS_SIGNALED;
  1496. phpdbg_out("\n");
  1497. phpdbg_notice("Program received signal SIGINT");
  1498. DO_INTERACTIVE(1);
  1499. }
  1500. next:
  1501. PHPDBG_G(last_line) = execute_data->opline->lineno;
  1502. /* stupid hack to make zend_do_fcall_common_helper return ZEND_VM_ENTER() instead of recursively calling zend_execute() and eventually segfaulting */
  1503. if ((execute_data->opline->opcode == ZEND_DO_FCALL ||
  1504. execute_data->opline->opcode == ZEND_DO_UCALL ||
  1505. execute_data->opline->opcode == ZEND_DO_FCALL_BY_NAME) &&
  1506. execute_data->call->func->type == ZEND_USER_FUNCTION) {
  1507. zend_execute_ex = execute_ex;
  1508. }
  1509. PHPDBG_G(vmret) = zend_vm_call_opcode_handler(execute_data);
  1510. zend_execute_ex = phpdbg_execute_ex;
  1511. if (PHPDBG_G(vmret) != 0) {
  1512. if (PHPDBG_G(vmret) < 0) {
  1513. PHPDBG_G(in_execution) = original_in_execution;
  1514. return;
  1515. } else {
  1516. execute_data = EG(current_execute_data);
  1517. }
  1518. }
  1519. }
  1520. zend_error_noreturn(E_ERROR, "Arrived at end of main loop which shouldn't happen");
  1521. } /* }}} */
  1522. /* only if *not* interactive and while executing */
  1523. void phpdbg_force_interruption(void) /* {{{ */ {
  1524. zend_object *exception = EG(exception);
  1525. zend_execute_data *data = EG(current_execute_data); /* should be always readable if not NULL */
  1526. PHPDBG_G(flags) |= PHPDBG_IN_SIGNAL_HANDLER;
  1527. if (data) {
  1528. if (data->func) {
  1529. if (ZEND_USER_CODE(data->func->type)) {
  1530. phpdbg_notice("Current opline: %p (op #%u) in %s:%u",
  1531. data->opline,
  1532. (uint32_t) (data->opline - data->func->op_array.opcodes),
  1533. data->func->op_array.filename->val,
  1534. data->opline->lineno);
  1535. } else if (data->func->internal_function.function_name) {
  1536. phpdbg_notice("Current opline: in internal function %s",
  1537. data->func->internal_function.function_name->val);
  1538. } else {
  1539. phpdbg_notice("Current opline: executing internal code");
  1540. }
  1541. } else {
  1542. phpdbg_notice("Current opline: %p (op_array information unavailable)",
  1543. data->opline);
  1544. }
  1545. } else {
  1546. phpdbg_notice("No information available about executing context");
  1547. }
  1548. DO_INTERACTIVE(0);
  1549. next:
  1550. PHPDBG_G(flags) &= ~PHPDBG_IN_SIGNAL_HANDLER;
  1551. if (PHPDBG_G(flags) & PHPDBG_IS_STOPPING) {
  1552. zend_bailout();
  1553. }
  1554. }
  1555. /* }}} */