db_run.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* $OpenBSD: db_run.c,v 1.25 2015/03/14 03:38:46 jsg Exp $ */
  2. /* $NetBSD: db_run.c,v 1.8 1996/02/05 01:57:12 christos Exp $ */
  3. /*
  4. * Mach Operating System
  5. * Copyright (c) 1993,1992,1991,1990 Carnegie Mellon University
  6. * All Rights Reserved.
  7. *
  8. * Permission to use, copy, modify and distribute this software and its
  9. * documentation is hereby granted, provided that both the copyright
  10. * notice and this permission notice appear in all copies of the
  11. * software, derivative works or modified versions, and any portions
  12. * thereof, and that both notices appear in supporting documentation.
  13. *
  14. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  15. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  16. * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  17. *
  18. * Carnegie Mellon requests users of this software to return to
  19. *
  20. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  21. * School of Computer Science
  22. * Carnegie Mellon University
  23. * Pittsburgh PA 15213-3890
  24. *
  25. * any improvements or extensions that they make and grant Carnegie Mellon
  26. * the rights to redistribute these changes.
  27. *
  28. * Author: David B. Golub, Carnegie Mellon University
  29. * Date: 7/90
  30. */
  31. /*
  32. * Commands to run process.
  33. */
  34. #include <sys/param.h>
  35. #include <sys/systm.h>
  36. #include <machine/db_machdep.h>
  37. #include <ddb/db_run.h>
  38. #include <ddb/db_break.h>
  39. #include <ddb/db_access.h>
  40. #ifdef SOFTWARE_SSTEP
  41. db_breakpoint_t db_not_taken_bkpt = 0;
  42. db_breakpoint_t db_taken_bkpt = 0;
  43. #endif
  44. int db_inst_count;
  45. #ifndef KGDB
  46. #include <ddb/db_watch.h>
  47. #include <ddb/db_output.h>
  48. #include <ddb/db_sym.h>
  49. #include <ddb/db_extern.h>
  50. int db_run_mode;
  51. #define STEP_NONE 0
  52. #define STEP_ONCE 1
  53. #define STEP_RETURN 2
  54. #define STEP_CALLT 3
  55. #define STEP_CONTINUE 4
  56. #define STEP_INVISIBLE 5
  57. #define STEP_COUNT 6
  58. boolean_t db_sstep_print;
  59. int db_loop_count;
  60. int db_call_depth;
  61. boolean_t
  62. db_stop_at_pc(db_regs_t *regs, boolean_t *is_breakpoint)
  63. {
  64. db_addr_t pc, old_pc;
  65. db_breakpoint_t bkpt;
  66. db_clear_breakpoints();
  67. db_clear_watchpoints();
  68. old_pc = pc = PC_REGS(regs);
  69. #ifdef FIXUP_PC_AFTER_BREAK
  70. if (*is_breakpoint) {
  71. /*
  72. * Breakpoint trap. Fix up the PC if the
  73. * machine requires it.
  74. */
  75. FIXUP_PC_AFTER_BREAK(regs);
  76. pc = PC_REGS(regs);
  77. }
  78. #endif
  79. /*
  80. * Now check for a breakpoint at this address.
  81. */
  82. bkpt = db_find_breakpoint(pc);
  83. if (bkpt) {
  84. if (--bkpt->count == 0) {
  85. db_clear_single_step(regs);
  86. bkpt->count = bkpt->init_count;
  87. *is_breakpoint = TRUE;
  88. return (TRUE); /* stop here */
  89. }
  90. } else if (*is_breakpoint
  91. #ifdef SOFTWARE_SSTEP
  92. && !((db_taken_bkpt && db_taken_bkpt->address == pc) ||
  93. (db_not_taken_bkpt && db_not_taken_bkpt->address == pc))
  94. #endif
  95. ) {
  96. #ifdef PC_ADVANCE
  97. PC_ADVANCE(regs);
  98. #else
  99. # ifdef SET_PC_REGS
  100. SET_PC_REGS(regs, old_pc);
  101. # else
  102. PC_REGS(regs) = old_pc;
  103. # endif
  104. #endif
  105. }
  106. db_clear_single_step(regs);
  107. *is_breakpoint = FALSE;
  108. if (db_run_mode == STEP_INVISIBLE) {
  109. db_run_mode = STEP_CONTINUE;
  110. return (FALSE); /* continue */
  111. }
  112. if (db_run_mode == STEP_COUNT) {
  113. return (FALSE); /* continue */
  114. }
  115. if (db_run_mode == STEP_ONCE) {
  116. if (--db_loop_count > 0) {
  117. if (db_sstep_print) {
  118. db_printf("\t\t");
  119. db_print_loc_and_inst(pc);
  120. db_printf("\n");
  121. }
  122. return (FALSE); /* continue */
  123. }
  124. }
  125. if (db_run_mode == STEP_RETURN) {
  126. db_expr_t ins = db_get_value(pc, sizeof(int), FALSE);
  127. /* continue until matching return */
  128. if (!inst_trap_return(ins) &&
  129. (!inst_return(ins) || --db_call_depth != 0)) {
  130. if (db_sstep_print) {
  131. if (inst_call(ins) || inst_return(ins)) {
  132. int i;
  133. db_printf("[after %6d] ", db_inst_count);
  134. for (i = db_call_depth; --i > 0; )
  135. db_printf(" ");
  136. db_print_loc_and_inst(pc);
  137. db_printf("\n");
  138. }
  139. }
  140. if (inst_call(ins))
  141. db_call_depth++;
  142. return (FALSE); /* continue */
  143. }
  144. }
  145. if (db_run_mode == STEP_CALLT) {
  146. db_expr_t ins = db_get_value(pc, sizeof(int), FALSE);
  147. /* continue until call or return */
  148. if (!inst_call(ins) && !inst_return(ins) &&
  149. !inst_trap_return(ins)) {
  150. return (FALSE); /* continue */
  151. }
  152. }
  153. db_run_mode = STEP_NONE;
  154. return (TRUE);
  155. }
  156. void
  157. db_restart_at_pc(db_regs_t *regs, boolean_t watchpt)
  158. {
  159. db_addr_t pc = PC_REGS(regs);
  160. if ((db_run_mode == STEP_COUNT) || (db_run_mode == STEP_RETURN) ||
  161. (db_run_mode == STEP_CALLT)) {
  162. db_expr_t ins;
  163. /*
  164. * We are about to execute this instruction,
  165. * so count it now.
  166. */
  167. ins = db_get_value(pc, sizeof(int), FALSE);
  168. db_inst_count++;
  169. #ifdef SOFTWARE_SSTEP
  170. /* XXX works on mips, but... */
  171. if (inst_branch(ins) || inst_call(ins)) {
  172. ins = db_get_value(next_instr_address(pc, 1),
  173. sizeof(int), FALSE);
  174. db_inst_count++;
  175. }
  176. #endif /* SOFTWARE_SSTEP */
  177. }
  178. if (db_run_mode == STEP_CONTINUE) {
  179. if (watchpt || db_find_breakpoint(pc)) {
  180. /*
  181. * Step over breakpoint/watchpoint.
  182. */
  183. db_run_mode = STEP_INVISIBLE;
  184. db_set_single_step(regs);
  185. } else {
  186. db_set_breakpoints();
  187. db_set_watchpoints();
  188. }
  189. } else {
  190. db_set_single_step(regs);
  191. }
  192. }
  193. void
  194. db_single_step(db_regs_t *regs)
  195. {
  196. if (db_run_mode == STEP_CONTINUE) {
  197. db_run_mode = STEP_INVISIBLE;
  198. db_set_single_step(regs);
  199. }
  200. }
  201. /* single-step */
  202. /*ARGSUSED*/
  203. void
  204. db_single_step_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
  205. {
  206. boolean_t print = FALSE;
  207. if (count == -1)
  208. count = 1;
  209. if (modif[0] == 'p')
  210. print = TRUE;
  211. db_run_mode = STEP_ONCE;
  212. db_loop_count = count;
  213. db_sstep_print = print;
  214. db_inst_count = 0;
  215. db_cmd_loop_done = 1;
  216. }
  217. /* trace and print until call/return */
  218. /*ARGSUSED*/
  219. void
  220. db_trace_until_call_cmd(db_expr_t addr, int have_addr, db_expr_t count,
  221. char *modif)
  222. {
  223. boolean_t print = FALSE;
  224. if (modif[0] == 'p')
  225. print = TRUE;
  226. db_run_mode = STEP_CALLT;
  227. db_sstep_print = print;
  228. db_inst_count = 0;
  229. db_cmd_loop_done = 1;
  230. }
  231. /*ARGSUSED*/
  232. void
  233. db_trace_until_matching_cmd(db_expr_t addr, int have_addr, db_expr_t count,
  234. char *modif)
  235. {
  236. boolean_t print = FALSE;
  237. if (modif[0] == 'p')
  238. print = TRUE;
  239. db_run_mode = STEP_RETURN;
  240. db_call_depth = 1;
  241. db_sstep_print = print;
  242. db_inst_count = 0;
  243. db_cmd_loop_done = 1;
  244. }
  245. /* continue */
  246. /*ARGSUSED*/
  247. void
  248. db_continue_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
  249. {
  250. if (modif[0] == 'c')
  251. db_run_mode = STEP_COUNT;
  252. else
  253. db_run_mode = STEP_CONTINUE;
  254. db_inst_count = 0;
  255. db_cmd_loop_done = 1;
  256. }
  257. #endif /* NO KGDB */
  258. #ifdef SOFTWARE_SSTEP
  259. /*
  260. * Software implementation of single-stepping.
  261. * If your machine does not have a trace mode
  262. * similar to the vax or sun ones you can use
  263. * this implementation, done for the mips.
  264. * Just define the above conditional and provide
  265. * the functions/macros defined below.
  266. *
  267. * extern boolean_t
  268. * inst_branch(ins), returns true if the instruction might branch
  269. * extern unsigned
  270. * branch_taken(ins, pc, getreg_val, regs),
  271. * return the address the instruction might
  272. * branch to
  273. * getreg_val(regs, reg), return the value of a user register,
  274. * as indicated in the hardware instruction
  275. * encoding, e.g. 8 for r8
  276. *
  277. * next_instr_address(pc, bd) returns the address of the first
  278. * instruction following the one at "pc",
  279. * which is either in the taken path of
  280. * the branch (bd==1) or not. This is
  281. * for machines (mips) with branch delays.
  282. *
  283. * A single-step may involve at most 2 breakpoints -
  284. * one for branch-not-taken and one for branch taken.
  285. * If one of these addresses does not already have a breakpoint,
  286. * we allocate a breakpoint and save it here.
  287. * These breakpoints are deleted on return.
  288. */
  289. void
  290. db_set_single_step(db_regs_t *regs)
  291. {
  292. db_addr_t pc = PC_REGS(regs);
  293. #ifndef SOFTWARE_SSTEP_EMUL
  294. db_addr_t brpc;
  295. u_int inst;
  296. /*
  297. * User was stopped at pc, e.g. the instruction
  298. * at pc was not executed.
  299. */
  300. inst = db_get_value(pc, sizeof(int), FALSE);
  301. if (inst_branch(inst) || inst_call(inst) || inst_return(inst)) {
  302. brpc = branch_taken(inst, pc, getreg_val, regs);
  303. if (brpc != pc) { /* self-branches are hopeless */
  304. db_taken_bkpt = db_set_temp_breakpoint(brpc);
  305. }
  306. #if 0
  307. /* XXX this seems like a true bug, no? */
  308. pc = next_instr_address(pc, 1);
  309. #endif
  310. }
  311. #endif /*SOFTWARE_SSTEP_EMUL*/
  312. pc = next_instr_address(pc, 0);
  313. db_not_taken_bkpt = db_set_temp_breakpoint(pc);
  314. }
  315. void
  316. db_clear_single_step(db_regs_t *regs)
  317. {
  318. if (db_taken_bkpt != 0) {
  319. db_delete_temp_breakpoint(db_taken_bkpt);
  320. db_taken_bkpt = 0;
  321. }
  322. if (db_not_taken_bkpt != 0) {
  323. db_delete_temp_breakpoint(db_not_taken_bkpt);
  324. db_not_taken_bkpt = 0;
  325. }
  326. }
  327. #endif /* SOFTWARE_SSTEP */