Trace syscall complete, kernel interface and userspace demo program

This commit is contained in:
Imbus 2025-05-09 08:47:44 +02:00
parent 641ebb302a
commit ca76d0772b
7 changed files with 33 additions and 1 deletions

View file

@ -138,6 +138,7 @@ UPROGS=\
$U/_clear\
$U/_halt\
$U/_reset\
$U/_traced\
fs.img: mkfs/mkfs README.md $(UPROGS)
mkfs/mkfs fs.img README.md $(UPROGS)

View file

@ -127,6 +127,7 @@ exec(char *path, char **argv)
p->sz = sz;
p->trapframe->epc = elf.entry; // initial program counter = main
p->trapframe->sp = sp; // initial stack pointer
p->mask = 0;
proc_freepagetable(oldpagetable, oldsz);
return argc; // this ends up in a0, the first argument to main(argc, argv)

View file

@ -112,6 +112,7 @@ allocproc(void)
struct proc *p;
for(p = proc; p < &proc[NPROC]; p++) {
p->mask = 0;
acquire(&p->lock);
if(p->state == UNUSED) {
goto found;
@ -304,6 +305,7 @@ fork(void)
safestrcpy(np->name, p->name, sizeof(p->name));
pid = np->pid;
np->mask = 0;
release(&np->lock);

View file

@ -124,6 +124,10 @@ syscall(void)
num = p->trapframe->a7;
if(num > 0 && num < NELEM(syscalls) && syscalls[num]) {
if(p->mask & (1 << num)) {
printf("%d %s: Trace: syscall #%d\n", p->pid, p->name, num);
}
// Use num to lookup the system call function for num, call it,
// and store its return value in p->trapframe->a0
p->trapframe->a0 = syscalls[num]();

View file

@ -93,7 +93,10 @@ sys_uptime(void)
u64
sys_trace(void)
{
/* TODO: Implement sys_trace */
int mask;
argint(0, &mask);
myproc()->mask = mask;
return 0;
}

18
user/traced.c Normal file
View file

@ -0,0 +1,18 @@
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#include "kernel/syscall.h"
int
main(int argc, char *argv[])
{
trace(1 << SYS_open);
int fd;
if((fd = open(argv[1], 0)) < 0) {
fprintf(stderr, "Traced: cannot open %s\n", argv[1]);
exit(1);
}
exit(0);
}

View file

@ -2,6 +2,9 @@
#include "../kernel/types.h"
#define stderr 2
#define stdout 1
struct stat;
/**