forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysproc.c
192 lines (153 loc) · 3.74 KB
/
sysproc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "types.h"
#include "x86.h"
#include "defs.h"
#include "date.h"
#include "param.h"
#include "memlayout.h"
#include "mmu.h"
#include "proc.h"
int
sys_fork(void)
{
return fork();
}
int
sys_exit(void)
{
exit();
return 0; // not reached
}
int
sys_wait(void)
{
return wait();
}
int
sys_kill(void)
{
int pid;
if(argint(0, &pid) < 0)
return -1;
return kill(pid);
}
int
sys_getpid(void)
{
return myproc()->pid;
}
int
sys_sbrk(void)
{
int addr;
int n;
if(argint(0, &n) < 0)
return -1;
addr = myproc()->sz;
if(growproc(n) < 0)
return -1;
return addr;
}
int
sys_sleep(void)
{
int n;
uint ticks0;
if(argint(0, &n) < 0)
return -1;
acquire(&tickslock);
ticks0 = ticks;
while(ticks - ticks0 < n){
if(myproc()->killed){
release(&tickslock);
return -1;
}
sleep(&ticks, &tickslock);
}
release(&tickslock);
return 0;
}
// return how many clock tick interrupts have occurred
// since start.
int
sys_uptime(void)
{
uint xticks;
acquire(&tickslock);
xticks = ticks;
release(&tickslock);
return xticks;
}
int sys_strace(void) {
int enable;
if (argint(0, &enable) < 0) {
return -1;
}
struct proc *p = myproc();
// Immediately disable strace if enable == 0
if (enable == 0) {
p->strace_enabled = 0;
} else {
p->strace_enabled = 1;
}
return 0;
}
// Helper: Convert an integer to a string
int itoa(int n, char *s) {
int i = 0, sign = n;
if (sign < 0) n = -n; // Handle negative numbers
// Generate digits in reverse order
do {
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
if (sign < 0) s[i++] = '-'; // Add negative sign
s[i] = '\0';
// Reverse the string
for (int j = 0, k = i - 1; j < k; j++, k--) {
char temp = s[j];
s[j] = s[k];
s[k] = temp;
}
return i; // Return the length of the string
}
int sys_strace_dump(void) {
struct proc *p = myproc(); // Get the current process
struct file *f = p->ofile[1]; // Get the file associated with stdout
if (!f) {
return -1; // No valid stdout file descriptor
}
// Determine how many events to display
int num_events = strace_count;
// Calculate the starting index for the oldest event
int start_idx = (strace_index - num_events + STRACE_BUFFER_SIZE) % STRACE_BUFFER_SIZE;
for (int i = 0; i < num_events; i++) {
int idx = (start_idx + i) % STRACE_BUFFER_SIZE; // Circular buffer logic
struct trace_event *event = &strace_buffer[idx];
// Ensure the event is valid before printing
if (event->pid == 0 && event->name[0] == '\0' && event->syscall[0] == '\0') {
continue; // Skip uninitialized entries
}
// Build the trace event string manually
char buf[128];
int len = 0;
safestrcpy(buf + len, "TRACE EVENT: pid = ", sizeof(buf) - len);
len += strlen(buf + len); // Update len after copying
len += itoa(event->pid, buf + len);
safestrcpy(buf + len, " | command_name = ", sizeof(buf) - len);
len += strlen(buf + len);
safestrcpy(buf + len, event->name, sizeof(buf) - len);
len += strlen(buf + len);
safestrcpy(buf + len, " | syscall = ", sizeof(buf) - len);
len += strlen(buf + len);
safestrcpy(buf + len, event->syscall, sizeof(buf) - len);
len += strlen(buf + len);
safestrcpy(buf + len, " | return_value = ", sizeof(buf) - len);
len += strlen(buf + len);
len += itoa(event->return_value, buf + len);
buf[len++] = '\n';
// Write to stdout (which may be redirected)
if (filewrite(f, buf, len) != len) {
return -1; // Error writing to file
}
}
return 0;
}