run.c 14 KB

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