summaryrefslogtreecommitdiff
path: root/sapi/fpm/fpm/fpm_php_trace.c
blob: 4ab2db6e3f6c2de90f8b8e23b4f82219df774cd0 (plain)
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

	/* $Id: fpm_php_trace.c,v 1.27.2.1 2008/11/15 00:57:24 anight Exp $ */
	/* (c) 2007,2008 Andrei Nigmatulin */

#include "fpm_config.h"

#if HAVE_FPM_TRACE

#include "php.h"
#include "php_main.h"

#include <stdio.h>
#include <stddef.h>
#if HAVE_INTTYPES_H
# include <inttypes.h>
#else
# include <stdint.h>
#endif
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <errno.h>

#include "fpm_trace.h"
#include "fpm_php_trace.h"
#include "fpm_children.h"
#include "fpm_worker_pool.h"
#include "fpm_process_ctl.h"
#include "fpm_scoreboard.h"

#include "zlog.h"


#define valid_ptr(p) ((p) && 0 == ((p) & (sizeof(long) - 1)))

#if SIZEOF_LONG == 4
#define PTR_FMT "08"
#elif SIZEOF_LONG == 8
#define PTR_FMT "016"
#endif


static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog) /* {{{ */
{
	int callers_limit = 20;
	pid_t pid = child->pid;
	struct timeval tv;
	static const int buf_size = 1024;
	char buf[buf_size];
	long execute_data;
	long path_translated;
	long l;

	gettimeofday(&tv, 0);

	zlog_print_time(&tv, buf, buf_size);

	fprintf(slowlog, "\n%s [pool %s] pid %d\n", buf, child->wp->config->name, (int) pid);

	if (0 > fpm_trace_get_long((long) &SG(request_info).path_translated, &l)) {
		return -1;
	}

	path_translated = l;

	if (0 > fpm_trace_get_strz(buf, buf_size, path_translated)) {
		return -1;
	}

	fprintf(slowlog, "script_filename = %s\n", buf);

	if (0 > fpm_trace_get_long((long) &EG(current_execute_data), &l)) {
		return -1;
	}

	execute_data = l;

	while (execute_data) {
		long function;
		long function_name;
		long file_name;
		long prev;
		uint lineno = 0;

		if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, func), &l)) {
			return -1;
		}

		function = l;

		if (valid_ptr(function)) {
			if (0 > fpm_trace_get_long(function + offsetof(zend_function, common.function_name), &l)) {
				return -1;
			}

			function_name = l;

			if (function_name == 0) {
				uint32_t *call_info = (uint32_t *)&l;
				if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, This.u1.type_info), &l)) {
					return -1;
				}

				if (ZEND_CALL_KIND_EX((*call_info) >> ZEND_CALL_INFO_SHIFT) == ZEND_CALL_TOP_CODE) {
					return 0;
				} else if (ZEND_CALL_KIND_EX(*(call_info) >> ZEND_CALL_INFO_SHIFT) == ZEND_CALL_NESTED_CODE) {
					memcpy(buf, "[INCLUDE_OR_EVAL]", sizeof("[INCLUDE_OR_EVAL]"));
				} else {
					ZEND_ASSERT(0);
				}
			} else {
				if (0 > fpm_trace_get_strz(buf, buf_size, function_name + offsetof(zend_string, val))) {
					return -1;
				}

			}
		} else {
			memcpy(buf, "???", sizeof("???"));
		}
		
		fprintf(slowlog, "[0x%" PTR_FMT "lx] ", execute_data);

		fprintf(slowlog, "%s()", buf);

		*buf = '\0';

		if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, prev_execute_data), &l)) {
			return -1;
		}

		execute_data = prev = l;

		while (prev) {
			zend_uchar *type;

			if (0 > fpm_trace_get_long(prev + offsetof(zend_execute_data, func), &l)) {
				return -1;
			}

			function = l;

			if (!valid_ptr(function)) {
				break;
			}

			type = (zend_uchar *)&l;
			if (0 > fpm_trace_get_long(function + offsetof(zend_function, type), &l)) { 
				return -1;
			}

			if (ZEND_USER_CODE(*type)) {
				if (0 > fpm_trace_get_long(function + offsetof(zend_op_array, filename), &l)) {
					return -1;
				}

				file_name = l;

				if (0 > fpm_trace_get_strz(buf, buf_size, file_name + offsetof(zend_string, val))) {
					return -1;
				}

				if (0 > fpm_trace_get_long(prev + offsetof(zend_execute_data, opline), &l)) {
					return -1;
				}

				if (valid_ptr(l)) {
					long opline = l;
					uint32_t *lu = (uint32_t *) &l;

					if (0 > fpm_trace_get_long(opline + offsetof(struct _zend_op, lineno), &l)) {
						return -1;
					}

					lineno = *lu;
				}
				break;
			} 

			if (0 > fpm_trace_get_long(prev + offsetof(zend_execute_data, prev_execute_data), &l)) {
				return -1;
			}

			prev = l;
		}

		fprintf(slowlog, " %s:%u\n", *buf ? buf : "unknown", lineno);

		if (0 == --callers_limit) {
			break;
		}
	}

	return 0;
}
/* }}} */

void fpm_php_trace(struct fpm_child_s *child) /* {{{ */
{
	fpm_scoreboard_update(0, 0, 0, 0, 0, 0, 1, FPM_SCOREBOARD_ACTION_INC, child->wp->scoreboard);
	FILE *slowlog;

	zlog(ZLOG_NOTICE, "about to trace %d", (int) child->pid);

	slowlog = fopen(child->wp->config->slowlog, "a+");

	if (!slowlog) {
		zlog(ZLOG_SYSERROR, "unable to open slowlog (%s)", child->wp->config->slowlog);
		goto done0;
	}

	if (0 > fpm_trace_ready(child->pid)) {
		goto done1;
	}

	if (0 > fpm_php_trace_dump(child, slowlog)) {
		fprintf(slowlog, "+++ dump failed\n");
	}

	if (0 > fpm_trace_close(child->pid)) {
		goto done1;
	}

done1:
	fclose(slowlog);

done0:
	fpm_pctl_kill(child->pid, FPM_PCTL_CONT);
	child->tracer = 0;

	zlog(ZLOG_NOTICE, "finished trace of %d", (int) child->pid);
}
/* }}} */

#endif