kdb_bp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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 (strncasecmp(argv[nextarg], "datar", sizeof("datar")) == 0)
  48. bp->bp_type = BP_ACCESS_WATCHPOINT;
  49. else if (strncasecmp(argv[nextarg], "dataw", sizeof("dataw")) == 0)
  50. bp->bp_type = BP_WRITE_WATCHPOINT;
  51. else if (strncasecmp(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. if (!bp->bp_type) {
  134. kdb_printf("Software breakpoints are unavailable.\n"
  135. " Boot the kernel with rodata=off\n"
  136. " OR use hw breaks: help bph\n");
  137. }
  138. return 1;
  139. }
  140. return 0;
  141. }
  142. /*
  143. * kdb_bp_install
  144. *
  145. * Install kdb_breakpoints prior to returning from the
  146. * kernel debugger. This allows the kdb_breakpoints to be set
  147. * upon functions that are used internally by kdb, such as
  148. * printk(). This function is only called once per kdb session.
  149. */
  150. void kdb_bp_install(struct pt_regs *regs)
  151. {
  152. int i;
  153. for (i = 0; i < KDB_MAXBPT; i++) {
  154. kdb_bp_t *bp = &kdb_breakpoints[i];
  155. if (KDB_DEBUG(BP)) {
  156. kdb_printf("%s: bp %d bp_enabled %d\n",
  157. __func__, i, bp->bp_enabled);
  158. }
  159. if (bp->bp_enabled)
  160. _kdb_bp_install(regs, bp);
  161. }
  162. }
  163. /*
  164. * kdb_bp_remove
  165. *
  166. * Remove kdb_breakpoints upon entry to the kernel debugger.
  167. *
  168. * Parameters:
  169. * None.
  170. * Outputs:
  171. * None.
  172. * Returns:
  173. * None.
  174. * Locking:
  175. * None.
  176. * Remarks:
  177. */
  178. void kdb_bp_remove(void)
  179. {
  180. int i;
  181. for (i = KDB_MAXBPT - 1; i >= 0; i--) {
  182. kdb_bp_t *bp = &kdb_breakpoints[i];
  183. if (KDB_DEBUG(BP)) {
  184. kdb_printf("%s: bp %d bp_enabled %d\n",
  185. __func__, i, bp->bp_enabled);
  186. }
  187. if (bp->bp_enabled)
  188. _kdb_bp_remove(bp);
  189. }
  190. }
  191. /*
  192. * kdb_printbp
  193. *
  194. * Internal function to format and print a breakpoint entry.
  195. *
  196. * Parameters:
  197. * None.
  198. * Outputs:
  199. * None.
  200. * Returns:
  201. * None.
  202. * Locking:
  203. * None.
  204. * Remarks:
  205. */
  206. static void kdb_printbp(kdb_bp_t *bp, int i)
  207. {
  208. kdb_printf("%s ", kdb_bptype(bp));
  209. kdb_printf("BP #%d at ", i);
  210. kdb_symbol_print(bp->bp_addr, NULL, KDB_SP_DEFAULT);
  211. if (bp->bp_enabled)
  212. kdb_printf("\n is enabled ");
  213. else
  214. kdb_printf("\n is disabled");
  215. kdb_printf(" addr at %016lx, hardtype=%d installed=%d\n",
  216. bp->bp_addr, bp->bp_type, bp->bp_installed);
  217. kdb_printf("\n");
  218. }
  219. /*
  220. * kdb_bp
  221. *
  222. * Handle the bp commands.
  223. *
  224. * [bp|bph] <addr-expression> [DATAR|DATAW]
  225. *
  226. * Parameters:
  227. * argc Count of arguments in argv
  228. * argv Space delimited command line arguments
  229. * Outputs:
  230. * None.
  231. * Returns:
  232. * Zero for success, a kdb diagnostic if failure.
  233. * Locking:
  234. * None.
  235. * Remarks:
  236. *
  237. * bp Set breakpoint on all cpus. Only use hardware assist if need.
  238. * bph Set breakpoint on all cpus. Force hardware register
  239. */
  240. static int kdb_bp(int argc, const char **argv)
  241. {
  242. int i, bpno;
  243. kdb_bp_t *bp, *bp_check;
  244. int diag;
  245. char *symname = NULL;
  246. long offset = 0ul;
  247. int nextarg;
  248. kdb_bp_t template = {0};
  249. if (argc == 0) {
  250. /*
  251. * Display breakpoint table
  252. */
  253. for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT;
  254. bpno++, bp++) {
  255. if (bp->bp_free)
  256. continue;
  257. kdb_printbp(bp, bpno);
  258. }
  259. return 0;
  260. }
  261. nextarg = 1;
  262. diag = kdbgetaddrarg(argc, argv, &nextarg, &template.bp_addr,
  263. &offset, &symname);
  264. if (diag)
  265. return diag;
  266. if (!template.bp_addr)
  267. return KDB_BADINT;
  268. /*
  269. * This check is redundant (since the breakpoint machinery should
  270. * be doing the same check during kdb_bp_install) but gives the
  271. * user immediate feedback.
  272. */
  273. diag = kgdb_validate_break_address(template.bp_addr);
  274. if (diag)
  275. return diag;
  276. /*
  277. * Find an empty bp structure to allocate
  278. */
  279. for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT; bpno++, bp++) {
  280. if (bp->bp_free)
  281. break;
  282. }
  283. if (bpno == KDB_MAXBPT)
  284. return KDB_TOOMANYBPT;
  285. if (strcmp(argv[0], "bph") == 0) {
  286. template.bp_type = BP_HARDWARE_BREAKPOINT;
  287. diag = kdb_parsebp(argc, argv, &nextarg, &template);
  288. if (diag)
  289. return diag;
  290. } else {
  291. template.bp_type = BP_BREAKPOINT;
  292. }
  293. /*
  294. * Check for clashing breakpoints.
  295. *
  296. * Note, in this design we can't have hardware breakpoints
  297. * enabled for both read and write on the same address.
  298. */
  299. for (i = 0, bp_check = kdb_breakpoints; i < KDB_MAXBPT;
  300. i++, bp_check++) {
  301. if (!bp_check->bp_free &&
  302. bp_check->bp_addr == template.bp_addr) {
  303. kdb_printf("You already have a breakpoint at "
  304. kdb_bfd_vma_fmt0 "\n", template.bp_addr);
  305. return KDB_DUPBPT;
  306. }
  307. }
  308. template.bp_enabled = 1;
  309. /*
  310. * Actually allocate the breakpoint found earlier
  311. */
  312. *bp = template;
  313. bp->bp_free = 0;
  314. kdb_printbp(bp, bpno);
  315. return 0;
  316. }
  317. /*
  318. * kdb_bc
  319. *
  320. * Handles the 'bc', 'be', and 'bd' commands
  321. *
  322. * [bd|bc|be] <breakpoint-number>
  323. * [bd|bc|be] *
  324. *
  325. * Parameters:
  326. * argc Count of arguments in argv
  327. * argv Space delimited command line arguments
  328. * Outputs:
  329. * None.
  330. * Returns:
  331. * Zero for success, a kdb diagnostic for failure
  332. * Locking:
  333. * None.
  334. * Remarks:
  335. */
  336. static int kdb_bc(int argc, const char **argv)
  337. {
  338. unsigned long addr;
  339. kdb_bp_t *bp = NULL;
  340. int lowbp = KDB_MAXBPT;
  341. int highbp = 0;
  342. int done = 0;
  343. int i;
  344. int diag = 0;
  345. int cmd; /* KDBCMD_B? */
  346. #define KDBCMD_BC 0
  347. #define KDBCMD_BE 1
  348. #define KDBCMD_BD 2
  349. if (strcmp(argv[0], "be") == 0)
  350. cmd = KDBCMD_BE;
  351. else if (strcmp(argv[0], "bd") == 0)
  352. cmd = KDBCMD_BD;
  353. else
  354. cmd = KDBCMD_BC;
  355. if (argc != 1)
  356. return KDB_ARGCOUNT;
  357. if (strcmp(argv[1], "*") == 0) {
  358. lowbp = 0;
  359. highbp = KDB_MAXBPT;
  360. } else {
  361. diag = kdbgetularg(argv[1], &addr);
  362. if (diag)
  363. return diag;
  364. /*
  365. * For addresses less than the maximum breakpoint number,
  366. * assume that the breakpoint number is desired.
  367. */
  368. if (addr < KDB_MAXBPT) {
  369. lowbp = highbp = addr;
  370. highbp++;
  371. } else {
  372. for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT;
  373. i++, bp++) {
  374. if (bp->bp_addr == addr) {
  375. lowbp = highbp = i;
  376. highbp++;
  377. break;
  378. }
  379. }
  380. }
  381. }
  382. /*
  383. * Now operate on the set of breakpoints matching the input
  384. * criteria (either '*' for all, or an individual breakpoint).
  385. */
  386. for (bp = &kdb_breakpoints[lowbp], i = lowbp;
  387. i < highbp;
  388. i++, bp++) {
  389. if (bp->bp_free)
  390. continue;
  391. done++;
  392. switch (cmd) {
  393. case KDBCMD_BC:
  394. bp->bp_enabled = 0;
  395. kdb_printf("Breakpoint %d at "
  396. kdb_bfd_vma_fmt " cleared\n",
  397. i, bp->bp_addr);
  398. bp->bp_addr = 0;
  399. bp->bp_free = 1;
  400. break;
  401. case KDBCMD_BE:
  402. bp->bp_enabled = 1;
  403. kdb_printf("Breakpoint %d at "
  404. kdb_bfd_vma_fmt " enabled",
  405. i, bp->bp_addr);
  406. kdb_printf("\n");
  407. break;
  408. case KDBCMD_BD:
  409. if (!bp->bp_enabled)
  410. break;
  411. bp->bp_enabled = 0;
  412. kdb_printf("Breakpoint %d at "
  413. kdb_bfd_vma_fmt " disabled\n",
  414. i, bp->bp_addr);
  415. break;
  416. }
  417. if (bp->bp_delay && (cmd == KDBCMD_BC || cmd == KDBCMD_BD)) {
  418. bp->bp_delay = 0;
  419. KDB_STATE_CLEAR(SSBPT);
  420. }
  421. }
  422. return (!done) ? KDB_BPTNOTFOUND : 0;
  423. }
  424. /*
  425. * kdb_ss
  426. *
  427. * Process the 'ss' (Single Step) command.
  428. *
  429. * ss
  430. *
  431. * Parameters:
  432. * argc Argument count
  433. * argv Argument vector
  434. * Outputs:
  435. * None.
  436. * Returns:
  437. * KDB_CMD_SS for success, a kdb error if failure.
  438. * Locking:
  439. * None.
  440. * Remarks:
  441. *
  442. * Set the arch specific option to trigger a debug trap after the next
  443. * instruction.
  444. */
  445. static int kdb_ss(int argc, const char **argv)
  446. {
  447. if (argc != 0)
  448. return KDB_ARGCOUNT;
  449. /*
  450. * Set trace flag and go.
  451. */
  452. KDB_STATE_SET(DOING_SS);
  453. return KDB_CMD_SS;
  454. }
  455. static kdbtab_t bptab[] = {
  456. { .name = "bp",
  457. .func = kdb_bp,
  458. .usage = "[<vaddr>]",
  459. .help = "Set/Display breakpoints",
  460. .flags = KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS,
  461. },
  462. { .name = "bl",
  463. .func = kdb_bp,
  464. .usage = "[<vaddr>]",
  465. .help = "Display breakpoints",
  466. .flags = KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS,
  467. },
  468. { .name = "bc",
  469. .func = kdb_bc,
  470. .usage = "<bpnum>",
  471. .help = "Clear Breakpoint",
  472. .flags = KDB_ENABLE_FLOW_CTRL,
  473. },
  474. { .name = "be",
  475. .func = kdb_bc,
  476. .usage = "<bpnum>",
  477. .help = "Enable Breakpoint",
  478. .flags = KDB_ENABLE_FLOW_CTRL,
  479. },
  480. { .name = "bd",
  481. .func = kdb_bc,
  482. .usage = "<bpnum>",
  483. .help = "Disable Breakpoint",
  484. .flags = KDB_ENABLE_FLOW_CTRL,
  485. },
  486. { .name = "ss",
  487. .func = kdb_ss,
  488. .usage = "",
  489. .help = "Single Step",
  490. .minlen = 1,
  491. .flags = KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS,
  492. },
  493. };
  494. static kdbtab_t bphcmd = {
  495. .name = "bph",
  496. .func = kdb_bp,
  497. .usage = "[<vaddr>]",
  498. .help = "[datar [length]|dataw [length]] Set hw brk",
  499. .flags = KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS,
  500. };
  501. /* Initialize the breakpoint table and register breakpoint commands. */
  502. void __init kdb_initbptab(void)
  503. {
  504. int i;
  505. kdb_bp_t *bp;
  506. /*
  507. * First time initialization.
  508. */
  509. memset(&kdb_breakpoints, '\0', sizeof(kdb_breakpoints));
  510. for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++)
  511. bp->bp_free = 1;
  512. kdb_register_table(bptab, ARRAY_SIZE(bptab));
  513. if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)
  514. kdb_register_table(&bphcmd, 1);
  515. }