home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 483 / szadbsrc / adb1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-21  |  6.3 KB  |  441 lines

  1. /* Copyright (c) 1990 by Sozobon, Limited.  Authors: Johann Ruegg, Don Dugger
  2.  *
  3.  * Permission is granted to anyone to use this software for any purpose
  4.  * on any computer system, and to redistribute it freely, with the
  5.  * following restrictions:
  6.  * 1) No charge may be made other than reasonable charges for reproduction.
  7.  * 2) Modified versions must be clearly marked as such.
  8.  * 3) The authors are not responsible for any harmful consequences
  9.  *    of using this software, even if they result from defects in it.
  10.  *
  11.  *    adb1.c
  12.  */
  13.  
  14. #include <setjmp.h>
  15. #include "adb.h"
  16.  
  17. #define IN_ADB1
  18. #include "lang.h"
  19.  
  20. extern struct file binary;
  21.  
  22. extern long dot;
  23. extern int dotoff;
  24. extern long maxoff;
  25. extern int ibase;
  26.  
  27. extern int lastc;
  28. extern char sign;
  29.  
  30. extern jmp_buf jmpb;
  31.  
  32.  
  33. getfmt(fmt)
  34. char *fmt;
  35. {
  36.     char c;
  37.  
  38.     while ((*fmt = getchr()) != '\n')
  39.         fmt++;
  40.     *fmt = '\0';
  41.     pushc('\n');
  42.     return;
  43. }
  44.  
  45. long getdot(d, n)
  46. long d;
  47. int n;
  48. {
  49.     long l;
  50.  
  51.     l = dot;
  52.     if (n == 2)
  53.         l &= 0xffff;
  54.     else if (n == 1)
  55.         l &= 0xff;
  56.     return(l);
  57. }
  58.  
  59. unsigned char
  60. getb(fp)
  61. struct file *fp;
  62. {
  63.     long getn();
  64.  
  65.     return(getn(dot + dotoff, 1));
  66. }
  67.  
  68. unsigned int
  69. getwd()
  70. {
  71.     long getn();
  72.  
  73.     return(getn(dot + dotoff, 2));
  74. }
  75.  
  76. putn(v, d, n)
  77. long v, d;
  78. int n;
  79. {
  80.     union {
  81.         int i;
  82.         long l;
  83.     } no, val;
  84.     long b;
  85.     register int o;
  86.     char *p, *s;
  87.  
  88.     b = d >> LOGBS;
  89.     o = d & (BSIZE - 1);
  90.     switch (n) {
  91.  
  92.     case 2:
  93.         p = (char *)(&no.i);
  94.         val.i = v;
  95.         s = (char *)(&val.i);
  96.         break;
  97.     case 4:
  98.         p = (char *)(&no.l);
  99.         val.l = v;
  100.         s = (char *)(&val.l);
  101.         break;
  102.  
  103.     }
  104.     rdsub(d, p, n, 0);
  105.     dotoff += n;
  106.     switch (n) {
  107.  
  108.     case 2:
  109.         bcopy(s, p, n);
  110.         break;
  111.     case 4:
  112.         bcopy(s, p, n);
  113.         break;
  114.  
  115.     }
  116.     wrsub(p, d, n, 0);
  117.     return;
  118. }
  119.  
  120. long
  121. getn(d, n)
  122. unsigned long d;
  123. int n;
  124. {
  125.     unsigned long b, no;
  126.     register unsigned int o;
  127.     char *p;
  128.  
  129.     b = d >> LOGBS;
  130.     o = d & (BSIZE - 1);
  131.     rdsub(d, &b, n, 0);
  132.     p = (char *)&b;
  133.     dotoff += n;
  134.     switch (n) {
  135.  
  136.     case 1:
  137.         no = *p;
  138.         break;
  139.     case 2:
  140.         no = *(int *)p;
  141.         break;
  142.     case 4:
  143.         no = *(long *)p;
  144.         break;
  145.  
  146.     }
  147.     return(no);
  148. }
  149.  
  150. puto(n, s)
  151. unsigned long n;
  152. int s;
  153. {
  154.  
  155.     if (n > 0)
  156.         puto((n >> 3) & 0x1fffffff, --s);
  157.     else
  158.         while (s-- > 0)
  159.             putchr(' ');
  160.     putchr((char)((n & 7) + '0'));
  161.     return;
  162. }
  163.  
  164. putd(n, s)
  165. long n;
  166. int s;
  167. {
  168.  
  169.     if (n < 0) {
  170.         s--;
  171.         n = -n;
  172.         if (n < 0) {
  173.             while (s-- > 0)
  174.                 putchr(' ');
  175.             putchr('?');
  176.             return;
  177.         } else
  178.             sign = '-';
  179.     }
  180.     if (n > 9)
  181.         putd(n / 10, --s);
  182.     else
  183.         while (s-- > 0)
  184.             putchr(' ');
  185.     if (sign) {
  186.         putchr(sign);
  187.         sign = 0;
  188.     }
  189.     putchr((char)((n % 10) + '0'));
  190.     return;
  191. }
  192.  
  193. putx(n, s)
  194. unsigned long n;
  195. int s;
  196. {
  197.  
  198.     if (n > 0xf)
  199.         putx((n >> 4) & 0xfffffff, --s);
  200.     else {
  201.         while (s-- > 0)
  202.             putchr(' ');
  203.     }
  204.     putchr("0123456789abcdef"[n & 0xf]);
  205.     return;
  206. }
  207.  
  208. prtn(n, s)
  209. long n;
  210. int s;
  211. {
  212.  
  213.     switch (ibase) {
  214.  
  215.     case 8:
  216.         puto(n, s);
  217.         break;
  218.     case 10:
  219.         putd(n, s);
  220.         break;
  221.     default:
  222.     case 16:
  223.         putx(n, s);
  224.         break;
  225.  
  226.     }
  227.     return;
  228. }
  229.  
  230. prt(s)
  231. char *s;
  232. {
  233.  
  234.     while (*s)
  235.         putchr(*s++);
  236.     return;
  237. }
  238.  
  239. putchr(c)
  240. char c;
  241. {
  242.     putchar(c);
  243. }
  244.  
  245. relsym()
  246. {
  247.     register struct symbol *sp;
  248.     extern struct basepg *bpage;
  249.  
  250.     sp = binary.symptr;
  251.     while (sp) {
  252.         sp->value += (long)bpage->p_tbase;
  253.         sp = sp->next;
  254.     }
  255. }
  256.  
  257. setsym(fd, n)
  258. struct file *fd;
  259. int n;
  260. {
  261.     static struct filsym sym;
  262.  
  263.     while (n--) {
  264.         if (read(fd->fid, &sym, sizeof(sym)) != sizeof(sym))
  265.             return -1;
  266.         if ((sym.flag & S_EXT) && 
  267.             (sym.flag & (S_DATA|S_TEXT|S_BSS))) {
  268.             addsym(&binary.symptr, &sym);
  269.         }
  270.     }
  271.     return 0;
  272. }
  273.  
  274. addsym(spp, sym)
  275. struct symbol **spp;
  276. struct filsym *sym;
  277. {
  278.     char *malloc();
  279.     register long v;
  280.     register int i;
  281.     char *cp, *cptr;
  282.     struct symbol *p;
  283.     register struct symbol *sp;
  284.  
  285.     v = sym->value;
  286.     sp = *spp;
  287.     while (sp)
  288.         if (sp->value > v)
  289.             break;
  290.         else {
  291.             spp = &sp->next;
  292.             sp = *spp;
  293.         }
  294.     if ((p = (struct symbol *)malloc(sizeof(*p))) == 0) {
  295. /*        prtf("can't allocate %i bytes for symbol\n", sizeof(*p)); */
  296.         prtf(M1, sizeof(*p));
  297.         return;
  298.     }
  299.     p->next = sp;
  300.     *spp = p;
  301.     p->value = v;
  302.     cp = sym->name;
  303.     cptr = p->name;
  304.     for (i = 0; i < sizeof(sym->name); i++)
  305.         if (*cptr = *cp++)
  306.             cptr++;
  307.         else
  308.             break;
  309.     *cptr = '\0';
  310.     return;
  311. }
  312.  
  313. struct symbol *
  314. findnam(cp, sp)
  315. char *cp;
  316. struct symbol *sp;
  317. {
  318.     while (sp) {
  319.         if (strncmp(cp, sp->name, 8) == 0)
  320.             return(sp);
  321.         else
  322.             sp = sp->next;
  323.     }
  324.     return(0);
  325. }
  326.  
  327. struct symbol *
  328. findsym(v, sp)
  329. unsigned long v;
  330. struct symbol *sp;
  331. {
  332.     struct symbol *lp;
  333.     unsigned long val;
  334.  
  335.     lp = sp;
  336.     while (sp) {
  337.         val = sp->value;
  338.         if (val > v)
  339.             break;
  340.         else {
  341.             lp = sp;
  342.             sp = sp->next;
  343.         }
  344.     }
  345.     if (lp && v >= lp->value && v < lp->value + maxoff)
  346.         return(lp);
  347.     return(0);
  348. }
  349.  
  350. htoi(cp)
  351. char *cp;
  352. {
  353.     int n;
  354.     char c;
  355.  
  356.     n = 0;
  357.     while (type(c = *cp++) & HEXDIG) {
  358.         if (c >= 'a' && c <= 'f')
  359.             c -= 'a' - '9' - 1;
  360.         else if (c >= 'A' && c <= 'F')
  361.             c -= 'A' - '9' - 1;
  362.         n = (n << 4) + (c - '0');
  363.     }
  364.     return(n);
  365. }
  366.  
  367. prtsym(v, sp)
  368. unsigned long v;
  369. struct symbol *sp;
  370. {
  371.  
  372.     prt(sp->name);
  373.     if (v != sp->value) {
  374.         putchr('+');
  375.         prtn(v - sp->value, 0);
  376.     }
  377.     return;
  378. }
  379.  
  380. rdsub(from, to, n, pid)
  381. char *from, *to;
  382. int n, pid;
  383. {
  384.     long l[2];
  385.     register int off;
  386.  
  387.     off = (int)from & (sizeof(l[0]) - 1);
  388.     from -= off;
  389.     l[0] = ptrace(RD_DATA, pid, from, 0);
  390.     if ((off + n) > sizeof(l[0]))
  391.         l[1] = ptrace(RD_DATA, pid, from + sizeof(l[0]), 0);
  392.     bcopy((char *)l + off, to, n);
  393.     return;
  394. }
  395.  
  396. wrsub(from, to, n, pid)
  397. char *from, *to;
  398. int n, pid;
  399. {
  400.     long l[2];
  401.     register int off;
  402.  
  403.     off = (int)to & (sizeof(l[0]) - 1);
  404.     to -= off;
  405.     if (off || n != sizeof(l[0]))
  406.         l[0] = ptrace(RD_DATA, pid, to, 0);
  407.     if ((off + n) > sizeof(l[0]))
  408.         l[1] = ptrace(RD_DATA, pid, to + sizeof(l[0]), 0);
  409.     bcopy(from, (char *)l + off, n);
  410.     ptrace(WR_DATA, pid, to, l[0]);
  411.     if ((off + n) > sizeof(l[0]))
  412.         ptrace(WR_DATA, pid, to + sizeof(l[0]), l[1]);
  413.     return;
  414. }
  415.  
  416. cmdsreg(c,fmt,get)
  417. int c;
  418. char *fmt;
  419. long (*get)();
  420. {
  421.     char buf[10], *cp, c;
  422.     int i;
  423.     struct regs *rp, *findreg();    
  424.  
  425.     cp = buf;
  426.     i = 0;
  427.     while (i<5 && (c = getchr()) != '\n') {
  428.         i++;
  429.         *cp++ = c;
  430.     }
  431.     *cp = '\0';
  432.     if (c == '\n')
  433.         pushc('\n');
  434.  
  435.     if (rp = findreg(buf)) {
  436.         *rp->value = dot;
  437.         return;
  438.     }
  439.     error(BADRNAME);
  440. }
  441.