run.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. static int child_pid; /* process id of child */
  25. static int to_child, from_child; /* file descriptors for communication */
  26. static int child_status;
  27. static int restoring;
  28. static int fild1[2], fild2[2]; /* pipe file descriptors */
  29. int db_ss;
  30. t_lineno currline, listline;
  31. static int catch_sigpipe();
  32. static int stopped();
  33. static int uputm(), ugetm();
  34. int
  35. init_run()
  36. {
  37. /* take file descriptors so that listing cannot take them */
  38. int i;
  39. for (i = IN_FD; i <= OUT_FD; i++) close(i);
  40. if (pipe(fild1) < 0 ||
  41. pipe(fild2) < 0 ||
  42. fild1[0] != IN_FD ||
  43. fild2[1] != OUT_FD) {
  44. return 0;
  45. }
  46. to_child = fild1[1];
  47. from_child = fild2[0];
  48. return 1;
  49. }
  50. int
  51. start_child(p)
  52. p_tree p;
  53. {
  54. /* start up the process to be debugged and set up communication */
  55. char *argp[MAXARG]; /* argument list */
  56. register p_tree pt = p->t_args[0], pt1;
  57. unsigned int nargs = 1; /* #args */
  58. char *in_redirect = 0; /* standard input redirected */
  59. char *out_redirect = 0; /* standard output redirected */
  60. signal_child(SIGKILL); /* like families in China, this debugger is only
  61. allowed one child
  62. */
  63. /* first check arguments and redirections and build argument list */
  64. while (pt) {
  65. switch(pt->t_oper) {
  66. case OP_LINK:
  67. pt1 = pt->t_args[1];
  68. pt = pt->t_args[0];
  69. continue;
  70. case OP_NAME:
  71. if (nargs < (MAXARG-1)) {
  72. argp[nargs++] = pt->t_str;
  73. }
  74. else {
  75. error("too many arguments");
  76. return 0;
  77. }
  78. break;
  79. case OP_INPUT:
  80. if (in_redirect) {
  81. error("input redirected twice?");
  82. return 0;
  83. }
  84. in_redirect = pt->t_str;
  85. break;
  86. case OP_OUTPUT:
  87. if (out_redirect) {
  88. error("output redirected twice?");
  89. return 0;
  90. }
  91. out_redirect = pt->t_str;
  92. break;
  93. }
  94. if (pt != pt1) pt = pt1;
  95. else break;
  96. }
  97. argp[0] = AObj;
  98. argp[nargs] = 0;
  99. /* create child process */
  100. child_pid = fork();
  101. if (child_pid < 0) {
  102. error("could not create child");
  103. return 0;
  104. }
  105. if (child_pid == 0) {
  106. /* this is the child process */
  107. close(fild1[1]);
  108. close(fild2[0]);
  109. signal(SIGINT, SIG_IGN);
  110. /* I/O redirection */
  111. if (in_redirect) {
  112. int fd;
  113. close(0);
  114. if ((fd = open(in_redirect, 0)) < 0 ||
  115. (fd != 0 && dup2(fd, 0) < 0)) {
  116. error("could not open input file");
  117. exit(1);
  118. }
  119. if (fd != 0) {
  120. close(fd);
  121. }
  122. }
  123. if (out_redirect) {
  124. int fd;
  125. close(1);
  126. if ((fd = creat(in_redirect, 0666)) < 0 ||
  127. (fd != 1 && dup2(fd, 1) < 0)) {
  128. error("could not open output file");
  129. exit(1);
  130. }
  131. if (fd != 1) {
  132. close(fd);
  133. }
  134. }
  135. /* and run process to be debugged */
  136. execv(AObj, argp);
  137. error("could not exec %s", AObj);
  138. exit(1);
  139. }
  140. /* debugger */
  141. close(fild1[0]);
  142. close(fild2[1]);
  143. pipe(fild1); /* to occupy file descriptors */
  144. signal(SIGPIPE, catch_sigpipe);
  145. if (! wait_for_child((char *) 0)) {
  146. error("child not responding");
  147. return 0;
  148. }
  149. do_items();
  150. if (! restoring) send_cont(1);
  151. return 1;
  152. }
  153. int
  154. wait_for_child(s)
  155. char *s; /* to pass on to 'stopped' */
  156. {
  157. struct message_hdr m;
  158. if (child_pid) {
  159. if (ugetm(&m)) {
  160. return stopped(s, (t_addr) m.m_size);
  161. }
  162. return 0;
  163. }
  164. return 1;
  165. }
  166. signal_child(sig)
  167. {
  168. if (child_pid) {
  169. kill(child_pid, sig);
  170. if (sig == SIGKILL) {
  171. wait(&child_status);
  172. init_run();
  173. }
  174. }
  175. }
  176. static int
  177. catch_sigpipe()
  178. {
  179. child_pid = 0;
  180. }
  181. static int
  182. ureceive(p, c)
  183. char *p;
  184. long c;
  185. {
  186. int i;
  187. if (! child_pid) return 0;
  188. while (c >= 0x1000) {
  189. i = read(from_child, p, 0x1000);
  190. if (i <= 0) {
  191. if (i == 0) child_pid = 0;
  192. return 0;
  193. }
  194. p += i;
  195. c -= i;
  196. }
  197. while (c > 0) {
  198. i = read(from_child, p, (int)c);
  199. if (i <= 0) {
  200. if (i == 0) child_pid = 0;
  201. return 0;
  202. }
  203. p += i;
  204. c -= i;
  205. }
  206. return c == 0;
  207. }
  208. static int
  209. usend(p, c)
  210. char *p;
  211. long c;
  212. {
  213. int i;
  214. while (c >= 0x1000) {
  215. i = write(to_child, p, 0x1000);
  216. if (i < 0) return 0;
  217. p += i;
  218. c -= i;
  219. }
  220. while (c > 0) {
  221. i = write(to_child, p, (int)c);
  222. if (i < 0) return 0;
  223. p += i;
  224. c -= i;
  225. }
  226. return 1;
  227. }
  228. static int
  229. ugetm(message)
  230. struct message_hdr *message;
  231. {
  232. if (! ureceive((char *) message, (long) sizeof(struct message_hdr))) {
  233. return 0;
  234. }
  235. if (debug) printf("Got %d\n", message->m_type);
  236. return 1;
  237. }
  238. static int
  239. uputm(message)
  240. struct message_hdr *message;
  241. {
  242. if (! usend((char *) message, (long) sizeof(struct message_hdr))) {
  243. return 0;
  244. }
  245. if (debug) printf("Sent %d\n", message->m_type);
  246. return 1;
  247. }
  248. static struct message_hdr answer;
  249. static int single_stepping;
  250. static int
  251. stopped(s, a)
  252. char *s; /* stop message */
  253. t_addr a; /* address where stopped */
  254. {
  255. p_position pos;
  256. if (s) {
  257. fprintf(db_out, "%s ", s);
  258. pos = print_position((t_addr) a, 1);
  259. fputs("\n", db_out);
  260. list_position(pos);
  261. }
  262. return 1;
  263. }
  264. static int
  265. could_send(m, stop_message)
  266. struct message_hdr *m;
  267. {
  268. int type;
  269. t_addr a;
  270. for (;;) {
  271. if (child_pid) {
  272. if (! uputm(m) ||
  273. ! ugetm(&answer)) {
  274. if (child_pid) {
  275. error("something wrong!");
  276. return 1;
  277. }
  278. wait(&child_status);
  279. init_run();
  280. if (child_status & 0177) {
  281. fprintf(db_out,
  282. "Child died with signal %d\n",
  283. child_status & 0177);
  284. }
  285. else {
  286. fprintf(db_out,
  287. "Child terminated, exit status %d\n",
  288. child_status >> 8);
  289. }
  290. return 1;
  291. }
  292. a = answer.m_size;
  293. type = answer.m_type;
  294. if (m->m_type & DB_RUN) {
  295. /* run command */
  296. CurrentScope = get_scope_from_addr((t_addr) a);
  297. if (! item_addr_actions(a) &&
  298. ( type == DB_SS || type == OK)) {
  299. /* no explicit breakpoints at this position.
  300. Also, child did not stop because of
  301. SETSS or SETSSF, otherwise we would
  302. have gotten END_SS.
  303. So, continue.
  304. */
  305. if ((m->m_type & ~ DB_SS) != CONT) {
  306. m->m_type = CONT | (m->m_type & DB_SS);
  307. }
  308. continue;
  309. }
  310. if (type != END_SS && single_stepping) {
  311. m->m_type = CLRSS;
  312. uputm(m) && ugetm(&answer);
  313. }
  314. single_stepping = 0;
  315. }
  316. if (stop_message) {
  317. stopped("stopped", a);
  318. handle_displays();
  319. }
  320. return 1;
  321. }
  322. return 0;
  323. }
  324. /*NOTREACHED*/
  325. }
  326. int
  327. get_bytes(size, from, to)
  328. long size;
  329. t_addr from;
  330. char *to;
  331. {
  332. struct message_hdr m;
  333. m.m_type = GETBYTES;
  334. m.m_size = size;
  335. put_int(m.m_buf, pointer_size, (long)from);
  336. if (! could_send(&m, 0)) {
  337. return 0;
  338. }
  339. if (answer.m_type == FAIL) {
  340. return 0;
  341. }
  342. assert(answer.m_type == DATA && answer.m_size == m.m_size);
  343. return ureceive(to, answer.m_size);
  344. }
  345. int
  346. set_bytes(size, from, to)
  347. long size;
  348. char *from;
  349. t_addr to;
  350. {
  351. struct message_hdr m;
  352. m.m_type = SETBYTES;
  353. m.m_size = size;
  354. put_int(m.m_buf, pointer_size, (long) to);
  355. return uputm(&m)
  356. && usend(from, size)
  357. && ugetm(&m)
  358. && m.m_type != FAIL;
  359. }
  360. int
  361. get_dump(globmessage, globbuf, stackmessage, stackbuf)
  362. struct message_hdr *globmessage, *stackmessage;
  363. char **globbuf, **stackbuf;
  364. {
  365. struct message_hdr m;
  366. m.m_type = DUMP;
  367. if (! could_send(&m, 0)) {
  368. return 0;
  369. }
  370. if (answer.m_type == FAIL) return 0;
  371. assert(answer.m_type == DGLOB);
  372. *globmessage = answer;
  373. *globbuf = Malloc((unsigned) answer.m_size);
  374. if (! ureceive(*globbuf, answer.m_size) || ! ugetm(stackmessage)) {
  375. free(*globbuf);
  376. return 0;
  377. }
  378. assert(stackmessage->m_type == DSTACK);
  379. *stackbuf = Malloc((unsigned) stackmessage->m_size);
  380. if (! ureceive(*stackbuf, stackmessage->m_size)) {
  381. free(*globbuf);
  382. free(*stackbuf);
  383. return 0;
  384. }
  385. put_int(globmessage->m_buf+SP_OFF*pointer_size, pointer_size,
  386. get_int(stackmessage->m_buf+SP_OFF*pointer_size, pointer_size, T_UNSIGNED));
  387. return 1;
  388. }
  389. int
  390. put_dump(globmessage, globbuf, stackmessage, stackbuf)
  391. struct message_hdr *globmessage, *stackmessage;
  392. char *globbuf, *stackbuf;
  393. {
  394. struct message_hdr m;
  395. if (! child_pid) {
  396. restoring = 1;
  397. start_child(run_command);
  398. restoring = 0;
  399. }
  400. return uputm(globmessage) &&
  401. usend(globbuf, globmessage->m_size) &&
  402. uputm(stackmessage) &&
  403. usend(stackbuf, stackmessage->m_size) &&
  404. ugetm(&m) && stopped("restored", m.m_size);
  405. }
  406. t_addr *
  407. get_EM_regs(level)
  408. int level;
  409. {
  410. struct message_hdr m;
  411. static t_addr buf[5];
  412. register t_addr *to = &buf[0];
  413. m.m_type = GETEMREGS;
  414. m.m_size = level;
  415. if (! could_send(&m, 0)) {
  416. return 0;
  417. }
  418. if (answer.m_type == FAIL) return 0;
  419. *to++ = (t_addr) get_int(answer.m_buf, pointer_size, T_UNSIGNED);
  420. *to++ = (t_addr) get_int(answer.m_buf+pointer_size, pointer_size, T_UNSIGNED);
  421. *to++ = (t_addr) get_int(answer.m_buf+2*pointer_size, pointer_size, T_UNSIGNED);
  422. *to++ = (t_addr) get_int(answer.m_buf+3*pointer_size, pointer_size, T_UNSIGNED);
  423. *to++ = (t_addr) get_int(answer.m_buf+4*pointer_size, pointer_size, T_UNSIGNED);
  424. return buf;
  425. }
  426. int
  427. set_pc(PC)
  428. t_addr PC;
  429. {
  430. struct message_hdr m;
  431. m.m_type = SETEMREGS;
  432. m.m_size = 0;
  433. put_int(m.m_buf+PC_OFF*pointer_size, pointer_size, (long)PC);
  434. return could_send(&m, 0) && answer.m_type != FAIL;
  435. }
  436. int
  437. send_cont(stop_message)
  438. int stop_message;
  439. {
  440. struct message_hdr m;
  441. m.m_type = (CONT | (db_ss ? DB_SS : 0));
  442. m.m_size = 0;
  443. return could_send(&m, stop_message) && answer.m_type != FAIL;
  444. }
  445. int
  446. do_single_step(type, count)
  447. int type;
  448. long count;
  449. {
  450. struct message_hdr m;
  451. m.m_type = type | (db_ss ? DB_SS : 0);
  452. m.m_size = count;
  453. single_stepping = 1;
  454. if (could_send(&m, 1) && answer.m_type != FAIL) {
  455. return 1;
  456. }
  457. single_stepping = 0;
  458. return 0;
  459. }
  460. int
  461. set_or_clear_breakpoint(a, type)
  462. t_addr a;
  463. int type;
  464. {
  465. struct message_hdr m;
  466. if (a == ILL_ADDR || a == NO_ADDR) return 0;
  467. m.m_type = type;
  468. m.m_size = a;
  469. if (debug) printf("%s breakpoint at 0x%lx\n", type == SETBP ? "setting" : "clearing", (long) a);
  470. if (! could_send(&m, 0)) { }
  471. return 1;
  472. }
  473. int
  474. set_or_clear_trace(start, end, type)
  475. t_addr start, end;
  476. int type;
  477. {
  478. struct message_hdr m;
  479. m.m_type = type;
  480. put_int(m.m_buf, pointer_size, (long)start);
  481. put_int(m.m_buf+pointer_size, pointer_size, (long)end);
  482. if (debug) printf("%s trace at [0x%lx,0x%lx]\n", type == SETTRACE ? "setting" : "clearing", (long) start, (long) end);
  483. if (! could_send(&m, 0)) { }
  484. return 1;
  485. }