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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/* $OpenBSD: dt_prov_syscall.c,v 1.4 2020/07/04 10:19:09 mpi Exp $ */
/*
* Copyright (c) 2019 Martin Pieuchot <mpi@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/atomic.h>
#include <sys/syscall.h>
#include <dev/dt/dtvar.h>
extern struct sysent sysent[];
/* Arrays of probes per syscall. */
struct dt_probe **dtps_entry;
struct dt_probe **dtps_return;
unsigned int dtps_nsysent = SYS_MAXSYSCALL;
/* Flags that make sense for this provider */
#define DTEVT_PROV_SYSCALL (DTEVT_COMMON|DTEVT_FUNCARGS)
int dt_prov_syscall_alloc(struct dt_probe *, struct dt_softc *,
struct dt_pcb_list *, struct dtioc_req *);
void dt_prov_syscall_entry(struct dt_provider *, ...);
void dt_prov_syscall_return(struct dt_provider *, ...);
struct dt_provider dt_prov_syscall = {
.dtpv_name = "syscall",
.dtpv_alloc = dt_prov_syscall_alloc,
.dtpv_enter = dt_prov_syscall_entry,
.dtpv_leave = dt_prov_syscall_return,
};
int
dt_prov_syscall_init(void)
{
struct dt_probe *dtp;
int i, len, nprobes = 0;
char *sysnb;
dtps_entry = mallocarray(dtps_nsysent, sizeof(dtp), M_DT,
M_NOWAIT|M_ZERO);
if (dtps_entry == NULL)
return 0;
dtps_return = mallocarray(dtps_nsysent, sizeof(dtp), M_DT,
M_NOWAIT|M_ZERO);
if (dtps_return == NULL) {
free(dtps_entry, M_DT, dtps_nsysent * sizeof(dtp));
return 0;
}
for (i = 0; i < dtps_nsysent; i++) {
if (sysent[i].sy_call == sys_nosys)
continue;
len = snprintf(NULL, 0, "sys%%%u", i);
sysnb = malloc(len + 1, M_DT, M_NOWAIT);
if (sysnb == NULL)
break;
snprintf(sysnb, len + 1, "sys%%%u", i);
dtp = dt_dev_alloc_probe(sysnb, "entry", &dt_prov_syscall);
if (dtp == NULL) {
free(sysnb, M_DT, len);
break;
}
dtp->dtp_nargs = sysent[i].sy_narg;
dtp->dtp_sysnum = i;
dtps_entry[i] = dtp;
dt_dev_register_probe(dtp);
nprobes++;
dtp = dt_dev_alloc_probe(sysnb, "return", &dt_prov_syscall);
if (dtp == NULL)
break;
dtp->dtp_sysnum = i;
dtps_return[i] = dtp;
dt_dev_register_probe(dtp);
nprobes++;
}
return nprobes;
}
int
dt_prov_syscall_alloc(struct dt_probe *dtp, struct dt_softc *sc,
struct dt_pcb_list *plist, struct dtioc_req *dtrq)
{
struct dt_pcb *dp;
KASSERT(dtioc_req_isvalid(dtrq));
KASSERT(TAILQ_EMPTY(plist));
KASSERT(dtp->dtp_prov == &dt_prov_syscall);
KASSERT((dtp->dtp_sysnum >= 0) && (dtp->dtp_sysnum < dtps_nsysent));
dp = dt_pcb_alloc(dtp, sc);
if (dp == NULL)
return ENOMEM;
dp->dp_filter = dtrq->dtrq_filter;
dp->dp_evtflags = dtrq->dtrq_evtflags & DTEVT_PROV_SYSCALL;
TAILQ_INSERT_HEAD(plist, dp, dp_snext);
return 0;
}
void
dt_prov_syscall_entry(struct dt_provider *dtpv, ...)
{
struct dt_probe *dtp;
struct dt_pcb *dp;
register_t sysnum;
size_t argsize;
register_t *args;
va_list ap;
KASSERT(dtpv == &dt_prov_syscall);
va_start(ap, dtpv);
sysnum = va_arg(ap, register_t);
argsize = va_arg(ap, size_t);
args = va_arg(ap, register_t*);
va_end(ap);
KASSERT((argsize / sizeof(register_t)) <= DTMAXSYSARGS);
if (sysnum < 0 || sysnum >= dtps_nsysent)
return;
dtp = dtps_entry[sysnum];
if (!dtp->dtp_recording)
return;
smr_read_enter();
SMR_SLIST_FOREACH(dp, &dtp->dtp_pcbs, dp_pnext) {
struct dt_evt *dtev;
dtev = dt_pcb_ring_get(dp, 0);
if (dtev == NULL)
continue;
if (ISSET(dp->dp_evtflags, DTEVT_FUNCARGS))
memcpy(dtev->dtev_sysargs, args, argsize);
dt_pcb_ring_consume(dp, dtev);
}
smr_read_leave();
}
void
dt_prov_syscall_return(struct dt_provider *dtpv, ...)
{
struct dt_probe *dtp;
struct dt_pcb *dp;
register_t sysnum;
int error;
register_t retval[2];
va_list ap;
KASSERT(dtpv == &dt_prov_syscall);
va_start(ap, dtpv);
sysnum = va_arg(ap, register_t);
error = va_arg(ap, int);
retval[0] = va_arg(ap, register_t);
retval[1] = va_arg(ap, register_t);
va_end(ap);
if (sysnum < 0 || sysnum >= dtps_nsysent)
return;
dtp = dtps_return[sysnum];
if (!dtp->dtp_recording)
return;
smr_read_enter();
SMR_SLIST_FOREACH(dp, &dtp->dtp_pcbs, dp_pnext) {
struct dt_evt *dtev;
dtev = dt_pcb_ring_get(dp, 0);
if (dtev == NULL)
continue;
if (error) {
dtev->dtev_sysretval[0] = -1;
dtev->dtev_sysretval[1] = 0;
dtev->dtev_syserror = error;
} else {
dtev->dtev_sysretval[0] = retval[0];
dtev->dtev_sysretval[1] = retval[1];
dtev->dtev_syserror = 0;
}
dt_pcb_ring_consume(dp, dtev);
}
smr_read_leave();
}
|