run.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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 int stack_offset;
  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. static int catch_sigpipe();
  38. static int stopped();
  39. static int uputm(), ugetm();
  40. static t_addr curr_stop;
  41. p_tree run_command;
  42. static
  43. ITOBUF(p, l, sz)
  44. register char *p;
  45. long l;
  46. int sz;
  47. {
  48. register int i;
  49. p +=sz;
  50. for (i = sz; i > 0; i--) {
  51. *--p = l;
  52. l >>= 8;
  53. }
  54. }
  55. static long
  56. BUFTOI(p, sz)
  57. register char *p;
  58. int sz;
  59. {
  60. register long l = 0;
  61. register int i;
  62. for (i = sz; i>0; i--) {
  63. l = (l << 8) | (*p++ & 0377);
  64. }
  65. return l;
  66. }
  67. int
  68. init_run()
  69. {
  70. /* take file descriptors so that listing cannot take them */
  71. int i;
  72. for (i = 3; i <= 6; i++) close(i);
  73. if (pipe(fild1) < 0 ||
  74. pipe(fild2) < 0 ||
  75. fild1[0] != 3 ||
  76. fild2[1] != 6) {
  77. return 0;
  78. }
  79. to_child = fild1[1];
  80. from_child = fild2[0];
  81. child_pid = 0;
  82. if (currfile) CurrentScope = currfile->sy_file->f_scope;
  83. currline = 0;
  84. return 1;
  85. }
  86. extern int errno;
  87. int
  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;
  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 0;
  118. }
  119. break;
  120. case OP_INPUT:
  121. if (in_redirect) {
  122. error("input redirected twice?");
  123. return 0;
  124. }
  125. in_redirect = pt->t_str;
  126. break;
  127. case OP_OUTPUT:
  128. if (out_redirect) {
  129. error("output redirected twice?");
  130. return 0;
  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 0;
  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. /* debugger */
  182. close(fild1[0]);
  183. close(fild2[1]);
  184. pipe(fild1); /* to occupy file descriptors */
  185. signal(SIGPIPE, catch_sigpipe);
  186. {
  187. struct message_hdr m;
  188. if (! ugetm(&m)) {
  189. error("child not responding");
  190. init_run();
  191. return 0;
  192. }
  193. curr_stop = BUFTOI(m.m_buf+1, (int) PS);
  194. CurrentScope = get_scope_from_addr(curr_stop);
  195. }
  196. perform_items();
  197. if (! restoring && ! item_addr_actions(curr_stop, M_OK, 1)) {
  198. send_cont(1);
  199. }
  200. else if (! restoring) {
  201. stopped("stopped", curr_stop);
  202. }
  203. return 1;
  204. }
  205. signal_child(sig)
  206. {
  207. if (child_pid) {
  208. kill(child_pid, sig);
  209. if (sig == SIGKILL) {
  210. wait(&child_status);
  211. init_run();
  212. }
  213. }
  214. }
  215. static int
  216. catch_sigpipe()
  217. {
  218. child_pid = 0;
  219. }
  220. static int
  221. ureceive(p, c)
  222. char *p;
  223. long c;
  224. {
  225. int i;
  226. char buf[0x1000];
  227. if (! child_pid) {
  228. error("no process");
  229. return 0;
  230. }
  231. if (! p) p = buf;
  232. while (c >= 0x1000) {
  233. i = read(from_child, p, 0x1000);
  234. if (i <= 0) {
  235. if (i == 0) {
  236. child_pid = 0;
  237. }
  238. else error("read failed");
  239. return 0;
  240. }
  241. if (p != buf) p += i;
  242. c -= i;
  243. }
  244. while (c > 0) {
  245. i = read(from_child, p, (int)c);
  246. if (i <= 0) {
  247. if (i == 0) {
  248. child_pid = 0;
  249. }
  250. else error("read failed");
  251. return 0;
  252. }
  253. p += i;
  254. c -= i;
  255. }
  256. return 1;
  257. }
  258. static int
  259. usend(p, c)
  260. char *p;
  261. long c;
  262. {
  263. int i;
  264. if (! child_pid) {
  265. error("no process");
  266. return 0;
  267. }
  268. while (c >= 0x1000) {
  269. i = write(to_child, p, 0x1000);
  270. if (i < 0) {
  271. if (child_pid) error("write failed");
  272. return 0;
  273. }
  274. p += i;
  275. c -= i;
  276. }
  277. while (c > 0) {
  278. i = write(to_child, p, (int)c);
  279. if (i < 0) {
  280. if (child_pid) error("write failed");
  281. return 0;
  282. }
  283. p += i;
  284. c -= i;
  285. }
  286. return 1;
  287. }
  288. static int
  289. ugetm(message)
  290. struct message_hdr *message;
  291. {
  292. if (! ureceive((char *) message, (long) sizeof(struct message_hdr))) {
  293. return 0;
  294. }
  295. if (debug) printf("Got %d\n", message->m_type);
  296. return 1;
  297. }
  298. static int
  299. uputm(message)
  300. struct message_hdr *message;
  301. {
  302. if (! usend((char *) message, (long) sizeof(struct message_hdr))) {
  303. return 0;
  304. }
  305. if (debug) printf("Sent %d\n", message->m_type);
  306. return 1;
  307. }
  308. static struct message_hdr answer;
  309. static int single_stepping;
  310. static int
  311. stopped(s, a)
  312. char *s; /* stop message */
  313. t_addr a; /* address where stopped */
  314. {
  315. p_position pos;
  316. if (s && a) {
  317. fprintf(db_out, "%s ", s);
  318. pos = print_position(a, 1);
  319. if (stop_reason) {
  320. fprintf(db_out, " (status entry %d)", stop_reason);
  321. }
  322. fputs("\n", db_out);
  323. list_position(pos);
  324. handle_displays();
  325. }
  326. curr_stop = a;
  327. CurrentScope = get_scope_from_addr(a);
  328. return 1;
  329. }
  330. static int
  331. could_send(m, stop_message)
  332. struct message_hdr *m;
  333. {
  334. int type;
  335. t_addr a;
  336. static int level = 0;
  337. int child_dead = 0;
  338. level++;
  339. for (;;) {
  340. if (! child_pid) {
  341. error("no process");
  342. return 0;
  343. }
  344. if (m->m_type & M_DB_RUN) {
  345. disable_intr = 0;
  346. stop_reason = 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. stopped("interrupted", (t_addr) BUFTOI(answer.m_buf+1, (int)PS));
  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 (m->m_type & M_DB_RUN) {
  389. /* run command */
  390. CurrentScope = get_scope_from_addr((t_addr) a);
  391. if (! item_addr_actions(a, type, stop_message) &&
  392. ( type == M_DB_SS || type == M_OK)) {
  393. /* no explicit breakpoints at this position.
  394. Also, child did not stop because of
  395. SETSS or SETSSF, otherwise we would
  396. have gotten END_SS.
  397. So, continue.
  398. */
  399. if ((m->m_type & 0177) != M_CONT) {
  400. m->m_type = M_CONT | (m->m_type & M_DB_SS);
  401. }
  402. continue;
  403. }
  404. if (type != M_END_SS && single_stepping) {
  405. m->m_type = M_CLRSS;
  406. if (! uputm(m) || ! ugetm(&answer)) return 0;
  407. }
  408. single_stepping = 0;
  409. }
  410. if (stop_message) {
  411. stopped("stopped", a);
  412. }
  413. level--;
  414. return 1;
  415. }
  416. /*NOTREACHED*/
  417. }
  418. static int
  419. getbytes(size, from, to, kind, errmess)
  420. long size;
  421. t_addr from;
  422. char *to;
  423. int kind;
  424. int errmess;
  425. {
  426. struct message_hdr m;
  427. m.m_type = kind;
  428. ITOBUF(m.m_buf+1, size, (int) LS);
  429. ITOBUF(m.m_buf+LS+1, (long)from, (int) PS);
  430. if (! could_send(&m, 0)) {
  431. return 0;
  432. }
  433. switch(answer.m_type) {
  434. case M_FAIL:
  435. if (errmess) error("could not get value");
  436. return 0;
  437. case M_INTR:
  438. if (errmess) error("interrupted");
  439. return 0;
  440. case M_DATA:
  441. return ureceive(to, BUFTOI(answer.m_buf+1, (int)LS));
  442. default:
  443. assert(0);
  444. }
  445. /*NOTREACHED*/
  446. }
  447. int
  448. get_bytes(size, from, to)
  449. long size;
  450. t_addr from;
  451. char *to;
  452. {
  453. return getbytes(size, from, to, M_GETBYTES, 1);
  454. }
  455. int
  456. get_string(size, from, to)
  457. long size;
  458. t_addr from;
  459. char *to;
  460. {
  461. int retval = getbytes(size, from, to, M_GETSTR, 0);
  462. to[(int)BUFTOI(answer.m_buf+1, (int)LS)] = 0;
  463. return retval;
  464. }
  465. set_bytes(size, from, to)
  466. long size;
  467. char *from;
  468. t_addr to;
  469. {
  470. struct message_hdr m;
  471. m.m_type = M_SETBYTES;
  472. ITOBUF(m.m_buf+1, size, (int) LS);
  473. ITOBUF(m.m_buf+LS+1, (long) to, (int) PS);
  474. if (! uputm(&m) || ! usend(from, size) || ! ugetm(&m)) {
  475. return;
  476. }
  477. switch(answer.m_type) {
  478. case M_FAIL:
  479. error("could not handle this SET request");
  480. break;
  481. case M_INTR:
  482. error("interrupted");
  483. break;
  484. case M_OK:
  485. break;
  486. default:
  487. assert(0);
  488. }
  489. }
  490. t_addr
  491. get_dump(globmessage, globbuf, stackmessage, stackbuf)
  492. struct message_hdr *globmessage, *stackmessage;
  493. char **globbuf, **stackbuf;
  494. {
  495. struct message_hdr m;
  496. long sz;
  497. m.m_type = M_DUMP;
  498. if (! could_send(&m, 0)) {
  499. return 0;
  500. }
  501. switch(answer.m_type) {
  502. case M_FAIL:
  503. error("request for DUMP failed");
  504. return 0;
  505. case M_INTR:
  506. error("interrupted");
  507. return 0;
  508. case M_DGLOB:
  509. break;
  510. default:
  511. assert(0);
  512. }
  513. *globmessage = answer;
  514. sz = BUFTOI(answer.m_buf+1, (int)LS);
  515. *globbuf = malloc((unsigned) sz);
  516. if (! ureceive(*globbuf, sz) || ! ugetm(stackmessage)) {
  517. if (*globbuf) free(*globbuf);
  518. return 0;
  519. }
  520. assert(stackmessage->m_type == M_DSTACK);
  521. sz = BUFTOI(stackmessage->m_buf+1, (int)LS);
  522. *stackbuf = malloc((unsigned) sz);
  523. if (! ureceive(*stackbuf, sz)) {
  524. if (*globbuf) free(*globbuf);
  525. if (*stackbuf) free(*stackbuf);
  526. return 0;
  527. }
  528. ITOBUF(globmessage->m_buf+SP_OFF, BUFTOI(stackmessage->m_buf+SP_OFF, (int)PS), (int) PS);
  529. if (! *globbuf || ! *stackbuf) {
  530. error("could not allocate enough memory");
  531. if (*globbuf) free(*globbuf);
  532. if (*stackbuf) free(*stackbuf);
  533. return 0;
  534. }
  535. return BUFTOI(globmessage->m_buf+PC_OFF, (int)PS);
  536. }
  537. int
  538. put_dump(globmessage, globbuf, stackmessage, stackbuf)
  539. struct message_hdr *globmessage, *stackmessage;
  540. char *globbuf, *stackbuf;
  541. {
  542. struct message_hdr m;
  543. int retval;
  544. if (! child_pid) {
  545. restoring = 1;
  546. start_child(run_command);
  547. restoring = 0;
  548. }
  549. retval = uputm(globmessage)
  550. && usend(globbuf, BUFTOI(globmessage->m_buf+1, (int) LS))
  551. && uputm(stackmessage)
  552. && usend(stackbuf, BUFTOI(stackmessage->m_buf+1, (int) LS))
  553. && ugetm(&m)
  554. && stopped("restored", BUFTOI(m.m_buf+1, (int) PS));
  555. return retval;
  556. }
  557. t_addr *
  558. get_EM_regs(level)
  559. int level;
  560. {
  561. struct message_hdr m;
  562. static t_addr buf[5];
  563. register t_addr *to = &buf[0];
  564. m.m_type = M_GETEMREGS;
  565. ITOBUF(m.m_buf+1, (long) level, (int) LS);
  566. if (! could_send(&m, 0)) {
  567. return 0;
  568. }
  569. switch(answer.m_type) {
  570. case M_FAIL:
  571. error("request for registers failed");
  572. return 0;
  573. case M_INTR:
  574. error("interrupted");
  575. return 0;
  576. case M_GETEMREGS:
  577. break;
  578. default:
  579. assert(0);
  580. }
  581. *to++ = (t_addr) BUFTOI(answer.m_buf+LB_OFF, (int)PS);
  582. *to++ = (t_addr) BUFTOI(answer.m_buf+AB_OFF, (int)PS);
  583. *to++ = (t_addr) BUFTOI(answer.m_buf+PC_OFF, (int)PS);
  584. *to++ = (t_addr) BUFTOI(answer.m_buf+HP_OFF, (int)PS);
  585. *to++ = (t_addr) BUFTOI(answer.m_buf+PC_OFF, (int)PS);
  586. return buf;
  587. }
  588. int
  589. set_pc(PC)
  590. t_addr PC;
  591. {
  592. struct message_hdr m;
  593. m.m_type = M_SETEMREGS;
  594. ITOBUF(m.m_buf+PC_OFF, (long)PC, (int)PS);
  595. if (! could_send(&m, 0)) return 0;
  596. switch(answer.m_type) {
  597. case M_FAIL:
  598. error("could not set PC to %lx", (long) PC);
  599. return 0;
  600. case M_INTR:
  601. error("interrupted");
  602. return 0;
  603. case M_OK:
  604. return 1;
  605. default:
  606. assert(0);
  607. }
  608. /*NOTREACHED*/
  609. }
  610. int
  611. send_cont(stop_message)
  612. int stop_message;
  613. {
  614. struct message_hdr m;
  615. m.m_type = (M_CONT | (db_ss ? M_DB_SS : 0));
  616. return could_send(&m, stop_message) && child_pid;
  617. }
  618. int
  619. singlestep(type, count)
  620. int type;
  621. long count;
  622. {
  623. struct message_hdr m;
  624. m.m_type = type | (db_ss ? M_DB_SS : 0);
  625. ITOBUF(m.m_buf+1, count, (int) LS);
  626. single_stepping = 1;
  627. if (could_send(&m, 1) && child_pid) return 1;
  628. single_stepping = 0;
  629. return 0;
  630. }
  631. int
  632. set_or_clear_breakpoint(a, type)
  633. t_addr a;
  634. int type;
  635. {
  636. struct message_hdr m;
  637. m.m_type = type;
  638. ITOBUF(m.m_buf+1, (long) a, (int) PS);
  639. if (debug) printf("%s breakpoint at 0x%lx\n", type == M_SETBP ? "setting" : "clearing", (long) a);
  640. if (child_pid && ! could_send(&m, 0)) {
  641. }
  642. return 1;
  643. }
  644. int
  645. set_or_clear_trace(start, end, type)
  646. t_addr start, end;
  647. int type;
  648. {
  649. struct message_hdr m;
  650. m.m_type = type;
  651. ITOBUF(m.m_buf+1, (long)start, (int) PS);
  652. ITOBUF(m.m_buf+PS+1, (long)end, (int) PS);
  653. if (debug) printf("%s trace at [0x%lx,0x%lx]\n", type == M_SETTRACE ? "setting" : "clearing", (long) start, (long) end);
  654. if (child_pid && ! could_send(&m, 0)) {
  655. return 0;
  656. }
  657. return 1;
  658. }