-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy patharg-decoder.c
More file actions
237 lines (195 loc) · 5.96 KB
/
arg-decoder.c
File metadata and controls
237 lines (195 loc) · 5.96 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Routines to take a syscallrecord and turn it into an ascii representation.
*/
#include <stdarg.h>
#include <stdio.h>
#include "arch.h" //PAGE_MASK
#include "arg-decoder.h"
#include "params.h" // verbosity
#include "pids.h"
#include "sanitise.h" // get_argval
#include "shm.h"
#include "syscall.h"
#include "tables.h"
#include "utils.h"
/*
* Bounded sprintf for the sptr/end rendering pattern.
* Advances sptr by the number of characters written, clamped to
* available space so we never write past end.
*/
static char * bprintf(char *sptr, char *end, const char *fmt, ...)
__attribute__((format(printf, 3, 4)));
static char * bprintf(char *sptr, char *end, const char *fmt, ...)
{
va_list ap;
int n;
if (sptr >= end)
return sptr;
va_start(ap, fmt);
n = vsnprintf(sptr, end - sptr, fmt, ap);
va_end(ap);
if (n > 0)
sptr += (n < end - sptr) ? n : end - sptr - 1;
return sptr;
}
static char * decode_argtype(char *sptr, char *end, unsigned long reg, enum argtype type)
{
if (is_typed_fdarg(type)) {
sptr = bprintf(sptr, end, "%ld", (long) reg);
return sptr;
}
switch (type) {
case ARG_PATHNAME:
sptr = bprintf(sptr, end, "\"%s\"", (char *) reg);
break;
case ARG_PID:
case ARG_FD:
case ARG_SOCKETINFO:
sptr = bprintf(sptr, end, "%ld", (long) reg);
break;
case ARG_MODE_T:
sptr = bprintf(sptr, end, "%o", (mode_t) reg);
break;
case ARG_ADDRESS:
case ARG_NON_NULL_ADDRESS:
case ARG_IOVEC:
case ARG_SOCKADDR:
sptr = bprintf(sptr, end, "0x%lx", reg);
break;
case ARG_MMAP:
/* Although generic sanitise has set this to a map struct,
* common_set_mmap_ptr_len() will subsequently set it to the ->ptr
* in the per syscall ->sanitise routine. */
sptr = bprintf(sptr, end, "0x%lx", reg);
break;
case ARG_OP:
case ARG_LIST:
sptr = bprintf(sptr, end, "0x%lx", reg);
break;
case ARG_UNDEFINED:
case ARG_LEN:
case ARG_RANGE:
case ARG_CPU:
case ARG_NUMA_NODE:
case ARG_IOVECLEN:
case ARG_SOCKADDRLEN:
if (((long) reg < -16384) || ((long) reg > 16384)) {
/* Print everything outside -16384 and 16384 as hex. */
sptr = bprintf(sptr, end, "0x%lx", reg);
} else {
/* Print everything else as signed decimal. */
sptr = bprintf(sptr, end, "%ld", (long) reg);
}
break;
default:
break;
}
return sptr;
}
static char * render_arg(struct syscallrecord *rec, char *sptr, char *end, unsigned int argnum, struct syscallentry *entry)
{
const char *name;
unsigned long reg;
enum argtype type;
type = entry->argtype[argnum - 1];
name = entry->argname[argnum - 1];
reg = get_argval(rec, argnum);
if (argnum != 1)
sptr = bprintf(sptr, end, ", ");
sptr = bprintf(sptr, end, "%s=", name);
sptr = decode_argtype(sptr, end, reg, type);
if (entry->decode != NULL) {
char *str;
str = entry->decode(rec, argnum);
if (str != NULL) {
sptr = bprintf(sptr, end, "%s", str);
free(str);
}
}
return sptr;
}
/*
* Render the live -vv trace line for one syscall into bufferstart.
* Returns the number of bytes written (excluding the NUL). Only feeds
* output_syscall_prefix; the post-mortem dumper reconstructs from the
* chronicle ring instead of reading the rendered prebuffer.
*/
static unsigned int render_syscall_prefix(struct syscallrecord *rec,
struct syscallentry *entry,
char *bufferstart)
{
struct childdata *child = this_child();
char *sptr = bufferstart;
char *end = bufferstart + PREBUFFER_LEN;
unsigned int i;
sptr = bprintf(sptr, end, "[child%u:%u] [%lu] %s",
child->num, __atomic_load_n(&pids[child->num], __ATOMIC_RELAXED), child->op_nr,
rec->do32bit == true ? "[32BIT] " : "");
sptr = bprintf(sptr, end, "%s(", entry->name);
for_each_arg(entry, i) {
sptr = render_arg(rec, sptr, end, i, entry);
}
sptr = bprintf(sptr, end, ") ");
return sptr - bufferstart;
}
static unsigned int render_syscall_postfix(struct syscallrecord *rec, char *bufferstart)
{
char *sptr = bufferstart;
char *end = bufferstart + POSTBUFFER_LEN;
if (IS_ERR(rec->retval)) {
sptr = bprintf(sptr, end, "= %ld (%s)",
(long) rec->retval, strerror(rec->errno_post));
} else {
sptr = bprintf(sptr, end, "= ");
if ((unsigned long) rec->retval > 10000)
sptr = bprintf(sptr, end, "0x%lx", rec->retval);
else
sptr = bprintf(sptr, end, "%ld", (long) rec->retval);
}
sptr = bprintf(sptr, end, "\n");
return sptr - bufferstart;
}
/* These next two functions are called from dispatch_step() once per syscall.
* They render a one-line trace into the shm-resident pre/postbuffer and feed
* it to output_rendered_buffer for live emission. output_rendered_buffer
* itself only prints at verbosity > 1, and the post-mortem dumper reads
* structured chronicle_slot fields rather than these rendered buffers, so
* the per-call render is wasted work on the common (verbosity <= 1) path.
* Skip it unless the live trace will be printed or a post-mortem is
* currently running (where any in-parent diagnostic that defensively reads
* rec->pre/postbuffer should still find a coherent string).
*/
void output_syscall_prefix(struct syscallrecord *rec, struct syscallentry *entry)
{
static char *buffer = NULL;
unsigned int len;
if (verbosity <= 1 &&
!__atomic_load_n(&shm->postmortem_in_progress, __ATOMIC_ACQUIRE)) {
rec->prebuffer[0] = '\0';
return;
}
if (buffer == NULL)
buffer = zmalloc(PREBUFFER_LEN);
len = render_syscall_prefix(rec, entry, buffer);
/* copy child-local buffer to shm, NUL-terminate */
memcpy(rec->prebuffer, buffer, len);
rec->prebuffer[len] = '\0';
output_rendered_buffer(rec->prebuffer);
}
void output_syscall_postfix(struct syscallrecord *rec)
{
static char *buffer = NULL;
unsigned int len;
if (verbosity <= 1 &&
!__atomic_load_n(&shm->postmortem_in_progress, __ATOMIC_ACQUIRE)) {
rec->postbuffer[0] = '\0';
return;
}
if (buffer == NULL)
buffer = zmalloc(POSTBUFFER_LEN);
len = render_syscall_postfix(rec, buffer);
/* copy child-local buffer to shm, NUL-terminate */
memcpy(rec->postbuffer, buffer, len);
rec->postbuffer[len] = '\0';
output_rendered_buffer(rec->postbuffer);
}