1
2
3
4
5
6
7
8
9
10
11
12#include <linux/string.h>
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/kdb.h>
16#include <linux/kgdb.h>
17#include <linux/smp.h>
18#include <linux/sched.h>
19#include <linux/interrupt.h>
20#include "kdb_private.h"
21
22
23
24
25kdb_bp_t kdb_breakpoints[KDB_MAXBPT];
26
27static void kdb_setsinglestep(struct pt_regs *regs)
28{
29 KDB_STATE_SET(DOING_SS);
30}
31
32static char *kdb_rwtypes[] = {
33 "Instruction(i)",
34 "Instruction(Register)",
35 "Data Write",
36 "I/O",
37 "Data Access"
38};
39
40static char *kdb_bptype(kdb_bp_t *bp)
41{
42 if (bp->bp_type < 0 || bp->bp_type > 4)
43 return "";
44
45 return kdb_rwtypes[bp->bp_type];
46}
47
48static int kdb_parsebp(int argc, const char **argv, int *nextargp, kdb_bp_t *bp)
49{
50 int nextarg = *nextargp;
51 int diag;
52
53 bp->bph_length = 1;
54 if ((argc + 1) != nextarg) {
55 if (strncasecmp(argv[nextarg], "datar", sizeof("datar")) == 0)
56 bp->bp_type = BP_ACCESS_WATCHPOINT;
57 else if (strncasecmp(argv[nextarg], "dataw", sizeof("dataw")) == 0)
58 bp->bp_type = BP_WRITE_WATCHPOINT;
59 else if (strncasecmp(argv[nextarg], "inst", sizeof("inst")) == 0)
60 bp->bp_type = BP_HARDWARE_BREAKPOINT;
61 else
62 return KDB_ARGCOUNT;
63
64 bp->bph_length = 1;
65
66 nextarg++;
67
68 if ((argc + 1) != nextarg) {
69 unsigned long len;
70
71 diag = kdbgetularg((char *)argv[nextarg],
72 &len);
73 if (diag)
74 return diag;
75
76
77 if (len > 8)
78 return KDB_BADLENGTH;
79
80 bp->bph_length = len;
81 nextarg++;
82 }
83
84 if ((argc + 1) != nextarg)
85 return KDB_ARGCOUNT;
86 }
87
88 *nextargp = nextarg;
89 return 0;
90}
91
92static int _kdb_bp_remove(kdb_bp_t *bp)
93{
94 int ret = 1;
95 if (!bp->bp_installed)
96 return ret;
97 if (!bp->bp_type)
98 ret = dbg_remove_sw_break(bp->bp_addr);
99 else
100 ret = arch_kgdb_ops.remove_hw_breakpoint(bp->bp_addr,
101 bp->bph_length,
102 bp->bp_type);
103 if (ret == 0)
104 bp->bp_installed = 0;
105 return ret;
106}
107
108static void kdb_handle_bp(struct pt_regs *regs, kdb_bp_t *bp)
109{
110 if (KDB_DEBUG(BP))
111 kdb_printf("regs->ip = 0x%lx\n", instruction_pointer(regs));
112
113
114
115
116 kdb_setsinglestep(regs);
117
118
119
120
121 bp->bp_delay = 0;
122 bp->bp_delayed = 1;
123}
124
125static int _kdb_bp_install(struct pt_regs *regs, kdb_bp_t *bp)
126{
127 int ret;
128
129
130
131
132 if (KDB_DEBUG(BP))
133 kdb_printf("%s: bp_installed %d\n",
134 __func__, bp->bp_installed);
135 if (!KDB_STATE(SSBPT))
136 bp->bp_delay = 0;
137 if (bp->bp_installed)
138 return 1;
139 if (bp->bp_delay || (bp->bp_delayed && KDB_STATE(DOING_SS))) {
140 if (KDB_DEBUG(BP))
141 kdb_printf("%s: delayed bp\n", __func__);
142 kdb_handle_bp(regs, bp);
143 return 0;
144 }
145 if (!bp->bp_type)
146 ret = dbg_set_sw_break(bp->bp_addr);
147 else
148 ret = arch_kgdb_ops.set_hw_breakpoint(bp->bp_addr,
149 bp->bph_length,
150 bp->bp_type);
151 if (ret == 0) {
152 bp->bp_installed = 1;
153 } else {
154 kdb_printf("%s: failed to set breakpoint at 0x%lx\n",
155 __func__, bp->bp_addr);
156 if (!bp->bp_type) {
157 kdb_printf("Software breakpoints are unavailable.\n"
158 " Boot the kernel with rodata=off\n"
159 " OR use hw breaks: help bph\n");
160 }
161 return 1;
162 }
163 return 0;
164}
165
166
167
168
169
170
171
172
173
174void kdb_bp_install(struct pt_regs *regs)
175{
176 int i;
177
178 for (i = 0; i < KDB_MAXBPT; i++) {
179 kdb_bp_t *bp = &kdb_breakpoints[i];
180
181 if (KDB_DEBUG(BP)) {
182 kdb_printf("%s: bp %d bp_enabled %d\n",
183 __func__, i, bp->bp_enabled);
184 }
185 if (bp->bp_enabled)
186 _kdb_bp_install(regs, bp);
187 }
188}
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205void kdb_bp_remove(void)
206{
207 int i;
208
209 for (i = KDB_MAXBPT - 1; i >= 0; i--) {
210 kdb_bp_t *bp = &kdb_breakpoints[i];
211
212 if (KDB_DEBUG(BP)) {
213 kdb_printf("%s: bp %d bp_enabled %d\n",
214 __func__, i, bp->bp_enabled);
215 }
216 if (bp->bp_enabled)
217 _kdb_bp_remove(bp);
218 }
219}
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238static void kdb_printbp(kdb_bp_t *bp, int i)
239{
240 kdb_printf("%s ", kdb_bptype(bp));
241 kdb_printf("BP #%d at ", i);
242 kdb_symbol_print(bp->bp_addr, NULL, KDB_SP_DEFAULT);
243
244 if (bp->bp_enabled)
245 kdb_printf("\n is enabled ");
246 else
247 kdb_printf("\n is disabled");
248
249 kdb_printf(" addr at %016lx, hardtype=%d installed=%d\n",
250 bp->bp_addr, bp->bp_type, bp->bp_installed);
251
252 kdb_printf("\n");
253}
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277static int kdb_bp(int argc, const char **argv)
278{
279 int i, bpno;
280 kdb_bp_t *bp, *bp_check;
281 int diag;
282 char *symname = NULL;
283 long offset = 0ul;
284 int nextarg;
285 kdb_bp_t template = {0};
286
287 if (argc == 0) {
288
289
290
291 for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT;
292 bpno++, bp++) {
293 if (bp->bp_free)
294 continue;
295 kdb_printbp(bp, bpno);
296 }
297
298 return 0;
299 }
300
301 nextarg = 1;
302 diag = kdbgetaddrarg(argc, argv, &nextarg, &template.bp_addr,
303 &offset, &symname);
304 if (diag)
305 return diag;
306 if (!template.bp_addr)
307 return KDB_BADINT;
308
309
310
311
312 for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT; bpno++, bp++) {
313 if (bp->bp_free)
314 break;
315 }
316
317 if (bpno == KDB_MAXBPT)
318 return KDB_TOOMANYBPT;
319
320 if (strcmp(argv[0], "bph") == 0) {
321 template.bp_type = BP_HARDWARE_BREAKPOINT;
322 diag = kdb_parsebp(argc, argv, &nextarg, &template);
323 if (diag)
324 return diag;
325 } else {
326 template.bp_type = BP_BREAKPOINT;
327 }
328
329
330
331
332
333
334
335 for (i = 0, bp_check = kdb_breakpoints; i < KDB_MAXBPT;
336 i++, bp_check++) {
337 if (!bp_check->bp_free &&
338 bp_check->bp_addr == template.bp_addr) {
339 kdb_printf("You already have a breakpoint at "
340 kdb_bfd_vma_fmt0 "\n", template.bp_addr);
341 return KDB_DUPBPT;
342 }
343 }
344
345 template.bp_enabled = 1;
346
347
348
349
350 *bp = template;
351 bp->bp_free = 0;
352
353 kdb_printbp(bp, bpno);
354
355 return 0;
356}
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377static int kdb_bc(int argc, const char **argv)
378{
379 unsigned long addr;
380 kdb_bp_t *bp = NULL;
381 int lowbp = KDB_MAXBPT;
382 int highbp = 0;
383 int done = 0;
384 int i;
385 int diag = 0;
386
387 int cmd;
388#define KDBCMD_BC 0
389#define KDBCMD_BE 1
390#define KDBCMD_BD 2
391
392 if (strcmp(argv[0], "be") == 0)
393 cmd = KDBCMD_BE;
394 else if (strcmp(argv[0], "bd") == 0)
395 cmd = KDBCMD_BD;
396 else
397 cmd = KDBCMD_BC;
398
399 if (argc != 1)
400 return KDB_ARGCOUNT;
401
402 if (strcmp(argv[1], "*") == 0) {
403 lowbp = 0;
404 highbp = KDB_MAXBPT;
405 } else {
406 diag = kdbgetularg(argv[1], &addr);
407 if (diag)
408 return diag;
409
410
411
412
413
414 if (addr < KDB_MAXBPT) {
415 bp = &kdb_breakpoints[addr];
416 lowbp = highbp = addr;
417 highbp++;
418 } else {
419 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT;
420 i++, bp++) {
421 if (bp->bp_addr == addr) {
422 lowbp = highbp = i;
423 highbp++;
424 break;
425 }
426 }
427 }
428 }
429
430
431
432
433
434 for (bp = &kdb_breakpoints[lowbp], i = lowbp;
435 i < highbp;
436 i++, bp++) {
437 if (bp->bp_free)
438 continue;
439
440 done++;
441
442 switch (cmd) {
443 case KDBCMD_BC:
444 bp->bp_enabled = 0;
445
446 kdb_printf("Breakpoint %d at "
447 kdb_bfd_vma_fmt " cleared\n",
448 i, bp->bp_addr);
449
450 bp->bp_addr = 0;
451 bp->bp_free = 1;
452
453 break;
454 case KDBCMD_BE:
455 bp->bp_enabled = 1;
456
457 kdb_printf("Breakpoint %d at "
458 kdb_bfd_vma_fmt " enabled",
459 i, bp->bp_addr);
460
461 kdb_printf("\n");
462 break;
463 case KDBCMD_BD:
464 if (!bp->bp_enabled)
465 break;
466
467 bp->bp_enabled = 0;
468
469 kdb_printf("Breakpoint %d at "
470 kdb_bfd_vma_fmt " disabled\n",
471 i, bp->bp_addr);
472
473 break;
474 }
475 if (bp->bp_delay && (cmd == KDBCMD_BC || cmd == KDBCMD_BD)) {
476 bp->bp_delay = 0;
477 KDB_STATE_CLEAR(SSBPT);
478 }
479 }
480
481 return (!done) ? KDB_BPTNOTFOUND : 0;
482}
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506static int kdb_ss(int argc, const char **argv)
507{
508 if (argc != 0)
509 return KDB_ARGCOUNT;
510
511
512
513 KDB_STATE_SET(DOING_SS);
514 return KDB_CMD_SS;
515}
516
517
518
519void __init kdb_initbptab(void)
520{
521 int i;
522 kdb_bp_t *bp;
523
524
525
526
527 memset(&kdb_breakpoints, '\0', sizeof(kdb_breakpoints));
528
529 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++)
530 bp->bp_free = 1;
531
532 kdb_register_flags("bp", kdb_bp, "[<vaddr>]",
533 "Set/Display breakpoints", 0,
534 KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS);
535 kdb_register_flags("bl", kdb_bp, "[<vaddr>]",
536 "Display breakpoints", 0,
537 KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS);
538 if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)
539 kdb_register_flags("bph", kdb_bp, "[<vaddr>]",
540 "[datar [length]|dataw [length]] Set hw brk", 0,
541 KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS);
542 kdb_register_flags("bc", kdb_bc, "<bpnum>",
543 "Clear Breakpoint", 0,
544 KDB_ENABLE_FLOW_CTRL);
545 kdb_register_flags("be", kdb_bc, "<bpnum>",
546 "Enable Breakpoint", 0,
547 KDB_ENABLE_FLOW_CTRL);
548 kdb_register_flags("bd", kdb_bc, "<bpnum>",
549 "Disable Breakpoint", 0,
550 KDB_ENABLE_FLOW_CTRL);
551
552 kdb_register_flags("ss", kdb_ss, "",
553 "Single Step", 1,
554 KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS);
555
556
557
558}
559