xref: /php-src/sapi/fpm/fpm/fpm_php_trace.c (revision ff2a211d)
1 	/* (c) 2007,2008 Andrei Nigmatulin */
2 
3 #include "fpm_config.h"
4 
5 #if HAVE_FPM_TRACE
6 
7 #include "php.h"
8 #include "php_main.h"
9 
10 #include <stdio.h>
11 #include <stddef.h>
12 #include <inttypes.h>
13 #include <unistd.h>
14 #include <sys/time.h>
15 #include <sys/types.h>
16 #include <errno.h>
17 
18 #include "fpm_trace.h"
19 #include "fpm_php_trace.h"
20 #include "fpm_children.h"
21 #include "fpm_worker_pool.h"
22 #include "fpm_process_ctl.h"
23 #include "fpm_scoreboard.h"
24 
25 #include "zlog.h"
26 
27 
28 #define valid_ptr(p) ((p) && 0 == ((p) & (sizeof(long) - 1)))
29 
30 #if SIZEOF_LONG == 4
31 #define PTR_FMT "08"
32 #elif SIZEOF_LONG == 8
33 #define PTR_FMT "016"
34 #endif
35 
36 
fpm_php_trace_dump(struct fpm_child_s * child,FILE * slowlog)37 static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog) /* {{{ */
38 {
39 	int callers_limit = child->wp->config->request_slowlog_trace_depth;
40 	pid_t pid = child->pid;
41 	struct timeval tv;
42 	char buf[1024];
43 	long execute_data;
44 	long path_translated;
45 	long l;
46 
47 	gettimeofday(&tv, 0);
48 
49 	zlog_print_time(&tv, buf, sizeof(buf));
50 
51 	fprintf(slowlog, "\n%s [pool %s] pid %d\n", buf, child->wp->config->name, (int) pid);
52 
53 	if (0 > fpm_trace_get_long((long) &SG(request_info).path_translated, &l)) {
54 		return -1;
55 	}
56 
57 	path_translated = l;
58 
59 	if (0 > fpm_trace_get_strz(buf, sizeof(buf), path_translated)) {
60 		return -1;
61 	}
62 
63 	fprintf(slowlog, "script_filename = %s\n", buf);
64 
65 	if (0 > fpm_trace_get_long((long) &EG(current_execute_data), &l)) {
66 		return -1;
67 	}
68 
69 	execute_data = l;
70 
71 	while (execute_data) {
72 		long function;
73 		long function_name;
74 		long file_name;
75 		long prev;
76 		uint32_t lineno = 0;
77 
78 		if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, func), &l)) {
79 			return -1;
80 		}
81 
82 		function = l;
83 
84 		if (valid_ptr(function)) {
85 			if (0 > fpm_trace_get_long(function + offsetof(zend_function, common.function_name), &l)) {
86 				return -1;
87 			}
88 
89 			function_name = l;
90 
91 			if (function_name == 0) {
92 				uint32_t *call_info = (uint32_t *)&l;
93 				if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, This.u1.type_info), &l)) {
94 					return -1;
95 				}
96 
97 				if (ZEND_CALL_KIND_EX(*call_info) == ZEND_CALL_TOP_CODE) {
98 					return 0;
99 				} else if (ZEND_CALL_KIND_EX(*call_info) == ZEND_CALL_NESTED_CODE) {
100 					memcpy(buf, "[INCLUDE_OR_EVAL]", sizeof("[INCLUDE_OR_EVAL]"));
101 				} else {
102 					ZEND_UNREACHABLE();
103 				}
104 			} else {
105 				if (0 > fpm_trace_get_strz(buf, sizeof(buf), function_name + offsetof(zend_string, val))) {
106 					return -1;
107 				}
108 
109 			}
110 		} else {
111 			memcpy(buf, "???", sizeof("???"));
112 		}
113 
114 		fprintf(slowlog, "[0x%" PTR_FMT "lx] ", execute_data);
115 
116 		fprintf(slowlog, "%s()", buf);
117 
118 		*buf = '\0';
119 
120 		if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, prev_execute_data), &l)) {
121 			return -1;
122 		}
123 
124 		execute_data = prev = l;
125 
126 		while (prev) {
127 			zend_uchar *type;
128 
129 			if (0 > fpm_trace_get_long(prev + offsetof(zend_execute_data, func), &l)) {
130 				return -1;
131 			}
132 
133 			function = l;
134 
135 			if (!valid_ptr(function)) {
136 				break;
137 			}
138 
139 			type = (zend_uchar *)&l;
140 			if (0 > fpm_trace_get_long(function + offsetof(zend_function, type), &l)) {
141 				return -1;
142 			}
143 
144 			if (ZEND_USER_CODE(*type)) {
145 				if (0 > fpm_trace_get_long(function + offsetof(zend_op_array, filename), &l)) {
146 					return -1;
147 				}
148 
149 				file_name = l;
150 
151 				if (0 > fpm_trace_get_strz(buf, sizeof(buf), file_name + offsetof(zend_string, val))) {
152 					return -1;
153 				}
154 
155 				if (0 > fpm_trace_get_long(prev + offsetof(zend_execute_data, opline), &l)) {
156 					return -1;
157 				}
158 
159 				if (valid_ptr(l)) {
160 					long opline = l;
161 					uint32_t *lu = (uint32_t *) &l;
162 
163 					if (0 > fpm_trace_get_long(opline + offsetof(struct _zend_op, lineno), &l)) {
164 						return -1;
165 					}
166 
167 					lineno = *lu;
168 				}
169 				break;
170 			}
171 
172 			if (0 > fpm_trace_get_long(prev + offsetof(zend_execute_data, prev_execute_data), &l)) {
173 				return -1;
174 			}
175 
176 			prev = l;
177 		}
178 
179 		fprintf(slowlog, " %s:%u\n", *buf ? buf : "unknown", lineno);
180 
181 		if (0 == --callers_limit) {
182 			break;
183 		}
184 	}
185 
186 	return 0;
187 }
188 /* }}} */
189 
fpm_php_trace(struct fpm_child_s * child)190 void fpm_php_trace(struct fpm_child_s *child) /* {{{ */
191 {
192 	fpm_scoreboard_update(0, 0, 0, 0, 0, 0, 1, FPM_SCOREBOARD_ACTION_INC, child->wp->scoreboard);
193 	FILE *slowlog;
194 
195 	zlog(ZLOG_NOTICE, "about to trace %d", (int) child->pid);
196 
197 	slowlog = fopen(child->wp->config->slowlog, "a+");
198 
199 	if (!slowlog) {
200 		zlog(ZLOG_SYSERROR, "unable to open slowlog (%s)", child->wp->config->slowlog);
201 		goto done0;
202 	}
203 
204 	if (0 > fpm_trace_ready(child->pid)) {
205 		goto done1;
206 	}
207 
208 	if (0 > fpm_php_trace_dump(child, slowlog)) {
209 		fprintf(slowlog, "+++ dump failed\n");
210 	}
211 
212 	if (0 > fpm_trace_close(child->pid)) {
213 		goto done1;
214 	}
215 
216 done1:
217 	fclose(slowlog);
218 
219 done0:
220 	fpm_pctl_kill(child->pid, FPM_PCTL_CONT);
221 	child->tracer = 0;
222 
223 	zlog(ZLOG_NOTICE, "finished trace of %d", (int) child->pid);
224 }
225 /* }}} */
226 
227 #endif
228