2 * Created by: Jason Wessel <jason.wessel@windriver.com>
4 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
11 #include <linux/kgdb.h>
12 #include <linux/kdb.h>
13 #include <linux/kdebug.h>
14 #include "kdb_private.h"
15 #include "../debug_core.h"
18 * KDB interface to KGDB internals
20 get_char_func kdb_poll_funcs
[] = {
28 EXPORT_SYMBOL_GPL(kdb_poll_funcs
);
31 EXPORT_SYMBOL_GPL(kdb_poll_idx
);
33 static struct kgdb_state
*kdb_ks
;
35 int kdb_stub(struct kgdb_state
*ks
)
39 unsigned long addr
= kgdb_arch_pc(ks
->ex_vector
, ks
->linux_regs
);
40 kdb_reason_t reason
= KDB_REASON_OOPS
;
41 kdb_dbtrap_t db_result
= KDB_DB_NOBPT
;
45 if (KDB_STATE(REENTRY
)) {
46 reason
= KDB_REASON_SWITCH
;
47 KDB_STATE_CLEAR(REENTRY
);
48 addr
= instruction_pointer(ks
->linux_regs
);
50 ks
->pass_exception
= 0;
51 if (atomic_read(&kgdb_setting_breakpoint
))
52 reason
= KDB_REASON_KEYBOARD
;
54 for (i
= 0, bp
= kdb_breakpoints
; i
< KDB_MAXBPT
; i
++, bp
++) {
55 if ((bp
->bp_enabled
) && (bp
->bp_addr
== addr
)) {
56 reason
= KDB_REASON_BREAK
;
57 db_result
= KDB_DB_BPT
;
58 if (addr
!= instruction_pointer(ks
->linux_regs
))
59 kgdb_arch_set_pc(ks
->linux_regs
, addr
);
63 if (reason
== KDB_REASON_BREAK
|| reason
== KDB_REASON_SWITCH
) {
64 for (i
= 0, bp
= kdb_breakpoints
; i
< KDB_MAXBPT
; i
++, bp
++) {
67 if (bp
->bp_addr
== addr
) {
71 * SSBPT is set when the kernel debugger must single step a
72 * task in order to re-establish an instruction breakpoint
73 * which uses the instruction replacement mechanism. It is
74 * cleared by any action that removes the need to single-step
77 reason
= KDB_REASON_BREAK
;
78 db_result
= KDB_DB_BPT
;
85 if (reason
!= KDB_REASON_BREAK
&& ks
->ex_vector
== 0 &&
86 ks
->signo
== SIGTRAP
) {
87 reason
= KDB_REASON_SSTEP
;
88 db_result
= KDB_DB_BPT
;
90 /* Set initial kdb state variables */
91 KDB_STATE_CLEAR(KGDB_TRANS
);
92 kdb_initial_cpu
= atomic_read(&kgdb_active
);
93 kdb_current_task
= kgdb_info
[ks
->cpu
].task
;
94 kdb_current_regs
= kgdb_info
[ks
->cpu
].debuggerinfo
;
95 /* Remove any breakpoints as needed by kdb and clear single step */
97 KDB_STATE_CLEAR(DOING_SS
);
98 KDB_STATE_CLEAR(DOING_SSB
);
100 /* zero out any offline cpu data */
101 for_each_present_cpu(i
) {
102 if (!cpu_online(i
)) {
103 kgdb_info
[i
].debuggerinfo
= NULL
;
104 kgdb_info
[i
].task
= NULL
;
107 if (ks
->err_code
== DIE_OOPS
|| reason
== KDB_REASON_OOPS
) {
108 ks
->pass_exception
= 1;
109 KDB_FLAG_SET(CATASTROPHIC
);
111 if (KDB_STATE(SSBPT
) && reason
== KDB_REASON_SSTEP
) {
112 KDB_STATE_CLEAR(SSBPT
);
113 KDB_STATE_CLEAR(DOING_SS
);
115 /* Start kdb main loop */
116 error
= kdb_main_loop(KDB_REASON_ENTER
, reason
,
117 ks
->err_code
, db_result
, ks
->linux_regs
);
120 * Upon exit from the kdb main loop setup break points and restart
121 * the system based on the requested continue state
123 kdb_initial_cpu
= -1;
124 kdb_current_task
= NULL
;
125 kdb_current_regs
= NULL
;
126 KDB_STATE_CLEAR(PAGER
);
127 kdbnearsym_cleanup();
128 if (error
== KDB_CMD_KGDB
) {
129 if (KDB_STATE(DOING_KGDB
))
130 KDB_STATE_CLEAR(DOING_KGDB
);
131 return DBG_PASS_EVENT
;
133 kdb_bp_install(ks
->linux_regs
);
134 dbg_activate_sw_breakpoints();
135 /* Set the exit state to a single step or a continue */
136 if (KDB_STATE(DOING_SS
))
137 gdbstub_state(ks
, "s");
139 gdbstub_state(ks
, "c");
141 KDB_FLAG_CLEAR(CATASTROPHIC
);
143 /* Invoke arch specific exception handling prior to system resume */
144 kgdb_info
[ks
->cpu
].ret_state
= gdbstub_state(ks
, "e");
145 if (ks
->pass_exception
)
146 kgdb_info
[ks
->cpu
].ret_state
= 1;
147 if (error
== KDB_CMD_CPU
) {
148 KDB_STATE_SET(REENTRY
);
150 * Force clear the single step bit because kdb emulates this
151 * differently vs the gdbstub
153 kgdb_single_step
= 0;
154 dbg_deactivate_sw_breakpoints();
155 return DBG_SWITCH_CPU_EVENT
;
157 return kgdb_info
[ks
->cpu
].ret_state
;
160 void kdb_gdb_state_pass(char *buf
)
162 gdbstub_state(kdb_ks
, buf
);