run.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /* $Header$ */
  2. /* Running a process and communication */
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <assert.h>
  6. #include <alloc.h>
  7. #include "ops.h"
  8. #include "message.h"
  9. #include "position.h"
  10. #include "tree.h"
  11. #include "file.h"
  12. #include "symbol.h"
  13. #include "idf.h"
  14. #include "scope.h"
  15. #include "type.h"
  16. #include "expr.h"
  17. #define MAXARG 128
  18. extern char *strncpy();
  19. extern struct idf *str2idf();
  20. extern char *AObj;
  21. extern FILE *db_out;
  22. extern int debug;
  23. extern char *progname;
  24. extern int child_interrupted;
  25. extern int interrupted;
  26. extern t_lineno currline;
  27. static int child_pid; /* process id of child */
  28. static int to_child, from_child; /* file descriptors for communication */
  29. static int child_status;
  30. static int restoring;
  31. static int fild1[2], fild2[2]; /* pipe file descriptors */
  32. int disable_intr = 1;
  33. int db_ss;
  34. int stack_offset;
  35. void signal_child();
  36. static void catch_sigpipe();
  37. static int stopped();
  38. static int uputm(), ugetm();
  39. static t_addr curr_stop;
  40. p_tree run_command;
  41. static void
  42. ITOBUF(p, l, sz)
  43. register char *p;
  44. long l;
  45. int sz;
  46. {
  47. register int i;
  48. p +=sz;
  49. for (i = sz; i > 0; i--) {
  50. *--p = l;
  51. l >>= 8;
  52. }
  53. }
  54. static long
  55. BUFTOI(p, sz)
  56. register char *p;
  57. int sz;
  58. {
  59. register long l = 0;
  60. register int i;
  61. for (i = sz; i>0; i--) {
  62. l = (l << 8) | (*p++ & 0377);
  63. }
  64. return l;
  65. }
  66. void
  67. init_run()
  68. {
  69. /* take file descriptors so that listing cannot take them */
  70. int i;
  71. for (i = 3; i <= 6; i++) close(i);
  72. if (pipe(fild1) < 0 ||
  73. pipe(fild2) < 0 ||
  74. fild1[0] != 3 ||
  75. fild2[1] != 6) {
  76. fatal("something wrong with file descriptors");
  77. }
  78. to_child = fild1[1];
  79. from_child = fild2[0];
  80. child_pid = 0;
  81. if (currfile) CurrentScope = currfile->sy_file->f_scope;
  82. currline = 0;
  83. }
  84. extern int errno;
  85. start_child(p)
  86. p_tree p;
  87. {
  88. /* start up the process to be debugged and set up communication */
  89. char *argp[MAXARG]; /* argument list */
  90. register p_tree pt = p->t_args[0], pt1 = 0;
  91. unsigned int nargs = 1; /* #args */
  92. char *in_redirect = 0; /* standard input redirected */
  93. char *out_redirect = 0; /* standard output redirected */
  94. signal_child(SIGKILL); /* like families in China, this debugger is only
  95. allowed one child
  96. */
  97. if (p != run_command) {
  98. freenode(run_command);
  99. run_command = p;
  100. }
  101. /* first check arguments and redirections and build argument list */
  102. while (pt) {
  103. switch(pt->t_oper) {
  104. case OP_LINK:
  105. pt1 = pt->t_args[1];
  106. pt = pt->t_args[0];
  107. continue;
  108. case OP_NAME:
  109. if (nargs < (MAXARG-1)) {
  110. argp[nargs++] = pt->t_str;
  111. }
  112. else {
  113. error("too many arguments");
  114. return;
  115. }
  116. break;
  117. case OP_INPUT:
  118. if (in_redirect) {
  119. error("input redirected twice?");
  120. return;
  121. }
  122. in_redirect = pt->t_str;
  123. break;
  124. case OP_OUTPUT:
  125. if (out_redirect) {
  126. error("output redirected twice?");
  127. return;
  128. }
  129. out_redirect = pt->t_str;
  130. break;
  131. }
  132. if (pt != pt1) pt = pt1;
  133. else break;
  134. }
  135. argp[0] = AObj;
  136. argp[nargs] = 0;
  137. /* create child process */
  138. child_pid = fork();
  139. if (child_pid < 0) {
  140. error("could not create child");
  141. return;
  142. }
  143. if (child_pid == 0) {
  144. /* this is the child process */
  145. close(fild1[1]);
  146. close(fild2[0]);
  147. signal(SIGINT, SIG_IGN);
  148. /* I/O redirection */
  149. if (in_redirect) {
  150. int fd;
  151. close(0);
  152. if ((fd = open(in_redirect, 0)) < 0 ||
  153. (fd != 0 && dup2(fd, 0) < 0)) {
  154. perror(progname);
  155. exit(1);
  156. }
  157. if (fd != 0) {
  158. close(fd);
  159. }
  160. }
  161. if (out_redirect) {
  162. int fd;
  163. close(1);
  164. if ((fd = creat(out_redirect, 0666)) < 0 ||
  165. (fd != 1 && dup2(fd, 1) < 0)) {
  166. perror(progname);
  167. exit(1);
  168. }
  169. if (fd != 1) {
  170. close(fd);
  171. }
  172. }
  173. /* and run process to be debugged */
  174. execv(AObj, argp);
  175. error("could not exec %s", AObj);
  176. exit(1);
  177. }
  178. /* close fild1[0] and fild2[1]; but we want those file descriptors occupied,
  179. so we re-occupy them.
  180. */
  181. close(fild1[0]);
  182. close(fild2[1]);
  183. pipe(fild1); /* to occupy file descriptors */
  184. signal(SIGPIPE, catch_sigpipe);
  185. {
  186. struct message_hdr m;
  187. if (! ugetm(&m)) {
  188. error("child not responding");
  189. init_run();
  190. return;
  191. }
  192. curr_stop = BUFTOI(m.m_buf+1, (int) PS);
  193. CurrentScope = get_scope_from_addr(curr_stop);
  194. }
  195. perform_items();
  196. if (! restoring) {
  197. int stop_reason = item_addr_actions(curr_stop, 1, 1);
  198. if (! stop_reason) (void) send_cont(1);
  199. else (void) stopped("stopped", curr_stop, stop_reason);
  200. }
  201. }
  202. void
  203. signal_child(sig)
  204. {
  205. if (child_pid) {
  206. kill(child_pid, sig);
  207. if (sig == SIGKILL) {
  208. wait(&child_status);
  209. init_run();
  210. }
  211. }
  212. }
  213. static void
  214. catch_sigpipe()
  215. {
  216. child_pid = 0;
  217. }
  218. static int
  219. ureceive(p, c)
  220. char *p;
  221. long c;
  222. {
  223. int i;
  224. char buf[0x1000];
  225. if (! child_pid) {
  226. error("no process");
  227. return 0;
  228. }
  229. if (! p) p = buf;
  230. while (c >= 0x1000) {
  231. i = read(from_child, p, 0x1000);
  232. if (i <= 0) {
  233. if (i == 0) {
  234. child_pid = 0;
  235. }
  236. else error("read failed");
  237. return 0;
  238. }
  239. if (p != buf) p += i;
  240. c -= i;
  241. }
  242. while (c > 0) {
  243. i = read(from_child, p, (int)c);
  244. if (i <= 0) {
  245. if (i == 0) {
  246. child_pid = 0;
  247. }
  248. else error("read failed");
  249. return 0;
  250. }
  251. p += i;
  252. c -= i;
  253. }
  254. return 1;
  255. }
  256. static int
  257. usend(p, c)
  258. char *p;
  259. long c;
  260. {
  261. int i;
  262. if (! child_pid) {
  263. error("no process");
  264. return 0;
  265. }
  266. while (c >= 0x1000) {
  267. i = write(to_child, p, 0x1000);
  268. if (i < 0) {
  269. if (child_pid) error("write failed");
  270. return 0;
  271. }
  272. p += i;
  273. c -= i;
  274. }
  275. while (c > 0) {
  276. i = write(to_child, p, (int)c);
  277. if (i < 0) {
  278. if (child_pid) error("write failed");
  279. return 0;
  280. }
  281. p += i;
  282. c -= i;
  283. }
  284. return 1;
  285. }
  286. static int
  287. ugetm(message)
  288. struct message_hdr *message;
  289. {
  290. if (! ureceive((char *) message, (long) sizeof(struct message_hdr))) {
  291. return 0;
  292. }
  293. if (debug) printf("Got %d\n", message->m_type);
  294. return 1;
  295. }
  296. static int
  297. uputm(message)
  298. struct message_hdr *message;
  299. {
  300. if (! usend((char *) message, (long) sizeof(struct message_hdr))) {
  301. return 0;
  302. }
  303. if (debug) printf("Sent %d\n", message->m_type);
  304. return 1;
  305. }
  306. static struct message_hdr answer;
  307. static int single_stepping;
  308. static int
  309. stopped(s, a, stop_reason)
  310. char *s; /* stop message */
  311. t_addr a; /* address where stopped */
  312. int stop_reason; /* status entry causing the stop */
  313. {
  314. p_position pos;
  315. if (s && a) {
  316. fprintf(db_out, "%s ", s);
  317. pos = print_position(a, 1);
  318. if (stop_reason) {
  319. fprintf(db_out, " (status entry %d)", stop_reason);
  320. }
  321. fputs("\n", db_out);
  322. list_position(pos);
  323. handle_displays();
  324. }
  325. curr_stop = a;
  326. CurrentScope = get_scope_from_addr(a);
  327. return 1;
  328. }
  329. static int
  330. could_send(m, stop_message)
  331. struct message_hdr *m;
  332. {
  333. int type;
  334. t_addr a;
  335. static int level = 0;
  336. int child_dead = 0;
  337. int stop_reason;
  338. level++;
  339. for (;;) {
  340. stop_reason = 0;
  341. if (! child_pid) {
  342. error("no process");
  343. return 0;
  344. }
  345. if (m->m_type & M_DB_RUN) {
  346. disable_intr = 0;
  347. stack_offset = 0;
  348. }
  349. if (!child_interrupted && (! uputm(m) || ! ugetm(&answer))) {
  350. child_dead = 1;
  351. }
  352. disable_intr = 1;
  353. if ((interrupted || child_interrupted) && ! child_dead) {
  354. while (child_interrupted && answer.m_type != M_INTR) {
  355. if (! ugetm(&answer)) {
  356. child_dead = 1;
  357. break;
  358. }
  359. }
  360. if (interrupted && ! child_dead) {
  361. level--;
  362. if (! level) {
  363. child_interrupted = 0;
  364. interrupted = 0;
  365. return stopped("interrupted", (t_addr) BUFTOI(answer.m_buf+1, (int)PS), 0);
  366. }
  367. return 1;
  368. }
  369. }
  370. if (child_dead) {
  371. wait(&child_status);
  372. if (child_status & 0177) {
  373. fprintf(db_out,
  374. "child died with signal %d\n",
  375. child_status & 0177);
  376. }
  377. else {
  378. fprintf(db_out,
  379. "child terminated, exit status %d\n",
  380. child_status >> 8);
  381. }
  382. init_run();
  383. level--;
  384. return 1;
  385. }
  386. a = BUFTOI(answer.m_buf+1, (int)PS);
  387. type = answer.m_type & 0377;
  388. if (type == M_END_SS) type = 0;
  389. else if (type == M_OK || type == M_DB_SS) type = 1;
  390. else type = 2;
  391. if (m->m_type & M_DB_RUN) {
  392. /* run command */
  393. CurrentScope = get_scope_from_addr((t_addr) a);
  394. if (!(stop_reason = item_addr_actions(a, type, stop_message))
  395. && (type == 1)) {
  396. /* no explicit breakpoints at this position.
  397. Also, child did not stop because of
  398. SETSS or SETSSF, otherwise we would
  399. have gotten END_SS.
  400. So, continue.
  401. */
  402. if ((m->m_type & 0177) != M_CONT) {
  403. m->m_type = M_CONT | (m->m_type & M_DB_SS);
  404. }
  405. continue;
  406. }
  407. if (type != 0 && single_stepping) {
  408. m->m_type = M_CLRSS;
  409. if (! uputm(m) || ! ugetm(&answer)) return 0;
  410. }
  411. single_stepping = 0;
  412. }
  413. level--;
  414. if (stop_message) {
  415. return stopped("stopped", a, stop_reason);
  416. }
  417. return 1;
  418. }
  419. /*NOTREACHED*/
  420. }
  421. static int
  422. getbytes(size, from, to, kind, errmess)
  423. long size;
  424. t_addr from;
  425. char *to;
  426. int kind;
  427. int errmess;
  428. {
  429. struct message_hdr m;
  430. m.m_type = kind;
  431. ITOBUF(m.m_buf+1, size, (int) LS);
  432. ITOBUF(m.m_buf+LS+1, (long)from, (int) PS);
  433. if (! could_send(&m, 0)) {
  434. return 0;
  435. }
  436. switch(answer.m_type) {
  437. case M_FAIL:
  438. if (errmess) error("could not get value");
  439. return 0;
  440. case M_INTR:
  441. if (errmess) error("interrupted");
  442. return 0;
  443. case M_DATA:
  444. return ureceive(to, BUFTOI(answer.m_buf+1, (int)LS));
  445. default:
  446. assert(0);
  447. }
  448. /*NOTREACHED*/
  449. }
  450. int
  451. get_bytes(size, from, to)
  452. long size;
  453. t_addr from;
  454. char *to;
  455. {
  456. return getbytes(size, from, to, M_GETBYTES, 1);
  457. }
  458. int
  459. get_string(size, from, to)
  460. long size;
  461. t_addr from;
  462. char *to;
  463. {
  464. int retval = getbytes(size, from, to, M_GETSTR, 0);
  465. to[(int)BUFTOI(answer.m_buf+1, (int)LS)] = 0;
  466. return retval;
  467. }
  468. void
  469. set_bytes(size, from, to)
  470. long size;
  471. char *from;
  472. t_addr to;
  473. {
  474. struct message_hdr m;
  475. m.m_type = M_SETBYTES;
  476. ITOBUF(m.m_buf+1, size, (int) LS);
  477. ITOBUF(m.m_buf+LS+1, (long) to, (int) PS);
  478. if (! uputm(&m) || ! usend(from, size) || ! ugetm(&m)) {
  479. return;
  480. }
  481. switch(answer.m_type) {
  482. case M_FAIL:
  483. error("could not handle this SET request");
  484. break;
  485. case M_INTR:
  486. error("interrupted");
  487. break;
  488. case M_OK:
  489. break;
  490. default:
  491. assert(0);
  492. }
  493. }
  494. t_addr
  495. get_dump(globbuf, stackbuf)
  496. char **globbuf, **stackbuf;
  497. {
  498. struct message_hdr m;
  499. struct message_hdr *globm, *stackm;
  500. long sz;
  501. m.m_type = M_DUMP;
  502. if (! could_send(&m, 0)) {
  503. return 0;
  504. }
  505. switch(answer.m_type) {
  506. case M_FAIL:
  507. error("request for DUMP failed");
  508. return 0;
  509. case M_INTR:
  510. error("interrupted");
  511. return 0;
  512. case M_DGLOB:
  513. break;
  514. default:
  515. assert(0);
  516. }
  517. sz = BUFTOI(answer.m_buf+1, (int)LS);
  518. *globbuf = malloc((unsigned) (sz+sizeof(struct message_hdr)));
  519. if (! *globbuf
  520. || ! ureceive(*globbuf+sizeof(struct message_hdr), sz)
  521. || ! ugetm(&m)) {
  522. if (*globbuf) free(*globbuf);
  523. else error("could not allocate enougn memory");
  524. return 0;
  525. }
  526. globm = (struct message_hdr *) *globbuf;
  527. *globm = answer;
  528. assert(m.m_type == M_DSTACK);
  529. sz = BUFTOI(m.m_buf+1, (int)LS);
  530. *stackbuf = malloc((unsigned) sz+sizeof(struct message_hdr));
  531. if (! *stackbuf || ! ureceive(*stackbuf+sizeof(struct message_hdr), sz)) {
  532. free(*globbuf);
  533. if (*stackbuf) free(*stackbuf);
  534. else error("could not allocate enougn memory");
  535. return 0;
  536. }
  537. stackm = (struct message_hdr *) *stackbuf;
  538. *stackm = m;
  539. ITOBUF(globm->m_buf+SP_OFF, BUFTOI(stackm->m_buf+SP_OFF, (int)PS), (int) PS);
  540. return BUFTOI(globm->m_buf+PC_OFF, (int)PS);
  541. }
  542. int
  543. put_dump(globbuf, stackbuf)
  544. char *globbuf, *stackbuf;
  545. {
  546. struct message_hdr m;
  547. struct message_hdr *globm = (struct message_hdr *) globbuf,
  548. *stackm = (struct message_hdr *) stackbuf;
  549. stackbuf += sizeof(struct message_hdr);
  550. globbuf += sizeof(struct message_hdr);
  551. if (! child_pid) {
  552. restoring = 1;
  553. start_child(run_command);
  554. restoring = 0;
  555. }
  556. return uputm(globm)
  557. && usend(globbuf, BUFTOI(globm->m_buf+1, (int) LS))
  558. && uputm(stackm)
  559. && usend(stackbuf, BUFTOI(stackm->m_buf+1, (int) LS))
  560. && ugetm(&m)
  561. && stopped("restored", BUFTOI(m.m_buf+1, (int) PS), 0);
  562. }
  563. t_addr *
  564. get_EM_regs(level)
  565. int level;
  566. {
  567. struct message_hdr m;
  568. static t_addr buf[5];
  569. register t_addr *to = &buf[0];
  570. m.m_type = M_GETEMREGS;
  571. ITOBUF(m.m_buf+1, (long) level, (int) LS);
  572. if (! could_send(&m, 0)) {
  573. return 0;
  574. }
  575. switch(answer.m_type) {
  576. case M_FAIL:
  577. error("request for registers failed");
  578. return 0;
  579. case M_INTR:
  580. error("interrupted");
  581. return 0;
  582. case M_GETEMREGS:
  583. break;
  584. default:
  585. assert(0);
  586. }
  587. *to++ = (t_addr) BUFTOI(answer.m_buf+LB_OFF, (int)PS);
  588. *to++ = (t_addr) BUFTOI(answer.m_buf+AB_OFF, (int)PS);
  589. *to++ = (t_addr) BUFTOI(answer.m_buf+PC_OFF, (int)PS);
  590. *to++ = (t_addr) BUFTOI(answer.m_buf+HP_OFF, (int)PS);
  591. *to++ = (t_addr) BUFTOI(answer.m_buf+PC_OFF, (int)PS);
  592. return buf;
  593. }
  594. int
  595. set_pc(PC)
  596. t_addr PC;
  597. {
  598. struct message_hdr m;
  599. m.m_type = M_SETEMREGS;
  600. ITOBUF(m.m_buf+PC_OFF, (long)PC, (int)PS);
  601. if (! could_send(&m, 0)) return 0;
  602. switch(answer.m_type) {
  603. case M_FAIL:
  604. error("could not set PC to %lx", (long) PC);
  605. return 0;
  606. case M_INTR:
  607. error("interrupted");
  608. return 0;
  609. case M_OK:
  610. return 1;
  611. default:
  612. assert(0);
  613. }
  614. /*NOTREACHED*/
  615. }
  616. int
  617. send_cont(stop_message)
  618. int stop_message;
  619. {
  620. struct message_hdr m;
  621. m.m_type = (M_CONT | (db_ss ? M_DB_SS : 0));
  622. return could_send(&m, stop_message) && child_pid;
  623. }
  624. int
  625. singlestep(type, count)
  626. int type;
  627. long count;
  628. {
  629. struct message_hdr m;
  630. m.m_type = (type ? M_SETSSF : M_SETSS) | (db_ss ? M_DB_SS : 0);
  631. ITOBUF(m.m_buf+1, count, (int) LS);
  632. single_stepping = 1;
  633. if (could_send(&m, 1) && child_pid) return 1;
  634. single_stepping = 0;
  635. return 0;
  636. }
  637. int
  638. set_or_clear_breakpoint(a, type)
  639. t_addr a;
  640. int type;
  641. {
  642. struct message_hdr m;
  643. m.m_type = type ? M_SETBP : M_CLRBP;
  644. ITOBUF(m.m_buf+1, (long) a, (int) PS);
  645. if (debug) printf("%s breakpoint at 0x%lx\n", type ? "setting" : "clearing", (long) a);
  646. if (child_pid && ! could_send(&m, 0)) {
  647. }
  648. return 1;
  649. }
  650. int
  651. set_or_clear_trace(start, end, type)
  652. t_addr start, end;
  653. int type;
  654. {
  655. struct message_hdr m;
  656. m.m_type = type ? M_SETTRACE : M_CLRTRACE;
  657. ITOBUF(m.m_buf+1, (long)start, (int) PS);
  658. ITOBUF(m.m_buf+PS+1, (long)end, (int) PS);
  659. if (debug) printf("%s trace at [0x%lx,0x%lx]\n", type ? "setting" : "clearing", (long) start, (long) end);
  660. if (child_pid && ! could_send(&m, 0)) {
  661. return 0;
  662. }
  663. return 1;
  664. }