do_comm.c 22 KB

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