make it look like kernel driver
[unix-history] / usr / src / old / dbx / events.c
index 2f480cb..2fc20c2 100644 (file)
@@ -1,6 +1,14 @@
-/* Copyright (c) 1982 Regents of the University of California */
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)events.c   5.1 (Berkeley) %G%";
+#endif not lint
 
 
-static char sccsid[] = "@(#)events.c 1.3 %G%";
+static char rcsid[] = "$Header: events.c,v 1.5 84/12/26 10:39:26 linton Exp $";
 
 /*
  * Event/breakpoint managment.
 
 /*
  * Event/breakpoint managment.
@@ -14,6 +22,7 @@ static char sccsid[] = "@(#)events.c 1.3 %G%";
 #include "eval.h"
 #include "source.h"
 #include "mappings.h"
 #include "eval.h"
 #include "source.h"
 #include "mappings.h"
+#include "runtime.h"
 #include "process.h"
 #include "machine.h"
 #include "lists.h"
 #include "process.h"
 #include "machine.h"
 #include "lists.h"
@@ -22,9 +31,9 @@ static char sccsid[] = "@(#)events.c 1.3 %G%";
 typedef struct Event *Event;
 typedef struct Breakpoint *Breakpoint;
 
 typedef struct Event *Event;
 typedef struct Breakpoint *Breakpoint;
 
-Boolean inst_tracing;
-Boolean single_stepping;
-Boolean isstopped;
+boolean inst_tracing;
+boolean single_stepping;
+boolean isstopped;
 
 #include "symbols.h"
 
 
 #include "symbols.h"
 
@@ -40,7 +49,7 @@ Symbol retaddrsym;
 
 struct Event {
     unsigned int id;
 
 struct Event {
     unsigned int id;
-    Boolean temporary;
+    boolean temporary;
     Node condition;
     Cmdlist actions;
 };
     Node condition;
     Cmdlist actions;
 };
@@ -50,6 +59,7 @@ struct Breakpoint {
     Address bpaddr;    
     Lineno bpline;
     Cmdlist actions;
     Address bpaddr;    
     Lineno bpline;
     Cmdlist actions;
+    boolean temporary;
 };
 
 typedef List Eventlist;
 };
 
 typedef List Eventlist;
@@ -60,8 +70,9 @@ typedef List Bplist;
 
 private Eventlist eventlist;           /* list of active events */
 private Bplist bplist;                 /* list of active breakpoints */
 
 private Eventlist eventlist;           /* list of active events */
 private Bplist bplist;                 /* list of active breakpoints */
