xref: /PHP-8.0/sapi/fpm/fpm/fpm_php_trace.c (revision 92c4b065)
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 	static const int buf_size = 1024;
43 	char buf[buf_size];
44 	long execute_data;
45 	long path_translated;
46 	long l;
47 
48 	gettimeofday(&tv, 0);
49 
50 	zlog_print_time(&tv, buf, buf_size);
51 
52 	fprintf(slowlog, "\n%s [pool %s] pid %d\n", buf, child->wp->config->name, (int) pid);
53 
54 	if (0 > fpm_trace_get_long((long) &SG(request_info).path_translated, &l)) {
55 		return -1;
56 	}
57 
58 	path_translated = l;
59 
60 	if (0 > fpm_trace_get_strz(buf, buf_size, path_translated)) {
61 		return -1;
62 	}
63 
64 	fprintf(slowlog, "script_filename = %s\n", buf);
65 
66 	if (0 > fpm_trace_get_long((long) &EG(current_execute_data), &l)) {
67 		return -1;
68 	}
69 
70 	execute_data = l;
71 
72 	while (execute_data) {
73 		long function;
74 		long function_name;
75 		long file_name;
76 		long prev;
77 		uint32_t lineno = 0;
78 
79 		if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, func), &l)) {
80 			return -1;
81 		}
82 
83 		function = l;
84 
85 		if (valid_ptr(function)) {
86 			if (0 > fpm_trace_get_long(function + offsetof(zend_function, common.function_name), &l)) {
87 				return -1;
88 			}
89 
90 			function_name = l;
91 
92 			if (function_name == 0) {
93 				uint32_t *call_info = (uint32_t *)&l;
94 				if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, This.u1.type_info), &l)) {
95 					return -1;
96 				}
97 
98 				if (ZEND_CALL_KIND_EX(*call_info) == ZEND_CALL_TOP_CODE) {
99 					return 0;
100 				} else if (ZEND_CALL_KIND_EX(*call_info) == ZEND_CALL_NESTED_CODE) {
101 					memcpy(buf, "[INCLUDE_OR_EVAL]", sizeof("[INCLUDE_OR_EVAL]"));
102 				} else {
103 					ZEND_UNREACHABLE();
104 				}
105 			} else {
106 				if (0 > fpm_trace_get_strz(buf, buf_size, function_name + offsetof(zend_string, val))) {
107 					return -1;
108 				}
109 
110 			}
111 		} else {
112 			memcpy(buf, "???", sizeof("???"));
113 		}
114 
115 		fprintf(slowlog, "[0x%" PTR_FMT "lx] ", execute_data);
116 
117 		fprintf(slowlog, "%s()", buf);
118 
119 		*buf = '\0';
120 
121 		if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, prev_execute_data), &l)) {
122 			return -1;
123 		}
124 
125 		execute_data = prev = l;
126 
127 		while (prev) {
128 			zend_uchar *type;
129 
130 			if (0 > fpm_trace_get_long(prev + offsetof(zend_execute_data, func), &l)) {
131 				return -1;
132 			}
133 
134 			function = l;
135 
136 			if (!valid_ptr(function)) {
137 				break;
138 			}
139 
140 			type = (zend_uchar *)&l;
141 			if (0 > fpm_trace_get_long(function + offsetof(zend_function, type), &l)) {
142 				return -1;
143 			}
144 
145 			if (ZEND_USER_CODE(*type)) {
146 				if (0 > fpm_trace_get_long(function + offsetof(zend_op_array, filename), &l)) {
147 					return -1;
148 				}
149 
150 				file_name = l;
151 
152 				if (0 > fpm_trace_get_strz(buf, buf_size, file_name + offsetof(zend_string, val))) {
153 					return -1;
154 				}
155 
156 				if (0 > fpm_trace_get_long(prev + offsetof(zend_execute_data, opline), &l)) {
157 					return -1;
158 				}
159 
160 				if (valid_ptr(l)) {
161 					long opline = l;
162 					uint32_t *lu = (uint32_t *) &l;
163 
164 					if (0 > fpm_trace_get_long(opline + offsetof(struct _zend_op, lineno), &l)) {
165 						return -1;
166 					}
167 
168 					lineno = *lu;
169 				}
170 				break;
171 			}
172 
173 			if (0 > fpm_trace_get_long(prev + offsetof(zend_execute_data, prev_execute_data), &l)) {
174 				return -1;
175 			}
176 
177 			prev = l;
178 		}
179 
180 		fprintf(slowlog, " %s:%u\n", *buf ? buf : "unknown", lineno);
181 
182 		if (0 == --callers_limit) {
183 			break;
184 		}
185 	}
186 
187 	return 0;
188 }
189 /* }}} */
190 
fpm_php_trace(struct fpm_child_s * child)191 void fpm_php_trace(struct fpm_child_s *child) /* {{{ */
192 {
193 	fpm_scoreboard_update(0, 0, 0, 0, 0, 0, 1, FPM_SCOREBOARD_ACTION_INC, child->wp->scoreboard);
194 	FILE *slowlog;
195 
196 	zlog(ZLOG_NOTICE, "about to trace %d", (int) child->pid);
197 
198 	slowlog = fopen(child->wp->config->slowlog, "a+");
199 
200 	if (!slowlog) {
201 		zlog(ZLOG_SYSERROR, "unable to open slowlog (%s)", child->wp->config->slowlog);
202 		goto done0;
203 	}
204 
205 	if (0 > fpm_trace_ready(child->pid)) {
206 		goto done1;
207 	}
208 
209 	if (0 > fpm_php_trace_dump(child, slowlog)) {
210 		fprintf(slowlog, "+++ dump failed\n");
211 	}
212 
213 	if (0 > fpm_trace_close(child->pid)) {
214 		goto done1;
215 	}
216 
217 done1:
218 	fclose(slowlog);
219 
220 done0:
221 	fpm_pctl_kill(child->pid, FPM_PCTL_CONT);
222 	child->tracer = 0;
223 
224 	zlog(ZLOG_NOTICE, "finished trace of %d", (int) child->pid);
225 }
226 /* }}} */
227 
228 #endif
229