15 static const char * const reg_names[];
19 s_unknown, s_ipc, s_syscall, s_pagefault, s_fputrap,
20 s_interrupt, s_timer_interrupt, s_slowtrap, s_user_invoke,
21 } Guessed_thread_state;
23 enum { MIN_SCREEN_HEIGHT = 20, MIN_SCREEN_WIDTH = 80 };
25 template < typename T > static T peek(T const *addr, Address_type user);
27 static int (*bp_test_log_only)();
28 static int (*bp_test_break)(char *errbuf, size_t bufsize);
31 static unsigned short rows, cols;
38 #include <sys/ioctl.h>
43 #include "initcalls.h"
45 #include "jdb_dbinfo.h"
46 #include "jdb_screen.h"
47 #include "kernel_console.h"
48 #include "kernel_thread.h"
51 #include "libc_support.h"
53 #include "mem_layout.h"
54 #include "push_console.h"
57 #include "static_init.h"
59 #include "thread_state.h"
60 #include "trap_state.h"
64 int (*Jdb::bp_test_log_only)();
65 int (*Jdb::bp_test_break)(char *errbuf, size_t bufsize);
67 unsigned short Jdb::rows, Jdb::cols;
69 const char * const Jdb::reg_names[] =
70 { "EAX", "EBX", "ECX", "EDX", "EBP", "ESI", "EDI", "EIP", "ESP", "EFL" };
72 static Proc::Status jdb_irq_state;
76 Jdb::enter_trap_handler(unsigned /*cpu*/)
80 // Set terminal raw mode
83 // Flush all output streams
89 Jdb::leave_trap_handler(unsigned)
91 // Restore terminal mode
94 // Flush all output streams
98 signal (SIGIO, SIG_IGN); // Ignore hardware interrupts
104 Jdb::handle_user_request(unsigned /*cpu*/)
109 Jdb::test_checksums()
113 // disable interrupts before entering the kernel debugger
116 Jdb::save_disable_irqs(unsigned cpu)
119 jdb_irq_state = Proc::cli_save();
122 // restore interrupts after leaving the kernel debugger
125 Jdb::restore_irqs(unsigned cpu)
128 Proc::sti_restore(jdb_irq_state);
131 STATIC_INITIALIZE_P(Jdb,JDB_INIT_PRIO);
133 PUBLIC static FIASCO_INIT
137 // Install JDB handler
138 Trap_state::base_handler = (Trap_state::Handler)enter_jdb;
140 // be sure that Push_console comes very first
141 static Push_console c;
142 Kconsole::console()->register_console(&c, 0);
144 register_libc_atexit(leave_getchar);
146 Thread::set_int3_handler(handle_int3_threadctx);
154 ioctl (fileno (stdin), TIOCGWINSZ, &win);
158 if (rows < MIN_SCREEN_HEIGHT || cols < MIN_SCREEN_WIDTH)
159 printf ("%sTerminal probably too small, should be at least %dx%d!\033[0m\n",
160 esc_emph, MIN_SCREEN_WIDTH, MIN_SCREEN_HEIGHT);
162 Jdb_screen::set_height(rows);
163 Jdb_screen::set_width(cols);
166 /** handle int3 debug extension */
167 PUBLIC static inline NOEXPORT
169 Jdb::int3_extension()
171 Jdb_entry_frame *entry_frame = Jdb::entry_frame.cpu(0);
172 Address addr = entry_frame->ip();
173 Address_type user = (entry_frame->cs() & 3) ? ADDR_USER : ADDR_KERNEL;
174 Unsigned8 todo = peek ((Unsigned8 *) addr, user);
175 Space *space = NULL; //get_task_id(0);
177 if (todo == 0x3c && peek ((Unsigned8 *) (addr+1), user) == 13)
180 entry_frame->_ax = Vkey::get();
185 else if (todo != 0xeb)
187 snprintf (error_buffer.cpu(0), sizeof (error_buffer.cpu(0)), "INT 3");
191 // todo == 0xeb => enter_kdebug()
193 Mword len = peek ((Unsigned8 *) ++addr, user);
196 peek (((Unsigned8 *) addr + 1), user) == '*' &&
197 peek (((Unsigned8 *) addr + 2), user) == '#')
199 char c = peek (((Unsigned8 *) addr + 3), user);
202 ? execute_command_ni(space, (char const *) entry_frame->_ax)
203 : execute_command_ni(space, (char const *)(addr + 3), len-2))
204 return 1; // => leave Jdb
207 len = len < sizeof(error_buffer.cpu(0))-1 ? len : sizeof(error_buffer.cpu(0))-1;
208 for (i = 0; i < len; i++)
209 error_buffer.cpu(0)[i] = peek ((Unsigned8 *) ++addr, user);
210 error_buffer.cpu(0)[i] = 0;
216 Jdb::handle_special_cmds(int)
221 Jdb::handle_debug_traps(unsigned cpu)
223 switch (entry_frame.cpu(cpu)->_trapno)
226 snprintf (error_buffer.cpu(cpu), sizeof (error_buffer.cpu(cpu)), "Interception");
229 if (int3_extension())
232 if (get_thread(cpu)->d_taskno())
234 if (bp_test_log_only && bp_test_log_only())
237 && bp_test_break(error_buffer.cpu(cpu), sizeof(error_buffer.cpu(cpu))))
248 Jdb::handle_nested_trap(Jdb_entry_frame *e)
250 printf("Trap in JDB: IP:%08lx\n", e->ip());
255 Jdb::handle_conditional_breakpoint(unsigned /*cpu*/)
261 Jdb::get_register(char *reg)
266 putchar(reg_name[0] = 'E');
273 putchar(reg_name[i] = c & 0xdf);
279 if (*((unsigned*)reg_name) == *((unsigned*)reg_names[i]))
292 Jdb::translate_task(Address /*addr*/, Space *task)
294 // we have no idea if addr belongs to kernel or user space
295 // since kernel and user occupy different address spaces
301 Jdb::virt_to_kvirt(Address virt, Mem_space* space)
303 Mem_space::Phys_addr phys;
304 Mem_space::Size size;
309 // We can directly access it via virtual addresses if it's kernel code
310 // (which is always mapped, but doesn't appear in the kernel pagetable)
311 // or if we find a mapping for it in the kernel's master pagetable.
312 return (virt >= (Address)&Mem_layout::load &&
313 virt < (Kernel_thread::init_done()
314 ? (Address)&Mem_layout::end
315 : (Address)&Mem_layout::initcall_end)
316 || Kernel_task::kernel_task()->mem_space()->v_lookup (
317 Mem_space::Addr::create(virt), 0, 0, 0))
324 // We can't directly access it because it's in a different host process
325 // but if the task's pagetable has a mapping for it, we can translate
326 // task-virtual -> physical -> kernel-virtual address and then access.
327 return (space->v_lookup (Mem_space::Addr::create(virt), &phys, &size, 0))
328 ? (Address) Kmem::phys_to_virt (phys.value() + (virt & (size.value()-1)))
333 IMPLEMENT inline NEEDS ["space.h"]
334 template <typename T>
336 Jdb::peek (T const *addr, Address_type user)
338 return current_mem_space()->peek(addr, user);
343 Jdb::peek_task(Address virt, Space *space, void *value, int width)
345 // make sure we don't cross a page boundary
346 if (virt & (width-1))
349 Address kvirt = virt_to_kvirt(virt, space?space->mem_space():0);
350 if (kvirt == (Address)-1)
353 memcpy(value, (void*)kvirt, width);
359 Jdb::poke_task(Address virt, Space *space, void const *value, int width)
361 // make sure we don't cross a page boundary
362 if (virt & (width-1))
365 Address kvirt = virt_to_kvirt(virt, space?space->mem_space():0);
367 if (kvirt == (Address)-1)
370 memcpy((void*)kvirt, value, width);
376 Jdb::is_adapter_memory(Address /*addr*/, Space * /*task*/)
381 #define WEAK __attribute__((weak))
382 extern "C" char in_slowtrap, in_page_fault, in_handle_fputrap;
383 extern "C" char in_interrupt, in_timer_interrupt, in_timer_interrupt_slow;
384 extern "C" char i30_ret_switch WEAK, in_slow_ipc1 WEAK;
385 extern "C" char in_slow_ipc2 WEAK, in_slow_ipc4;
386 extern "C" char in_sc_ipc1 WEAK, in_sc_ipc2 WEAK, in_syscall WEAK;
389 /** Try to guess the thread state of t by walking down the kernel stack and
390 * locking at the first return address we find. */
392 static Jdb::Guessed_thread_state
393 Jdb::guess_thread_state(Thread *t)
395 Guessed_thread_state state = s_unknown;
396 Mword *ktop = (Mword*)((Mword)context_of(t->get_kernel_sp()) +
397 Config::thread_block_size);
399 for (int i=-1; i>-26; i--)
403 if (ktop[i] == (Mword)&in_page_fault)
405 if ((ktop[i] == (Mword)&i30_ret_switch) ||// shortcut.S, int 0x30
406 (ktop[i] == (Mword)&in_slow_ipc1) || // shortcut.S, int 0x30
407 (ktop[i] == (Mword)&in_slow_ipc4) || // entry.S, int 0x30
408 #if !defined(CONFIG_ASSEMBLER_IPC_SHORTCUT)
409 (ktop[i] == (Mword)&in_sc_ipc1) || // entry.S, int 0x30
410 (ktop[i] == (Mword)&in_sc_ipc2) || // entry.S, sysenter
414 else if (ktop[i] == (Mword)&in_syscall)
416 else if (ktop[i] == (Mword)&Thread::user_invoke)
417 state = s_user_invoke;
418 else if (ktop[i] == (Mword)&in_handle_fputrap)
420 else if (ktop[i] == (Mword)&in_interrupt)
422 else if ((ktop[i] == (Mword)&in_timer_interrupt) ||
423 (ktop[i] == (Mword)&in_timer_interrupt_slow))
424 state = s_timer_interrupt;
425 else if (ktop[i] == (Mword)&in_slowtrap)
427 if (state != s_unknown)
432 if (state == s_unknown && (t->state() & Thread_ipc_mask))
438 // Don't make these members of Jdb else we have to include <termios.h>
439 // into the Jdb interface ...
440 static struct termios raw, new_raw;
441 static int getchar_entered;
443 /** prepare Linux console for raw input */
448 if (!getchar_entered++)
450 tcgetattr (fileno (stdin), &raw);
451 memcpy(&new_raw, &raw, sizeof(new_raw));
452 new_raw.c_lflag &= ~(ICANON|ECHO);
453 new_raw.c_cc[VMIN] = 0;
454 new_raw.c_cc[VTIME] = 1;
455 tcsetattr (fileno (stdin), TCSAFLUSH, &new_raw);
459 /** restore Linux console. */
464 if (!--getchar_entered)
465 tcsetattr (fileno (stdin), TCSAFLUSH, &raw);
468 //----------------------------------------------------------------------------
469 IMPLEMENTATION [ux && mp]:
473 Jdb::send_nmi(unsigned /*cpu*/)