-private Integer eventid;               /* id number of next allocated event */
-private Integer trid;                  /* id number of next allocated trace */
+private Event curevent;                        /* most recently created event */
+private integer eventid;               /* id number of current event */
+private integer trid;                  /* id number of current trace */
 
 typedef struct Trcmd {
     Integer trid;
 
 typedef struct Trcmd {
     Integer trid;
@@ -112,7 +123,7 @@ public bpinit()
  */
 
 public Event event_alloc(istmp, econd, cmdlist)
  */
 
 public Event event_alloc(istmp, econd, cmdlist)
-Boolean istmp;
+boolean istmp;
 Node econd;
 Cmdlist cmdlist;
 {
 Node econd;
 Cmdlist cmdlist;
 {
@@ -125,40 +136,51 @@ Cmdlist cmdlist;
     e->condition = econd;
     e->actions = cmdlist;
     eventlist_append(e, eventlist);
     e->condition = econd;
     e->actions = cmdlist;
     eventlist_append(e, eventlist);
+    curevent = e;
     translate(e);
     return e;
 }
 
 /*
  * Delete the event with the given id.
     translate(e);
     return e;
 }
 
 /*
  * Delete the event with the given id.
+ * Returns whether it's successful or not.
  */
 
  */
 
-public delevent(id)
+public boolean delevent (id)
 unsigned int id;
 {
     Event e;
     Breakpoint bp;
     Trcmd t;
 unsigned int id;
 {
     Event e;
     Breakpoint bp;
     Trcmd t;
+    boolean found;
 
 
+    found = false;
     foreach (Event, e, eventlist)
        if (e->id == id) {
     foreach (Event, e, eventlist)
        if (e->id == id) {
-           list_delete(list_curitem(eventlist), eventlist);
+           found = true;
            foreach (Breakpoint, bp, bplist)
                if (bp->event == e) {
            foreach (Breakpoint, bp, bplist)
                if (bp->event == e) {
+                   if (tracebpts) {
+                       printf("deleting breakpoint at 0x%x\n", bp->bpaddr);
+                       fflush(stdout);
+                   }
                    list_delete(list_curitem(bplist), bplist);
                }
            endfor
                    list_delete(list_curitem(bplist), bplist);
                }
            endfor
+           list_delete(list_curitem(eventlist), eventlist);
            break;
        }
     endfor
     foreach (Trcmd, t, eachline)
        if (t->event->id == id) {
            break;
        }
     endfor
     foreach (Trcmd, t, eachline)
        if (t->event->id == id) {
+           found = true;
            printrmtr(t);
            list_delete(list_curitem(eachline), eachline);
        }
     endfor
     foreach (Trcmd, t, eachinst)
        if (t->event->id == id) {
            printrmtr(t);
            list_delete(list_curitem(eachline), eachline);
        }
     endfor
     foreach (Trcmd, t, eachinst)
        if (t->event->id == id) {
+           found = true;
            printrmtr(t);
            list_delete(list_curitem(eachinst), eachinst);
        }
            printrmtr(t);
            list_delete(list_curitem(eachinst), eachinst);
        }
@@ -169,6 +191,7 @@ unsigned int id;
            single_stepping = false;
        }
     }
            single_stepping = false;
        }
     }
+    return found;
 }
 
 /*
 }
 
 /*
@@ -194,15 +217,17 @@ Event e;
                if (s == linesym) {
                    if (place->op == O_QLINE) {
                        line = place->value.arg[1]->value.lcon;
                if (s == linesym) {
                    if (place->op == O_QLINE) {
                        line = place->value.arg[1]->value.lcon;
-                       addr = objaddr(line,
-                           place->value.arg[0]->value.scon);
+                       addr = objaddr(line, place->value.arg[0]->value.scon);
                    } else {
                        eval(place);
                        line = pop(long);
                        addr = objaddr(line, cursource);
                    }
                    if (addr == NOADDR) {
                    } else {
                        eval(place);
                        line = pop(long);
                        addr = objaddr(line, cursource);
                    }
                    if (addr == NOADDR) {
-                       delevent(e->id);
+                       if (not delevent(e->id)) {
+                           printf("!! dbx.translate: can't undo event %d?\n",
+                               e->id);
+                       }
                        beginerrmsg();
                        fprintf(stderr, "no executable code at line ");
                        prtree(stderr, place);
                        beginerrmsg();
                        fprintf(stderr, "no executable code at line ");
                        prtree(stderr, place);
@@ -306,11 +331,11 @@ Node exp;
  * This can only be done if there are no breakpoints within the function.
  */
 
  * This can only be done if there are no breakpoints within the function.
  */
 
-public Boolean canskip(f)
+public boolean canskip(f)
 Symbol f;
 {
     Breakpoint p;
 Symbol f;
 {
     Breakpoint p;
-    Boolean ok;
+    boolean ok;
 
     ok = true;
     foreach (Breakpoint, p, bplist)
 
     ok = true;
     foreach (Breakpoint, p, bplist)
@@ -347,7 +372,7 @@ Event e;
     Command cmd;
 
     if (not isredirected()) {
     Command cmd;
 
     if (not isredirected()) {
-       printf("(%d) ", e->id);
+       printeventid(e->id);
     }
     cmd = list_element(Command, list_head(e->actions));
     if (cmd->op == O_PRINTCALL) {
     }
     cmd = list_element(Command, list_head(e->actions));
     if (cmd->op == O_PRINTCALL) {
@@ -371,6 +396,12 @@ Event e;
     printf("\n");
 }
 
     printf("\n");
 }
 
+private printeventid (id)
+integer id;
+{
+    printf("[%d] ", id);
+}
+
 /*
  * Print out a condition.
  */
 /*
  * Print out a condition.
  */
@@ -422,8 +453,13 @@ Cmdlist actions;
     p->bpaddr = addr;
     p->bpline = line;
     p->actions = actions;
     p->bpaddr = addr;
     p->bpline = line;
     p->actions = actions;
+    p->temporary = false;
     if (tracebpts) {
     if (tracebpts) {
-       printf("new bp at 0x%x\n", addr);
+       if (e == nil) {
+           printf("new bp at 0x%x for event ??\n", addr, e->id);
+       } else {
+           printf("new bp at 0x%x for event %d\n", addr, e->id);
+       }
        fflush(stdout);
     }
     bplist_append(p, bplist);
        fflush(stdout);
     }
     bplist_append(p, bplist);
@@ -440,7 +476,9 @@ public bpfree()
 
     fixbps();
     foreach (Event, e, eventlist)
 
     fixbps();
     foreach (Event, e, eventlist)
-       delevent(e->id);
+       if (not delevent(e->id)) {
+           printf("!! dbx.bpfree: can't delete event %d\n", e->id);
+       }
        list_delete(list_curitem(eventlist), eventlist);
     endfor
 }
        list_delete(list_curitem(eventlist), eventlist);
     endfor
 }
