2002-07-09 16:24:59 +04:00
|
|
|
/* This file contains the debugger */
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
|
|
|
|
** Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
2002-08-14 01:42:53 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <kernel.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <arch/int.h>
|
|
|
|
#include <smp.h>
|
|
|
|
#include <console.h>
|
|
|
|
#include <memheap.h>
|
|
|
|
#include <gdb.h>
|
|
|
|
#include <Errors.h>
|
|
|
|
#include <int.h>
|
|
|
|
|
|
|
|
#include <arch/dbg_console.h>
|
|
|
|
#include <arch/debug.h>
|
|
|
|
#include <arch/cpu.h>
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
|
|
|
|
int dbg_register_file[2][14]; /* XXXmpetit -- must be made generic */
|
|
|
|
|
|
|
|
|
|
|
|
static bool serial_debug_on = false;
|
|
|
|
static spinlock_t dbg_spinlock = 0;
|
|
|
|
static int debugger_on_cpu = -1;
|
|
|
|
|
|
|
|
struct debugger_command
|
|
|
|
{
|
|
|
|
struct debugger_command *next;
|
2002-07-18 02:07:37 +04:00
|
|
|
int (*func)(int, char **);
|
|
|
|
const char *name;
|
2002-07-09 16:24:59 +04:00
|
|
|
const char *description;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct debugger_command *commands;
|
|
|
|
|
|
|
|
#define LINE_BUF_SIZE 1024
|
|
|
|
#define MAX_ARGS 16
|
|
|
|
#define HISTORY_SIZE 16
|
|
|
|
|
|
|
|
static char line_buf[HISTORY_SIZE][LINE_BUF_SIZE] = { "", };
|
|
|
|
static char parse_line[LINE_BUF_SIZE] = "";
|
|
|
|
static int cur_line = 0;
|
|
|
|
static char *args[MAX_ARGS] = { NULL, };
|
|
|
|
|
|
|
|
#define distance(a, b) ((a) < (b) ? (b) - (a) : (a) - (b))
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
debug_read_line(char *buf, int max_len)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
int ptr = 0;
|
|
|
|
bool done = false;
|
|
|
|
int cur_history_spot = cur_line;
|
|
|
|
|
2002-08-14 04:04:59 +04:00
|
|
|
while (!done) {
|
2002-07-09 16:24:59 +04:00
|
|
|
c = arch_dbg_con_read();
|
2002-08-14 04:04:59 +04:00
|
|
|
switch (c) {
|
2002-07-09 16:24:59 +04:00
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
buf[ptr++] = '\0';
|
|
|
|
dbg_puts("\n");
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
case 8: // backspace
|
2002-08-14 04:04:59 +04:00
|
|
|
if (ptr > 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dbg_puts("\x1b[1D"); // move to the left one
|
|
|
|
dbg_putch(' ');
|
|
|
|
dbg_puts("\x1b[1D"); // move to the left one
|
|
|
|
ptr--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 27: // escape sequence
|
|
|
|
c = arch_dbg_con_read(); // should be '['
|
|
|
|
c = arch_dbg_con_read();
|
2002-08-14 04:04:59 +04:00
|
|
|
switch (c) {
|
2002-07-09 16:24:59 +04:00
|
|
|
case 67: // right arrow acts like space
|
|
|
|
buf[ptr++] = ' ';
|
|
|
|
dbg_putch(' ');
|
|
|
|
break;
|
|
|
|
case 68: // left arrow acts like backspace
|
2002-08-14 04:04:59 +04:00
|
|
|
if (ptr > 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dbg_puts("\x1b[1D"); // move to the left one
|
|
|
|
dbg_putch(' ');
|
|
|
|
dbg_puts("\x1b[1D"); // move to the left one
|
|
|
|
ptr--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 65: // up arrow
|
|
|
|
case 66: // down arrow
|
|
|
|
{
|
|
|
|
int history_line = 0;
|
|
|
|
|
|
|
|
// dprintf("1c %d h %d ch %d\n", cur_line, history_line, cur_history_spot);
|
|
|
|
|
2002-08-14 04:04:59 +04:00
|
|
|
if (c == 65) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// up arrow
|
|
|
|
history_line = cur_history_spot - 1;
|
2002-08-14 04:04:59 +04:00
|
|
|
if (history_line < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
history_line = HISTORY_SIZE - 1;
|
|
|
|
} else {
|
|
|
|
// down arrow
|
2002-08-14 04:04:59 +04:00
|
|
|
if (cur_history_spot != cur_line) {
|
2002-07-09 16:24:59 +04:00
|
|
|
history_line = cur_history_spot + 1;
|
|
|
|
if(history_line >= HISTORY_SIZE)
|
|
|
|
history_line = 0;
|
2002-08-14 04:04:59 +04:00
|
|
|
} else
|
2002-07-09 16:24:59 +04:00
|
|
|
break; // nothing to do here
|
|
|
|
}
|
|
|
|
|
|
|
|
// dprintf("2c %d h %d ch %d\n", cur_line, history_line, cur_history_spot);
|
|
|
|
|
|
|
|
// swap the current line with something from the history
|
|
|
|
if(ptr > 0)
|
|
|
|
dprintf("\x1b[%dD", ptr); // move to beginning of line
|
|
|
|
|
|
|
|
strcpy(buf, line_buf[history_line]);
|
|
|
|
ptr = strlen(buf);
|
|
|
|
dprintf("%s\x1b[K", buf); // print the line and clear the rest
|
|
|
|
cur_history_spot = history_line;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '$':
|
|
|
|
case '+':
|
|
|
|
/* HACK ALERT!!!
|
|
|
|
*
|
|
|
|
* If we get a $ at the beginning of the line
|
|
|
|
* we assume we are talking with GDB
|
|
|
|
*/
|
2002-08-14 04:04:59 +04:00
|
|
|
if (ptr == 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
strcpy(buf, "gdb");
|
|
|
|
ptr= 4;
|
|
|
|
done= true;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* fall thru */
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
buf[ptr++] = c;
|
|
|
|
dbg_putch(c);
|
|
|
|
}
|
2002-08-14 04:04:59 +04:00
|
|
|
if (ptr >= max_len - 2) {
|
2002-07-09 16:24:59 +04:00
|
|
|
buf[ptr++] = '\0';
|
|
|
|
dbg_puts("\n");
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
debug_parse_line(char *buf, char **argv, int *argc, int max_args)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int pos = 0;
|
|
|
|
|
|
|
|
strcpy(parse_line, buf);
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
if (!isspace(parse_line[0])) {
|
2002-07-09 16:24:59 +04:00
|
|
|
argv[0] = parse_line;
|
|
|
|
*argc = 1;
|
2002-08-14 04:04:59 +04:00
|
|
|
} else
|
2002-07-09 16:24:59 +04:00
|
|
|
*argc = 0;
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
while (parse_line[pos] != '\0') {
|
|
|
|
if (isspace(parse_line[pos])) {
|
2002-07-09 16:24:59 +04:00
|
|
|
parse_line[pos] = '\0';
|
|
|
|
// scan all of the whitespace out of this
|
2002-08-14 04:04:59 +04:00
|
|
|
while (isspace(parse_line[++pos]))
|
2002-07-09 16:24:59 +04:00
|
|
|
;
|
2002-08-14 04:04:59 +04:00
|
|
|
if (parse_line[pos] == '\0')
|
2002-07-09 16:24:59 +04:00
|
|
|
break;
|
|
|
|
argv[*argc] = &parse_line[pos];
|
|
|
|
(*argc)++;
|
|
|
|
|
2002-08-14 04:04:59 +04:00
|
|
|
if (*argc >= max_args - 1)
|
2002-07-09 16:24:59 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *argc;
|
|
|
|
}
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
kernel_debugger_loop()
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int argc;
|
|
|
|
struct debugger_command *cmd;
|
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
disable_interrupts();
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
dprintf("kernel debugger on cpu %d\n", smp_get_current_cpu());
|
|
|
|
debugger_on_cpu = smp_get_current_cpu();
|
|
|
|
|
|
|
|
for(;;) {
|
2002-07-18 02:07:37 +04:00
|
|
|
dprintf("kdebug> ");
|
2002-07-09 16:24:59 +04:00
|
|
|
debug_read_line(line_buf[cur_line], LINE_BUF_SIZE);
|
|
|
|
debug_parse_line(line_buf[cur_line], args, &argc, MAX_ARGS);
|
2002-08-14 04:04:59 +04:00
|
|
|
if (argc <= 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
debugger_on_cpu = smp_get_current_cpu();
|
|
|
|
|
|
|
|
cmd = commands;
|
2002-08-14 04:04:59 +04:00
|
|
|
while (cmd != NULL) {
|
|
|
|
if (strcmp(args[0], cmd->name) == 0) {
|
2002-07-09 16:24:59 +04:00
|
|
|
cmd->func(argc, args);
|
2002-07-18 02:07:37 +04:00
|
|
|
break;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
cmd = cmd->next;
|
|
|
|
}
|
2002-08-14 04:04:59 +04:00
|
|
|
if (cmd == NULL)
|
|
|
|
dprintf("unknown command, enter \"help\" to get a list of all supported commands\n");
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
cur_line++;
|
2002-08-14 04:04:59 +04:00
|
|
|
if (cur_line >= HISTORY_SIZE)
|
2002-07-09 16:24:59 +04:00
|
|
|
cur_line = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
kernel_debugger(const char * message)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
dbg_save_registers(&(dbg_register_file[smp_get_current_cpu()][0]));
|
|
|
|
|
2002-07-18 02:07:37 +04:00
|
|
|
if (message) {
|
|
|
|
dprintf(message);
|
|
|
|
dprintf("\n");
|
|
|
|
};
|
2002-10-05 23:45:31 +04:00
|
|
|
|
2002-07-18 02:07:37 +04:00
|
|
|
dprintf("Welcome to Kernel Debugging Land...\n");
|
2002-07-09 16:24:59 +04:00
|
|
|
kernel_debugger_loop();
|
|
|
|
}
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
panic(const char *fmt, ...)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
va_list args;
|
|
|
|
char temp[128];
|
|
|
|
int state;
|
|
|
|
|
|
|
|
dbg_set_serial_debug(true);
|
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
state = disable_interrupts();
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
ret = vsprintf(temp, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
dprintf("PANIC%d: %s", smp_get_current_cpu(), temp);
|
|
|
|
|
2002-08-14 04:04:59 +04:00
|
|
|
if (debugger_on_cpu != smp_get_current_cpu()) {
|
2002-07-09 16:24:59 +04:00
|
|
|
// halt all of the other cpus
|
|
|
|
|
|
|
|
// XXX need to flush current smp mailbox to make sure this goes
|
|
|
|
// through. Otherwise it'll hang
|
|
|
|
smp_send_broadcast_ici(SMP_MSG_CPU_HALT, 0, 0, 0, NULL, SMP_MSG_FLAG_SYNC);
|
|
|
|
}
|
|
|
|
|
2002-07-18 02:07:37 +04:00
|
|
|
kernel_debugger(NULL);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(state);
|
2002-07-09 16:24:59 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
dprintf(const char *fmt, ...)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char temp[512];
|
|
|
|
int ret = 0;
|
|
|
|
|
2002-08-14 04:04:59 +04:00
|
|
|
if (serial_debug_on) {
|
2002-07-09 16:24:59 +04:00
|
|
|
va_start(args, fmt);
|
|
|
|
ret = vsprintf(temp, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
dbg_puts(temp);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
char
|
|
|
|
dbg_putch(char c)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
char ret;
|
2002-07-25 05:05:51 +04:00
|
|
|
int flags = disable_interrupts();
|
2002-07-09 16:24:59 +04:00
|
|
|
acquire_spinlock(&dbg_spinlock);
|
|
|
|
|
2002-08-14 04:04:59 +04:00
|
|
|
if (serial_debug_on)
|
2002-07-09 16:24:59 +04:00
|
|
|
ret = arch_dbg_con_putch(c);
|
|
|
|
else
|
|
|
|
ret = c;
|
|
|
|
|
|
|
|
release_spinlock(&dbg_spinlock);
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(flags);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
dbg_puts(const char *s)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-07-25 05:05:51 +04:00
|
|
|
int flags = disable_interrupts();
|
2002-07-09 16:24:59 +04:00
|
|
|
acquire_spinlock(&dbg_spinlock);
|
|
|
|
|
2002-08-14 04:04:59 +04:00
|
|
|
if (serial_debug_on)
|
2002-07-09 16:24:59 +04:00
|
|
|
arch_dbg_con_puts(s);
|
|
|
|
|
|
|
|
release_spinlock(&dbg_spinlock);
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(flags);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
int
|
2002-08-14 04:04:59 +04:00
|
|
|
add_debugger_command(const char *name, int (*func)(int, char **), const char *desc)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
struct debugger_command *cmd;
|
|
|
|
|
2002-08-14 04:04:59 +04:00
|
|
|
cmd = (struct debugger_command *)kmalloc(sizeof(struct debugger_command));
|
2002-07-18 02:07:37 +04:00
|
|
|
if (cmd == NULL)
|
2002-07-09 16:24:59 +04:00
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
cmd->func = func;
|
2002-07-18 02:07:37 +04:00
|
|
|
cmd->name = name;
|
2002-07-09 16:24:59 +04:00
|
|
|
cmd->description = desc;
|
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
flags = disable_interrupts();
|
2002-07-09 16:24:59 +04:00
|
|
|
acquire_spinlock(&dbg_spinlock);
|
|
|
|
|
|
|
|
cmd->next = commands;
|
|
|
|
commands = cmd;
|
|
|
|
|
|
|
|
release_spinlock(&dbg_spinlock);
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(flags);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
return B_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
remove_debugger_command(const char * name, int (*func)(int, char **))
|
2002-07-18 02:07:37 +04:00
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
struct debugger_command *cmd;
|
|
|
|
struct debugger_command *prev;
|
|
|
|
|
2002-07-25 05:05:51 +04:00
|
|
|
flags = disable_interrupts();
|
2002-07-18 02:07:37 +04:00
|
|
|
acquire_spinlock(&dbg_spinlock);
|
|
|
|
|
|
|
|
prev = NULL;
|
|
|
|
cmd = commands;
|
|
|
|
while (cmd) {
|
|
|
|
if (strcmp(cmd->name, name) == 0 &&
|
|
|
|
cmd->func == func)
|
|
|
|
break;
|
|
|
|
prev = cmd;
|
|
|
|
cmd = cmd->next;
|
2002-08-14 04:04:59 +04:00
|
|
|
}
|
2002-07-18 02:07:37 +04:00
|
|
|
|
|
|
|
if (cmd) {
|
|
|
|
if (cmd == commands)
|
|
|
|
commands = cmd->next;
|
|
|
|
else
|
|
|
|
prev->next = cmd->next;
|
2002-08-14 04:04:59 +04:00
|
|
|
}
|
2002-07-18 02:07:37 +04:00
|
|
|
|
|
|
|
release_spinlock(&dbg_spinlock);
|
2002-07-25 05:05:51 +04:00
|
|
|
restore_interrupts(flags);
|
2002-07-18 02:07:37 +04:00
|
|
|
|
|
|
|
if (cmd) {
|
|
|
|
kfree(cmd);
|
|
|
|
return B_NO_ERROR;
|
2002-08-14 04:04:59 +04:00
|
|
|
}
|
2002-07-18 02:07:37 +04:00
|
|
|
|
|
|
|
return B_NAME_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
static int
|
|
|
|
cmd_reboot(int argc, char **argv)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
reboot();
|
2002-07-18 02:07:37 +04:00
|
|
|
return 0; // I'll be really suprised if this line ever run! ;-)
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
cmd_help(int argc, char **argv)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct debugger_command *cmd;
|
|
|
|
|
|
|
|
dprintf("debugger commands:\n");
|
|
|
|
cmd = commands;
|
2002-08-14 04:04:59 +04:00
|
|
|
while (cmd != NULL) {
|
2002-07-18 02:07:37 +04:00
|
|
|
dprintf(" %-32s\t\t%s\n", cmd->name, cmd->description);
|
2002-07-09 16:24:59 +04:00
|
|
|
cmd = cmd->next;
|
|
|
|
}
|
2002-08-14 04:04:59 +04:00
|
|
|
|
2002-07-18 02:07:37 +04:00
|
|
|
return 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
dbg_init(kernel_args *ka)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
commands = NULL;
|
|
|
|
|
|
|
|
return arch_dbg_con_init(ka);
|
|
|
|
}
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
dbg_init2(kernel_args *ka)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-07-18 02:07:37 +04:00
|
|
|
add_debugger_command("help", &cmd_help, "List all debugger commands");
|
|
|
|
add_debugger_command("reboot", &cmd_reboot, "Reboot");
|
|
|
|
add_debugger_command("gdb", &cmd_gdb, "Connect to remote gdb");
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-08-14 04:04:59 +04:00
|
|
|
return arch_dbg_init(ka);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
bool
|
|
|
|
dbg_set_serial_debug(bool new_val)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int temp = serial_debug_on;
|
|
|
|
serial_debug_on = new_val;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2002-08-14 01:42:53 +04:00
|
|
|
|
|
|
|
bool
|
|
|
|
dbg_get_serial_debug()
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
return serial_debug_on;
|
|
|
|
}
|
|
|
|
|