kdb_bp.c 11 KB

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