-
Notifications
You must be signed in to change notification settings - Fork 66
/
event_fout.c
246 lines (222 loc) · 8.81 KB
/
event_fout.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
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
238
239
240
241
242
243
244
245
246
#include "phpspy.h"
typedef struct event_handler_fout_udata_s {
int fd;
char *buf;
char *cur;
size_t buf_size;
size_t rem;
int use_mutex;
} event_handler_fout_udata_t;
static int event_handler_fout_write(event_handler_fout_udata_t *udata);
static int event_handler_fout_snprintf(char **s, size_t *n, size_t *ret_len, int repl_delim, const char *fmt, ...);
static int event_handler_fout_open(int *fd);
static pthread_mutex_t event_handler_fout_mutex = PTHREAD_MUTEX_INITIALIZER;
int event_handler_fout(struct trace_context_s *context, int event_type) {
int rv, fd;
size_t len;
trace_frame_t *frame;
trace_request_t *request;
event_handler_fout_udata_t *udata;
struct timeval tv;
udata = (event_handler_fout_udata_t*)context->event_udata;
if (!udata && event_type != PHPSPY_TRACE_EVENT_INIT) {
return PHPSPY_ERR;
}
len = 0;
switch (event_type) {
case PHPSPY_TRACE_EVENT_INIT:
try(rv, event_handler_fout_open(&fd));
udata = calloc(1, sizeof(event_handler_fout_udata_t));
udata->fd = fd;
udata->buf_size = opt_fout_buffer_size + 1; /* + 1 for null char */
udata->buf = malloc(udata->buf_size);
udata->cur = udata->buf;
udata->rem = udata->buf_size;
udata->use_mutex = context->event_handler_opts != NULL
&& strchr(context->event_handler_opts, 'm') != NULL ? 1 : 0;
context->event_udata = udata;
break;
case PHPSPY_TRACE_EVENT_STACK_BEGIN:
udata->cur = udata->buf;
udata->cur[0] = '\0';
udata->rem = udata->buf_size;
break;
case PHPSPY_TRACE_EVENT_FRAME:
frame = &context->event.frame;
try(rv, event_handler_fout_snprintf(
&udata->cur,
&udata->rem,
&len,
1,
"%d %.*s%s%.*s %.*s:%d",
frame->depth,
(int)frame->loc.class_len, frame->loc.class,
frame->loc.class_len > 0 ? "::" : "",
(int)frame->loc.func_len, frame->loc.func,
(int)frame->loc.file_len, frame->loc.file,
frame->loc.lineno
));
try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
break;
case PHPSPY_TRACE_EVENT_VARPEEK:
try(rv, event_handler_fout_snprintf(
&udata->cur,
&udata->rem,
&len,
1,
"# varpeek %s@%s = %s",
context->event.varpeek.var->name,
context->event.varpeek.entry->filename_lineno,
context->event.varpeek.zval_str
));
try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
break;
case PHPSPY_TRACE_EVENT_GLOPEEK:
try(rv, event_handler_fout_snprintf(
&udata->cur,
&udata->rem,
&len,
1,
"# glopeek %s = %s",
context->event.glopeek.gentry->key,
context->event.glopeek.zval_str
));
try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
break;
case PHPSPY_TRACE_EVENT_REQUEST:
request = &context->event.request;
try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 1, "# uri = %s", request->uri));
try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 1, "# path = %s", request->path));
try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 1, "# qstring = %s", request->qstring));
try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 1, "# cookie = %s", request->cookie));
try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 1, "# ts = %f", request->ts));
try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
break;
case PHPSPY_TRACE_EVENT_MEM:
try(rv, event_handler_fout_snprintf(
&udata->cur,
&udata->rem,
&len,
1,
"# mem %lu %lu",
(uint64_t)context->event.mem.size,
(uint64_t)context->event.mem.peak
));
try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
break;
case PHPSPY_TRACE_EVENT_STACK_END:
if (udata->cur == udata->buf) {
/* buffer is empty */
break;
}
if (opt_filter_re) {
rv = regexec(opt_filter_re, udata->buf, 0, NULL, 0);
if (opt_filter_negate == 0 && rv != 0) break;
if (opt_filter_negate != 0 && rv == 0) break;
}
do {
if (opt_verbose_fields_ts) {
gettimeofday(&tv, NULL);
try_break(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 1, "# trace_ts = %f", (double)(tv.tv_sec + tv.tv_usec / 1000000.0)));
try_break(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
}
if (opt_verbose_fields_pid) {
try_break(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 1, "# pid = %d", context->target.pid));
try_break(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
}
try_break(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_trace_delim));
} while (0);
try(rv, event_handler_fout_write(udata));
break;
case PHPSPY_TRACE_EVENT_DEINIT:
close(udata->fd);
free(udata->buf);
free(udata);
break;
}
return PHPSPY_OK;
}
static int event_handler_fout_write(event_handler_fout_udata_t *udata) {
int rv;
ssize_t write_len;
rv = PHPSPY_OK;
write_len = (udata->cur - udata->buf);
if (write_len < 1) {
/* nothing to write */
return rv;
}
if (udata->use_mutex) {
pthread_mutex_lock(&event_handler_fout_mutex);
}
if (write(udata->fd, udata->buf, write_len) != write_len) {
log_error("event_handler_fout: Write failed (%s)\n", errno != 0 ? strerror(errno) : "partial");
rv = PHPSPY_ERR;
}
if (udata->use_mutex) {
pthread_mutex_unlock(&event_handler_fout_mutex);
}
return rv;
}
static int event_handler_fout_snprintf(char **s, size_t *n, size_t *ret_len, int repl_delim, const char *fmt, ...) {
int len, i;
va_list vl;
char *c;
va_start(vl, fmt);
len = vsnprintf(*s, *n, fmt, vl);
va_end(vl);
if (len < 0 || (size_t)len >= *n) {
log_error("event_handler_fout_snprintf: Not enough space in buffer; truncating\n");
return PHPSPY_ERR | PHPSPY_ERR_BUF_FULL;
}
if (repl_delim) {
for (i = 0; i < len; i++) { /* TODO optimize */
c = *s + i;
if (*c == opt_trace_delim || *c == opt_frame_delim) {
*c = '?';
}
}
}
*s += len;
*n -= len;
*ret_len = (size_t)len;
return PHPSPY_OK;
}
static int event_handler_fout_open(int *fd) {
int tfd = -1, errno_saved;
char *path, *apath = NULL;
if (strcmp(opt_path_output, "-") == 0) {
tfd = dup(STDOUT_FILENO);
if (tfd < 0) {
perror("event_handler_fout_open: dup");
return PHPSPY_ERR;
}
*fd = tfd;
return PHPSPY_OK;
}
if (strstr(opt_path_output, "%d") != NULL) {
if (asprintf(&apath, opt_path_output, gettid()) < 0) {
errno = ENOMEM;
perror("event_handler_fout_open: asprintf");
return PHPSPY_ERR;
}
path = apath;
} else {
path = opt_path_output;
}
tfd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
errno_saved = errno;
if (apath) {
free(apath);
}
if (tfd < 0) {
errno = errno_saved;
perror("event_handler_fout_open: open");
return PHPSPY_ERR;
}
*fd = tfd;
return PHPSPY_OK;
}