run.c 13 KB

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