kdb_bp.c 11 KB

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