do_comm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /* $Id$ */
  2. /* Implementation of the do_ routines */
  3. #include <stdio.h>
  4. #include <assert.h>
  5. #include <alloc.h>
  6. #include "operator.h"
  7. #include "position.h"
  8. #include "tree.h"
  9. #include "idf.h"
  10. #include "Lpars.h"
  11. #include "type.h"
  12. #include "expr.h"
  13. #include "symbol.h"
  14. #include "scope.h"
  15. #include "file.h"
  16. #include "misc.h"
  17. extern FILE *db_out;
  18. extern t_lineno listline, currline;
  19. extern int interrupted;
  20. extern int stack_offset;
  21. p_tree print_command;
  22. extern void set_bytes();
  23. /*ARGSUSED*/
  24. do_noop(p)
  25. p_tree p;
  26. {
  27. }
  28. /* ------------------------------------------------------------- */
  29. /* implementation of the help command */
  30. do_help(p)
  31. p_tree p;
  32. {
  33. p = p->t_args[0];
  34. if (p && p->t_idf) switch(p->t_idf->id_reserved) {
  35. case HELP:
  36. fputs("help [ <commandname> ]\n", db_out);
  37. fputs("? [ <commandname> ]\n", db_out);
  38. fputs(" Print a command summary, or some more help on <commandname>.\n", db_out);
  39. return;
  40. case LIST:
  41. fputs("list [ <start> | <func> ] [ , <cnt> | - [ <end> ] ]\n", db_out);
  42. fputs("l [ <start> | <func> ] [ , <cnt> | - [ <end> ] ]\n", db_out);
  43. fputs(" List lines from the current source file, starting with either\n", db_out);
  44. fputs(" line <start> or some lines before the first statement of <func> or\n", db_out);
  45. fputs(" the current line. Either list <cnt> lines or <wsize> lines,\n", db_out);
  46. fputs(" except when a range is given.\n", db_out);
  47. fputs(" <wsize> is the last <cnt> given, or 10.\n", db_out);
  48. return;
  49. case XFILE:
  50. fputs("file [ <name> | ? ]\n", db_out);
  51. fputs(" Print the name of the current source file, or change the\n", db_out);
  52. fputs(" current source file to <name>, or print the files that\n", db_out);
  53. fputs(" the debugger knows about.\n", db_out);
  54. return;
  55. case SOURCE:
  56. fputs("source <filename>\n", db_out);
  57. fputs(" Read commands from the file <filename>\n", db_out);
  58. return;
  59. case FRAME:
  60. fputs("frame [ [ + | - ] <num> ]\n", db_out);
  61. fputs(" Sets the 'current' frame to frame <num>. The currently active\n", db_out);
  62. fputs(" procedure has frame 0. If <num> is not given, print the 'current' frame.\n", db_out);
  63. fputs(" If <num> is given with a + or -, go up or down <num> frames relative\n", db_out);
  64. fputs(" to the current one.\n", db_out);
  65. return;
  66. case LOG:
  67. fputs("log [ <name> | off ]\n", db_out);
  68. fputs(" Creates a logfile <name> of the commands given.\n", db_out);
  69. fputs(" When no argument is given, the current logfile is printed.\n", db_out);
  70. fputs(" If the argument is 'off', logging is turned off.\n", db_out);
  71. return;
  72. case RUN:
  73. fputs("run [ <args> ] [ < <infile> ] [ > <outfile> ]\n", db_out);
  74. fputs(" Start executing debuggee with command line arguments <args> and\n", db_out);
  75. fputs(" possible redirection of standard input and/or standard output.\n", db_out);
  76. return;
  77. case RERUN:
  78. fputs("rerun [ ? ]\n", db_out);
  79. fputs(" If the ? is given, prints the last run command;\n", db_out);
  80. fputs(" otherwise repeats the last run command.\n", db_out);
  81. return;
  82. case STOP:
  83. fputs("stop [ <pos> ] [ if <cond> ]\n", db_out);
  84. fputs(" Stop execution when position <pos> is reached, and then when\n", db_out);
  85. fputs(" <cond> becomes true. If no <pos> is given, stop when <cond>\n", db_out);
  86. fputs(" becomes true. If no <cond> is given, stop when <pos> is reached.\n", db_out);
  87. fputs(" Either a position or a condition (or both) must be given.\n", db_out);
  88. return;
  89. case WHEN:
  90. fputs("when [ <pos> ] [ if <cond> ] { <command> [ ; <command> ] ... } \n", db_out);
  91. fputs(" Execute the <command>s when position <pos> is reached, and then when\n", db_out);
  92. fputs(" <cond> becomes true. If no <pos> is given, do this when <cond>\n", db_out);
  93. fputs(" becomes true. If no <cond> is given, do this when <pos> is reached.\n", db_out);
  94. fputs(" Either a position or a condition (or both) must be given.\n", db_out);
  95. return;
  96. case CONT:
  97. fputs("cont [ <cnt> ] [ at <line> ]\n", db_out);
  98. fputs("c [ <cnt> ] [ at <line> ]\n", db_out);
  99. fputs(" Continue execution, skipping <cnt> or 1 breakpoints;a\n", db_out);
  100. fputs(" if <line> is given, continue at <line>.\n", db_out);
  101. return;
  102. case STEP:
  103. case NEXT:
  104. fputs("step [ <cnt> ]\n", db_out);
  105. fputs("s [ <cnt> ]\n", db_out);
  106. fputs("next [ <cnt> ]\n", db_out);
  107. fputs("n [ <cnt> ]\n", db_out);
  108. fputs(" Execute the next <cnt> or 1 source line(s).\n", db_out);
  109. fputs(" Step (s) steps into function-calls.\n", db_out);
  110. fputs(" Next (n) steps past function-calls.\n", db_out);
  111. return;
  112. case WHERE:
  113. fputs("where [ <cnt> ]\n", db_out);
  114. fputs("w [ <cnt> ]\n", db_out);
  115. fputs(" List all, or the top <cnt> or the bottom -<cnt> active functions.\n", db_out);
  116. return;
  117. case STATUS:
  118. fputs("status\n", db_out);
  119. fputs(" display active traces, stops, whens, displays, and dumps.\n", db_out);
  120. return;
  121. case DELETE:
  122. fputs("delete [ <num> [ , <num> ] ... ]\n", db_out);
  123. fputs("d [ <num> [ , <num> ] ...] \n", db_out);
  124. fputs(" Remove the command(s) corresponding to <num> (as displayed by 'status').\n", db_out);
  125. fputs(" If no <num> is given, remove the current stopping point.\n", db_out);
  126. return;
  127. case SET:
  128. fputs("set <desig> to <exp>\n", db_out);
  129. fputs(" Assign the value of <exp> to <desig>.\n", db_out);
  130. return;
  131. case PRINT:
  132. fputs("print [ <exp> [ , <exp> ] ...]\n", db_out);
  133. fputs("p [ <exp> [ , <exp> ] ...]\n", db_out);
  134. fputs(" Print the value of each <exp>, or repeat the last print command\n", db_out);
  135. return;
  136. case DISPLAY:
  137. fputs("display <exp> [ , <exp> ] ...\n", db_out);
  138. fputs(" Print the value of each <exp> whenever the debuggee stops.\n", db_out);
  139. return;
  140. case DUMP:
  141. fputs("dump\n", db_out);
  142. fputs(" Saves the state of the debuggee; it can be restored with the restore command.\n", db_out);
  143. return;
  144. case RESTORE:
  145. fputs("restore [ <num> ]\n", db_out);
  146. fputs("r [ <num> ]\n", db_out);
  147. fputs(" Restore the state of the dump associated with <num>,\n", db_out);
  148. fputs(" or restore the state of the last dump.\n", db_out);
  149. return;
  150. case TRACE:
  151. fputs("trace [ on <exp> ] [ <pos> ] [ if <cond> ]\n", db_out);
  152. fputs("t [ on <exp> ] [ <pos> ] [ if <cond> ]\n", db_out);
  153. fputs(" Without args, display each source line before execution.\n", db_out);
  154. fputs(" In addition, display <exp> in the on-clause.\n", db_out);
  155. fputs(" If <pos> is given and indicates a function, only display\n", db_out);
  156. fputs(" tracing information while executing this function.\n", db_out);
  157. fputs(" If it indicates a line number, only display tracing information\n", db_out);
  158. fputs(" whenever the source line is reached.\n", db_out);
  159. fputs(" If <cond> is given, only display tracing info when it evaluates to non-zero.\n", db_out);
  160. return;
  161. case FIND:
  162. fputs("find <name>\n", db_out);
  163. fputs(" Prints the fully qualified name of all symbols matching <name>.\n", db_out);
  164. return;
  165. case WHICH:
  166. fputs("which <name>\n", db_out);
  167. fputs(" Prints the fully qualified name of <name>.\n", db_out);
  168. return;
  169. case DISABLE:
  170. fputs("disable [ <num> [ , <num> ] ... ]\n", db_out);
  171. fputs(" Disable the command(s) corresponding to <num> (as displayed by 'status').\n", db_out);
  172. fputs(" If no <num> is given, disable the current stopping point.\n", db_out);
  173. return;
  174. case ENABLE:
  175. fputs("enable [ <num> [ , <num> ] ... ]\n", db_out);
  176. fputs(" Enable the command(s) corresponding to <num> (as displayed by 'status'.)\n", db_out);
  177. fputs(" If no <num> is given, enable the current stopping point (not effective).\n", db_out);
  178. return;
  179. }
  180. else if (p && p->t_str) {
  181. if (! strcmp(p->t_str, "!")) {
  182. fputs("! <shell command>\n", db_out);
  183. fputs(" Execute the given shell command.\n", db_out);
  184. return;
  185. }
  186. }
  187. fputs("cont [ <cnt> ] [ at <line> ]\n", db_out);
  188. fputs("delete [ <num> [ , <num> ] ... ]\n", db_out);
  189. fputs("disable [ <num> [ , <num> ] ... ]\n", db_out);
  190. fputs("display <exp> [ , <exp> ] ...\n", db_out);
  191. fputs("dump\n", db_out);
  192. fputs("enable [ <num> [ , <num> ] ... ]\n", db_out);
  193. fputs("file [ <name> | ? ]\n", db_out);
  194. fputs("find <name>\n", db_out);
  195. fputs("frame [ [ + | - ] <num> ]\n", db_out);
  196. fputs("help [ <commandname> ]\n", db_out);
  197. fputs("list [ <start> | <func> ] [ , <cnt> | - [ <end> ] ]\n", db_out);
  198. fputs("log [ <name> | off ]\n", db_out);
  199. fputs("next [ <cnt> ]\n", db_out);
  200. fputs("print [ <exp> [ , <exp> ] ... ]\n", db_out);
  201. fputs("rerun [ ? ]\n", db_out);
  202. fputs("restore [ <num> ]\n", db_out);
  203. fputs("run [ <args> ] [ < <infile> ] [ > <outfile> ]\n", db_out);
  204. fputs("set <desig> to <exp>\n", db_out);
  205. fputs("source <filename>\n", db_out);
  206. fputs("status\n", db_out);
  207. fputs("step [ <cnt> ]\n", db_out);
  208. fputs("stop [ <pos> ] [ if <cond> ]\n", db_out);
  209. fputs("trace [ on <exp> ] [ <pos> ] [ if <cond> ]\n", db_out);
  210. fputs("when [ <pos> ] [ if <cond> ] { <command> [ ; <command> ] ... } \n", db_out);
  211. fputs("where [ <cnt> ]\n", db_out);
  212. fputs("which <name>\n", db_out);
  213. fputs("! <shell command>\n", db_out);
  214. }
  215. /* ------------------------------------------------------------- */
  216. /* implementation of dump/restore commands */
  217. extern p_tree get_from_item_list();
  218. extern t_addr get_dump();
  219. struct dump {
  220. char *globals, *stack;
  221. struct dump *next;
  222. };
  223. static struct dump *last_dump;
  224. do_dump(p)
  225. p_tree p;
  226. {
  227. struct dump *d = (struct dump *) malloc(sizeof(struct dump));
  228. if (! d) {
  229. error("could not allocate enough memory");
  230. return;
  231. }
  232. p->t_address = get_dump(&d->globals, &d->stack);
  233. if (! p->t_address) {
  234. free((char *) d);
  235. return;
  236. }
  237. p->t_args[0] = (struct tree *) d;
  238. add_to_item_list(p);
  239. d->next = last_dump;
  240. last_dump = d;
  241. }
  242. do_restore(p)
  243. p_tree p;
  244. {
  245. struct dump *d;
  246. if (p->t_args[0]) {
  247. p = get_from_item_list((int) p->t_args[0]->t_ival);
  248. if (!p || p->t_oper != OP_DUMP) {
  249. error("no such dump");
  250. return;
  251. }
  252. d = (struct dump *) p->t_args[0];
  253. }
  254. else d = last_dump;
  255. if (! d) {
  256. error("no dumps");
  257. return;
  258. }
  259. if (! put_dump(d->globals, d->stack)) {
  260. error("restoring failed");
  261. }
  262. perform_items();
  263. }
  264. free_dump(p)
  265. p_tree p;
  266. {
  267. struct dump *d = (struct dump *) p->t_args[0];
  268. free(d->globals);
  269. free(d->stack);
  270. if (d == last_dump) last_dump = d->next;
  271. else {
  272. register struct dump *d1 = last_dump;
  273. while (d1->next != d) d1 = d1->next;
  274. d1->next = d->next;
  275. }
  276. free((char *) d);
  277. }
  278. /* ------------------------------------------------------------- */
  279. /* implementation of the find command */
  280. do_find(p)
  281. p_tree p;
  282. {
  283. /* Print all identifications of p->t_args[0]. */
  284. register p_symbol s;
  285. p_tree arg;
  286. p = p->t_args[0];
  287. switch(p->t_oper) {
  288. case OP_NAME:
  289. s = p->t_idf->id_def;
  290. while (s) {
  291. pr_sym(s);
  292. s = s->sy_next;
  293. }
  294. break;
  295. case OP_SELECT:
  296. arg = p->t_args[1];
  297. assert(arg->t_oper == OP_NAME);
  298. s = arg->t_idf->id_def;
  299. while (s) {
  300. if (consistent(p, s->sy_scope)) {
  301. pr_sym(s);
  302. }
  303. s = s->sy_next;
  304. }
  305. break;
  306. default:
  307. assert(0);
  308. }
  309. }
  310. /* ------------------------------------------------------------- */
  311. /* implementation of the which command */
  312. do_which(p)
  313. p_tree p;
  314. {
  315. p_symbol sym = identify(p->t_args[0], 0xffff);
  316. if ( sym) pr_sym(sym);
  317. }
  318. /* ------------------------------------------------------------- */
  319. /* implementation of the list command */
  320. extern t_addr get_addr_from_node();
  321. do_list(p)
  322. p_tree p;
  323. {
  324. int l1, l2;
  325. static int wsize = 10;
  326. if (p->t_args[1]) {
  327. l2 = p->t_args[1]->t_ival;
  328. if (l2 >= 0) {
  329. if (l2 == 0) l2 = 1;
  330. wsize = l2;
  331. }
  332. }
  333. else l2 = wsize;
  334. if (! p->t_args[0]) {
  335. l1 = listline;
  336. if (! l1) {
  337. listline = currline - (wsize/2);
  338. l1 = listline;
  339. }
  340. }
  341. else {
  342. if (p->t_args[0]->t_oper == OP_AT) {
  343. l1 = p->t_args[0]->t_lino;
  344. if (p->t_args[0]->t_filename) {
  345. newfile(str2idf(p->t_args[0]->t_filename, 0));
  346. }
  347. }
  348. else {
  349. t_addr a = get_addr_from_node(p->t_args[0]);
  350. p_position pos;
  351. p_symbol oldlistfile = listfile;
  352. if (a == ILL_ADDR) {
  353. return;
  354. }
  355. pos = get_position_from_addr(a);
  356. newfile(str2idf(pos->filename, 1));
  357. if (listfile != oldlistfile) {
  358. warning("switching to file %s", listfile->sy_idf->id_text);
  359. }
  360. l1 = pos->lineno - (l2 > 0 ? l2 : wsize)/2;
  361. if (l1 < 1) l1 = 1;
  362. }
  363. }
  364. if (listfile) {
  365. if (l2 < 0) {
  366. l2 = -l2;
  367. if (l1 > l2) l2 = 1;
  368. else l2 -= l1 - 1;
  369. }
  370. lines(listfile->sy_file, l1, l2);
  371. listline = l1 + l2;
  372. }
  373. else error("no current file");
  374. }
  375. /* ------------------------------------------------------------- */
  376. /* implementation of the file command */
  377. do_file(p)
  378. p_tree p;
  379. {
  380. FILE *f;
  381. if (p->t_args[0]) {
  382. if (! strcmp(p->t_args[0]->t_str, "?")) {
  383. register p_symbol sym = PervasiveScope->sc_symbs;
  384. while (sym) {
  385. if (sym->sy_class == FILESYM) {
  386. fprintf(db_out, "%s\n", sym->sy_idf->id_text);
  387. }
  388. sym = sym->sy_prev_sc;
  389. }
  390. return;
  391. }
  392. if ((f = fopen(p->t_args[0]->t_str, "r")) == NULL) {
  393. error("could not open %s", p->t_args[0]->t_str);
  394. return;
  395. }
  396. fclose(f);
  397. newfile(p->t_args[0]->t_idf);
  398. }
  399. else if (listfile) fprintf(db_out, "%s\n", listfile->sy_idf->id_text);
  400. else error("no current file");
  401. }
  402. /* ------------------------------------------------------------- */
  403. /* implementation of stop/when command */
  404. setstop(p, kind)
  405. p_tree p;
  406. int kind;
  407. {
  408. t_addr a = get_addr_from_node(p->t_args[0]);
  409. if (a == ILL_ADDR) return 0;
  410. p->t_address = a;
  411. if (a != NO_ADDR) {
  412. if (! set_or_clear_breakpoint(a, kind)) {
  413. return 0;
  414. }
  415. }
  416. return 1;
  417. }
  418. do_stop(p)
  419. p_tree p;
  420. {
  421. if (! setstop(p, 1)) {
  422. return;
  423. }
  424. add_to_item_list(p);
  425. }
  426. /* ------------------------------------------------------------- */
  427. /* implementation of the trace command */
  428. settrace(p, kind)
  429. p_tree p;
  430. int kind;
  431. {
  432. t_addr a, e;
  433. a = get_addr_from_node(p->t_args[0]);
  434. if (a == NO_ADDR) return 1;
  435. if (a == ILL_ADDR) return 0;
  436. if (p->t_args[0]->t_oper == OP_AT) {
  437. e = a;
  438. p->t_address = a;
  439. }
  440. else {
  441. p_scope sc = get_next_scope_from_addr(a+1);
  442. if (sc) e = sc->sc_start - 1;
  443. else e = 0xffffffff;
  444. }
  445. return set_or_clear_trace(a, e, kind);
  446. }
  447. do_trace(p)
  448. p_tree p;
  449. {
  450. p->t_address = NO_ADDR;
  451. if (! settrace(p, 1)) {
  452. return;
  453. }
  454. add_to_item_list(p);
  455. }
  456. /* ------------------------------------------------------------- */
  457. /* implementation of the enable/disable commands */
  458. static
  459. able(p, kind)
  460. p_tree p;
  461. int kind;
  462. {
  463. if (!p) {
  464. able_item(0, kind);
  465. return;
  466. }
  467. switch(p->t_oper) {
  468. case OP_LINK:
  469. able(p->t_args[0], kind);
  470. able(p->t_args[1], kind);
  471. break;
  472. case OP_INTEGER:
  473. able_item((int)p->t_ival, kind);
  474. break;
  475. default:
  476. assert(0);
  477. }
  478. }
  479. do_enable(p)
  480. p_tree p;
  481. {
  482. able(p->t_args[0], 0);
  483. }
  484. do_disable(p)
  485. p_tree p;
  486. {
  487. able(p->t_args[0], 1);
  488. }
  489. /* ------------------------------------------------------------- */
  490. /* implementation of the cont command */
  491. do_continue(p)
  492. p_tree p;
  493. {
  494. int count;
  495. if (p) {
  496. count = p->t_args[0]->t_ival;
  497. if (p->t_args[1]) {
  498. t_addr a = get_addr_from_position(&(p->t_args[1]->t_pos));
  499. p_scope sc = get_scope_from_addr(a);
  500. if (a == ILL_ADDR || base_scope(sc) != base_scope(CurrentScope)) {
  501. error("cannot continue at line %d",
  502. p->t_args[1]->t_lino);
  503. return;
  504. }
  505. if (! set_pc(a)) {
  506. return;
  507. }
  508. }
  509. }
  510. else count = 1;
  511. while (count--) {
  512. if (! send_cont(count==0)) {
  513. break;
  514. }
  515. }
  516. if (count > 0) {
  517. fprintf(db_out, "Only %ld breakpoints skipped\n",
  518. p->t_args[0]->t_ival - count);
  519. }
  520. }
  521. /* ------------------------------------------------------------- */
  522. /* implementation of the step command */
  523. do_step(p)
  524. p_tree p;
  525. {
  526. p = p->t_args[0];
  527. if (! singlestep(0, p ? p->t_ival : 1L)) {
  528. }
  529. }
  530. /* ------------------------------------------------------------- */
  531. /* implementation of the next command */
  532. do_next(p)
  533. p_tree p;
  534. {
  535. p = p->t_args[0];
  536. if (! singlestep(1, p? p->t_ival : 1L)) {
  537. }
  538. }
  539. extern t_addr *get_EM_regs();
  540. /* ------------------------------------------------------------- */
  541. /* implementation of the regs command (temporarily) */
  542. do_regs(p)
  543. p_tree p;
  544. {
  545. t_addr *buf;
  546. int n = 0;
  547. p = p->t_args[0];
  548. if (p) n = p->t_ival;
  549. if (! (buf = get_EM_regs(n))) {
  550. return;
  551. }
  552. fprintf(db_out, "EM registers %d levels back:\n", n);
  553. fprintf(db_out, "\tLocalBase =\t0x%lx\n\tArgumentBase =\t0x%lx\n",
  554. (long) buf[0], (long) buf[1]);
  555. fprintf(db_out, "\tProgramCounter=\t0x%lx\n\tHeapPointer = \t0x%lx\n",
  556. (long) buf[2],
  557. (long) buf[3]);
  558. fprintf(db_out, "\tStackPointer =\t0x%lx\n", (long) buf[4]);
  559. }
  560. /* ------------------------------------------------------------- */
  561. /* implementation of the where command */
  562. static t_addr where_PC;
  563. static int
  564. where_entry(num)
  565. int num;
  566. {
  567. t_addr *buf;
  568. t_addr AB;
  569. p_scope sc;
  570. if (! (buf = get_EM_regs(num))) return 0;
  571. AB = buf[1];
  572. where_PC = buf[2];
  573. if (! AB) return 0;
  574. sc = base_scope(get_scope_from_addr(where_PC));
  575. if (! sc || sc->sc_start > where_PC) return 0;
  576. fprintf(db_out, "%s(", sc->sc_definedby->sy_idf->id_text);
  577. print_params(sc->sc_definedby->sy_type, AB, has_static_link(sc));
  578. fputs(") ", db_out);
  579. (void) print_position(where_PC, 0);
  580. fputs("\n", db_out);
  581. return 1;
  582. }
  583. /*ARGSUSED*/
  584. do_where(p)
  585. p_tree p;
  586. {
  587. int i = 0;
  588. unsigned int cnt;
  589. unsigned int maxcnt = 0xffff;
  590. p_scope sc;
  591. t_addr *buf;
  592. t_addr PC;
  593. p = p->t_args[0];
  594. if (p && p->t_ival < 0) {
  595. for (;;) {
  596. buf = get_EM_regs(i++);
  597. if (! buf || ! buf[1]) break;
  598. PC = buf[2];
  599. sc = base_scope(get_scope_from_addr(PC));
  600. if (! sc || sc->sc_start > PC) break;
  601. if (interrupted) return;
  602. }
  603. i--;
  604. maxcnt = - p->t_ival;
  605. i -= maxcnt;
  606. if (i < 0) i = 0;
  607. }
  608. else if (p) maxcnt = p->t_ival;
  609. for (cnt = maxcnt; cnt != 0; cnt--) {
  610. if (interrupted) return;
  611. if (! where_entry(i++)) return;
  612. }
  613. }
  614. /* ------------------------------------------------------------- */
  615. /* implementation of the delete command */
  616. do_delete(p)
  617. p_tree p;
  618. {
  619. switch(p->t_oper) {
  620. case OP_DELETE:
  621. if (! p->t_args[0]) {
  622. remove_from_item_list(0);
  623. }
  624. else do_delete(p->t_args[0]);
  625. break;
  626. case OP_LINK:
  627. do_delete(p->t_args[0]);
  628. do_delete(p->t_args[1]);
  629. break;
  630. case OP_INTEGER:
  631. remove_from_item_list((int) p->t_ival);
  632. break;
  633. default:
  634. assert(0);
  635. }
  636. }
  637. /* ------------------------------------------------------------- */
  638. /* implementation of the print command */
  639. do_print(p)
  640. p_tree p;
  641. {
  642. char *buf = 0;
  643. char *format = 0;
  644. long size;
  645. p_type tp;
  646. switch(p->t_oper) {
  647. case OP_PRINT:
  648. if (p->t_args[0] == 0) {
  649. p = print_command;
  650. if (p == 0) {
  651. error("no previous print command");
  652. break;
  653. }
  654. }
  655. else if (p != print_command) {
  656. /* freenode(print_command); No, could be in when-list */
  657. print_command = p;
  658. }
  659. /* fall through */
  660. case OP_DISPLAY:
  661. do_print(p->t_args[0]);
  662. break;
  663. case OP_LINK:
  664. do_print(p->t_args[0]);
  665. do_print(p->t_args[1]);
  666. break;
  667. default:
  668. if (interrupted || ! eval_expr(p, &buf, &size, &tp)) return;
  669. print_node(db_out, p, 0);
  670. fputs(" = ", db_out);
  671. if (p->t_oper == OP_FORMAT) {
  672. format = p->t_args[1]->t_str;
  673. }
  674. print_val(tp, size, buf, 0, 0, format);
  675. if (buf) free(buf);
  676. fputs("\n", db_out);
  677. break;
  678. }
  679. }
  680. /* ------------------------------------------------------------- */
  681. /* implementation of the set command */
  682. do_set(p)
  683. p_tree p;
  684. {
  685. char *buf = 0;
  686. long size, size2;
  687. p_type tp, tp2;
  688. t_addr a;
  689. if (interrupted || ! eval_desig(p->t_args[0], &a, &size, &tp) ||
  690. ! eval_expr(p->t_args[1], &buf, &size2, &tp2) ||
  691. ! convert(&buf, &size2, &tp2, tp, size)) {
  692. if (buf) free(buf);
  693. return;
  694. }
  695. if (interrupted) {
  696. free(buf);
  697. return;
  698. }
  699. set_bytes(size, buf, a);
  700. free(buf);
  701. }
  702. /* ------------------------------------------------------------- */
  703. /* implementation of the source command */
  704. extern FILE *db_in;
  705. do_source(p)
  706. p_tree p;
  707. {
  708. FILE *old_db_in = db_in;
  709. p = p->t_args[0];
  710. if ((db_in = fopen(p->t_str, "r")) == NULL) {
  711. db_in = old_db_in;
  712. error("could not open %s", p->t_str);
  713. return;
  714. }
  715. Commands();
  716. fclose(db_in);
  717. db_in = old_db_in;
  718. }
  719. /* ------------------------------------------------------------- */
  720. do_prcomm(p)
  721. p_tree p;
  722. {
  723. print_node(db_out, p->t_args[0], 1);
  724. }
  725. /* ------------------------------------------------------------- */
  726. /* stack frame commands: frame, down, up */
  727. extern int stack_offset;
  728. static
  729. frame_pos(diff)
  730. int diff;
  731. {
  732. if (stack_offset+diff < 0) diff = - stack_offset;
  733. if (! where_entry(stack_offset+diff)) {
  734. error("no frame %d", stack_offset+diff);
  735. return;
  736. }
  737. stack_offset += diff;
  738. list_position(get_position_from_addr(where_PC));
  739. CurrentScope = get_scope_from_addr(where_PC);
  740. }
  741. do_frame(p)
  742. p_tree p;
  743. {
  744. if (p->t_args[0]) {
  745. frame_pos((int) p->t_args[0]->t_ival - stack_offset);
  746. }
  747. else frame_pos(0);
  748. }
  749. do_up(p)
  750. p_tree p;
  751. {
  752. if (p->t_args[0]) {
  753. frame_pos((int) p->t_args[0]->t_ival);
  754. }
  755. else frame_pos(1);
  756. }
  757. do_down(p)
  758. p_tree p;
  759. {
  760. if (p->t_args[0]) {
  761. frame_pos(-(int) p->t_args[0]->t_ival);
  762. }
  763. else frame_pos(-1);
  764. }
  765. /* ------------------------------------------------------------- */
  766. /* log command */
  767. static char *logfile;
  768. static FILE *logfd;
  769. do_log(p)
  770. p_tree p;
  771. {
  772. p = p->t_args[0];
  773. if (p) {
  774. if (logfd && ! strcmp(p->t_str, "off")) {
  775. fprintf(db_out, "stopped logging on %s\n", logfile);
  776. fclose(logfd);
  777. logfd = NULL;
  778. return;
  779. }
  780. if (logfd) {
  781. error("already logging on %s", logfile);
  782. return;
  783. }
  784. logfile = p->t_str;
  785. if ((logfd = fopen(logfile, "w")) == NULL) {
  786. error("could not open %s", logfile);
  787. return;
  788. }
  789. fprintf(db_out, "started logging on %s\n", logfile);
  790. }
  791. else if (logfd) {
  792. fprintf(db_out, "the current logfile is %s\n", logfile);
  793. }
  794. else {
  795. error("no current logfile");
  796. }
  797. }
  798. extern int item_count;
  799. extern int in_wheninvoked;
  800. enterlog(p)
  801. p_tree p;
  802. {
  803. register p_tree p1;
  804. if (logfd && ! in_wheninvoked) {
  805. switch(p->t_oper) {
  806. case OP_SOURCE:
  807. case OP_LOG:
  808. break;
  809. case OP_DELETE:
  810. case OP_ENABLE:
  811. case OP_DISABLE:
  812. case OP_RESTORE:
  813. /* Change absolute item numbers into relative ones
  814. for safer replay
  815. */
  816. p1 = p->t_args[0];
  817. while (p1 && p1->t_oper == OP_LINK) {
  818. register p_tree p2 = p1->t_args[0];
  819. if (p2->t_ival > 0 && p2->t_ival <= item_count) {
  820. p2->t_ival = p2->t_ival - item_count - 1;
  821. }
  822. p1 = p1->t_args[1];
  823. }
  824. if (p1 && p1->t_ival > 0 && p1->t_ival <= item_count) {
  825. p1->t_ival = p1->t_ival - item_count - 1;
  826. }
  827. /* Fall through */
  828. default:
  829. print_node(logfd, p, 1);
  830. fflush(logfd);
  831. break;
  832. }
  833. }
  834. }