source: mainline/kernel/arch/amd64/src/debugger.c@ 7d42cc9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7d42cc9 was a000878c, checked in by Martin Decky <martin@…>, 16 years ago

make sure that all statically allocated strings are declared as "const char *"
and are treated as read-only

  • Property mode set to 100644
File size: 9.4 KB
RevLine 
[4e49572]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[4e49572]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[06e1e95]29/** @addtogroup amd64debug
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[4e49572]35#include <arch/debugger.h>
36#include <console/kconsole.h>
37#include <console/cmd.h>
38#include <print.h>
39#include <panic.h>
40#include <interrupt.h>
41#include <arch/asm.h>
42#include <arch/cpu.h>
43#include <debug.h>
44#include <func.h>
[6c6a19e6]45#include <smp/ipi.h>
[e2b762ec]46#include <symtab.h>
47
[4e49572]48typedef struct {
[7f1c620]49 uintptr_t address; /**< Breakpoint address */
[4e49572]50 int flags; /**< Flags regarding breakpoint */
51 int counter; /**< How many times the exception occured */
52} bpinfo_t;
53
54static bpinfo_t breakpoints[BKPOINTS_MAX];
55SPINLOCK_INITIALIZE(bkpoint_lock);
56
[76fca31]57#ifdef CONFIG_KCONSOLE
58
[4e49572]59static int cmd_print_breakpoints(cmd_arg_t *argv);
60static cmd_info_t bkpts_info = {
61 .name = "bkpts",
62 .description = "Print breakpoint table.",
63 .func = cmd_print_breakpoints,
64 .argc = 0,
65};
66
67static int cmd_del_breakpoint(cmd_arg_t *argv);
68static cmd_arg_t del_argv = {
69 .type = ARG_TYPE_INT
70};
71static cmd_info_t delbkpt_info = {
72 .name = "delbkpt",
73 .description = "delbkpt <number> - Delete breakpoint.",
74 .func = cmd_del_breakpoint,
75 .argc = 1,
76 .argv = &del_argv
77};
78
79static int cmd_add_breakpoint(cmd_arg_t *argv);
80static cmd_arg_t add_argv = {
81 .type = ARG_TYPE_INT
82};
83static cmd_info_t addbkpt_info = {
84 .name = "addbkpt",
85 .description = "addbkpt <&symbol> - new breakpoint.",
86 .func = cmd_add_breakpoint,
87 .argc = 1,
88 .argv = &add_argv
89};
90
91static cmd_arg_t addw_argv = {
92 .type = ARG_TYPE_INT
93};
94static cmd_info_t addwatchp_info = {
95 .name = "addwatchp",
96 .description = "addbwatchp <&symbol> - new write watchpoint.",
97 .func = cmd_add_breakpoint,
98 .argc = 1,
99 .argv = &addw_argv
100};
101
[76fca31]102#endif /* CONFIG_KCONSOLE */
[4e49572]103
[6c6a19e6]104/* Setup DR register according to table */
105static void setup_dr(int curidx)
106{
[7f1c620]107 unative_t dr7;
[6c6a19e6]108 bpinfo_t *cur = &breakpoints[curidx];
109 int flags = breakpoints[curidx].flags;
110
111 /* Disable breakpoint in DR7 */
112 dr7 = read_dr7();
113 dr7 &= ~(0x2 << (curidx*2));
114
115 if (cur->address) { /* Setup DR register */
116 /* Set breakpoint to debug registers */
117 switch (curidx) {
118 case 0:
119 write_dr0(cur->address);
120 break;
121 case 1:
122 write_dr1(cur->address);
123 break;
124 case 2:
125 write_dr2(cur->address);
126 break;
127 case 3:
128 write_dr3(cur->address);
129 break;
130 }
131 /* Set type to requested breakpoint & length*/
132 dr7 &= ~ (0x3 << (16 + 4*curidx));
133 dr7 &= ~ (0x3 << (18 + 4*curidx));
134 if ((flags & BKPOINT_INSTR)) {
135 ;
136 } else {
[f4c2b6a]137
138#ifdef __32_BITS__
139 dr7 |= ((unative_t) 0x3) << (18 + 4 * curidx);
140#endif
141
142#ifdef __64_BITS__
143 dr7 |= ((unative_t) 0x2) << (18 + 4 * curidx);
144#endif
[6c6a19e6]145
146 if ((flags & BKPOINT_WRITE))
[f4c2b6a]147 dr7 |= ((unative_t) 0x1) << (16 + 4 * curidx);
[6c6a19e6]148 else if ((flags & BKPOINT_READ_WRITE))
[f4c2b6a]149 dr7 |= ((unative_t) 0x3) << (16 + 4 * curidx);
[6c6a19e6]150 }
151
152 /* Enable global breakpoint */
[d7c9fcb]153 dr7 |= 0x2 << (curidx * 2);
[6c6a19e6]154
155 write_dr7(dr7);
156
157 }
158}
159
[4e49572]160/** Enable hardware breakpoint
161 *
162 * @param where Address of HW breakpoint
163 * @param flags Type of breakpoint (EXECUTE, WRITE)
164 * @return Debug slot on success, -1 - no available HW breakpoint
165 */
[7f043c0]166int breakpoint_add(const void *where, const int flags, int curidx)
[4e49572]167{
168 ipl_t ipl;
169 int i;
[6c6a19e6]170 bpinfo_t *cur;
[4e49572]171
[7f043c0]172 ASSERT(flags & (BKPOINT_INSTR | BKPOINT_WRITE | BKPOINT_READ_WRITE));
[4e49572]173
174 ipl = interrupts_disable();
175 spinlock_lock(&bkpoint_lock);
176
[6c6a19e6]177 if (curidx == -1) {
178 /* Find free space in slots */
[7f043c0]179 for (i = 0; i < BKPOINTS_MAX; i++)
[6c6a19e6]180 if (!breakpoints[i].address) {
181 curidx = i;
182 break;
183 }
184 if (curidx == -1) {
185 /* Too many breakpoints */
186 spinlock_unlock(&bkpoint_lock);
187 interrupts_restore(ipl);
188 return -1;
[4e49572]189 }
190 }
[6c6a19e6]191 cur = &breakpoints[curidx];
192
[7f1c620]193 cur->address = (uintptr_t) where;
[4e49572]194 cur->flags = flags;
195 cur->counter = 0;
196
[6c6a19e6]197 setup_dr(curidx);
[4e49572]198
199 spinlock_unlock(&bkpoint_lock);
200 interrupts_restore(ipl);
201
[6c6a19e6]202 /* Send IPI */
[6da1013f]203// ipi_broadcast(VECTOR_DEBUG_IPI);
[6c6a19e6]204
[4e49572]205 return curidx;
206}
207
[6da1013f]208#ifdef __64_BITS__
209 #define getip(x) ((x)->rip)
[23d22eb]210#else
[6da1013f]211 #define getip(x) ((x)->eip)
[23d22eb]212#endif
213
[4e49572]214static void handle_exception(int slot, istate_t *istate)
215{
216 ASSERT(breakpoints[slot].address);
217
218 /* Handle zero checker */
219 if (! (breakpoints[slot].flags & BKPOINT_INSTR)) {
220 if ((breakpoints[slot].flags & BKPOINT_CHECK_ZERO)) {
[7f1c620]221 if (*((unative_t *) breakpoints[slot].address) != 0)
[4e49572]222 return;
[d7c9fcb]223 printf("*** Found ZERO on address %lx (slot %d) ***\n",
224 breakpoints[slot].address, slot);
[4e49572]225 } else {
[7f043c0]226 printf("Data watchpoint - new data: %lx\n",
[d7c9fcb]227 *((unative_t *) breakpoints[slot].address));
[4e49572]228 }
229 }
[e2b762ec]230
[e16e0d59]231 printf("Reached breakpoint %d:%lx (%s)\n", slot, getip(istate),
232 symtab_fmt_name_lookup(getip(istate)));
[76fca31]233
234#ifdef CONFIG_KCONSOLE
235 atomic_set(&haltstate, 1);
[0b6eba5]236 kconsole("debug", "Debug console ready.\n", false);
[76fca31]237 atomic_set(&haltstate, 0);
238#endif
[4e49572]239}
240
241void breakpoint_del(int slot)
242{
243 bpinfo_t *cur;
244 ipl_t ipl;
245
246 ipl = interrupts_disable();
247 spinlock_lock(&bkpoint_lock);
248
249 cur = &breakpoints[slot];
250 if (!cur->address) {
251 spinlock_unlock(&bkpoint_lock);
252 interrupts_restore(ipl);
253 return;
254 }
255
256 cur->address = NULL;
257
[6c6a19e6]258 setup_dr(slot);
[4e49572]259
260 spinlock_unlock(&bkpoint_lock);
261 interrupts_restore(ipl);
[26d3ae2]262// ipi_broadcast(VECTOR_DEBUG_IPI);
[4e49572]263}
264
[6c6a19e6]265
266
[7f043c0]267static void debug_exception(int n __attribute__((unused)), istate_t *istate)
[6c6a19e6]268{
[7f1c620]269 unative_t dr6;
[6c6a19e6]270 int i;
271
272 /* Set RF to restart the instruction */
[6da1013f]273#ifdef __64_BITS__
[6c6a19e6]274 istate->rflags |= RFLAGS_RF;
275#else
276 istate->eflags |= EFLAGS_RF;
277#endif
278
279 dr6 = read_dr6();
280 for (i=0; i < BKPOINTS_MAX; i++) {
281 if (dr6 & (1 << i)) {
282 dr6 &= ~ (1 << i);
283 write_dr6(dr6);
284
285 handle_exception(i, istate);
286 }
287 }
288}
289
290#ifdef CONFIG_SMP
[d7c9fcb]291static void
292debug_ipi(int n __attribute__((unused)),
293 istate_t *istate __attribute__((unused)))
[6c6a19e6]294{
295 int i;
296
297 spinlock_lock(&bkpoint_lock);
[7f043c0]298 for (i = 0; i < BKPOINTS_MAX; i++)
[6c6a19e6]299 setup_dr(i);
300 spinlock_unlock(&bkpoint_lock);
301}
302#endif
[4e49572]303
304/** Initialize debugger */
305void debugger_init()
306{
307 int i;
308
[d7c9fcb]309 for (i = 0; i < BKPOINTS_MAX; i++)
[4e49572]310 breakpoints[i].address = NULL;
[76fca31]311
312#ifdef CONFIG_KCONSOLE
[4e49572]313 cmd_initialize(&bkpts_info);
314 if (!cmd_register(&bkpts_info))
[76fca31]315 printf("Cannot register command %s\n", bkpts_info.name);
[4e49572]316
317 cmd_initialize(&delbkpt_info);
318 if (!cmd_register(&delbkpt_info))
[76fca31]319 printf("Cannot register command %s\n", delbkpt_info.name);
[4e49572]320
321 cmd_initialize(&addbkpt_info);
322 if (!cmd_register(&addbkpt_info))
[76fca31]323 printf("Cannot register command %s\n", addbkpt_info.name);
[4e49572]324
325 cmd_initialize(&addwatchp_info);
326 if (!cmd_register(&addwatchp_info))
[76fca31]327 printf("Cannot register command %s\n", addwatchp_info.name);
328#endif /* CONFIG_KCONSOLE */
[4e49572]329
[d7c9fcb]330 exc_register(VECTOR_DEBUG, "debugger", debug_exception);
[6c6a19e6]331#ifdef CONFIG_SMP
[d7c9fcb]332 exc_register(VECTOR_DEBUG_IPI, "debugger_smp", debug_ipi);
[6c6a19e6]333#endif
[4e49572]334}
[b45c443]335
[76fca31]336#ifdef CONFIG_KCONSOLE
337/** Print table of active breakpoints */
338int cmd_print_breakpoints(cmd_arg_t *argv __attribute__((unused)))
339{
340 unsigned int i;
341
[6da1013f]342#ifdef __32_BITS__
[76fca31]343 printf("# Count Address In symbol\n");
344 printf("-- ----- ---------- ---------\n");
345#endif
346
347#ifdef __64_BITS__
348 printf("# Count Address In symbol\n");
349 printf("-- ----- ------------------ ---------\n");
350#endif
351
352 for (i = 0; i < BKPOINTS_MAX; i++)
353 if (breakpoints[i].address) {
[a000878c]354 const char *symbol = symtab_fmt_name_lookup(
[e16e0d59]355 breakpoints[i].address);
[76fca31]356
357#ifdef __32_BITS__
358 printf("%-2u %-5d %#10zx %s\n", i,
359 breakpoints[i].counter, breakpoints[i].address,
360 symbol);
361#endif
362
363#ifdef __64_BITS__
364 printf("%-2u %-5d %#18zx %s\n", i,
365 breakpoints[i].counter, breakpoints[i].address,
366 symbol);
367#endif
368
369 }
370 return 1;
371}
372
373/** Remove breakpoint from table */
374int cmd_del_breakpoint(cmd_arg_t *argv)
375{
376 unative_t bpno = argv->intval;
377 if (bpno > BKPOINTS_MAX) {
378 printf("Invalid breakpoint number.\n");
379 return 0;
380 }
381 breakpoint_del(argv->intval);
382 return 1;
383}
384
385/** Add new breakpoint to table */
386static int cmd_add_breakpoint(cmd_arg_t *argv)
387{
388 int flags;
389 int id;
390
391 if (argv == &add_argv) {
392 flags = BKPOINT_INSTR;
393 } else { /* addwatchp */
394 flags = BKPOINT_WRITE;
395 }
396 printf("Adding breakpoint on address: %p\n", argv->intval);
397 id = breakpoint_add((void *)argv->intval, flags, -1);
398 if (id < 0)
399 printf("Add breakpoint failed.\n");
400 else
401 printf("Added breakpoint %d.\n", id);
402
403 return 1;
404}
405#endif /* CONFIG_KCONSOLE */
406
[06e1e95]407/** @}
[b45c443]408 */
Note: See TracBrowser for help on using the repository browser.