format.c 7.2 KB

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