Skip to content

Commit

Permalink
Lab traps: backtrace
Browse files Browse the repository at this point in the history
  • Loading branch information
foyoodo committed Mar 21, 2021
1 parent 2455162 commit f70996c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions kernel/defs.h
Original file line number Diff line number Diff line change
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
15 changes: 15 additions & 0 deletions kernel/printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,18 @@ printfinit(void)
initlock(&pr.lock, "pr");
pr.locking = 1;
}

void
backtrace(void)
{
printf("backtrace:\n");

uint64 fp = r_fp();
uint64 bottom = PGROUNDUP(fp);

while (fp < bottom) {
uint64 ra = *(uint64*)(fp - 8);
printf("%p\n", ra);
fp = *(uint64*)(fp - 16);
}
}
8 changes: 8 additions & 0 deletions kernel/riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ r_ra()
return x;
}

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

// flush the TLB.
static inline void
sfence_vma()
Expand Down
3 changes: 3 additions & 0 deletions kernel/sysproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ sys_sleep(void)
sleep(&ticks, &tickslock);
}
release(&tickslock);

backtrace();

return 0;
}

Expand Down

0 comments on commit f70996c

Please sign in to comment.