phpdbg_cmd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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 "phpdbg.h"
  21. #include "phpdbg_cmd.h"
  22. #include "phpdbg_utils.h"
  23. #include "phpdbg_set.h"
  24. #include "phpdbg_prompt.h"
  25. #include "phpdbg_io.h"
  26. ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
  27. static inline const char *phpdbg_command_name(const phpdbg_command_t *command, char *buffer) {
  28. size_t pos = 0;
  29. if (command->parent) {
  30. memcpy(&buffer[pos], command->parent->name, command->parent->name_len);
  31. pos += command->parent->name_len;
  32. memcpy(&buffer[pos], " ", sizeof(" ")-1);
  33. pos += (sizeof(" ")-1);
  34. }
  35. memcpy(&buffer[pos], command->name, command->name_len);
  36. pos += command->name_len;
  37. buffer[pos] = 0;
  38. return buffer;
  39. }
  40. PHPDBG_API const char *phpdbg_get_param_type(const phpdbg_param_t *param) /* {{{ */
  41. {
  42. switch (param->type) {
  43. case STACK_PARAM:
  44. return "stack";
  45. case EMPTY_PARAM:
  46. return "empty";
  47. case ADDR_PARAM:
  48. return "address";
  49. case NUMERIC_PARAM:
  50. return "numeric";
  51. case METHOD_PARAM:
  52. return "method";
  53. case NUMERIC_FUNCTION_PARAM:
  54. return "function opline";
  55. case NUMERIC_METHOD_PARAM:
  56. return "method opline";
  57. case FILE_PARAM:
  58. return "file or file opline";
  59. case STR_PARAM:
  60. return "string";
  61. default: /* this is bad */
  62. return "unknown";
  63. }
  64. }
  65. PHPDBG_API void phpdbg_clear_param(phpdbg_param_t *param) /* {{{ */
  66. {
  67. if (param) {
  68. switch (param->type) {
  69. case FILE_PARAM:
  70. efree(param->file.name);
  71. break;
  72. case METHOD_PARAM:
  73. efree(param->method.class);
  74. efree(param->method.name);
  75. break;
  76. case STR_PARAM:
  77. efree(param->str);
  78. break;
  79. default:
  80. break;
  81. }
  82. }
  83. } /* }}} */
  84. PHPDBG_API char* phpdbg_param_tostring(const phpdbg_param_t *param, char **pointer) /* {{{ */
  85. {
  86. switch (param->type) {
  87. case STR_PARAM:
  88. ZEND_IGNORE_VALUE(asprintf(pointer, "%s", param->str));
  89. break;
  90. case ADDR_PARAM:
  91. ZEND_IGNORE_VALUE(asprintf(pointer, ZEND_ULONG_FMT, param->addr));
  92. break;
  93. case NUMERIC_PARAM:
  94. ZEND_IGNORE_VALUE(asprintf(pointer, "%li", param->num));
  95. break;
  96. case METHOD_PARAM:
  97. ZEND_IGNORE_VALUE(asprintf(pointer, "%s::%s", param->method.class, param->method.name));
  98. break;
  99. case FILE_PARAM:
  100. if (param->num) {
  101. ZEND_IGNORE_VALUE(asprintf(pointer, "%s:%lu#%lu", param->file.name, param->file.line, param->num));
  102. } else {
  103. ZEND_IGNORE_VALUE(asprintf(pointer, "%s:%lu", param->file.name, param->file.line));
  104. }
  105. break;
  106. case NUMERIC_FUNCTION_PARAM:
  107. ZEND_IGNORE_VALUE(asprintf(pointer, "%s#%lu", param->str, param->num));
  108. break;
  109. case NUMERIC_METHOD_PARAM:
  110. ZEND_IGNORE_VALUE(asprintf(pointer, "%s::%s#%lu", param->method.class, param->method.name, param->num));
  111. break;
  112. default:
  113. *pointer = strdup("unknown");
  114. }
  115. return *pointer;
  116. } /* }}} */
  117. PHPDBG_API void phpdbg_copy_param(const phpdbg_param_t* src, phpdbg_param_t* dest) /* {{{ */
  118. {
  119. switch ((dest->type = src->type)) {
  120. case STACK_PARAM:
  121. /* nope */
  122. break;
  123. case STR_PARAM:
  124. dest->str = estrndup(src->str, src->len);
  125. dest->len = src->len;
  126. break;
  127. case OP_PARAM:
  128. dest->str = estrndup(src->str, src->len);
  129. dest->len = src->len;
  130. break;
  131. case ADDR_PARAM:
  132. dest->addr = src->addr;
  133. break;
  134. case NUMERIC_PARAM:
  135. dest->num = src->num;
  136. break;
  137. case METHOD_PARAM:
  138. dest->method.class = estrdup(src->method.class);
  139. dest->method.name = estrdup(src->method.name);
  140. break;
  141. case NUMERIC_FILE_PARAM:
  142. case FILE_PARAM:
  143. dest->file.name = estrdup(src->file.name);
  144. dest->file.line = src->file.line;
  145. if (src->num)
  146. dest->num = src->num;
  147. break;
  148. case NUMERIC_FUNCTION_PARAM:
  149. dest->str = estrndup(src->str, src->len);
  150. dest->num = src->num;
  151. dest->len = src->len;
  152. break;
  153. case NUMERIC_METHOD_PARAM:
  154. dest->method.class = estrdup(src->method.class);
  155. dest->method.name = estrdup(src->method.name);
  156. dest->num = src->num;
  157. break;
  158. case EMPTY_PARAM: { /* do nothing */ } break;
  159. default: {
  160. /* not yet */
  161. }
  162. }
  163. } /* }}} */
  164. PHPDBG_API zend_ulong phpdbg_hash_param(const phpdbg_param_t *param) /* {{{ */
  165. {
  166. zend_ulong hash = param->type;
  167. switch (param->type) {
  168. case STACK_PARAM:
  169. /* nope */
  170. break;
  171. case STR_PARAM:
  172. hash += zend_inline_hash_func(param->str, param->len);
  173. break;
  174. case METHOD_PARAM:
  175. hash += zend_inline_hash_func(param->method.class, strlen(param->method.class));
  176. hash += zend_inline_hash_func(param->method.name, strlen(param->method.name));
  177. break;
  178. case FILE_PARAM:
  179. hash += zend_inline_hash_func(param->file.name, strlen(param->file.name));
  180. hash += param->file.line;
  181. if (param->num)
  182. hash += param->num;
  183. break;
  184. case ADDR_PARAM:
  185. hash += param->addr;
  186. break;
  187. case NUMERIC_PARAM:
  188. hash += param->num;
  189. break;
  190. case NUMERIC_FUNCTION_PARAM:
  191. hash += zend_inline_hash_func(param->str, param->len);
  192. hash += param->num;
  193. break;
  194. case NUMERIC_METHOD_PARAM:
  195. hash += zend_inline_hash_func(param->method.class, strlen(param->method.class));
  196. hash += zend_inline_hash_func(param->method.name, strlen(param->method.name));
  197. if (param->num)
  198. hash+= param->num;
  199. break;
  200. case EMPTY_PARAM: { /* do nothing */ } break;
  201. default: {
  202. /* not yet */
  203. }
  204. }
  205. return hash;
  206. } /* }}} */
  207. PHPDBG_API zend_bool phpdbg_match_param(const phpdbg_param_t *l, const phpdbg_param_t *r) /* {{{ */
  208. {
  209. if (l && r) {
  210. if (l->type == r->type) {
  211. switch (l->type) {
  212. case STACK_PARAM:
  213. /* nope, or yep */
  214. return 1;
  215. break;
  216. case NUMERIC_FUNCTION_PARAM:
  217. if (l->num != r->num) {
  218. break;
  219. }
  220. /* break intentionally omitted */
  221. case STR_PARAM:
  222. return (l->len == r->len) &&
  223. (memcmp(l->str, r->str, l->len) == SUCCESS);
  224. case NUMERIC_PARAM:
  225. return (l->num == r->num);
  226. case ADDR_PARAM:
  227. return (l->addr == r->addr);
  228. case FILE_PARAM: {
  229. if (l->file.line == r->file.line) {
  230. size_t lengths[2] = {
  231. strlen(l->file.name), strlen(r->file.name)};
  232. if (lengths[0] == lengths[1]) {
  233. if ((!l->num && !r->num) || (l->num == r->num)) {
  234. return (memcmp(
  235. l->file.name, r->file.name, lengths[0]) == SUCCESS);
  236. }
  237. }
  238. }
  239. } break;
  240. case NUMERIC_METHOD_PARAM:
  241. if (l->num != r->num) {
  242. break;
  243. }
  244. /* break intentionally omitted */
  245. case METHOD_PARAM: {
  246. size_t lengths[2] = {
  247. strlen(l->method.class), strlen(r->method.class)};
  248. if (lengths[0] == lengths[1]) {
  249. if (memcmp(l->method.class, r->method.class, lengths[0]) == SUCCESS) {
  250. lengths[0] = strlen(l->method.name);
  251. lengths[1] = strlen(r->method.name);
  252. if (lengths[0] == lengths[1]) {
  253. return (memcmp(
  254. l->method.name, r->method.name, lengths[0]) == SUCCESS);
  255. }
  256. }
  257. }
  258. } break;
  259. case EMPTY_PARAM:
  260. return 1;
  261. default: {
  262. /* not yet */
  263. }
  264. }
  265. }
  266. }
  267. return 0;
  268. } /* }}} */
  269. /* {{{ */
  270. PHPDBG_API void phpdbg_param_debug(const phpdbg_param_t *param, const char *msg) {
  271. if (param && param->type) {
  272. switch (param->type) {
  273. case STR_PARAM:
  274. fprintf(stderr, "%s STR_PARAM(%s=%zu)\n", msg, param->str, param->len);
  275. break;
  276. case ADDR_PARAM:
  277. fprintf(stderr, "%s ADDR_PARAM(" ZEND_ULONG_FMT ")\n", msg, param->addr);
  278. break;
  279. case NUMERIC_FILE_PARAM:
  280. fprintf(stderr, "%s NUMERIC_FILE_PARAM(%s:#%lu)\n", msg, param->file.name, param->file.line);
  281. break;
  282. case FILE_PARAM:
  283. fprintf(stderr, "%s FILE_PARAM(%s:%lu)\n", msg, param->file.name, param->file.line);
  284. break;
  285. case METHOD_PARAM:
  286. fprintf(stderr, "%s METHOD_PARAM(%s::%s)\n", msg, param->method.class, param->method.name);
  287. break;
  288. case NUMERIC_METHOD_PARAM:
  289. fprintf(stderr, "%s NUMERIC_METHOD_PARAM(%s::%s)\n", msg, param->method.class, param->method.name);
  290. break;
  291. case NUMERIC_FUNCTION_PARAM:
  292. fprintf(stderr, "%s NUMERIC_FUNCTION_PARAM(%s::%ld)\n", msg, param->str, param->num);
  293. break;
  294. case NUMERIC_PARAM:
  295. fprintf(stderr, "%s NUMERIC_PARAM(%ld)\n", msg, param->num);
  296. break;
  297. case COND_PARAM:
  298. fprintf(stderr, "%s COND_PARAM(%s=%zu)\n", msg, param->str, param->len);
  299. break;
  300. case OP_PARAM:
  301. fprintf(stderr, "%s OP_PARAM(%s=%zu)\n", msg, param->str, param->len);
  302. break;
  303. default: {
  304. /* not yet */
  305. }
  306. }
  307. }
  308. } /* }}} */
  309. /* {{{ */
  310. PHPDBG_API void phpdbg_stack_free(phpdbg_param_t *stack) {
  311. if (stack && stack->next) {
  312. phpdbg_param_t *remove = stack->next;
  313. while (remove) {
  314. phpdbg_param_t *next = NULL;
  315. if (remove->next)
  316. next = remove->next;
  317. switch (remove->type) {
  318. case NUMERIC_METHOD_PARAM:
  319. case METHOD_PARAM:
  320. if (remove->method.class) {
  321. efree(remove->method.class);
  322. }
  323. if (remove->method.name) {
  324. efree(remove->method.name);
  325. }
  326. break;
  327. case NUMERIC_FUNCTION_PARAM:
  328. case STR_PARAM:
  329. case OP_PARAM:
  330. case EVAL_PARAM:
  331. case SHELL_PARAM:
  332. case COND_PARAM:
  333. case RUN_PARAM:
  334. if (remove->str) {
  335. efree(remove->str);
  336. }
  337. break;
  338. case NUMERIC_FILE_PARAM:
  339. case FILE_PARAM:
  340. if (remove->file.name) {
  341. efree(remove->file.name);
  342. }
  343. break;
  344. default: {
  345. /* nothing */
  346. }
  347. }
  348. free(remove);
  349. remove = NULL;
  350. if (next)
  351. remove = next;
  352. else break;
  353. }
  354. }
  355. stack->next = NULL;
  356. } /* }}} */
  357. /* {{{ */
  358. PHPDBG_API void phpdbg_stack_push(phpdbg_param_t *stack, phpdbg_param_t *param) {
  359. phpdbg_param_t *next = calloc(1, sizeof(phpdbg_param_t));
  360. if (!next) {
  361. return;
  362. }
  363. *(next) = *(param);
  364. next->next = NULL;
  365. if (stack->top == NULL) {
  366. stack->top = next;
  367. next->top = NULL;
  368. stack->next = next;
  369. } else {
  370. stack->top->next = next;
  371. next->top = stack->top;
  372. stack->top = next;
  373. }
  374. stack->len++;
  375. } /* }}} */
  376. /* {{{ */
  377. PHPDBG_API void phpdbg_stack_separate(phpdbg_param_t *param) {
  378. phpdbg_param_t *stack = calloc(1, sizeof(phpdbg_param_t));
  379. stack->type = STACK_PARAM;
  380. stack->next = param->next;
  381. param->next = stack;
  382. stack->top = param->top;
  383. } /* }}} */
  384. PHPDBG_API int phpdbg_stack_verify(const phpdbg_command_t *command, phpdbg_param_t **stack) {
  385. if (command) {
  386. char buffer[128] = {0,};
  387. const phpdbg_param_t *top = (stack != NULL) ? *stack : NULL;
  388. const char *arg = command->args;
  389. size_t least = 0L,
  390. received = 0L,
  391. current = 0L;
  392. zend_bool optional = 0;
  393. /* check for arg spec */
  394. if (!(arg) || !(*arg)) {
  395. if (!top || top->type == STACK_PARAM) {
  396. return SUCCESS;
  397. }
  398. phpdbg_error("command", "type=\"toomanyargs\" command=\"%s\" expected=\"0\"", "The command \"%s\" expected no arguments",
  399. phpdbg_command_name(command, buffer));
  400. return FAILURE;
  401. }
  402. least = 0L;
  403. /* count least amount of arguments */
  404. while (arg && *arg) {
  405. if (arg[0] == '|') {
  406. break;
  407. }
  408. least++;
  409. arg++;
  410. }
  411. arg = command->args;
  412. #define verify_arg(e, a, t) if (!(a)) { \
  413. if (!optional) { \
  414. phpdbg_error("command", "type=\"noarg\" command=\"%s\" expected=\"%s\" num=\"%lu\"", "The command \"%s\" expected %s and got nothing at parameter %lu", \
  415. phpdbg_command_name(command, buffer), \
  416. (e), \
  417. current); \
  418. return FAILURE;\
  419. } \
  420. } else if ((a)->type != (t)) { \
  421. phpdbg_error("command", "type=\"wrongarg\" command=\"%s\" expected=\"%s\" got=\"%s\" num=\"%lu\"", "The command \"%s\" expected %s and got %s at parameter %lu", \
  422. phpdbg_command_name(command, buffer), \
  423. (e),\
  424. phpdbg_get_param_type((a)), \
  425. current); \
  426. return FAILURE; \
  427. }
  428. while (arg && *arg) {
  429. if (top && top->type == STACK_PARAM) {
  430. break;
  431. }
  432. current++;
  433. switch (*arg) {
  434. case '|': {
  435. current--;
  436. optional = 1;
  437. arg++;
  438. } continue;
  439. case 'i': verify_arg("raw input", top, STR_PARAM); break;
  440. case 's': verify_arg("string", top, STR_PARAM); break;
  441. case 'n': verify_arg("number", top, NUMERIC_PARAM); break;
  442. case 'm': verify_arg("method", top, METHOD_PARAM); break;
  443. case 'a': verify_arg("address", top, ADDR_PARAM); break;
  444. case 'f': verify_arg("file:line", top, FILE_PARAM); break;
  445. case 'c': verify_arg("condition", top, COND_PARAM); break;
  446. case 'o': verify_arg("opcode", top, OP_PARAM); break;
  447. case 'b': verify_arg("boolean", top, NUMERIC_PARAM); break;
  448. case '*': { /* do nothing */ } break;
  449. }
  450. if (top) {
  451. top = top->next;
  452. } else {
  453. break;
  454. }
  455. received++;
  456. arg++;
  457. }
  458. #undef verify_arg
  459. if ((received < least)) {
  460. phpdbg_error("command", "type=\"toofewargs\" command=\"%s\" expected=\"%d\" argtypes=\"%s\" got=\"%d\"", "The command \"%s\" expected at least %lu arguments (%s) and received %lu",
  461. phpdbg_command_name(command, buffer),
  462. least,
  463. command->args,
  464. received);
  465. return FAILURE;
  466. }
  467. }
  468. return SUCCESS;
  469. }
  470. /* {{{ */
  471. PHPDBG_API const phpdbg_command_t *phpdbg_stack_resolve(const phpdbg_command_t *commands, const phpdbg_command_t *parent, phpdbg_param_t **top) {
  472. const phpdbg_command_t *command = commands;
  473. phpdbg_param_t *name = *top;
  474. const phpdbg_command_t *matched[3] = {NULL, NULL, NULL};
  475. ulong matches = 0L;
  476. while (command && command->name && command->handler) {
  477. if (name->len == 1 || command->name_len >= name->len) {
  478. /* match single letter alias */
  479. if (command->alias && (name->len == 1)) {
  480. if (command->alias == (*name->str)) {
  481. matched[matches] = command;
  482. matches++;
  483. }
  484. } else {
  485. /* match full, case insensitive, command name */
  486. if (strncasecmp(command->name, name->str, name->len) == SUCCESS) {
  487. if (matches < 3) {
  488. /* only allow abbreviating commands that can be aliased */
  489. if ((name->len != command->name_len && command->alias) || name->len == command->name_len) {
  490. matched[matches] = command;
  491. matches++;
  492. }
  493. /* exact match */
  494. if (name->len == command->name_len) {
  495. break;
  496. }
  497. } else {
  498. break;
  499. }
  500. }
  501. }
  502. }
  503. command++;
  504. }
  505. switch (matches) {
  506. case 0:
  507. if (parent) {
  508. phpdbg_error("command", "type=\"notfound\" command=\"%s\" subcommand=\"%s\"", "The command \"%s %s\" could not be found", parent->name, name->str);
  509. } else {
  510. phpdbg_error("command", "type=\"notfound\" command=\"%s\"", "The command \"%s\" could not be found", name->str);
  511. }
  512. return parent;
  513. case 1:
  514. (*top) = (*top)->next;
  515. command = matched[0];
  516. break;
  517. default: {
  518. char *list = NULL;
  519. uint32_t it = 0;
  520. size_t pos = 0;
  521. while (it < matches) {
  522. if (!list) {
  523. list = emalloc(matched[it]->name_len + 1 + (it + 1 < matches ? sizeof(", ") - 1 : 0));
  524. } else {
  525. list = erealloc(list, (pos + matched[it]->name_len) + 1 + (it + 1 < matches ? sizeof(", ") - 1 : 0));
  526. }
  527. memcpy(&list[pos], matched[it]->name, matched[it]->name_len);
  528. pos += matched[it]->name_len;
  529. if ((it + 1) < matches) {
  530. memcpy(&list[pos], ", ", sizeof(", ") - 1);
  531. pos += (sizeof(", ") - 1);
  532. }
  533. list[pos] = 0;
  534. it++;
  535. }
  536. /* ", " separated matches */
  537. phpdbg_error("command", "type=\"ambiguous\" command=\"%s\" matches=\"%lu\" matched=\"%s\"", "The command \"%s\" is ambigious, matching %lu commands (%s)", name->str, matches, list);
  538. efree(list);
  539. return NULL;
  540. }
  541. }
  542. if (command->subs && (*top) && ((*top)->type == STR_PARAM)) {
  543. return phpdbg_stack_resolve(command->subs, command, top);
  544. } else {
  545. return command;
  546. }
  547. return NULL;
  548. } /* }}} */
  549. static int phpdbg_internal_stack_execute(phpdbg_param_t *stack, zend_bool allow_async_unsafe) {
  550. const phpdbg_command_t *handler = NULL;
  551. phpdbg_param_t *top = (phpdbg_param_t *) stack->next;
  552. switch (top->type) {
  553. case EVAL_PARAM:
  554. phpdbg_activate_err_buf(0);
  555. phpdbg_free_err_buf();
  556. return PHPDBG_COMMAND_HANDLER(ev)(top);
  557. case RUN_PARAM:
  558. if (!allow_async_unsafe) {
  559. phpdbg_error("signalsegv", "command=\"run\"", "run command is disallowed during hard interrupt");
  560. }
  561. phpdbg_activate_err_buf(0);
  562. phpdbg_free_err_buf();
  563. return PHPDBG_COMMAND_HANDLER(run)(top);
  564. case SHELL_PARAM:
  565. if (!allow_async_unsafe) {
  566. phpdbg_error("signalsegv", "command=\"sh\"", "sh command is disallowed during hard interrupt");
  567. return FAILURE;
  568. }
  569. phpdbg_activate_err_buf(0);
  570. phpdbg_free_err_buf();
  571. return PHPDBG_COMMAND_HANDLER(sh)(top);
  572. case STR_PARAM: {
  573. handler = phpdbg_stack_resolve(phpdbg_prompt_commands, NULL, &top);
  574. if (handler) {
  575. if (!allow_async_unsafe && !(handler->flags & PHPDBG_ASYNC_SAFE)) {
  576. phpdbg_error("signalsegv", "command=\"%s\"", "%s command is disallowed during hard interrupt", handler->name);
  577. return FAILURE;
  578. }
  579. if (phpdbg_stack_verify(handler, &top) == SUCCESS) {
  580. phpdbg_activate_err_buf(0);
  581. phpdbg_free_err_buf();
  582. return handler->handler(top);
  583. }
  584. }
  585. } return FAILURE;
  586. default:
  587. phpdbg_error("command", "type=\"invalidcommand\"", "The first parameter makes no sense !");
  588. return FAILURE;
  589. }
  590. return SUCCESS;
  591. } /* }}} */
  592. /* {{{ */
  593. PHPDBG_API int phpdbg_stack_execute(phpdbg_param_t *stack, zend_bool allow_async_unsafe) {
  594. phpdbg_param_t *top = stack;
  595. if (stack->type != STACK_PARAM) {
  596. phpdbg_error("command", "type=\"nostack\"", "The passed argument was not a stack !");
  597. return FAILURE;
  598. }
  599. if (!stack->len) {
  600. phpdbg_error("command", "type=\"emptystack\"", "The stack contains nothing !");
  601. return FAILURE;
  602. }
  603. do {
  604. if (top->type == STACK_PARAM) {
  605. int result;
  606. if ((result = phpdbg_internal_stack_execute(top, allow_async_unsafe)) != SUCCESS) {
  607. return result;
  608. }
  609. }
  610. } while ((top = top->next));
  611. return SUCCESS;
  612. } /* }}} */
  613. PHPDBG_API char *phpdbg_read_input(char *buffered) /* {{{ */
  614. {
  615. char buf[PHPDBG_MAX_CMD];
  616. char *cmd = NULL;
  617. char *buffer = NULL;
  618. if ((PHPDBG_G(flags) & (PHPDBG_IS_STOPPING | PHPDBG_IS_RUNNING)) != PHPDBG_IS_STOPPING) {
  619. if ((PHPDBG_G(flags) & PHPDBG_IS_REMOTE) && (buffered == NULL) && !phpdbg_active_sigsafe_mem()) {
  620. fflush(PHPDBG_G(io)[PHPDBG_STDOUT].ptr);
  621. }
  622. if (buffered == NULL) {
  623. #ifdef HAVE_PHPDBG_READLINE
  624. /* note: EOF makes readline write prompt again in local console mode - and ignored if compiled without readline */
  625. if ((PHPDBG_G(flags) & PHPDBG_IS_REMOTE) || !isatty(PHPDBG_G(io)[PHPDBG_STDIN].fd))
  626. #endif
  627. {
  628. phpdbg_write("prompt", "", "%s", phpdbg_get_prompt());
  629. phpdbg_consume_stdin_line(cmd = buf);
  630. }
  631. #ifdef HAVE_PHPDBG_READLINE
  632. else {
  633. cmd = readline(phpdbg_get_prompt());
  634. PHPDBG_G(last_was_newline) = 1;
  635. if (!cmd) {
  636. PHPDBG_G(flags) |= PHPDBG_IS_QUITTING | PHPDBG_IS_DISCONNECTED;
  637. zend_bailout();
  638. }
  639. add_history(cmd);
  640. }
  641. #endif
  642. } else {
  643. cmd = buffered;
  644. }
  645. buffer = estrdup(cmd);
  646. #ifdef HAVE_PHPDBG_READLINE
  647. if (!buffered && cmd && !(PHPDBG_G(flags) & PHPDBG_IS_REMOTE) && isatty(PHPDBG_G(io)[PHPDBG_STDIN].fd)) {
  648. free(cmd);
  649. }
  650. #endif
  651. }
  652. if (buffer && isspace(*buffer)) {
  653. char *trimmed = buffer;
  654. while (isspace(*trimmed))
  655. trimmed++;
  656. trimmed = estrdup(trimmed);
  657. efree(buffer);
  658. buffer = trimmed;
  659. }
  660. if (buffer && strlen(buffer)) {
  661. if (PHPDBG_G(buffer)) {
  662. free(PHPDBG_G(buffer));
  663. }
  664. PHPDBG_G(buffer) = strdup(buffer);
  665. } else if (PHPDBG_G(buffer)) {
  666. if (buffer) {
  667. efree(buffer);
  668. }
  669. buffer = estrdup(PHPDBG_G(buffer));
  670. }
  671. return buffer;
  672. } /* }}} */
  673. PHPDBG_API void phpdbg_destroy_input(char **input) /*{{{ */
  674. {
  675. efree(*input);
  676. } /* }}} */
  677. PHPDBG_API int phpdbg_ask_user_permission(const char *question) {
  678. if (!(PHPDBG_G(flags) & PHPDBG_WRITE_XML)) {
  679. char buf[PHPDBG_MAX_CMD];
  680. phpdbg_out("%s", question);
  681. phpdbg_out(" (type y or n): ");
  682. while (1) {
  683. phpdbg_consume_stdin_line(buf);
  684. if (buf[1] == '\n' && (buf[0] == 'y' || buf[0] == 'n')) {
  685. if (buf[0] == 'y') {
  686. return SUCCESS;
  687. }
  688. return FAILURE;
  689. }
  690. phpdbg_out("Please enter either y (yes) or n (no): ");
  691. }
  692. }
  693. return SUCCESS;
  694. }