#include <sys/param.h>
#include <sys/systm.h>
#include <machine/db_machdep.h>
#include <ddb/db_lex.h>
#include <ddb/db_output.h>
#include <ddb/db_command.h>
#include <ddb/db_sym.h>
#include <ddb/db_access.h>
#include <ddb/db_extern.h>
#include <ddb/db_interface.h>
char db_examine_format[TOK_STRING_SIZE] = "x";
void db_examine(vaddr_t, char *, int);
void db_search(vaddr_t, int, db_expr_t, db_expr_t, db_expr_t);
void
db_examine_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
{
if (modif[0] != '\0')
db_strlcpy(db_examine_format, modif, sizeof(db_examine_format));
if (count == -1)
count = 1;
db_examine((vaddr_t)addr, db_examine_format, count);
}
void
db_examine(vaddr_t addr, char *fmt, int count)
{
int i, c;
db_expr_t value;
int size;
int width;
int bytes;
char * fp;
vaddr_t incr;
int dis;
char tmpfmt[28];
while (--count >= 0) {
fp = fmt;
size = 4;
width = 12;
incr = 0;
dis = 0;
while ((c = *fp++) != 0) {
if (db_print_position() == 0) {
db_printsym(addr, DB_STGY_ANY, db_printf);
db_printf(":\t");
db_prev = addr;
}
incr = size;
switch (c) {
case 'b':
size = 1;
width = 4;
break;
case 'h':
size = 2;
width = 8;
break;
case 'l':
size = 4;
width = 12;
break;
#ifdef __LP64__
case 'q':
size = 8;
width = 20;
break;
#endif
case 'a':
db_printf("= 0x%lx\n", (long)addr);
incr = 0;
break;
case 'r':
value = db_get_value(addr, size, 1);
db_format(tmpfmt, sizeof tmpfmt,
(long)value, DB_FORMAT_R, 0, width);
db_printf("%-*s", width, tmpfmt);
break;
case 'x':
value = db_get_value(addr, size, 0);
db_printf("%*lx", width, (long)value);
break;
case 'm':
incr = 0;
bytes = 0;
do {
for (i = 0; i < size; i++) {
value =
db_get_value(addr+bytes, 1,
0);
db_printf("%02lx",
(long)value);
bytes++;
if (!(bytes % 4))
db_printf(" ");
}
} while ((bytes != 16) && count--);
db_printf("%-*s",
(16-bytes)*2 + (4 - bytes/4) + 1, " ");
while (bytes--) {
value = db_get_value(addr + incr, 1, 0);
incr++;
if (value >= ' ' && value <= '~')
db_printf("%c", (int)value);
else
db_printf(".");
}
db_printf("\n");
break;
case 'z':
value = db_get_value(addr, size, 1);
db_format(tmpfmt, sizeof tmpfmt,
(long)value, DB_FORMAT_Z, 0, width);
db_printf("%-*s", width, tmpfmt);
break;
case 'd':
value = db_get_value(addr, size, 1);
db_printf("%-*ld", width, (long)value);
break;
case 'u':
value = db_get_value(addr, size, 0);
db_printf("%-*lu", width, (long)value);
break;
case 'o':
value = db_get_value(addr, size, 0);
db_printf("%-*lo", width, value);
break;
case 'c':
value = db_get_value(addr, 1, 0);
incr = 1;
if (value >= ' ' && value <= '~')
db_printf("%c", (int)value);
else
db_printf("\\%03o", (int)value);
break;
case 's':
incr = 0;
for (;;) {
value = db_get_value(addr + incr, 1,
0);
incr++;
if (value == 0)
break;
if (value >= ' ' && value <= '~')
db_printf("%c", (int)value);
else
db_printf("\\%03o", (int)value);
}
break;
case 'i':
case 'I':
dis = c;
break;
default:
incr = 0;
break;
}
}
switch (dis) {
case 'i':
addr = db_disasm(addr, 0);
break;
case 'I':
addr = db_disasm(addr, 1);
break;
default:
addr += incr;
break;
}
if (db_print_position() != 0)
db_printf("\n");
}
db_next = addr;
}
char db_print_format = 'x';
void
db_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
{
db_expr_t value;
char tmpfmt[28];
if (modif[0] != '\0')
db_print_format = modif[0];
switch (db_print_format) {
case 'a':
db_printsym((vaddr_t)addr, DB_STGY_ANY, db_printf);
break;
case 'r':
db_printf("%s", db_format(tmpfmt, sizeof tmpfmt, addr,
DB_FORMAT_R, 0, sizeof(db_expr_t) * 2 * 6 / 5));
break;
case 'x':
db_printf("%*lx", (uint)sizeof(db_expr_t) * 2, addr);
break;
case 'z':
db_printf("%s", db_format(tmpfmt, sizeof tmpfmt, addr,
DB_FORMAT_Z, 0, sizeof(db_expr_t) * 2));
break;
case 'd':
db_printf("%*ld", (uint)sizeof(db_expr_t) * 2 * 6 / 5, addr);
break;
case 'u':
db_printf("%*lu", (uint)sizeof(db_expr_t) * 2 * 6 / 5, addr);
break;
case 'o':
db_printf("%*lo", (uint)sizeof(db_expr_t) * 2 * 4 / 3, addr);
break;
case 'c':
value = addr & 0xFF;
if (value >= ' ' && value <= '~')
db_printf("%c", (int)value);
else
db_printf("\\%03o", (int)value);
break;
}
db_printf("\n");
}
void
db_print_loc_and_inst(vaddr_t loc)
{
db_printsym(loc, DB_STGY_PROC, db_printf);
if (loc != 0) {
db_printf(":\t");
db_disasm(loc, 0);
}
}
size_t
db_strlcpy(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
size_t n = siz;
if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0)
break;
} while (--n != 0);
}
if (n == 0) {
if (siz != 0)
*d = '\0';
while (*s++)
continue;
}
return(s - src - 1);
}
void
db_search_cmd(db_expr_t daddr, int have_addr, db_expr_t dcount, char *modif)
{
int t;
vaddr_t addr;
int size;
db_expr_t value;
db_expr_t mask;
db_expr_t count;
t = db_read_token();
if (t == tSLASH) {
t = db_read_token();
if (t != tIDENT) {
bad_modifier:
db_printf("Bad modifier\n");
db_flush_lex();
return;
}
if (!strcmp(db_tok_string, "b"))
size = 1;
else if (!strcmp(db_tok_string, "h"))
size = 2;
else if (!strcmp(db_tok_string, "l"))
size = 4;
else
goto bad_modifier;
} else {
db_unread_token(t);
size = 4;
}
if (!db_expression(&value)) {
db_printf("Address missing\n");
db_flush_lex();
return;
}
addr = (vaddr_t) value;
if (!db_expression(&value)) {
db_printf("Value missing\n");
db_flush_lex();
return;
}
if (!db_expression(&mask))
mask = (int) ~0;
t = db_read_token();
if (t == tCOMMA) {
if (!db_expression(&count)) {
db_printf("Count missing\n");
db_flush_lex();
return;
}
} else {
db_unread_token(t);
count = -1;
}
db_skip_to_eol();
db_search(addr, size, value, mask, count);
}
void
db_search(vaddr_t addr, int size, db_expr_t value, db_expr_t mask,
db_expr_t count)
{
while (count < 0 || count-- != 0) {
db_prev = addr;
if ((db_get_value(addr, size, 0) & mask) == value)
break;
addr += size;
}
db_next = addr;
}