xref: /PHP-5.3/sapi/fpm/fpm/fpm_php_trace.c (revision 4e396d73)
1 
2 	/* $Id: fpm_php_trace.c,v 1.27.2.1 2008/11/15 00:57:24 anight Exp $ */
3 	/* (c) 2007,2008 Andrei Nigmatulin */
4 
5 #include "fpm_config.h"
6 
7 #if HAVE_FPM_TRACE
8 
9 #include "php.h"
10 #include "php_main.h"
11 
12 #include <stdio.h>
13 #include <stddef.h>
14 #if HAVE_INTTYPES_H
15 # include <inttypes.h>
16 #else
17 # include <stdint.h>
18 #endif
19 #include <unistd.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <errno.h>
23 
24 #include "fpm_trace.h"
25 #include "fpm_php_trace.h"
26 #include "fpm_children.h"
27 #include "fpm_worker_pool.h"
28 #include "fpm_process_ctl.h"
29 
30 #include "zlog.h"
31 
32 
33 #define valid_ptr(p) ((p) && 0 == ((p) & (sizeof(long) - 1)))
34 
35 #if SIZEOF_LONG == 4
36 #define PTR_FMT "08"
37 #elif SIZEOF_LONG == 8
38 #define PTR_FMT "016"
39 #endif
40 
41 
fpm_php_trace_dump(struct fpm_child_s * child,FILE * slowlog TSRMLS_DC)42 static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog TSRMLS_DC) /* {{{ */
43 {
44 	int callers_limit = 20;
45 	pid_t pid = child->pid;
46 	struct timeval tv;
47 	static const int buf_size = 1024;
48 	char buf[buf_size];
49 	long execute_data;
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_strz(buf, buf_size, (long) &SG(request_info).path_translated)) {
59 		return -1;
60 	}
61 
62 	fprintf(slowlog, "script_filename = %s\n", buf);
63 
64 	if (0 > fpm_trace_get_long((long) &EG(current_execute_data), &l)) {
65 		return -1;
66 	}
67 
68 	execute_data = l;
69 
70 	while (execute_data) {
71 		long function;
72 		uint lineno = 0;
73 
74 		fprintf(slowlog, "[0x%" PTR_FMT "lx] ", execute_data);
75 
76 		if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, function_state.function), &l)) {
77 			return -1;
78 		}
79 
80 		function = l;
81 
82 		if (valid_ptr(function)) {
83 			if (0 > fpm_trace_get_strz(buf, buf_size, function + offsetof(zend_function, common.function_name))) {
84 				return -1;
85 			}
86 
87 			fprintf(slowlog, "%s()", buf);
88 		} else {
89 			fprintf(slowlog, "???");
90 		}
91 
92 		if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, op_array), &l)) {
93 			return -1;
94 		}
95 
96 		*buf = '\0';
97 
98 		if (valid_ptr(l)) {
99 			long op_array = l;
100 
101 			if (0 > fpm_trace_get_strz(buf, buf_size, op_array + offsetof(zend_op_array, filename))) {
102 				return -1;
103 			}
104 		}
105 
106 		if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, opline), &l)) {
107 			return -1;
108 		}
109 
110 		if (valid_ptr(l)) {
111 			long opline = l;
112 			uint *lu = (uint *) &l;
113 
114 			if (0 > fpm_trace_get_long(opline + offsetof(struct _zend_op, lineno), &l)) {
115 				return -1;
116 			}
117 
118 			lineno = *lu;
119 		}
120 
121 		fprintf(slowlog, " %s:%u\n", *buf ? buf : "unknown", lineno);
122 
123 		if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, prev_execute_data), &l)) {
124 			return -1;
125 		}
126 
127 		execute_data = l;
128 
129 		if (0 == --callers_limit) {
130 			break;
131 		}
132 	}
133 	return 0;
134 }
135 /* }}} */
136 
fpm_php_trace(struct fpm_child_s * child)137 void fpm_php_trace(struct fpm_child_s *child) /* {{{ */
138 {
139 	TSRMLS_FETCH();
140 	FILE *slowlog;
141 
142 	zlog(ZLOG_NOTICE, "about to trace %d", (int) child->pid);
143 
144 	slowlog = fopen(child->wp->config->slowlog, "a+");
145 
146 	if (!slowlog) {
147 		zlog(ZLOG_SYSERROR, "unable to open slowlog (%s)", child->wp->config->slowlog);
148 		goto done0;
149 	}
150 
151 	if (0 > fpm_trace_ready(child->pid)) {
152 		goto done1;
153 	}
154 
155 	if (0 > fpm_php_trace_dump(child, slowlog TSRMLS_CC)) {
156 		fprintf(slowlog, "+++ dump failed\n");
157 	}
158 
159 	if (0 > fpm_trace_close(child->pid)) {
160 		goto done1;
161 	}
162 
163 done1:
164 	fclose(slowlog);
165 
166 done0:
167 	fpm_pctl_kill(child->pid, FPM_PCTL_CONT);
168 	child->tracer = 0;
169 
170 	zlog(ZLOG_NOTICE, "finished trace of %d", (int) child->pid);
171 }
172 /* }}} */
173 
174 #endif
175 
176