format.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. *
  3. * debugger
  4. *
  5. */
  6. #include "defs.h"
  7. #include "fns.h"
  8. void
  9. scanform(long icount, int prt, char *ifp, Map *map, int literal)
  10. {
  11. char *fp;
  12. char c;
  13. int fcount;
  14. ADDR savdot;
  15. int firstpass;
  16. firstpass = 1;
  17. while (icount) {
  18. fp=ifp;
  19. savdot=dot;
  20. /*now loop over format*/
  21. while (*fp) {
  22. if (!isdigit(*fp))
  23. fcount = 1;
  24. else {
  25. fcount = 0;
  26. while (isdigit(c = *fp++)) {
  27. fcount *= 10;
  28. fcount += c-'0';
  29. }
  30. fp--;
  31. }
  32. if (*fp==0)
  33. break;
  34. fp=exform(fcount,prt,fp,map,literal,firstpass);
  35. firstpass = 0;
  36. }
  37. dotinc=dot-savdot;
  38. dot=savdot;
  39. if (--icount)
  40. dot=inkdot(dotinc);
  41. }
  42. }
  43. char *
  44. exform(int fcount, int prt, char *ifp, Map *map, int literal, int firstpass)
  45. {
  46. /* execute single format item `fcount' times
  47. * sets `dotinc' and moves `dot'
  48. * returns address of next format item
  49. */
  50. vlong v;
  51. long w;
  52. ulong savdot;
  53. char *fp;
  54. char c, modifier;
  55. int i;
  56. ushort sh, *sp;
  57. uchar ch, *cp;
  58. Symbol s;
  59. char buf[512];
  60. extern int printcol;
  61. fp = 0;
  62. while (fcount > 0) {
  63. fp = ifp;
  64. c = *fp;
  65. modifier = *fp++;
  66. if (firstpass) {
  67. firstpass = 0;
  68. if (!literal && (c == 'i' || c == 'I' || c == 'M')
  69. && (dot & (mach->pcquant-1))) {
  70. dprint("warning: instruction not aligned");
  71. printc('\n');
  72. }
  73. if (prt && modifier != 'a' && modifier != 'A') {
  74. symoff(buf, 512, dot, CANY);
  75. dprint("%s%c%16t", buf, map==symmap? '?':'/');
  76. }
  77. }
  78. if (printcol==0 && modifier != 'a' && modifier != 'A')
  79. dprint("\t\t");
  80. switch(modifier) {
  81. case SPC:
  82. case TB:
  83. dotinc = 0;
  84. break;
  85. case 't':
  86. case 'T':
  87. dprint("%*t", fcount);
  88. dotinc = 0;
  89. return(fp);
  90. case 'a':
  91. symoff(buf, sizeof(buf), dot, CANY);
  92. dprint("%s%c%16t", buf, map==symmap? '?':'/');
  93. dotinc = 0;
  94. break;
  95. case 'A':
  96. dprint("%#lux%10t", dot);
  97. dotinc = 0;
  98. break;
  99. case 'p':
  100. if (get4(map, dot, &w) < 0)
  101. error("%r");
  102. symoff(buf, sizeof(buf), w, CANY);
  103. dprint("%s%16t", buf);
  104. dotinc = mach->szaddr;
  105. break;
  106. case 'u':
  107. case 'd':
  108. case 'x':
  109. case 'o':
  110. case 'q':
  111. if (literal)
  112. sh = (ushort) dot;
  113. else if (get2(map, dot, &sh) < 0)
  114. error("%r");
  115. w = sh;
  116. dotinc = 2;
  117. if (c == 'u')
  118. dprint("%-8lud", w);
  119. else if (c == 'x')
  120. dprint("%-8#lux", w);
  121. else if (c == 'd')
  122. dprint("%-8ld", w);
  123. else if (c == 'o')
  124. dprint("%-8#luo", w);
  125. else if (c == 'q')
  126. dprint("%-8#lo", w);
  127. break;
  128. case 'U':
  129. case 'D':
  130. case 'X':
  131. case 'O':
  132. case 'Q':
  133. if (literal)
  134. w = (long) dot;
  135. else if (get4(map, dot, &w) < 0)
  136. error("%r");
  137. dotinc = 4;
  138. if (c == 'U')
  139. dprint("%-16lud", w);
  140. else if (c == 'X')
  141. dprint("%-16#lux", w);
  142. else if (c == 'D')
  143. dprint("%-16ld", w);
  144. else if (c == 'O')
  145. dprint("%-#16luo", w);
  146. else if (c == 'Q')
  147. dprint("%-#16lo", w);
  148. break;
  149. case 'Z':
  150. case 'V':
  151. case 'Y':
  152. if (literal)
  153. v = dot;
  154. else if (get8(map, dot, &v) < 0)
  155. error("%r");
  156. dotinc = 8;
  157. if (c == 'Y')
  158. dprint("%-20#llux", v);
  159. else if (c == 'V')
  160. dprint("%-20lld", v);
  161. else if (c == 'Z')
  162. dprint("%-20llud", v);
  163. break;
  164. case 'B':
  165. case 'b':
  166. case 'c':
  167. case 'C':
  168. if (literal)
  169. ch = (uchar) dot;
  170. else if (get1(map, dot, &ch, 1) < 0)
  171. error("%r");
  172. if (modifier == 'C')
  173. printesc(ch);
  174. else if (modifier == 'B' || modifier == 'b')
  175. dprint("%-8#lux", (long) ch);
  176. else
  177. printc(ch);
  178. dotinc = 1;
  179. break;
  180. case 'r':
  181. if (literal)
  182. sh = (ushort) dot;
  183. else if (get2(map, dot, &sh) < 0)
  184. error("%r");
  185. dprint("%C", sh);
  186. dotinc = 2;
  187. break;
  188. case 'R':
  189. if (literal) {
  190. sp = (ushort*) &dot;
  191. dprint("%C%C", sp[0], sp[1]);
  192. endline();
  193. dotinc = 4;
  194. break;
  195. }
  196. savdot=dot;
  197. while ((i = get2(map, dot, &sh) > 0) && sh) {
  198. dot=inkdot(2);
  199. dprint("%C", sh);
  200. endline();
  201. }
  202. if (i < 0)
  203. error("%r");
  204. dotinc = dot-savdot+2;
  205. dot=savdot;
  206. break;
  207. case 's':
  208. if (literal) {
  209. cp = (uchar*) &dot;
  210. for (i = 0; i < 4; i++)
  211. buf[i] = cp[i];
  212. buf[i] = 0;
  213. dprint("%s", buf);
  214. endline();
  215. dotinc = 4;
  216. break;
  217. }
  218. savdot = dot;
  219. for(;;){
  220. i = 0;
  221. do{
  222. if (get1(map, dot, (uchar*)&buf[i], 1) < 0)
  223. error("%r");
  224. dot = inkdot(1);
  225. i++;
  226. }while(!fullrune(buf, i));
  227. if(buf[0] == 0)
  228. break;
  229. buf[i] = 0;
  230. dprint("%s", buf);
  231. endline();
  232. }
  233. dotinc = dot-savdot+1;
  234. dot = savdot;
  235. break;
  236. case 'S':
  237. if (literal) {
  238. cp = (uchar*) &dot;
  239. for (i = 0; i < 4; i++)
  240. printesc(cp[i]);
  241. endline();
  242. dotinc = 4;
  243. break;
  244. }
  245. savdot=dot;
  246. while ((i = get1(map, dot, &ch, 1) > 0) && ch) {
  247. dot=inkdot(1);
  248. printesc(ch);
  249. endline();
  250. }
  251. if (i < 0)
  252. error("%r");
  253. dotinc = dot-savdot+1;
  254. dot=savdot;
  255. break;
  256. case 'I':
  257. case 'i':
  258. dotinc = machdata->das(map, dot, modifier, buf, sizeof(buf));
  259. if (dotinc < 0)
  260. error("%r");
  261. dprint("%s\n", buf);
  262. break;
  263. case 'M':
  264. dotinc = machdata->hexinst(map, dot, buf, sizeof(buf));
  265. if (dotinc < 0)
  266. error("%r");
  267. dprint("%s", buf);
  268. if (*fp) {
  269. dotinc = 0;
  270. dprint("%48t");
  271. } else
  272. dprint("\n");
  273. break;
  274. case 'f':
  275. /* BUG: 'f' and 'F' assume szdouble is sizeof(vlong) in the literal case */
  276. if (literal) {
  277. v = machdata->swav((ulong)dot);
  278. memmove(buf, &v, mach->szfloat);
  279. }else if (get1(map, dot, (uchar*)buf, mach->szfloat) < 0)
  280. error("%r");
  281. machdata->sftos(buf, sizeof(buf), (void*) buf);
  282. dprint("%s\n", buf);
  283. dotinc = mach->szfloat;
  284. break;
  285. case 'F':
  286. /* BUG: 'f' and 'F' assume szdouble is sizeof(vlong) in the literal case */
  287. if (literal) {
  288. v = machdata->swav(dot);
  289. memmove(buf, &v, mach->szdouble);
  290. }else if (get1(map, dot, (uchar*)buf, mach->szdouble) < 0)
  291. error("%r");
  292. machdata->dftos(buf, sizeof(buf), (void*) buf);
  293. dprint("%s\n", buf);
  294. dotinc = mach->szdouble;
  295. break;
  296. case 'n':
  297. case 'N':
  298. printc('\n');
  299. dotinc=0;
  300. break;
  301. case '"':
  302. dotinc=0;
  303. while (*fp != '"' && *fp)
  304. printc(*fp++);
  305. if (*fp)
  306. fp++;
  307. break;
  308. case '^':
  309. dot=inkdot(-dotinc*fcount);
  310. return(fp);
  311. case '+':
  312. dot=inkdot((WORD)fcount);
  313. return(fp);
  314. case '-':
  315. dot=inkdot(-(WORD)fcount);
  316. return(fp);
  317. case 'z':
  318. if (findsym(dot, CTEXT, &s))
  319. dprint("%s() ", s.name);
  320. printsource(dot);
  321. printc(EOR);
  322. return fp;
  323. default:
  324. error("bad modifier");
  325. }
  326. if (map->seg[0].fd >= 0)
  327. dot=inkdot(dotinc);
  328. fcount--;
  329. endline();
  330. }
  331. return(fp);
  332. }
  333. void
  334. printesc(int c)
  335. {
  336. static char hex[] = "0123456789abcdef";
  337. if (c < SPC || c >= 0177)
  338. dprint("\\x%c%c", hex[(c&0xF0)>>4], hex[c&0xF]);
  339. else
  340. printc(c);
  341. }
  342. ADDR
  343. inkdot(WORD incr)
  344. {
  345. ADDR newdot;
  346. newdot=dot+incr;
  347. if ((incr >= 0 && newdot < dot)
  348. || (incr < 0 && newdot > dot))
  349. error("address wraparound");
  350. return(newdot);
  351. }