Skip to content

Commit

Permalink
Add trace syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
cw00h committed Jan 9, 2023
1 parent 1e6e6ca commit ee2f0a2
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ UPROGS=\
$U/_grind\
$U/_wc\
$U/_zombie\
$U/_trace\



Expand Down
3 changes: 3 additions & 0 deletions kernel/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ fork(void)
// copy saved user registers.
*(np->trapframe) = *(p->trapframe);

// Copy tracemask
np->tracemask = p->tracemask;

// Cause fork to return 0 in the child.
np->trapframe->a0 = 0;

Expand Down
1 change: 1 addition & 0 deletions kernel/proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,5 @@ struct proc {
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
char name[16]; // Process name (debugging)
int tracemask; // Mask for syscall trace
};
30 changes: 30 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,33 @@ static uint64 (*syscalls[])(void) = {
[SYS_link] sys_link,
[SYS_mkdir] sys_mkdir,
[SYS_close] sys_close,
[SYS_trace] sys_trace,
};

char *syscall_names[] = {
"",
"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 +166,8 @@ syscall(void)
num = p->trapframe->a7;
if(num > 0 && num < NELEM(syscalls) && syscalls[num]) {
p->trapframe->a0 = syscalls[num]();
if(p->tracemask & (1 << (num)))
printf("%d: syscall %s -> %d\n", p->pid, syscall_names[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
9 changes: 9 additions & 0 deletions kernel/sysproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,12 @@ sys_uptime(void)
release(&tickslock);
return xticks;
}

uint64 sys_trace(void) {
int mask;

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

myproc()->tracemask = 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 ee2f0a2

Please sign in to comment.