port 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // portable acid for all architectures
  2. defn pfl(addr)
  3. {
  4. print(pcfile(addr), ":", pcline(addr), "\n");
  5. }
  6. defn
  7. notestk(addr)
  8. {
  9. local pc, sp;
  10. complex Ureg addr;
  11. pc = addr.pc\X;
  12. sp = addr.sp\X;
  13. print("Note pc:", pc, " sp:", sp, " ", fmt(pc, 'a'), " ");
  14. pfl(pc);
  15. _stk(pc, sp, linkreg(addr), 1);
  16. }
  17. defn
  18. notelstk(addr)
  19. {
  20. local pc, sp;
  21. complex Ureg addr;
  22. pc = addr.pc\X;
  23. sp = addr.sp\X;
  24. print("Note pc:", pc, " sp:", sp, " ", fmt(pc, 'a'), " ");
  25. pfl(pc);
  26. _stk(pc, sp, linkreg(addr), 1);
  27. }
  28. defn labstk(l) // trace from a label
  29. {
  30. _stk(*(l+4), *l, linkreg(0), 0);
  31. }
  32. defn params(param)
  33. {
  34. while param do {
  35. sym = head param;
  36. print(sym[0], "=", sym[1]);
  37. param = tail param;
  38. if param then
  39. print (",");
  40. }
  41. }
  42. defn locals(l)
  43. {
  44. local sym;
  45. while l do {
  46. sym = head l;
  47. print("\t", sym[0], "=", sym[1], "\n");
  48. l = tail l;
  49. }
  50. }
  51. defn _stk(pc, sp, link, dolocals)
  52. {
  53. local stk;
  54. print("At pc:", pc, ":", fmt(pc, 'a'), " ");
  55. pfl(pc);
  56. stk = strace(pc, sp, link);
  57. while stk do {
  58. frame = head stk;
  59. print(fmt(frame[0], 'a'), "(");
  60. params(frame[2]);
  61. print(") ", pcfile(frame[0]), ":", pcline(frame[0]));
  62. print("\n\tcalled from ", fmt(frame[1], 'a'), " ");
  63. pfl(frame[1]);
  64. stk = tail stk;
  65. if dolocals then
  66. locals(frame[3]);
  67. }
  68. }
  69. defn findsrc(file)
  70. {
  71. local lst, src;
  72. if file[0] == '/' then {
  73. src = file(file);
  74. if src != {} then {
  75. srcfiles = append srcfiles, file;
  76. srctext = append srctext, src;
  77. return src;
  78. }
  79. return {};
  80. }
  81. lst = srcpath;
  82. while head lst do {
  83. src = file(head lst+file);
  84. if src != {} then {
  85. srcfiles = append srcfiles, file;
  86. srctext = append srctext, src;
  87. return src;
  88. }
  89. lst = tail lst;
  90. }
  91. }
  92. defn line(addr)
  93. {
  94. local src, file;
  95. file = pcfile(addr);
  96. src = match(file, srcfiles);
  97. if src >= 0 then
  98. src = srctext[src];
  99. else
  100. src = findsrc(file);
  101. if src == {} then {
  102. print("no source for ", file, "\n");
  103. return {};
  104. }
  105. line = pcline(addr)-1;
  106. print(file, ":", src[line], "\n");
  107. }
  108. defn addsrcdir(dir)
  109. {
  110. dir = dir+"/";
  111. if match(dir, srcpath) >= 0 then {
  112. print("already in srcpath\n");
  113. return {};
  114. }
  115. srcpath = {dir}+srcpath;
  116. }
  117. defn source()
  118. {
  119. local l;
  120. l = srcpath;
  121. while l do {
  122. print(head l, "\n");
  123. l = tail l;
  124. }
  125. l = srcfiles;
  126. while l do {
  127. print("\t", head l, "\n");
  128. l = tail l;
  129. }
  130. }
  131. defn Bsrc(addr)
  132. {
  133. local lst;
  134. lst = srcpath;
  135. file = pcfile(addr);
  136. if file[0] == '/' && access(file) then {
  137. rc("B "+file+":"+itoa(pcline(addr)));
  138. return {};
  139. }
  140. while head lst do {
  141. name = head lst+file;
  142. if access(name) then {
  143. rc("B "+name+":"+itoa(pcline(addr)));
  144. return {};
  145. }
  146. lst = tail lst;
  147. }
  148. print("no source for ", file, "\n");
  149. }
  150. defn src(addr)
  151. {
  152. local src, file, line, cline, text;
  153. file = pcfile(addr);
  154. src = match(file, srcfiles);
  155. if src >= 0 then
  156. src = srctext[src];
  157. else
  158. src = findsrc(file);
  159. if src == {} then {
  160. print("no source for ", file, "\n");
  161. return {};
  162. }
  163. cline = pcline(addr)-1;
  164. print(file, ":", cline+1, "\n");
  165. line = cline-5;
  166. loop 0,10 do {
  167. if line >= 0 then {
  168. if line == cline then
  169. print(">");
  170. else
  171. print(" ");
  172. text = src[line];
  173. if text == {} then
  174. return {};
  175. print(line+1, "\t", text, "\n");
  176. }
  177. line = line+1;
  178. }
  179. }
  180. defn step() // single step the process
  181. {
  182. local lst, lpl, addr, bput;
  183. bput = 0;
  184. if match(*PC, bplist) >= 0 then { // Sitting on a breakpoint
  185. bput = fmt(*PC, bpfmt);
  186. *bput = @bput;
  187. }
  188. lst = follow(*PC);
  189. lpl = lst;
  190. while lpl do { // place break points
  191. *(head lpl) = bpinst;
  192. lpl = tail lpl;
  193. }
  194. startstop(pid); // do the step
  195. while lst do { // remove the breakpoints
  196. addr = fmt(head lst, bpfmt);
  197. *addr = @addr;
  198. lst = tail lst;
  199. }
  200. if bput != 0 then
  201. *bput = bpinst;
  202. }
  203. defn bpset(addr) // set a breakpoint
  204. {
  205. if status(pid) != "Stopped" then {
  206. print("Waiting...\n");
  207. stop(pid);
  208. }
  209. if match(addr, bplist) >= 0 then
  210. print("breakpoint already set at ", fmt(addr, 'a'), "\n");
  211. else {
  212. *fmt(addr, bpfmt) = bpinst;
  213. bplist = append bplist, addr;
  214. }
  215. }
  216. defn bptab() // print a table of breakpoints
  217. {
  218. local lst, addr;
  219. lst = bplist;
  220. while lst do {
  221. addr = head lst;
  222. print("\t", fmt(addr, 'X'), " ", fmt(addr, 'a'), " ", fmt(addr, 'i'), "\n");
  223. lst = tail lst;
  224. }
  225. }
  226. defn bpdel(addr) // delete a breakpoint
  227. {
  228. local n, pc, nbplist;
  229. n = match(addr, bplist);
  230. if n < 0 then {
  231. print("no breakpoint at ", fmt(addr, 'a'), "\n");
  232. return {};
  233. }
  234. addr = fmt(addr, bpfmt);
  235. *addr = @addr;
  236. nbplist = {}; // delete from list
  237. while bplist do {
  238. pc = head bplist;
  239. if pc != addr then
  240. nbplist = append nbplist, pc;
  241. bplist = tail bplist;
  242. }
  243. bplist = nbplist; // delete from memory
  244. }
  245. defn cont() // continue execution
  246. {
  247. local addr;
  248. addr = fmt(*PC, bpfmt);
  249. if match(addr, bplist) >= 0 then { // Sitting on a breakpoint
  250. *addr = @addr;
  251. step(); // Step over
  252. *addr = bpinst;
  253. }
  254. startstop(pid); // Run
  255. }
  256. defn stopped(pid) // called from acid when a process changes state
  257. {
  258. pstop(pid); // stub so this is easy to replace
  259. }
  260. defn procs() // print status of processes
  261. {
  262. local c, lst, cpid;
  263. cpid = pid;
  264. lst = proclist;
  265. while lst do {
  266. np = head lst;
  267. setproc(np);
  268. if np == cpid then
  269. c = '>';
  270. else
  271. c = ' ';
  272. print(fmt(c, 'c'), np, ": ", status(np), " at ", fmt(*PC, 'a'), " setproc(", np, ")\n");
  273. lst = tail lst;
  274. }
  275. pid = cpid;
  276. if pid != 0 then
  277. setproc(pid);
  278. }
  279. _asmlines = 30;
  280. defn asm(addr)
  281. {
  282. local bound;
  283. bound = fnbound(addr);
  284. addr = fmt(addr, 'i');
  285. loop 1,_asmlines do {
  286. print(fmt(addr, 'a'), " ", fmt(addr, 'X'));
  287. print("\t", @addr++, "\n");
  288. if bound != {} && addr > bound[1] then {
  289. lasmaddr = addr;
  290. return {};
  291. }
  292. }
  293. lasmaddr = addr;
  294. }
  295. defn casm()
  296. {
  297. asm(lasmaddr);
  298. }
  299. defn win()
  300. {
  301. local npid, estr;
  302. bplist = {};
  303. notes = {};
  304. estr = "/sys/lib/acid/window '0 0 600 400' "+textfile;
  305. if progargs != "" then
  306. estr = estr+" "+progargs;
  307. npid = rc(estr);
  308. npid = atoi(npid);
  309. if npid == 0 then
  310. error("win failed to create process");
  311. setproc(npid);
  312. stopped(npid);
  313. }
  314. defn win2()
  315. {
  316. local npid, estr;
  317. bplist = {};
  318. notes = {};
  319. estr = "/sys/lib/acid/transcript '0 0 600 400' '100 100 700 500' "+textfile;
  320. if progargs != "" then
  321. estr = estr+" "+progargs;
  322. npid = rc(estr);
  323. npid = atoi(npid);
  324. if npid == 0 then
  325. error("win failed to create process");
  326. setproc(npid);
  327. stopped(npid);
  328. }
  329. defn new()
  330. {
  331. bplist = {};
  332. newproc(progargs);
  333. // Dont miss the delay slot calls
  334. bpset(follow(main)[0]);
  335. cont();
  336. bpdel(*PC);
  337. }
  338. defn stmnt() // step one statement
  339. {
  340. local line;
  341. line = pcline(*PC);
  342. while 1 do {
  343. step();
  344. if line != pcline(*PC) then {
  345. src(*PC);
  346. return {};
  347. }
  348. }
  349. }
  350. defn func() // step until we leave the current function
  351. {
  352. local bound, end, start, pc;
  353. bound = fnbound(*PC);
  354. if bound == {} then {
  355. print("cannot locate text symbol\n");
  356. return {};
  357. }
  358. pc = *PC;
  359. start = bound[0];
  360. end = bound[1];
  361. while pc >= start && pc < end do {
  362. step();
  363. pc = *PC;
  364. }
  365. }
  366. defn next()
  367. {
  368. local sp, bound;
  369. sp = *SP;
  370. bound = fnbound(*PC);
  371. stmnt();
  372. pc = *PC;
  373. if pc >= bound[0] && pc < bound[1] then
  374. return {};
  375. while (pc < bound[0] || pc > bound[1]) && sp >= *SP do {
  376. step();
  377. pc = *PC;
  378. }
  379. src(*PC);
  380. }
  381. defn dump(addr, n, fmt)
  382. {
  383. loop 0, n do {
  384. print(fmt(addr, 'X'), ": ");
  385. addr = mem(addr, fmt);
  386. }
  387. }
  388. defn mem(addr, fmt)
  389. {
  390. local i, c, n;
  391. i = 0;
  392. while fmt[i] != 0 do {
  393. c = fmt[i];
  394. n = 0;
  395. while '0' <= fmt[i] && fmt[i] <= '9' do {
  396. n = 10*n + fmt[i]-'0';
  397. i = i+1;
  398. }
  399. if n <= 0 then n = 1;
  400. addr = fmt(addr, fmt[i]);
  401. while n > 0 do {
  402. print(*addr++, " ");
  403. n = n-1;
  404. }
  405. i = i+1;
  406. }
  407. print("\n");
  408. return addr;
  409. }
  410. defn symbols(pattern)
  411. {
  412. local l, s;
  413. l = symbols;
  414. while l do {
  415. s = head l;
  416. if regexp(pattern, s[0]) then
  417. print(s[0], "\t", s[1], "\t", s[2], "\n");
  418. l = tail l;
  419. }
  420. }
  421. defn spsrch(len)
  422. {
  423. local addr, a, s, e;
  424. addr = *SP;
  425. s = origin & 0x7fffffff;
  426. e = etext & 0x7fffffff;
  427. loop 1, len do {
  428. a = *addr++;
  429. c = a & 0x7fffffff;
  430. if c > s && c < e then {
  431. print("src(", a, ")\n");
  432. pfl(a);
  433. }
  434. }
  435. }
  436. progargs="";
  437. print("/sys/lib/acid/port");