php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
fpm_php_trace.c
Go to the documentation of this file.
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
37static 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
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
98 return 0;
100 memcpy(buf, "[INCLUDE_OR_EVAL]", sizeof("[INCLUDE_OR_EVAL]"));
101 } else {
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
121 return -1;
122 }
123
124 execute_data = prev = l;
125
126 while (prev) {
128
130 return -1;
131 }
132
133 function = l;
134
135 if (!valid_ptr(function)) {
136 break;
137 }
138
139 type = (zend_uchar *)&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
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
190void fpm_php_trace(struct fpm_child_s *child) /* {{{ */
191{
192 fpm_scoreboard_update(0, 0, 0, 0, 0, 0, 1, 0, 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
216done1:
217 fclose(slowlog);
218
219done0:
221 child->tracer = 0;
222
223 zlog(ZLOG_NOTICE, "finished trace of %d", (int) child->pid);
224}
225/* }}} */
226
227#endif
#define SG(v)
Definition SAPI.h:160
fprintf($stream, string $format, mixed ... $values)
prev(array|object &$array)
gettimeofday(bool $as_float=false)
fclose($stream)
fopen(string $filename, string $mode, bool $use_include_path=false, $context=null)
zend_ffi_type * type
Definition ffi.c:3812
memcpy(ptr1, ptr2, size)
zval * val
Definition ffi.c:4262
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
void fpm_php_trace(struct fpm_child_s *)
int fpm_pctl_kill(pid_t pid, int how)
@ FPM_PCTL_CONT
void fpm_scoreboard_update(int idle, int active, int lq, int lq_len, int requests, int max_children_reached, int slow_rq, size_t memory_peak, int action, struct fpm_scoreboard_s *scoreboard)
#define FPM_SCOREBOARD_ACTION_INC
int fpm_trace_get_strz(char *buf, size_t sz, long addr)
Definition fpm_trace.c:9
int fpm_trace_close(pid_t pid)
int fpm_trace_get_long(long addr, long *data)
int fpm_trace_ready(pid_t pid)
#define offsetof(STRUCTURE, FIELD)
struct timeval tv
Definition session.c:1280
void(* tracer)(struct fpm_child_s *)
struct fpm_worker_pool_s * wp
struct fpm_worker_pool_config_s * config
struct fpm_scoreboard_s * scoreboard
execute_data func
#define ZEND_USER_CODE(type)
#define ZEND_CALL_TOP_CODE
#define ZEND_CALL_NESTED_CODE
struct _zend_op_array zend_op_array
#define ZEND_CALL_KIND_EX(call_info)
union _zend_function zend_function
#define EG(v)
struct _zend_string zend_string
#define ZEND_UNREACHABLE()
unsigned char zend_uchar
Definition zend_types.h:57
struct _zend_execute_data zend_execute_data
Definition zend_types.h:91
uint32_t call_info
call prev_execute_data
function(EX_VAR(opline->result.var))
execute_data
size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len)
Definition zlog.c:77
#define ZLOG_SYSERROR
Definition zlog.h:53
@ ZLOG_NOTICE
Definition zlog.h:43
#define zlog(flags,...)
Definition zlog.h:9