1 /* $Id: run.c,v 1.18 1994/06/24 11:01:05 ceriel Exp $ */
3 /* Running a process and communication */
25 extern char *strncpy();
26 extern char *malloc();
27 extern struct idf *str2idf();
32 extern char *progname;
33 extern int child_interrupted;
34 extern int interrupted;
35 extern t_lineno currline;
37 static int child_pid; /* process id of child */
38 static int to_child, from_child; /* file descriptors for communication */
39 static int child_status;
41 static int fild1[2], fild2[2]; /* pipe file descriptors */
49 static void catch_sigpipe();
51 static int uputm(), ugetm();
52 static t_addr curr_stop;
64 for (i = sz; i > 0; i--) {
78 for (i = sz; i>0; i--) {
79 l = (l << 8) | (*p++ & 0377);
87 /* take file descriptors so that listing cannot take them */
90 for (i = 3; i <= 6; i++) close(i);
91 if (pipe(fild1) < 0 ||
95 fatal("something wrong with file descriptors");
98 from_child = fild2[0];
100 if (currfile) CurrentScope = currfile->sy_file->f_scope;
109 /* start up the process to be debugged and set up communication */
111 char *argp[MAXARG]; /* argument list */
112 register p_tree pt = p->t_args[0], pt1 = 0;
113 unsigned int nargs = 1; /* #args */
114 char *in_redirect = 0; /* standard input redirected */
115 char *out_redirect = 0; /* standard output redirected */
117 signal_child(SIGKILL); /* like families in China, this debugger is only
121 if (p != run_command) {
122 freenode(run_command);
125 /* first check arguments and redirections and build argument list */
133 if (nargs < (MAXARG-1)) {
134 argp[nargs++] = pt->t_str;
137 error("too many arguments");
143 error("input redirected twice?");
146 in_redirect = pt->t_str;
150 error("output redirected twice?");
153 out_redirect = pt->t_str;
156 if (pt != pt1) pt = pt1;
162 /* create child process */
165 error("could not create child");
168 if (child_pid == 0) {
169 /* this is the child process */
173 signal(SIGINT, SIG_IGN);
175 /* I/O redirection */
180 if ((fd = open(in_redirect, 0)) < 0 ||
181 (fd != 0 && dup2(fd, 0) < 0)) {
193 if ((fd = creat(out_redirect, 0666)) < 0 ||
194 (fd != 1 && dup2(fd, 1) < 0)) {
203 /* and run process to be debugged */
205 error("could not exec %s", AObj);
209 /* close fild1[0] and fild2[1]; but we want those file descriptors occupied,
210 so we re-occupy them.
215 pipe(fild1); /* to occupy file descriptors */
217 signal(SIGPIPE, catch_sigpipe);
219 struct message_hdr m;
222 error("child not responding");
226 curr_stop = BUFTOI(m.m_buf+1, PS);
227 CurrentScope = get_scope_from_addr(curr_stop);
231 int stop_reason = item_addr_actions(curr_stop, 1, 1);
232 if (! stop_reason) (void) send_cont(1);
233 else (void) stopped("stopped", curr_stop, stop_reason);
241 kill(child_pid, sig);
242 if (sig == SIGKILL) {
260 int i = read(f, p, c);
262 while (i < 0 && errno == EINTR) i = read(f, p, c);
280 while (c >= 0x1000) {
281 i = do_read(from_child, p, 0x1000);
286 else error("read failed");
289 if (p != buf) p += i;
293 i = do_read(from_child, p, (int)c);
298 else error("read failed");
318 while (c >= 0x1000) {
319 i = write(to_child, p, 0x1000);
321 if (child_pid) error("write failed");
328 i = write(to_child, p, (int)c);
330 if (child_pid) error("write failed");
341 struct message_hdr *message;
343 if (! ureceive((char *) message, (long) sizeof(struct message_hdr))) {
346 if (debug) printf("Got %d\n", message->m_type);
352 struct message_hdr *message;
354 if (! usend((char *) message, (long) sizeof(struct message_hdr))) {
357 if (debug) printf("Sent %d\n", message->m_type);
361 static struct message_hdr answer;
362 static int single_stepping;
365 stopped(s, a, stop_reason)
366 char *s; /* stop message */
367 t_addr a; /* address where stopped */
368 int stop_reason; /* status entry causing the stop */
373 fprintf(db_out, "%s ", s);
374 pos = print_position(a, 1);
376 fprintf(db_out, " (status entry %d)", stop_reason);
383 CurrentScope = get_scope_from_addr(a);
388 could_send(m, stop_message)
389 struct message_hdr *m;
393 static int level = 0;
404 if (m->m_type & M_DB_RUN) {
408 if (!child_interrupted && (! uputm(m) || ! ugetm(&answer))) {
412 if ((interrupted || child_interrupted) && ! child_dead) {
413 while (child_interrupted && answer.m_type != M_INTR) {
414 if (! ugetm(&answer)) {
419 if (interrupted && ! child_dead) {
422 child_interrupted = 0;
424 return stopped("interrupted", (t_addr) BUFTOI(answer.m_buf+1, PS), 0);
431 if (child_status & 0177) {
433 "child died with signal %d\n",
434 child_status & 0177);
438 "child terminated, exit status %d\n",
445 a = BUFTOI(answer.m_buf+1, PS);
446 type = answer.m_type & 0377;
447 if (type == M_END_SS) type = 0;
448 else if (type == M_OK || type == M_DB_SS) type = 1;
450 if (m->m_type & M_DB_RUN) {
452 CurrentScope = get_scope_from_addr((t_addr) a);
453 if (!(stop_reason = item_addr_actions(a, type, stop_message))
455 /* no explicit breakpoints at this position.
456 Also, child did not stop because of
457 SETSS or SETSSF, otherwise we would
461 if ((m->m_type & 0177) != M_CONT) {
462 m->m_type = M_CONT | (m->m_type & M_DB_SS);
466 if (type != 0 && single_stepping) {
468 if (! uputm(m) || ! ugetm(&answer)) return 0;
474 return stopped("stopped", a, stop_reason);
482 getbytes(size, from, to, kind, errmess)
489 struct message_hdr m;
492 ITOBUF(m.m_buf+1, size, LS);
493 ITOBUF(m.m_buf+LS+1, (long)from, PS);
495 if (! could_send(&m, 0)) {
499 switch(answer.m_type) {
501 if (errmess) error("could not get value");
504 if (errmess) error("interrupted");
507 return ureceive(to, BUFTOI(answer.m_buf+1, LS));
515 get_bytes(size, from, to)
520 return getbytes(size, from, to, M_GETBYTES, 1);
524 get_string(size, from, to)
529 int retval = getbytes(size, from, to, M_GETSTR, 0);
531 to[(int)BUFTOI(answer.m_buf+1, LS)] = 0;
536 set_bytes(size, from, to)
541 struct message_hdr m;
543 m.m_type = M_SETBYTES;
544 ITOBUF(m.m_buf+1, size, LS);
545 ITOBUF(m.m_buf+LS+1, (long) to, PS);
547 if (! uputm(&m) || ! usend(from, size) || ! ugetm(&m)) {
550 switch(answer.m_type) {
552 error("could not handle this SET request");
555 error("interrupted");
565 get_dump(globbuf, stackbuf)
566 char **globbuf, **stackbuf;
568 struct message_hdr m;
569 struct message_hdr *globm, *stackm;
573 if (! could_send(&m, 0)) {
576 switch(answer.m_type) {
578 error("request for DUMP failed");
581 error("interrupted");
589 sz = BUFTOI(answer.m_buf+1, LS);
590 *globbuf = malloc((unsigned) (sz+sizeof(struct message_hdr)));
592 || ! ureceive(*globbuf+sizeof(struct message_hdr), sz)
594 if (*globbuf) free(*globbuf);
595 else error("could not allocate enougn memory");
598 globm = (struct message_hdr *) *globbuf;
601 assert(m.m_type == M_DSTACK);
602 sz = BUFTOI(m.m_buf+1, LS);
603 *stackbuf = malloc((unsigned) sz+sizeof(struct message_hdr));
604 if (! *stackbuf || ! ureceive(*stackbuf+sizeof(struct message_hdr), sz)) {
606 if (*stackbuf) free(*stackbuf);
607 else error("could not allocate enougn memory");
610 stackm = (struct message_hdr *) *stackbuf;
612 ITOBUF(globm->m_buf+SP_OFF, BUFTOI(stackm->m_buf+SP_OFF, PS), PS);
613 return BUFTOI(globm->m_buf+PC_OFF, PS);
617 put_dump(globbuf, stackbuf)
618 char *globbuf, *stackbuf;
620 struct message_hdr m;
621 struct message_hdr *globm = (struct message_hdr *) globbuf,
622 *stackm = (struct message_hdr *) stackbuf;
624 stackbuf += sizeof(struct message_hdr);
625 globbuf += sizeof(struct message_hdr);
628 start_child(run_command);
632 && usend(globbuf, BUFTOI(globm->m_buf+1, LS))
634 && usend(stackbuf, BUFTOI(stackm->m_buf+1, LS))
636 && stopped("restored", BUFTOI(m.m_buf+1, PS), 0);
643 struct message_hdr m;
644 static t_addr buf[5];
645 register t_addr *to = &buf[0];
647 m.m_type = M_GETEMREGS;
648 ITOBUF(m.m_buf+1, (long) level, LS);
650 if (! could_send(&m, 0)) {
653 switch(answer.m_type) {
655 error("request for registers failed");
658 error("interrupted");
665 *to++ = (t_addr) BUFTOI(answer.m_buf+LB_OFF, PS);
666 *to++ = (t_addr) BUFTOI(answer.m_buf+AB_OFF, PS);
667 *to++ = (t_addr) BUFTOI(answer.m_buf+PC_OFF, PS);
668 *to++ = (t_addr) BUFTOI(answer.m_buf+HP_OFF, PS);
669 *to++ = (t_addr) BUFTOI(answer.m_buf+PC_OFF, PS);
677 struct message_hdr m;
679 m.m_type = M_SETEMREGS;
680 ITOBUF(m.m_buf+PC_OFF, (long)PC, PS);
681 if (! could_send(&m, 0)) return 0;
682 switch(answer.m_type) {
684 error("could not set PC to %lx", (long) PC);
687 error("interrupted");
698 send_cont(stop_message)
701 struct message_hdr m;
703 m.m_type = (M_CONT | (db_ss ? M_DB_SS : 0));
704 return could_send(&m, stop_message) && child_pid;
708 singlestep(type, count)
712 struct message_hdr m;
714 m.m_type = (type ? M_SETSSF : M_SETSS) | (db_ss ? M_DB_SS : 0);
715 ITOBUF(m.m_buf+1, count, LS);
717 if (could_send(&m, 1) && child_pid) return 1;
723 set_or_clear_breakpoint(a, type)
727 struct message_hdr m;
729 m.m_type = type ? M_SETBP : M_CLRBP;
730 ITOBUF(m.m_buf+1, (long) a, PS);
731 if (debug) printf("%s breakpoint at 0x%lx\n", type ? "setting" : "clearing", (long) a);
732 if (child_pid && ! could_send(&m, 0)) {
739 set_or_clear_trace(start, end, type)
743 struct message_hdr m;
745 m.m_type = type ? M_SETTRACE : M_CLRTRACE;
746 ITOBUF(m.m_buf+1, (long)start, PS);
747 ITOBUF(m.m_buf+PS+1, (long)end, PS);
748 if (debug) printf("%s trace at [0x%lx,0x%lx]\n", type ? "setting" : "clearing", (long) start, (long) end);
749 if (child_pid && ! could_send(&m, 0)) {