URI: 
       tDebugging libthread for acme. - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
   DIR commit 02a1a5c18bec9ca13fad2b7b12ae65c580f6fdf4
   DIR parent 315e309098f8b9f6ee8f869ceef8ea0aacce6c60
  HTML Author: rsc <devnull@localhost>
       Date:   Fri,  5 Mar 2004 01:12:11 +0000
       
       Debugging libthread for acme.
       
       Diffstat:
         M src/cmd/acme/acme.c                 |       2 ++
         M src/libthread/channel.c             |      49 ++++++++++++++++++++++++-------
         M src/libthread/main.c                |       8 ++++++++
         M src/libthread/sched.c               |      14 ++++++++++++--
         M src/libthread/threadimpl.h          |       1 +
       
       5 files changed, 62 insertions(+), 12 deletions(-)
       ---
   DIR diff --git a/src/cmd/acme/acme.c b/src/cmd/acme/acme.c
       t@@ -63,6 +63,8 @@ threadmain(int argc, char *argv[])
                int ncol;
                Display *d;
        
       +extern int _threaddebuglevel;
       +_threaddebuglevel = ~0;
                rfork(RFENVG|RFNAMEG);
        
                ncol = -1;
   DIR diff --git a/src/libthread/channel.c b/src/libthread/channel.c
       t@@ -9,6 +9,16 @@ static int altexec(Alt*, int);
        int _threadhighnentry;
        int _threadnalt;
        
       +static void
       +setuserpc(ulong pc)
       +{
       +        Thread *t;
       +
       +        t = _threadgetproc()->thread;
       +        if(t)
       +                t->userpc = pc;
       +}
       +
        static int
        canexec(Alt *a)
        {
       t@@ -86,8 +96,8 @@ chancreate(int elemsize, int elemcnt)
                return c;
        }
        
       -int
       -alt(Alt *alts)
       +static int
       +_alt(Alt *alts)
        {
                Alt *a, *xa;
                Channel *volatile c;
       t@@ -197,6 +207,13 @@ _threadnalt++;
                return a - alts;
        }
        
       +int
       +alt(Alt *alts)
       +{
       +        setuserpc(getcallerpc(&alts));
       +        return _alt(alts);
       +}
       +
        static int
        runop(int op, Channel *c, void *v, int nb)
        {
       t@@ -214,7 +231,7 @@ runop(int op, Channel *c, void *v, int nb)
                a[1].op = CHANEND;
                if(nb)
                        a[1].op = CHANNOBLK;
       -        switch(r=alt(a)){
       +        switch(r=_alt(a)){
                case -1:        /* interrupted */
                        return -1;
                case 1:        /* nonblocking, didn't accomplish anything */
       t@@ -232,24 +249,28 @@ runop(int op, Channel *c, void *v, int nb)
        int
        recv(Channel *c, void *v)
        {
       +        setuserpc(getcallerpc(&c));
                return runop(CHANRCV, c, v, 0);
        }
        
        int
        nbrecv(Channel *c, void *v)
        {
       +        setuserpc(getcallerpc(&c));
                return runop(CHANRCV, c, v, 1);
        }
        
        int
        send(Channel *c, void *v)
        {
       +        setuserpc(getcallerpc(&c));
                return runop(CHANSND, c, v, 0);
        }
        
        int
        nbsend(Channel *c, void *v)
        {
       +        setuserpc(getcallerpc(&c));
                return runop(CHANSND, c, v, 1);
        }
        
       t@@ -266,6 +287,7 @@ channelsize(Channel *c, int sz)
        int
        sendul(Channel *c, ulong v)
        {
       +        setuserpc(getcallerpc(&c));
                channelsize(c, sizeof(ulong));
                return send(c, &v);
        }
       t@@ -275,8 +297,9 @@ recvul(Channel *c)
        {
                ulong v;
        
       +        setuserpc(getcallerpc(&c));
                channelsize(c, sizeof(ulong));
       -        if(recv(c, &v) < 0)
       +        if(runop(CHANRCV, c, &v, 0) < 0)
                        return ~0;
                return v;
        }
       t@@ -284,8 +307,9 @@ recvul(Channel *c)
        int
        sendp(Channel *c, void *v)
        {
       +        setuserpc(getcallerpc(&c));
                channelsize(c, sizeof(void*));
       -        return send(c, &v);
       +        return runop(CHANSND, c, &v, 0);
        }
        
        void*
       t@@ -293,8 +317,9 @@ recvp(Channel *c)
        {
                void *v;
        
       +        setuserpc(getcallerpc(&c));
                channelsize(c, sizeof(void*));
       -        if(recv(c, &v) < 0)
       +        if(runop(CHANRCV, c, &v, 0) < 0)
                        return nil;
                return v;
        }
       t@@ -302,8 +327,9 @@ recvp(Channel *c)
        int
        nbsendul(Channel *c, ulong v)
        {
       +        setuserpc(getcallerpc(&c));
                channelsize(c, sizeof(ulong));
       -        return nbsend(c, &v);
       +        return runop(CHANSND, c, &v, 1);
        }
        
        ulong
       t@@ -311,8 +337,9 @@ nbrecvul(Channel *c)
        {
                ulong v;
        
       +        setuserpc(getcallerpc(&c));
                channelsize(c, sizeof(ulong));
       -        if(nbrecv(c, &v) == 0)
       +        if(runop(CHANRCV, c, &v, 1) == 0)
                        return 0;
                return v;
        }
       t@@ -320,8 +347,9 @@ nbrecvul(Channel *c)
        int
        nbsendp(Channel *c, void *v)
        {
       +        setuserpc(getcallerpc(&c));
                channelsize(c, sizeof(void*));
       -        return nbsend(c, &v);
       +        return runop(CHANSND, c, &v, 1);
        }
        
        void*
       t@@ -329,8 +357,9 @@ nbrecvp(Channel *c)
        {
                void *v;
        
       +        setuserpc(getcallerpc(&c));
                channelsize(c, sizeof(void*));
       -        if(nbrecv(c, &v) == 0)
       +        if(runop(CHANRCV, c, &v, 1) == 0)
                        return nil;
                return v;
        }
   DIR diff --git a/src/libthread/main.c b/src/libthread/main.c
       t@@ -15,6 +15,13 @@ static void mainlauncher(void*);
        extern void (*_sysfatal)(char*, va_list);
        
        void
       +_threadstatus(int x)
       +{
       +        USED(x);
       +        threadstatus();
       +}
       +
       +void
        _threaddie(int x)
        {
                extern char *_threadexitsallstatus;
       t@@ -38,6 +45,7 @@ main(int argc, char **argv)
        
                signal(SIGTERM, _threaddie);
                signal(SIGCHLD, _nop);
       +        signal(SIGINFO, _threadstatus);
        //        rfork(RFREND);
        
        //_threaddebuglevel = (DBGSCHED|DBGCHAN|DBGREND)^~0;
   DIR diff --git a/src/libthread/sched.c b/src/libthread/sched.c
       t@@ -4,7 +4,6 @@
        
        //static Thread        *runthread(Proc*);
        
       -#if 0
        static char *_psstate[] = {
                "Dead",
                "Running",
       t@@ -19,7 +18,6 @@ psstate(int s)
                        return "unknown";
                return _psstate[s];
        }
       -#endif
        
        void
        _schedinit(void *arg)
       t@@ -271,3 +269,15 @@ yield(void)
                _sched();
        }
        
       +void
       +threadstatus(void)
       +{
       +        Proc *p;
       +        Thread *t;
       +
       +        p = _threadgetproc();
       +        for(t=p->threads.head; t; t=t->nextt)
       +                fprint(2, "[%3d] %s userpc=%lux\n",
       +                        t->id, psstate(t->state), t->userpc);
       +}
       +        
   DIR diff --git a/src/libthread/threadimpl.h b/src/libthread/threadimpl.h
       t@@ -96,6 +96,7 @@ struct Thread
        
                Chanstate        chan;                /* which channel operation is current */
                Alt                *alt;                        /* pointer to current alt structure (debugging) */
       +        ulong                userpc;
        
                void*        udata[NPRIV];        /* User per-thread data pointer */
        };