Skip to content

Commit

Permalink
Lab syscall: tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
foyoodo committed Mar 11, 2021
1 parent 8813049 commit 9b48439
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ UPROGS=\
$U/_grind\
$U/_wc\
$U/_zombie\
$U/_trace\



Expand Down
1 change: 1 addition & 0 deletions kernel/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ fork(void)
return -1;
}
np->sz = p->sz;
np->mask = p->mask;

np->parent = p;

Expand Down
1 change: 1 addition & 0 deletions kernel/proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ struct proc {
int killed; // If non-zero, have been killed
int xstate; // Exit status to be returned to parent's wait
int pid; // Process ID
int mask; // Tracing mask

// these are private to the process, so p->lock need not be held.
uint64 kstack; // Virtual address of kernel stack
Expand Down
11 changes: 11 additions & 0 deletions kernel/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ extern uint64 sys_unlink(void);
extern uint64 sys_wait(void);
extern uint64 sys_write(void);
extern uint64 sys_uptime(void);
extern uint64 sys_trace(void);

static uint64 (*syscalls[])(void) = {
[SYS_fork] sys_fork,
Expand All @@ -127,6 +128,13 @@ static uint64 (*syscalls[])(void) = {
[SYS_link] sys_link,
[SYS_mkdir] sys_mkdir,
[SYS_close] sys_close,
[SYS_trace] sys_trace,
};

static char *syscall_list[23] = {
"none", "fork", "exit", "wait", "pipe", "read", "kill", "exec",
"fstat", "chdir", "dup", "getpid", "sbrk", "sleep", "uptime", "open",
"write", "mknod", "unlink", "link", "mkdir", "close", "trace"
};

void
Expand All @@ -138,6 +146,9 @@ syscall(void)
num = p->trapframe->a7;
if(num > 0 && num < NELEM(syscalls) && syscalls[num]) {
p->trapframe->a0 = syscalls[num]();
if (p->mask & (1 << num))
printf("%d: syscall %s -> %d\n", p->pid, syscall_list[num],
p->trapframe->a0);
} else {
printf("%d %s: unknown sys call %d\n",
p->pid, p->name, num);
Expand Down
1 change: 1 addition & 0 deletions kernel/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
#define SYS_link 19
#define SYS_mkdir 20
#define SYS_close 21
#define SYS_trace 22
13 changes: 13 additions & 0 deletions kernel/sysproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,16 @@ sys_uptime(void)
release(&tickslock);
return xticks;
}

uint64
sys_trace(void)
{
int mask;

if (argint(0, &mask) < 0)
return -1;

myproc()->mask = mask;

return 0;
}
1 change: 1 addition & 0 deletions user/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ int getpid(void);
char* sbrk(int);
int sleep(int);
int uptime(void);
int trace(int);

// ulib.c
int stat(const char*, struct stat*);
Expand Down
1 change: 1 addition & 0 deletions user/usys.pl
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ sub entry {
entry("sbrk");
entry("sleep");
entry("uptime");
entry("trace");

0 comments on commit 9b48439

Please sign in to comment.