Annotation of researchv9/jtools/src/pi/hostproc.c, revision 1.1.1.1

1.1       root        1: #include "univ.h"
                      2: #include "process.pri"
                      3: #include "sigmask.h"
                      4: #include "hostcore.h"
                      5: #include "expr.pub"
                      6: #include "master.pub"
                      7: #include "bpts.pri"
                      8: #include "frame.pri"
                      9: #include "memory.pub"
                     10: #include "symtab.pub"
                     11: #include "symbol.h"
                     12: #include "srcdir.h"
                     13: #include "asm.pub"
                     14: #include <CC/stdio.h>
                     15: SRCFILE("hostproc.c")
                     16: 
                     17: int access(char*,int);
                     18: int kill(int,int);
                     19: 
                     20: static char *pathexpand(char *f)
                     21: {
                     22:        static char file[128];
                     23:        extern char *PATH;
                     24:        register char *pa, *p;
                     25: 
                     26:        if (*f != '/' && strncmp(f, "./", 2) && strncmp(f, "../", 3) && 
                     27:            (pa=PATH)!=0){
                     28:                while(*pa){
                     29:                        for(p=file; *pa && *pa!=':'; p++,pa++)
                     30:                                *p= *pa;
                     31:                        if(p!=file)
                     32:                                *p++='/';
                     33:                        if(*pa)
                     34:                                pa++;
                     35:                        (void)strcpy(p, f);
                     36:                        if (access(file, 5) != -1)
                     37:                                return file;
                     38:                }
                     39:        }
                     40:        if (access(f, 5) != -1 )
                     41:                return f;
                     42:        return 0;
                     43: }
                     44: 
                     45: void HostProcess::takeoverstopped()
                     46: {
                     47:        if( pad ){
                     48:                openstopped();
                     49:                insert(ERRORKEY, "take over: already open");
                     50:                return;
                     51:        }
                     52:        Pick( "take over", (Action)&HostProcess::substitute, (long) this );
                     53: }
                     54: 
                     55: void HostProcess::reopendump()
                     56: {
                     57:        char *error;
                     58: 
                     59:        insert(ERRORKEY, 0);
                     60:        if( error = core->reopen(procpath, stabpath) ){
                     61:                insert(ERRORKEY, error);
                     62:                return;
                     63:        }
                     64:        docycle();
                     65: }
                     66: 
                     67: void HostProcess::substitute(HostProcess *t)
                     68: {
                     69:        char *error, *oldprocpath, *oldstabpath, *oldcomment;
                     70: 
                     71:        insert(ERRORKEY, 0);
                     72:        if( !core ){
                     73:                insert(ERRORKEY, "that ought to work - but it doesn't");
                     74:                return;
                     75:        }
                     76:        if( !core->online() ){
                     77:                insert(ERRORKEY, "cannot take over a coredump");
                     78:                return;
                     79:        }
                     80:        _bpts->lift();
                     81:        if( error = core->reopen(t->procpath, t->stabpath) ){
                     82:                _bpts->lay();
                     83:                insert(ERRORKEY, error);
                     84:                return;
                     85:        }
                     86:                                        /*      t->kill();      */
                     87:        oldprocpath = procpath;
                     88:        oldstabpath = stabpath;
                     89:        oldcomment = comment;
                     90:        procpath = t->procpath;
                     91:        stabpath = t->stabpath;
                     92:        comment = t->comment;
                     93:        master->makeproc( oldprocpath, oldstabpath, oldcomment );
                     94:        master->insert(t);
                     95:        master->insert(this);
                     96:        banner();
                     97:        if( _asm ) _asm->banner();
                     98:        if( _bpts ) _bpts->banner();
                     99:        if( memory ) memory->banner();
                    100:        if( globals ) globals->banner();
                    101:        if( sigmsk ){
                    102:                sigmsk->banner();
                    103:                sigmsk->updatecore();
                    104:        }
                    105:        if( srcdir ) srcdir->banner();
                    106:        core->symtab()->banner();
                    107:        pad->clear();
                    108:        _bpts->lay();
                    109:        docycle();
                    110: }
                    111: 
                    112: int HostProcess::accept( Action a )
                    113: {
                    114:        return a == (Action)&HostProcess::substitute;
                    115: }
                    116: 
                    117: void HostProcess::imprint()
                    118: {
                    119:        char *parentpath = sf("%s%d", slashname(procpath), hostcore()->ppid() );
                    120:        insert(ERRORKEY, "parent=%s", parentpath);
                    121:        Process *p = master->search(parentpath);
                    122:        if( !p ){
                    123:                insert(ERRORKEY, "parent (%d) not opened", hostcore()->ppid());
                    124:                return;
                    125:        }
                    126:        _bpts->liftparents(p->_bpts);
                    127: }
                    128: 
                    129: void HostProcess::userclose()
                    130: {
                    131:        if( sigmsk ){
                    132:                sigmsk->hostclose();
                    133:                delete sigmsk;
                    134:                sigmsk = 0;
                    135:        }
                    136:        Process::userclose();
                    137: }
                    138: 
                    139: int HostProcess::openstopped(long ischild)
                    140: {
                    141:        Menu m, s;
                    142:        char *error;
                    143: 
                    144:        Process::openpad();
                    145:        if( core ) return 1;
                    146:        insert(ERRORKEY, "Checking process and symbol table...");
                    147:        core = (Core*) new HostCore(this, master);
                    148:        if( error = core->open() ){
                    149:                delete core;
                    150:                core = 0;
                    151:                if( ischild )
                    152:                        m.last( "open child", (Action)&HostProcess::open, 1 );
                    153:                else
                    154:                        m.last( "open process", (Action)&HostProcess::open, 0 );
                    155:                pad->menu( m );
                    156:                insert(ERRORKEY, error);
                    157:                return 0;
                    158:        }
                    159:        insert(ERRORKEY, core->symtab()->warn());
                    160:        globals = new Globals(core);
                    161:        _asm = core->newAsm();
                    162:        if( core->online() ){
                    163:                m.last( "stop", (Action)&HostProcess::stop );
                    164:                m.last( "run",  (Action)&Process::go  );
                    165:        }
                    166:        m.last( "src text",  (Action)&Process::srcfiles    );   /* should check */
                    167:        m.last( "Globals",   (Action)&Process::openglobals );
                    168:        m.last( "RawMemory", (Action)&Process::openmemory  );
                    169:        s.last( "Assembler", (Action)&Process::openasm     );
                    170:        s.last( "User Types",(Action)&Process::opentypes   );
                    171:        if( core->online() ){
                    172:                s.last("Journal", (Action)&Process::openjournal);
                    173:                s.last("Signals", (Action)&HostProcess::opensigmask);
                    174:                s.last("Bpt List", (Action)&HostProcess::openbpts);
                    175:                _bpts = new Bpts(core);
                    176:                _bpts->lay();
                    177:                if( ischild ) imprint();
                    178:                sigmsk = new SigMask(hostcore());
                    179:                m.last("kill?",   (Action)&HostProcess::destroy     );
                    180:        }
                    181:        m.last(s.index("more"));
                    182:        pad->menu(m);
                    183:        pad->makecurrent();
                    184:        docycle();
                    185:        return 1;
                    186: }
                    187: 
                    188: void HostProcess::opensigmask()
                    189: {
                    190:        if( sigmsk ) sigmsk->open();
                    191: }
                    192: 
                    193: void HostProcess::destroy()
                    194: {
                    195:        IF_LIVE( !core->online() ) return;
                    196:        insert(ERRORKEY, core->destroy());
                    197:        docycle();
                    198: }
                    199: 
                    200: void HostProcess::stop()
                    201: {
                    202:        IF_LIVE( !core->online() ) return;
                    203:        if( !(sigmsk->mask&sigmsk->bit(SIGSTOP)) ) sigmsk->setsig(SIGSTOP);
                    204:        Process::stop();
                    205: }
                    206: 
                    207: Index HostProcess::carte()
                    208: {
                    209:        Menu m;
                    210:        if( !strcmp(procpath,"!") ){
                    211:                m.last( "hang & open proc", (Action)&HostProcess::hangopen );
                    212:                m.last( "hang & take over", (Action)&HostProcess::hangtakeover );
                    213:        } else if( !strcmp(basename(procpath), "core") )
                    214:                m.last( "open coredump",(Action)&HostProcess::openstopped );
                    215:        else {
                    216:                m.last( "open process",  (Action)&HostProcess::open, 0 );
                    217:                m.last( "take over",    (Action)&HostProcess::takeover );
                    218:                m.last( "open child", (Action)&HostProcess::open, 1 );
                    219:        }
                    220:        return m.index();
                    221: }
                    222: 
                    223: void HostProcess::hang()
                    224: {
                    225:        char program[128];
                    226:        char *argv[10];
                    227:        
                    228:        char *from = stabpath;
                    229:        char *to = argv[0] = program;
                    230:        int i = 1;
                    231:        for(;;) {
                    232:                while(*from && *from != ' ')
                    233:                        *to++ = *from++;
                    234:                *to++ = 0;
                    235:                if (*from == 0) {
                    236:                        argv[i] = 0;
                    237:                        break;
                    238:                } else {
                    239:                        while (*++from == ' ')
                    240:                                ;
                    241:                        if (*from)
                    242:                                argv[i++] = to;
                    243:                }
                    244:        }
                    245:        char *ssave = stabpath;
                    246:        stabpath  = sf("%s", argv[0]);
                    247:        int pid = ::fork();
                    248:        if( !pid ){
                    249:                int fd;
                    250:                for( fd = 0; fd < _NFILE; ++fd ) ::close(fd);
                    251:                ::open("/dev/null", 2);
                    252:                ::dup2(0, 1);
                    253:                ::dup2(0, 2);
                    254:                ::setpgrp(0, ::getpid());
                    255:                 ::ptrace(PTRACE_TRACEME, 0, 0, 0, 0);
                    256:                 ::execvp(argv[0], argv);
                    257:                ::exit(0);
                    258:        }               
                    259:        procpath = sf("%d", pid);
                    260:        master->makeproc("!", ssave);
                    261:        master->insert(this);
                    262: }
                    263: 
                    264: void HostProcess::hangopen()
                    265: {
                    266:        hang();
                    267:        openstopped();
                    268: }
                    269: 
                    270: void HostProcess::hangtakeover()
                    271: {
                    272:        hang();
                    273:        takeoverstopped();
                    274: }
                    275: 
                    276: void HostProcess::open(long ischild)
                    277: {
                    278:        char *error;
                    279:        int pid = 0;
                    280: 
                    281:        if( core )
                    282:                return;
                    283:        if (!stabpath) {
                    284:                char *nstab;
                    285:                char file[80];
                    286:                sscanf(comment, " %*s %*s %*s %s", file);
                    287:                if ((nstab = pathexpand(file)) == 0) {
                    288:                        error = "Can't find symbol table file";
                    289:                        goto err;
                    290:                }
                    291:                stabpath = sf("%s", nstab);
                    292:                comment = 0;
                    293:                master->insert(this);
                    294:        }
                    295:        sscanf(procpath, "%d", &pid);
                    296:        ::kill(pid, SIGCONT);
                    297:        if (::ptrace(PTRACE_ATTACH, pid, 0, 0, 0)) {
                    298:                error = "Can't attach to process";
                    299:                goto err;
                    300:        }
                    301:        openstopped(ischild);
                    302:        return;
                    303: err:
                    304:        Process::openpad();
                    305:        insert(ERRORKEY, error);
                    306:        return;
                    307:        
                    308: }
                    309: 
                    310: void HostProcess::takeover()
                    311: {
                    312:        if (!stabpath) {
                    313:                char *nstab;
                    314:                char file[80];
                    315:                sscanf(comment, " %*s %*s %*s %s", file);
                    316:                if ((nstab = pathexpand(file)) == 0)
                    317:                        return;
                    318:                stabpath = sf("%s", nstab);
                    319:                comment = 0;
                    320:                master->insert(this);
                    321:        }
                    322:        int pid = 0;
                    323:        sscanf(procpath, "%d", &pid);
                    324:        ::kill(pid, SIGCONT);
                    325:        if (::ptrace(PTRACE_ATTACH, pid, 0, 0, 0))
                    326:                return;
                    327:        takeoverstopped();
                    328: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.