kdb_bp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * Kernel Debugger Architecture Independent Breakpoint Handler
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (c) 1999-2004 Silicon Graphics, Inc. All Rights Reserved.
  9. * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
  10. */
  11. #include <linux/string.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/kdb.h>
  15. #include <linux/kgdb.h>
  16. #include <linux/smp.h>
  17. #include <linux/sched.h>
  18. #include <linux/interrupt.h>
  19. #include "kdb_private.h"
  20. /*
  21. * Table of kdb_breakpoints
  22. */
  23. kdb_bp_t kdb_breakpoints[KDB_MAXBPT];
  24. static void kdb_setsinglestep(struct pt_regs *regs)
  25. {
  26. KDB_STATE_SET(DOING_SS);
  27. }
  28. static char *kdb_rwtypes[] = {
  29. "Instruction(i)",
  30. "Instruction(Register)",
  31. "Data Write",
  32. "I/O",
  33. "Data Access"
  34. };
  35. static char *kdb_bptype(kdb_bp_t *bp)
  36. {
  37. if (bp->bp_type < 0 || bp->bp_type > 4)
  38. return "";
  39. return kdb_rwtypes[bp->bp_type];
  40. }
  41. static int kdb_parsebp(int argc, const char **argv, int *nextargp, kdb_bp_t *bp)
  42. {
  43. int nextarg = *nextargp;
  44. int diag;
  45. bp->bph_length = 1;
  46. if ((argc + 1) != nextarg) {
  47. if (strnicmp(argv[nextarg], "datar", sizeof("datar")) == 0)
  48. bp->bp_type = BP_ACCESS_WATCHPOINT;
  49. else if (strnicmp(argv[nextarg], "dataw", sizeof("dataw")) == 0)
  50. bp->bp_type = BP_WRITE_WATCHPOINT;
  51. else if (strnicmp(argv[nextarg], "inst", sizeof("inst")) == 0)
  52. bp->bp_type = BP_HARDWARE_BREAKPOINT;
  53. else
  54. return KDB_ARGCOUNT;
  55. bp->bph_length = 1;
  56. nextarg++;
  57. if ((argc + 1) != nextarg) {
  58. unsigned long len;
  59. diag = kdbgetularg((char *)argv[nextarg],
  60. &len);
  61. if (diag)
  62. return diag;
  63. if (len > 8)
  64. return KDB_BADLENGTH;
  65. bp->bph_length = len;
  66. nextarg++;
  67. }
  68. if ((argc + 1) != nextarg)
  69. return KDB_ARGCOUNT;
  70. }
  71. *nextargp = nextarg;
  72. return 0;
  73. }
  74. static int _kdb_bp_remove(kdb_bp_t *bp)
  75. {
  76. int ret = 1;
  77. if (!bp->bp_installed)
  78. return ret;
  79. if (!bp->bp_type)
  80. ret = dbg_remove_sw_break(bp->bp_addr);
  81. else
  82. ret = arch_kgdb_ops.remove_hw_breakpoint(bp->bp_addr,
  83. bp->bph_length,
  84. bp->bp_type);
  85. if (ret == 0)
  86. bp->bp_installed = 0;
  87. return ret;
  88. }
  89. static void kdb_handle_bp(struct pt_regs *regs, kdb_bp_t *bp)
  90. {
  91. if (KDB_DEBUG(BP))
  92. kdb_printf("regs->ip = 0x%lx\n", instruction_pointer(regs));
  93. /*
  94. * Setup single step
  95. */
  96. kdb_setsinglestep(regs);
  97. /*
  98. * Reset delay attribute
  99. */
  100. bp->bp_delay = 0;
  101. bp->bp_delayed = 1;
  102. }
  103. static int _kdb_bp_install(struct pt_regs *regs, kdb_bp_t *bp)
  104. {
  105. int ret;
  106. /*
  107. * Install the breakpoint, if it is not already installed.
  108. */
  109. if (KDB_DEBUG(BP))
  110. kdb_printf("%s: bp_installed %d\n",
  111. __func__, bp->bp_installed);
  112. if (!KDB_STATE(SSBPT))
  113. bp->bp_delay = 0;
  114. if (bp->bp_installed)
  115. return 1;
  116. if (bp->bp_delay || (bp->bp_delayed && KDB_STATE(DOING_SS))) {
  117. if (KDB_DEBUG(BP))
  118. kdb_printf("%s: delayed bp\n", __func__);
  119. kdb_handle_bp(regs, bp);
  120. return 0;
  121. }
  122. if (!bp->bp_type)
  123. ret = dbg_set_sw_break(bp->bp_addr);
  124. else
  125. ret = arch_kgdb_ops.set_hw_breakpoint(bp->bp_addr,
  126. bp->bph_length,
  127. bp->bp_type);
  128. if (ret == 0) {
  129. bp->bp_installed = 1;
  130. } else {
  131. kdb_printf("%s: failed to set breakpoint at 0x%lx\n",
  132. __func__, bp->bp_addr);
  133. return 1;
  134. }
  135. return 0;
  136. }
  137. /*
  138. * kdb_bp_install
  139. *
  140. * Install kdb_breakpoints prior to returning from the
  141. * kernel debugger. This allows the kdb_breakpoints to be set
  142. * upon functions that are used internally by kdb, such as
  143. * printk(). This function is only called once per kdb session.
  144. */
  145. void kdb_bp_install(struct pt_regs *regs)
  146. {
  147. int i;
  148. for (i = 0; i < KDB_MAXBPT; i++) {
  149. kdb_bp_t *bp = &kdb_breakpoints[i];
  150. if (KDB_DEBUG(BP)) {
  151. kdb_printf("%s: bp %d bp_enabled %d\n",
  152. __func__, i, bp->bp_enabled);
  153. }
  154. if (bp->bp_enabled)
  155. _kdb_bp_install(regs, bp);
  156. }
  157. }
  158. /*
  159. * kdb_bp_remove
  160. *
  161. * Remove kdb_breakpoints upon entry to the kernel debugger.
  162. *
  163. * Parameters:
  164. * None.
  165. * Outputs:
  166. * None.
  167. * Returns:
  168. * None.
  169. * Locking:
  170. * None.
  171. * Remarks:
  172. */
  173. void kdb_bp_remove(void)
  174. {
  175. int i;
  176. for (i = KDB_MAXBPT - 1; i >= 0; i--) {
  177. kdb_bp_t *bp = &kdb_breakpoints[i];
  178. if (KDB_DEBUG(BP)) {
  179. kdb_printf("%s: bp %d bp_enabled %d\n",
  180. __func__, i, bp->bp_enabled);
  181. }
  182. if (bp->bp_enabled)
  183. _kdb_bp_remove(bp);
  184. }
  185. }
  186. /*
  187. * kdb_printbp
  188. *
  189. * Internal function to format and print a breakpoint entry.
  190. *
  191. * Parameters:
  192. * None.
  193. * Outputs:
  194. * None.
  195. * Returns:
  196. * None.
  197. * Locking:
  198. * None.
  199. * Remarks:
  200. */
  201. static void kdb_printbp(kdb_bp_t *bp, int i)
  202. {
  203. kdb_printf("%s ", kdb_bptype(bp));
  204. kdb_printf("BP #%d at ", i);
  205. kdb_symbol_print(bp->bp_addr, NULL, KDB_SP_DEFAULT);
  206. if (bp->bp_enabled)
  207. kdb_printf("\n is enabled");
  208. else
  209. kdb_printf("\n is disabled");
  210. kdb_printf("\taddr at %016lx, hardtype=%d installed=%d\n",
  211. bp->bp_addr, bp->bp_type, bp->bp_installed);
  212. kdb_printf("\n");
  213. }
  214. /*
  215. * kdb_bp
  216. *
  217. * Handle the bp commands.
  218. *
  219. * [bp|bph] <addr-expression> [DATAR|DATAW]
  220. *
  221. * Parameters:
  222. * argc Count of arguments in argv
  223. * argv Space delimited command line arguments
  224. * Outputs:
  225. * None.
  226. * Returns:
  227. * Zero for success, a kdb diagnostic if failure.
  228. * Locking:
  229. * None.
  230. * Remarks:
  231. *
  232. * bp Set breakpoint on all cpus. Only use hardware assist if need.
  233. * bph Set breakpoint on all cpus. Force hardware register
  234. */
  235. static int kdb_bp(int argc, const char **argv)
  236. {
  237. int i, bpno;
  238. kdb_bp_t *bp, *bp_check;
  239. int diag;
  240. int free;
  241. char *symname = NULL;
  242. long offset = 0ul;
  243. int nextarg;
  244. kdb_bp_t template = {0};
  245. if (argc == 0) {
  246. /*
  247. * Display breakpoint table
  248. */
  249. for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT;
  250. bpno++, bp++) {
  251. if (bp->bp_free)
  252. continue;
  253. kdb_printbp(bp, bpno);
  254. }
  255. return 0;
  256. }
  257. nextarg = 1;
  258. diag = kdbgetaddrarg(argc, argv, &nextarg, &template.bp_addr,
  259. &offset, &symname);
  260. if (diag)
  261. return diag;
  262. if (!template.bp_addr)
  263. return KDB_BADINT;
  264. /*
  265. * Find an empty bp structure to allocate
  266. */
  267. free = KDB_MAXBPT;
  268. for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT; bpno++, bp++) {
  269. if (bp->bp_free)
  270. break;
  271. }
  272. if (bpno == KDB_MAXBPT)
  273. return KDB_TOOMANYBPT;
  274. if (strcmp(argv[0], "bph") == 0) {
  275. template.bp_type = BP_HARDWARE_BREAKPOINT;
  276. diag = kdb_parsebp(argc, argv, &nextarg, &template);
  277. if (diag)
  278. return diag;
  279. } else {
  280. template.bp_type = BP_BREAKPOINT;
  281. }
  282. /*
  283. * Check for clashing breakpoints.
  284. *
  285. * Note, in this design we can't have hardware breakpoints
  286. * enabled for both read and write on the same address.
  287. */
  288. for (i = 0, bp_check = kdb_breakpoints; i < KDB_MAXBPT;
  289. i++, bp_check++) {
  290. if (!bp_check->bp_free &&
  291. bp_check->bp_addr == template.bp_addr) {
  292. kdb_printf("You already have a breakpoint at "
  293. kdb_bfd_vma_fmt0 "\n", template.bp_addr);
  294. return KDB_DUPBPT;
  295. }
  296. }
  297. template.bp_enabled = 1;
  298. /*
  299. * Actually allocate the breakpoint found earlier
  300. */
  301. *bp = template;
  302. bp->bp_free = 0;
  303. kdb_printbp(bp, bpno);
  304. return 0;
  305. }
  306. /*
  307. * kdb_bc
  308. *
  309. * Handles the 'bc', 'be', and 'bd' commands
  310. *
  311. * [bd|bc|be] <breakpoint-number>
  312. * [bd|bc|be] *
  313. *
  314. * Parameters:
  315. * argc Count of arguments in argv
  316. * argv Space delimited command line arguments
  317. * Outputs:
  318. * None.
  319. * Returns:
  320. * Zero for success, a kdb diagnostic for failure
  321. * Locking:
  322. * None.
  323. * Remarks:
  324. */
  325. static int kdb_bc(int argc, const char **argv)
  326. {
  327. unsigned long addr;
  328. kdb_bp_t *bp = NULL;
  329. int lowbp = KDB_MAXBPT;
  330. int highbp = 0;
  331. int done = 0;
  332. int i;
  333. int diag = 0;
  334. int cmd; /* KDBCMD_B? */
  335. #define KDBCMD_BC 0
  336. #define KDBCMD_BE 1
  337. #define KDBCMD_BD 2
  338. if (strcmp(argv[0], "be") == 0)
  339. cmd = KDBCMD_BE;
  340. else if (strcmp(argv[0], "bd") == 0)
  341. cmd = KDBCMD_BD;
  342. else
  343. cmd = KDBCMD_BC;
  344. if (argc != 1)
  345. return KDB_ARGCOUNT;
  346. if (strcmp(argv[1], "*") == 0) {
  347. lowbp = 0;
  348. highbp = KDB_MAXBPT;
  349. } else {
  350. diag = kdbgetularg(argv[1], &addr);
  351. if (diag)
  352. return diag;
  353. /*
  354. * For addresses less than the maximum breakpoint number,
  355. * assume that the breakpoint number is desired.
  356. */
  357. if (addr < KDB_MAXBPT) {
  358. bp = &kdb_breakpoints[addr];
  359. lowbp = highbp = addr;
  360. highbp++;
  361. } else {
  362. for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT;
  363. i++, bp++) {
  364. if (bp->bp_addr == addr) {
  365. lowbp = highbp = i;
  366. highbp++;
  367. break;
  368. }
  369. }
  370. }
  371. }
  372. /*
  373. * Now operate on the set of breakpoints matching the input
  374. * criteria (either '*' for all, or an individual breakpoint).
  375. */
  376. for (bp = &kdb_breakpoints[lowbp], i = lowbp;
  377. i < highbp;
  378. i++, bp++) {
  379. if (bp->bp_free)
  380. continue;
  381. done++;
  382. switch (cmd) {
  383. case KDBCMD_BC:
  384. bp->bp_enabled = 0;
  385. kdb_printf("Breakpoint %d at "
  386. kdb_bfd_vma_fmt " cleared\n",
  387. i, bp->bp_addr);
  388. bp->bp_addr = 0;
  389. bp->bp_free = 1;
  390. break;
  391. case KDBCMD_BE:
  392. bp->bp_enabled = 1;
  393. kdb_printf("Breakpoint %d at "
  394. kdb_bfd_vma_fmt " enabled",
  395. i, bp->bp_addr);
  396. kdb_printf("\n");
  397. break;
  398. case KDBCMD_BD:
  399. if (!bp->bp_enabled)
  400. break;
  401. bp->bp_enabled = 0;
  402. kdb_printf("Breakpoint %d at "
  403. kdb_bfd_vma_fmt " disabled\n",
  404. i, bp->bp_addr);
  405. break;
  406. }
  407. if (bp->bp_delay && (cmd == KDBCMD_BC || cmd == KDBCMD_BD)) {
  408. bp->bp_delay = 0;
  409. KDB_STATE_CLEAR(SSBPT);
  410. }
  411. }
  412. return (!done) ? KDB_BPTNOTFOUND : 0;
  413. }
  414. /*
  415. * kdb_ss
  416. *
  417. * Process the 'ss' (Single Step) and 'ssb' (Single Step to Branch)
  418. * commands.
  419. *
  420. * ss
  421. * ssb
  422. *
  423. * Parameters:
  424. * argc Argument count
  425. * argv Argument vector
  426. * Outputs:
  427. * None.
  428. * Returns:
  429. * KDB_CMD_SS[B] for success, a kdb error if failure.
  430. * Locking:
  431. * None.
  432. * Remarks:
  433. *
  434. * Set the arch specific option to trigger a debug trap after the next
  435. * instruction.
  436. *
  437. * For 'ssb', set the trace flag in the debug trap handler
  438. * after printing the current insn and return directly without
  439. * invoking the kdb command processor, until a branch instruction
  440. * is encountered.
  441. */
  442. static int kdb_ss(int argc, const char **argv)
  443. {
  444. int ssb = 0;
  445. ssb = (strcmp(argv[0], "ssb") == 0);
  446. if (argc != 0)
  447. return KDB_ARGCOUNT;
  448. /*
  449. * Set trace flag and go.
  450. */
  451. KDB_STATE_SET(DOING_SS);
  452. if (ssb) {
  453. KDB_STATE_SET(DOING_SSB);
  454. return KDB_CMD_SSB;
  455. }
  456. return KDB_CMD_SS;
  457. }
  458. /* Initialize the breakpoint table and register breakpoint commands. */
  459. void __init kdb_initbptab(void)
  460. {
  461. int i;
  462. kdb_bp_t *bp;
  463. /*
  464. * First time initialization.
  465. */
  466. memset(&kdb_breakpoints, '\0', sizeof(kdb_breakpoints));
  467. for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++)
  468. bp->bp_free = 1;
  469. kdb_register_repeat("bp", kdb_bp, "[<vaddr>]",
  470. "Set/Display breakpoints", 0, KDB_REPEAT_NO_ARGS);
  471. kdb_register_repeat("bl", kdb_bp, "[<vaddr>]",
  472. "Display breakpoints", 0, KDB_REPEAT_NO_ARGS);
  473. if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)
  474. kdb_register_repeat("bph", kdb_bp, "[<vaddr>]",
  475. "[datar [length]|dataw [length]] Set hw brk", 0, KDB_REPEAT_NO_ARGS);
  476. kdb_register_repeat("bc", kdb_bc, "<bpnum>",
  477. "Clear Breakpoint", 0, KDB_REPEAT_NONE);
  478. kdb_register_repeat("be", kdb_bc, "<bpnum>",
  479. "Enable Breakpoint", 0, KDB_REPEAT_NONE);
  480. kdb_register_repeat("bd", kdb_bc, "<bpnum>",
  481. "Disable Breakpoint", 0, KDB_REPEAT_NONE);
  482. kdb_register_repeat("ss", kdb_ss, "",
  483. "Single Step", 1, KDB_REPEAT_NO_ARGS);
  484. kdb_register_repeat("ssb", kdb_ss, "",
  485. "Single step to branch/call", 0, KDB_REPEAT_NO_ARGS);
  486. /*
  487. * Architecture dependent initialization.
  488. */
  489. }