Skip to content

Commit

Permalink
finished lab Backtrace
Browse files Browse the repository at this point in the history
  • Loading branch information
Nowherechan committed Mar 17, 2023
1 parent baa20a4 commit be1a4f8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .vscode/settings.json
@@ -0,0 +1,5 @@
{
"files.associations": {
"proc.h": "c"
}
}
1 change: 1 addition & 0 deletions kernel/defs.h
Expand Up @@ -80,6 +80,7 @@ int pipewrite(struct pipe*, uint64, int);
void printf(char*, ...);
void panic(char*) __attribute__((noreturn));
void printfinit(void);
void backtrace(void);

// proc.c
int cpuid(void);
Expand Down
13 changes: 13 additions & 0 deletions kernel/printf.c
Expand Up @@ -133,3 +133,16 @@ printfinit(void)
initlock(&pr.lock, "pr");
pr.locking = 1;
}

void
backtrace(void) // For lab-4 Backtrace
{
uint64 fp /* frame pointer */ = r_fp(),
ba /* base addr */ = PGROUNDUP(fp);

printf("backtrace:\n");
while (fp < ba) {
printf("%p\n", *(uint64*)(fp-8));
fp = *(uint64*)(fp-16);
}
}
8 changes: 8 additions & 0 deletions kernel/riscv.h
Expand Up @@ -287,6 +287,14 @@ intr_get()
return (x & SSTATUS_SIE) != 0;
}

static inline uint64
r_fp()
{
uint64 x;
asm volatile("mv %0, s0" : "=r" (x) );
return x;
}

static inline uint64
r_sp()
{
Expand Down
2 changes: 1 addition & 1 deletion kernel/sysproc.c
Expand Up @@ -53,7 +53,7 @@ sys_sleep(void)
{
int n;
uint ticks0;

backtrace(); // For lab-4 Backtrace
argint(0, &n);
if(n < 0)
n = 0;
Expand Down

0 comments on commit be1a4f8

Please sign in to comment.