phpdbg_prompt.c 57 KB

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