Debian Codegraph @

rpython/translator/c/src/debug_print.c

raw symbols SCIP
1 #include <string.h>
2 #include <stddef.h>
3 #include <stdlib.h>
4
5 #include <stdio.h>
6 #ifndef _WIN32
7 #include <unistd.h>
8 #include <time.h>
9 #include <sys/time.h>
10 #else
11 #define WIN32_LEAN_AND_MEAN
12 #include <windows.h>
13 #endif
14 #include "common_header.h"
15 #include "src/profiling.h"
16 #include "src/debug_print.h"
17
18 long pypy_have_debug_prints = -1;
19 FILE *pypy_debug_file = NULL;
20 static unsigned char debug_ready = 0;
21 static unsigned char debug_profile = 0;
22 static char *debug_start_colors_1 = "";
23 static char *debug_start_colors_2 = "";
24 static char *debug_stop_colors = "";
25 static char *debug_prefix = NULL;
26
27 static void pypy_debug_open(void)
28 {
29 char *filename = getenv("PYPYLOG");
30
31 if (filename && filename[0])
32 {
33 char *newfilename = NULL, *escape;
34 char *colon = strchr(filename, ':');
35 if (filename[0] == '+')
36 {
37 filename += 1;
38 colon = NULL;
39 }
40 if (!colon)
41 {
42 /* PYPYLOG=+filename (or just 'filename') --- profiling version */
43 debug_profile = 1;
44 pypy_setup_profiling();
45 }
46 else
47 {
48 /* PYPYLOG=prefix:filename --- conditional logging */
49 int n = colon - filename;
50 debug_prefix = malloc(n + 1);
51 memcpy(debug_prefix, filename, n);
52 debug_prefix[n] = '\0';
53 filename = colon + 1;
54 }
55 escape = strstr(filename, "%d");
56 if (escape) /* a "%d" in the filename is replaced with the pid */
57 {
58 newfilename = malloc(strlen(filename) + 32);
59 if (newfilename != NULL)
60 {
61 char *p = newfilename;
62 memcpy(p, filename, escape - filename);
63 p += escape - filename;
64 sprintf(p, "%ld", (long)getpid());
65 strcat(p, escape + 2);
66 filename = newfilename;
67 }
68 }
69 if (strcmp(filename, "-") != 0)
70 {
71 pypy_debug_file = fopen(filename, "w");
72 }
73
74 if (escape)
75 {
76 free(newfilename); /* if not null */
77 /* the env var is kept and passed to subprocesses */
78 }
79 else
80 {
81 #ifndef _WIN32
82 unsetenv("PYPYLOG");
83 #else
84 putenv("PYPYLOG=");
85 #endif
86 }
87 }
88 if (!pypy_debug_file)
89 {
90 pypy_debug_file = stderr;
91 if (isatty(2))
92 {
93 debug_start_colors_1 = "\033[1m\033[31m";
94 debug_start_colors_2 = "\033[31m";
95 debug_stop_colors = "\033[0m";
96 }
97 }
98 debug_ready = 1;
99 }
100
101 long pypy_debug_offset(void)
102 {
103 if (!debug_ready)
104 return -1;
105 /* The following fflush() makes sure everything is written now, which
106 is just before a fork(). So we can fork() and close the file in
107 the subprocess without ending up with the content of the buffer
108 written twice. */
109 fflush(pypy_debug_file);
110
111 // note that we deliberately ignore errno, since -1 is fine
112 // in case this is not a real file
113 return ftell(pypy_debug_file);
114 }
115
116 void pypy_debug_ensure_opened(void)
117 {
118 if (!debug_ready)
119 pypy_debug_open();
120 }
121
122 void pypy_debug_forked(Signed original_offset)
123 {
124 /* 'original_offset' ignored. It used to be that the forked log
125 files started with this offset printed out, so that we can
126 rebuild the tree structure. That's overkill... */
127 (void)original_offset;
128
129 if (pypy_debug_file)
130 {
131 if (pypy_debug_file != stderr)
132 fclose(pypy_debug_file);
133 pypy_debug_file = NULL;
134 /* if PYPYLOG was set to a name with "%d" in it, it is still
135 alive, and will be reopened with the new subprocess' pid as
136 soon as it logs anything */
137 debug_ready = 0;
138 }
139 }
140
141
142 #ifndef _WIN32
143
144 RPY_EXTERN long long pypy_read_timestamp(void)
145 {
146 # ifdef CLOCK_THREAD_CPUTIME_ID
147 struct timespec tspec;
148 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tspec);
149 return ((long long)tspec.tv_sec) * 1000000000LL + tspec.tv_nsec;
150 # else
151 /* argh, we don't seem to have clock_gettime(). Bad OS. */
152 struct timeval tv;
153 gettimeofday(&tv, NULL);
154 return ((long long)tv.tv_sec) * 1000000LL + tv.tv_usec;
155 # endif
156 }
157 #endif
158
159
160 static unsigned char startswithoneof(const char *str, const char *substr)
161 {
162 /* any([str.startswith(x) for x in substr.split(',')]) */
163 const char *p = str;
164 for (; *substr; substr++)
165 {
166 if (*substr != ',')
167 {
168 if (p && *p++ != *substr)
169 p = NULL; /* mismatch */
170 }
171 else if (p != NULL)
172 return 1; /* match */
173 else
174 p = str; /* mismatched, retry with the next */
175 }
176 return p != NULL;
177 }
178
179 static long oneofstartswith(const char *str, const char *substr)
180 {
181 /* any([x.startswith(substr) for x in str.split(',')]) */
182 const char *p = substr;
183 for (; *str; str++) {
184 if (p) {
185 if (*p++ != *str)
186 p = NULL; /* mismatch */
187 else if (*p == '\0')
188 return 1; /* full substring match */
189 }
190 if (*str == ',')
191 p = substr; /* restart looking */
192 }
193 return 0;
194 }
195
196 #if defined(_MSC_VER) || defined(__MINGW32__)
197 #define PYPY_LONG_LONG_PRINTF_FORMAT "I64"
198 #else
199 #define PYPY_LONG_LONG_PRINTF_FORMAT "ll"
200 #endif
201
202 static long long display_startstop(const char *prefix, const char *postfix,
203 const char *category, const char *colors)
204 {
205 long long timestamp;
206 READ_TIMESTAMP(timestamp);
207 fprintf(pypy_debug_file, "%s[%"PYPY_LONG_LONG_PRINTF_FORMAT"x] %s%s%s\n%s",
208 colors,
209 timestamp, prefix, category, postfix,
210 debug_stop_colors);
211 return timestamp;
212 }
213
214 long long pypy_debug_start(const char *category, Signed timestamp)
215 {
216 long long result = 42;
217 pypy_debug_ensure_opened();
218 /* Enter a nesting level. Nested debug_prints are disabled by default
219 because the following left shift introduces a 0 in the last bit.
220 Note that this logic assumes that we are never going to nest
221 debug_starts more than 31 levels (63 on 64-bits). */
222 pypy_have_debug_prints <<= 1;
223 if (!debug_profile)
224 {
225 /* non-profiling version */
226 if (!debug_prefix || !startswithoneof(category, debug_prefix))
227 {
228 /* wrong section name, or no PYPYLOG at all, skip it */
229 if (timestamp)
230 READ_TIMESTAMP(result);
231 return result;
232 }
233 /* else make this subsection active */
234 pypy_have_debug_prints |= 1;
235 }
236 return display_startstop("{", "", category, debug_start_colors_1);
237 }
238
239 long long pypy_debug_stop(const char *category, Signed timestamp)
240 {
241 long long result = 42;
242 if (debug_profile | (pypy_have_debug_prints & 1))
243 {
244 result = display_startstop("", "}", category, debug_start_colors_2);
245 fflush(pypy_debug_file);
246 }
247 else if (timestamp)
248 {
249 READ_TIMESTAMP(result);
250 }
251 pypy_have_debug_prints >>= 1;
252 return result;
253 }
254
255 long pypy_have_debug_prints_for(const char *category_prefix)
256 {
257 pypy_debug_ensure_opened();
258 return (!debug_profile && debug_prefix &&
259 /* if 'PYPYLOG=abc,xyz:-' and prefix=="ab", then return 1 */
260 (oneofstartswith(debug_prefix, category_prefix) ||
261 /* if prefix=="abcdef" and 'PYPYLOG=abc,xyz:-' then return 1 */
262 startswithoneof(category_prefix, debug_prefix)));
263 }