kdb_bp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. * Find an empty bp structure to allocate
  270. */
  271. for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT; bpno++, bp++) {
  272. if (bp->bp_free)
  273. break;
  274. }
  275. if (bpno == KDB_MAXBPT)
  276. return KDB_TOOMANYBPT;
  277. if (strcmp(argv[0], "bph") == 0) {
  278. template.bp_type = BP_HARDWARE_BREAKPOINT;
  279. diag = kdb_parsebp(argc, argv, &nextarg, &template);
  280. if (diag)
  281. return diag;
  282. } else {
  283. template.bp_type = BP_BREAKPOINT;
  284. }
  285. /*
  286. * Check for clashing breakpoints.
  287. *
  288. * Note, in this design we can't have hardware breakpoints
  289. * enabled for both read and write on the same address.
  290. */
  291. for (i = 0, bp_check = kdb_breakpoints; i < KDB_MAXBPT;
  292. i++, bp_check++) {
  293. if (!bp_check->bp_free &&
  294. bp_check->bp_addr == template.bp_addr) {
  295. kdb_printf("You already have a breakpoint at "
  296. kdb_bfd_vma_fmt0 "\n", template.bp_addr);
  297. return KDB_DUPBPT;
  298. }
  299. }
  300. template.bp_enabled = 1;
  301. /*
  302. * Actually allocate the breakpoint found earlier
  303. */
  304. *bp = template;
  305. bp->bp_free = 0;
  306. kdb_printbp(bp, bpno);
  307. return 0;
  308. }
  309. /*
  310. * kdb_bc
  311. *
  312. * Handles the 'bc', 'be', and 'bd' commands
  313. *
  314. * [bd|bc|be] <breakpoint-number>
  315. * [bd|bc|be] *
  316. *
  317. * Parameters:
  318. * argc Count of arguments in argv
  319. * argv Space delimited command line arguments
  320. * Outputs:
  321. * None.
  322. * Returns:
  323. * Zero for success, a kdb diagnostic for failure
  324. * Locking:
  325. * None.
  326. * Remarks:
  327. */
  328. static int kdb_bc(int argc, const char **argv)
  329. {
  330. unsigned long addr;
  331. kdb_bp_t *bp = NULL;
  332. int lowbp = KDB_MAXBPT;
  333. int highbp = 0;
  334. int done = 0;
  335. int i;
  336. int diag = 0;
  337. int cmd; /* KDBCMD_B? */
  338. #define KDBCMD_BC 0
  339. #define KDBCMD_BE 1
  340. #define KDBCMD_BD 2
  341. if (strcmp(argv[0], "be") == 0)
  342. cmd = KDBCMD_BE;
  343. else if (strcmp(argv[0], "bd") == 0)
  344. cmd = KDBCMD_BD;
  345. else
  346. cmd = KDBCMD_BC;
  347. if (argc != 1)
  348. return KDB_ARGCOUNT;
  349. if (strcmp(argv[1], "*") == 0) {
  350. lowbp = 0;
  351. highbp = KDB_MAXBPT;
  352. } else {
  353. diag = kdbgetularg(argv[1], &addr);
  354. if (diag)
  355. return diag;
  356. /*
  357. * For addresses less than the maximum breakpoint number,
  358. * assume that the breakpoint number is desired.
  359. */
  360. if (addr < KDB_MAXBPT) {
  361. bp = &kdb_breakpoints[addr];
  362. lowbp = highbp = addr;
  363. highbp++;
  364. } else {
  365. for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT;
  366. i++, bp++) {
  367. if (bp->bp_addr == addr) {
  368. lowbp = highbp = i;
  369. highbp++;
  370. break;
  371. }
  372. }
  373. }
  374. }
  375. /*
  376. * Now operate on the set of breakpoints matching the input
  377. * criteria (either '*' for all, or an individual breakpoint).
  378. */
  379. for (bp = &kdb_breakpoints[lowbp], i = lowbp;
  380. i < highbp;
  381. i++, bp++) {
  382. if (bp->bp_free)
  383. continue;
  384. done++;
  385. switch (cmd) {
  386. case KDBCMD_BC:
  387. bp->bp_enabled = 0;
  388. kdb_printf("Breakpoint %d at "
  389. kdb_bfd_vma_fmt " cleared\n",
  390. i, bp->bp_addr);
  391. bp->bp_addr = 0;
  392. bp->bp_free = 1;
  393. break;
  394. case KDBCMD_BE:
  395. bp->bp_enabled = 1;
  396. kdb_printf("Breakpoint %d at "
  397. kdb_bfd_vma_fmt " enabled",
  398. i, bp->bp_addr);
  399. kdb_printf("\n");
  400. break;
  401. case KDBCMD_BD:
  402. if (!bp->bp_enabled)
  403. break;
  404. bp->bp_enabled = 0;
  405. kdb_printf("Breakpoint %d at "
  406. kdb_bfd_vma_fmt " disabled\n",
  407. i, bp->bp_addr);
  408. break;
  409. }
  410. if (bp->bp_delay && (cmd == KDBCMD_BC || cmd == KDBCMD_BD)) {
  411. bp->bp_delay = 0;
  412. KDB_STATE_CLEAR(SSBPT);
  413. }
  414. }
  415. return (!done) ? KDB_BPTNOTFOUND : 0;
  416. }
  417. /*
  418. * kdb_ss
  419. *
  420. * Process the 'ss' (Single Step) command.
  421. *
  422. * ss
  423. *
  424. * Parameters:
  425. * argc Argument count
  426. * argv Argument vector
  427. * Outputs:
  428. * None.
  429. * Returns:
  430. * KDB_CMD_SS for success, a kdb error if failure.
  431. * Locking:
  432. * None.
  433. * Remarks:
  434. *
  435. * Set the arch specific option to trigger a debug trap after the next
  436. * instruction.
  437. */
  438. static int kdb_ss(int argc, const char **argv)
  439. {
  440. if (argc != 0)
  441. return KDB_ARGCOUNT;
  442. /*
  443. * Set trace flag and go.
  444. */
  445. KDB_STATE_SET(DOING_SS);
  446. return KDB_CMD_SS;
  447. }
  448. /* Initialize the breakpoint table and register breakpoint commands. */
  449. void __init kdb_initbptab(void)
  450. {
  451. int i;
  452. kdb_bp_t *bp;
  453. /*
  454. * First time initialization.
  455. */
  456. memset(&kdb_breakpoints, '\0', sizeof(kdb_breakpoints));
  457. for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++)
  458. bp->bp_free = 1;
  459. kdb_register_flags("bp", kdb_bp, "[<vaddr>]",
  460. "Set/Display breakpoints", 0,
  461. KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS);
  462. kdb_register_flags("bl", kdb_bp, "[<vaddr>]",
  463. "Display breakpoints", 0,
  464. KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS);
  465. if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)
  466. kdb_register_flags("bph", kdb_bp, "[<vaddr>]",
  467. "[datar [length]|dataw [length]] Set hw brk", 0,
  468. KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS);
  469. kdb_register_flags("bc", kdb_bc, "<bpnum>",
  470. "Clear Breakpoint", 0,
  471. KDB_ENABLE_FLOW_CTRL);
  472. kdb_register_flags("be", kdb_bc, "<bpnum>",
  473. "Enable Breakpoint", 0,
  474. KDB_ENABLE_FLOW_CTRL);
  475. kdb_register_flags("bd", kdb_bc, "<bpnum>",
  476. "Disable Breakpoint", 0,
  477. KDB_ENABLE_FLOW_CTRL);
  478. kdb_register_flags("ss", kdb_ss, "",
  479. "Single Step", 1,
  480. KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS);
  481. /*
  482. * Architecture dependent initialization.
  483. */
  484. }