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