Print expanded source
This commit is contained in:
85
debugus.c
85
debugus.c
@@ -54,6 +54,51 @@ int msleep(long msec)
|
||||
#define INIT_SCRIPT ".debugusrc.js"
|
||||
#define MAX_JS_FUNCS 100
|
||||
|
||||
void print_source(const char *file, size_t line)
|
||||
{
|
||||
FILE *src = fopen(file, "r");
|
||||
if (src == NULL) {
|
||||
LOG_ERR("No source file found\n");
|
||||
return;
|
||||
}
|
||||
fseek(src, 0L, SEEK_END);
|
||||
long sz = ftell(src);
|
||||
rewind(src);
|
||||
|
||||
char *srcbuf = malloc(sz+1);
|
||||
fread(srcbuf, sz, 1, src);
|
||||
srcbuf[sz] = '\0';
|
||||
|
||||
const int expand = 4;
|
||||
|
||||
char *p = srcbuf;
|
||||
size_t i = 0;
|
||||
while (p) {
|
||||
char *next = strchr(p, '\n');
|
||||
if (next) {
|
||||
*next = '\0';
|
||||
}
|
||||
|
||||
int lo = line - expand;
|
||||
int hi = line + expand;
|
||||
if (i >= (lo < 0 ? 0 : lo) && i <= hi ) {
|
||||
char prefix[20];
|
||||
snprintf(prefix, sizeof(prefix), "%2s %6zu", i == line ? "->" : "", i);
|
||||
printf("%s %s\n", prefix, p);
|
||||
}
|
||||
|
||||
if (next) {
|
||||
*next = '\n';
|
||||
}
|
||||
|
||||
p = next ? (next + 1) : NULL;
|
||||
i++;
|
||||
}
|
||||
|
||||
free(srcbuf);
|
||||
fclose(src);
|
||||
}
|
||||
|
||||
// Registers
|
||||
// Taken from da goat https://source.winehq.org/source/dlls/dbghelp/cpu_x86_64.c
|
||||
|
||||
@@ -219,46 +264,6 @@ void dbg_handle_sigsegv(Dbg *dbg, siginfo_t info)
|
||||
}
|
||||
}
|
||||
|
||||
void print_source(const char *file, size_t line)
|
||||
{
|
||||
FILE *src = fopen(file, "r");
|
||||
if (src == NULL) {
|
||||
LOG_ERR("No source file found\n");
|
||||
return;
|
||||
}
|
||||
fseek(src, 0L, SEEK_END);
|
||||
long sz = ftell(src);
|
||||
rewind(src);
|
||||
|
||||
char *srcbuf = malloc(sz+1);
|
||||
fread(srcbuf, sz, 1, src);
|
||||
srcbuf[sz] = '\0';
|
||||
|
||||
char *p = srcbuf;
|
||||
size_t i = 0;
|
||||
while (p) {
|
||||
char *next = strchr(p, '\n');
|
||||
if (next) {
|
||||
*next = '\0';
|
||||
}
|
||||
|
||||
if (i == line) {
|
||||
LOG_INF("Source:\n");
|
||||
printf("%6zu %s\n", line, p);
|
||||
}
|
||||
|
||||
if (next) {
|
||||
*next = '\n';
|
||||
}
|
||||
|
||||
p = next ? (next + 1) : NULL;
|
||||
i++;
|
||||
}
|
||||
|
||||
free(srcbuf);
|
||||
fclose(src);
|
||||
}
|
||||
|
||||
void dbg_handle_sigtrap(Dbg *dbg, siginfo_t info)
|
||||
{
|
||||
void dbg_set_rip(Dbg *dbg, uint64_t v);
|
||||
|
||||
Reference in New Issue
Block a user