@@ -450,25 +488,39 @@ public bpfree()
  * and if so do the associated commands.
  */
 
  * and if so do the associated commands.
  */
 
-public Boolean bpact()
+public boolean bpact()
 {
     register Breakpoint p;
 {
     register Breakpoint p;
-    Boolean found;
+    boolean found;
+    integer eventId;
 
     found = false;
     foreach (Breakpoint, p, bplist)
        if (p->bpaddr == pc) {
            if (tracebpts) {
 
     found = false;
     foreach (Breakpoint, p, bplist)
        if (p->bpaddr == pc) {
            if (tracebpts) {
-               printf("breakpoint found at location 0x%x\n", pc);
+               printf("breakpoint for event %d found at location 0x%x\n",
+                   p->event->id, pc);
            }
            found = true;
            if (p->event->temporary) {
            }
            found = true;
            if (p->event->temporary) {
-               delevent(p->event->id);
+               if (not delevent(p->event->id)) {
+                   printf("!! dbx.bpact: can't find event %d\n",
+                       p->event->id);
+               }
            }
            evalcmdlist(p->actions);
            }
            evalcmdlist(p->actions);
+           if (isstopped) {
+               eventId = p->event->id;
+           }
+           if (p->temporary) {
+               list_delete(list_curitem(bplist), bplist);
+           }
        }
     endfor
     if (isstopped) {
        }
     endfor
     if (isstopped) {
+       if (found) {
+           printeventid(eventId);
+       }
        printstatus();
     }
     fflush(stdout);
        printstatus();
     }
     fflush(stdout);
@@ -485,20 +537,25 @@ public Boolean bpact()
  */
 
 public traceon(inst, event, cmdlist)
  */
 
 public traceon(inst, event, cmdlist)
-Boolean inst;
+boolean inst;
 Event event;
 Cmdlist cmdlist;
 {
     register Trcmd trcmd;
     Breakpoint bp;
 Event event;
 Cmdlist cmdlist;
 {
     register Trcmd trcmd;
     Breakpoint bp;
-    Node until;
     Cmdlist actions;
     Address ret;
     Cmdlist actions;
     Address ret;
+    Event e;
 
 
+    if (event == nil) {
+       e = curevent;
+    } else {
+       e = event;
+    }
     trcmd = new(Trcmd);
     ++trid;
     trcmd->trid = trid;
     trcmd = new(Trcmd);
     ++trid;
     trcmd->trid = trid;
-    trcmd->event = event;
+    trcmd->event = e;
     trcmd->cmdlist = cmdlist;
     single_stepping = true;
     if (inst) {
     trcmd->cmdlist = cmdlist;
     single_stepping = true;
     if (inst) {
@@ -509,12 +566,12 @@ Cmdlist cmdlist;
     }
     ret = return_addr();
     if (ret != 0) {
     }
     ret = return_addr();
     if (ret != 0) {
-       until = build(O_EQ, build(O_SYM, pcsym), build(O_LCON, ret));
        actions = buildcmdlist(build(O_TRACEOFF, trcmd->trid));
        actions = buildcmdlist(build(O_TRACEOFF, trcmd->trid));
-       event_once(until, actions);
+       bp = bp_alloc(e, (Address) ret, 0, actions);
+       bp->temporary = true;
     }
     if (tracebpts) {
     }
     if (tracebpts) {
-       printf("adding trace %d for event %d\n", trcmd->trid, event->id);
+       printf("adding trace %d for event %d\n", trcmd->trid, e->id);
     }
 }
 
     }
 }
 
@@ -527,7 +584,7 @@ public traceoff(id)
 Integer id;
 {
     register Trcmd t;
 Integer id;
 {
     register Trcmd t;
-    register Boolean found;
+    register boolean found;
 
     found = false;
     foreach (Trcmd, t, eachline)
 
     found = false;
     foreach (Trcmd, t, eachline)
@@ -548,7 +605,8 @@ Integer id;
            }
        endfor
        if (not found) {
            }
        endfor
        if (not found) {
-           panic("missing trid %d", id);
+           beginerrmsg();
+           fprintf(stderr, "[internal error: trace id %d not found]\n", id);
        }
     }
     if (list_size(eachinst) == 0) {
        }
     }
     if (list_size(eachinst) == 0) {
@@ -597,24 +655,24 @@ public printnews()
  * note it if we're tracing lines.
  */
 
  * note it if we're tracing lines.
  */
 
-private Boolean chklist();
+private boolean chklist();
 
 public callnews(iscall)
 
 public callnews(iscall)
-Boolean iscall;
+boolean iscall;
 {
     if (not chklist(eachline, iscall)) {
        chklist(eachinst, iscall);
     }
 }
 
 {
     if (not chklist(eachline, iscall)) {
        chklist(eachinst, iscall);
     }
 }
 
-private Boolean chklist(list, iscall)
+private boolean chklist(list, iscall)
 List list;
 List list;
-Boolean iscall;
+boolean iscall;
 {
     register Trcmd t;
     register Command cmd;
 
 {
     register Trcmd t;
     register Command cmd;
 
-    curfunc = whatblock(pc);
+    setcurfunc(whatblock(pc));
     foreach (Trcmd, t, list)
        foreach (Command, cmd, t->cmdlist)
            if (cmd->op == O_PRINTSRCPOS and
     foreach (Trcmd, t, list)
        foreach (Command, cmd, t->cmdlist)
            if (cmd->op == O_PRINTSRCPOS and
@@ -663,7 +721,7 @@ private Trinfo findtrinfo(p)
 Node p;
 {
     register Trinfo tp;
 Node p;
 {
     register Trinfo tp;
-    Boolean isnew;
+    boolean isnew;
 
     isnew = true;
     if (trinfolist == nil) {
 
     isnew = true;
     if (trinfolist == nil) {
@@ -702,17 +760,20 @@ Node p;
     register Trinfo tp;
     register int n;
     char buff[MAXTRSIZE];
     register Trinfo tp;
     register int n;
     char buff[MAXTRSIZE];
+    Filename curfile;
     static Lineno prevline;
     static Lineno prevline;
+    static Filename prevfile;
 
     tp = findtrinfo(p);
     n = size(p->nodetype);
     dread(buff, tp->traddr, n);
 
     tp = findtrinfo(p);
     n = size(p->nodetype);
     dread(buff, tp->traddr, n);
+    curfile = srcfilename(pc);
     if (tp->trvalue == nil) {
        tp->trvalue = newarr(char, n);
        mov(buff, tp->trvalue, n);
        mov(buff, sp, n);
        sp += n;
     if (tp->trvalue == nil) {
        tp->trvalue = newarr(char, n);
        mov(buff, tp->trvalue, n);
        mov(buff, sp, n);
        sp += n;
-       printf("initially (at line %d):\t", curline);
+       printf("initially (at line %d in \"%s\"):\t", curline, curfile);
        prtree(stdout, p);
        printf(" = ");
        printval(p->nodetype);
        prtree(stdout, p);
        printf(" = ");
        printval(p->nodetype);
@@ -721,13 +782,14 @@ Node p;
        mov(buff, tp->trvalue, n);
        mov(buff, sp, n);
        sp += n;
        mov(buff, tp->trvalue, n);
        mov(buff, sp, n);
        sp += n;
-       printf("after line %d:\t", prevline);
+       printf("after line %d in \"%s\":\t", prevline, prevfile);
        prtree(stdout, p);
        printf(" = ");
        printval(p->nodetype);
        putchar('\n');
     }
     prevline = curline;
        prtree(stdout, p);
        printf(" = ");
        printval(p->nodetype);
        putchar('\n');
     }
     prevline = curline;
+    prevfile = curfile;
 }
 
 /*
 }
 
 /*
@@ -751,6 +813,13 @@ Node p;
        isstopped = true;
     } else if (cmp(tp->trvalue, buff, n) != 0) {
        mov(buff, tp->trvalue, n);
        isstopped = true;
     } else if (cmp(tp->trvalue, buff, n) != 0) {
        mov(buff, tp->trvalue, n);
+       mov(buff, sp, n);
+       sp += n;
+       printf("after line %d:\t", prevline);
+       prtree(stdout, p);
+       printf(" = ");
+       printval(p->nodetype);
+       putchar('\n');
        isstopped = true;
     }
     prevline = curline;
        isstopped = true;
     }
     prevline = curline;
@@ -788,7 +857,9 @@ public fixbps()
     trfree();
     foreach (Event, e, eventlist)
        if (e->temporary) {
     trfree();
     foreach (Event, e, eventlist)
        if (e->temporary) {
-           delevent(e->id);
+           if (not delevent(e->id)) {
+               printf("!! dbx.fixbps: can't find event %d\n", e->id);
+           }
        }
     endfor
     foreach (Trcmd, t, eachline)
        }
     endfor
     foreach (Trcmd, t, eachline)