1 /****************************************************************************
2 * Copyright 2018-2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
4 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 * and: Juergen Pfeifer *
35 ****************************************************************************/
36
37 /*
38 * lib_trace.c - Tracing/Debugging routines
39 *
40 * The _tracef() function is originally from pcurses (by Pavel Curtis) in 1982.
41 * pcurses allowed one to enable/disable tracing using traceon() and traceoff()
42 * functions. ncurses provides a trace() function which allows one to
43 * selectively enable or disable several tracing features.
44 */
45
46 #include <curses.priv.h>
47 #include <tic.h>
48
49 #include <ctype.h>
50
51 MODULE_ID("$Id: lib_trace.c,v 1.99 2021/06/26 20:44:59 tom Exp $")
52
53 NCURSES_EXPORT_VAR(unsigned) _nc_tracing = 0; /* always define this */
54
55 #ifdef TRACE
56
57 #if USE_REENTRANT
58 NCURSES_EXPORT(const char *)
NCURSES_PUBLIC_VAR(_nc_tputs_trace)59 NCURSES_PUBLIC_VAR(_nc_tputs_trace) (void)
60 {
61 return CURRENT_SCREEN ? CURRENT_SCREEN->_tputs_trace : _nc_prescreen._tputs_trace;
62 }
63 NCURSES_EXPORT(long)
NCURSES_PUBLIC_VAR(_nc_outchars)64 NCURSES_PUBLIC_VAR(_nc_outchars) (void)
65 {
66 return CURRENT_SCREEN ? CURRENT_SCREEN->_outchars : _nc_prescreen._outchars;
67 }
68 NCURSES_EXPORT(void)
_nc_set_tputs_trace(const char * s)69 _nc_set_tputs_trace(const char *s)
70 {
71 if (CURRENT_SCREEN)
72 CURRENT_SCREEN->_tputs_trace = s;
73 else
74 _nc_prescreen._tputs_trace = s;
75 }
76 NCURSES_EXPORT(void)
_nc_count_outchars(long increment)77 _nc_count_outchars(long increment)
78 {
79 if (CURRENT_SCREEN)
80 CURRENT_SCREEN->_outchars += increment;
81 else
82 _nc_prescreen._outchars += increment;
83 }
84 #else
85 NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace = "";
86 NCURSES_EXPORT_VAR(long) _nc_outchars = 0;
87 #endif
88
89 #define MyFP _nc_globals.trace_fp
90 #define MyFD _nc_globals.trace_fd
91 #define MyInit _nc_globals.trace_opened
92 #define MyPath _nc_globals.trace_fname
93 #define MyLevel _nc_globals.trace_level
94 #define MyNested _nc_globals.nested_tracef
95 #endif /* TRACE */
96
97 NCURSES_EXPORT(unsigned)
curses_trace(unsigned tracelevel)98 curses_trace(unsigned tracelevel)
99 {
100 unsigned result;
101 #if defined(TRACE)
102 result = _nc_tracing;
103 if ((MyFP == 0) && tracelevel) {
104 MyInit = TRUE;
105 if (MyFD >= 0) {
106 MyFP = fdopen(MyFD, BIN_W);
107 } else {
108 if (MyPath[0] == '\0') {
109 size_t size = sizeof(MyPath) - 12;
110 if (getcwd(MyPath, size) == 0) {
111 perror("curses: Can't get working directory");
112 exit(EXIT_FAILURE);
113 }
114 MyPath[size] = '\0';
115 assert(strlen(MyPath) <= size);
116 _nc_STRCAT(MyPath, "/trace", sizeof(MyPath));
117 if (_nc_is_dir_path(MyPath)) {
118 _nc_STRCAT(MyPath, ".log", sizeof(MyPath));
119 }
120 }
121 #define SAFE_MODE (O_CREAT | O_EXCL | O_RDWR)
122 if (_nc_access(MyPath, W_OK) < 0
123 || (MyFD = safe_open3(MyPath, SAFE_MODE, 0600)) < 0
124 || (MyFP = fdopen(MyFD, BIN_W)) == 0) {
125 ; /* EMPTY */
126 }
127 }
128 _nc_tracing = tracelevel;
129 /* Try to set line-buffered mode, or (failing that) unbuffered,
130 * so that the trace-output gets flushed automatically at the
131 * end of each line. This is useful in case the program dies.
132 */
133 if (MyFP != 0) {
134 #if HAVE_SETVBUF /* ANSI */
135 (void) setvbuf(MyFP, (char *) 0, _IOLBF, (size_t) 0);
136 #elif HAVE_SETBUF /* POSIX */
137 (void) setbuffer(MyFP, (char *) 0);
138 #endif
139 }
140 _tracef("TRACING NCURSES version %s.%d (tracelevel=%#x)",
141 NCURSES_VERSION,
142 NCURSES_VERSION_PATCH,
143 tracelevel);
144 } else if (tracelevel == 0) {
145 if (MyFP != 0) {
146 MyFD = dup(MyFD); /* allow reopen of same file */
147 fclose(MyFP);
148 MyFP = 0;
149 }
150 _nc_tracing = tracelevel;
151 } else if (_nc_tracing != tracelevel) {
152 _nc_tracing = tracelevel;
153 _tracef("tracelevel=%#x", tracelevel);
154 }
155 #else
156 (void) tracelevel;
157 result = 0;
158 #endif
159 return result;
160 }
161
162 #if defined(TRACE)
163 NCURSES_EXPORT(void)
trace(const unsigned int tracelevel)164 trace(const unsigned int tracelevel)
165 {
166 curses_trace(tracelevel);
167 }
168
169 static void
_nc_va_tracef(const char * fmt,va_list ap)170 _nc_va_tracef(const char *fmt, va_list ap)
171 {
172 static const char Called[] = T_CALLED("");
173 static const char Return[] = T_RETURN("");
174
175 bool before = FALSE;
176 bool after = FALSE;
177 unsigned doit = _nc_tracing;
178 int save_err = errno;
179 FILE *fp = MyFP;
180
181 #ifdef TRACE
182 /* verbose-trace in the command-line utilities relies on this */
183 if (fp == 0 && !MyInit && _nc_tracing >= DEBUG_LEVEL(1))
184 fp = stderr;
185 #endif
186
187 if (strlen(fmt) >= sizeof(Called) - 1) {
188 if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
189 before = TRUE;
190 MyLevel++;
191 } else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
192 after = TRUE;
193 }
194 if (before || after) {
195 if ((MyLevel <= 1)
196 || (doit & TRACE_ICALLS) != 0)
197 doit &= (TRACE_CALLS | TRACE_CCALLS);
198 else
199 doit = 0;
200 }
201 }
202
203 if (doit != 0 && fp != 0) {
204 #ifdef USE_PTHREADS
205 /*
206 * TRACE_ICALLS is "really" needed to show normal use with threaded
207 * applications, since anything can be running during a napms(),
208 * making it appear in the hierarchical trace as it other functions
209 * are being called.
210 *
211 * Rather than add the complication of a per-thread stack, just
212 * show the thread-id in each line of the trace.
213 */
214 # if USE_WEAK_SYMBOLS
215 if ((pthread_self))
216 # endif
217 #ifdef _NC_WINDOWS
218 fprintf(fp, "%#lx:", (long) (intptr_t) pthread_self().p);
219 #else
220 fprintf(fp, "%#lx:", (long) (intptr_t) pthread_self());
221 #endif
222 #endif
223 if (before || after) {
224 int n;
225 for (n = 1; n < MyLevel; n++)
226 fputs("+ ", fp);
227 }
228 vfprintf(fp, fmt, ap);
229 fputc('\n', fp);
230 fflush(fp);
231 }
232
233 if (after && MyLevel)
234 MyLevel--;
235
236 errno = save_err;
237 }
238
239 NCURSES_EXPORT(void)
_tracef(const char * fmt,...)240 _tracef(const char *fmt, ...)
241 {
242 va_list ap;
243
244 va_start(ap, fmt);
245 _nc_va_tracef(fmt, ap);
246 va_end(ap);
247 }
248
249 /* Trace 'bool' return-values */
250 NCURSES_EXPORT(NCURSES_BOOL)
_nc_retrace_bool(int code)251 _nc_retrace_bool(int code)
252 {
253 T((T_RETURN("%s"), code ? "TRUE" : "FALSE"));
254 return code;
255 }
256
257 /* Trace 'char' return-values */
258 NCURSES_EXPORT(char)
_nc_retrace_char(int code)259 _nc_retrace_char(int code)
260 {
261 T((T_RETURN("%c"), code));
262 return (char) code;
263 }
264
265 /* Trace 'int' return-values */
266 NCURSES_EXPORT(int)
_nc_retrace_int(int code)267 _nc_retrace_int(int code)
268 {
269 T((T_RETURN("%d"), code));
270 return code;
271 }
272
273 /* Trace 'unsigned' return-values */
274 NCURSES_EXPORT(unsigned)
_nc_retrace_unsigned(unsigned code)275 _nc_retrace_unsigned(unsigned code)
276 {
277 T((T_RETURN("%#x"), code));
278 return code;
279 }
280
281 /* Trace 'char*' return-values */
282 NCURSES_EXPORT(char *)
_nc_retrace_ptr(char * code)283 _nc_retrace_ptr(char *code)
284 {
285 T((T_RETURN("%s"), _nc_visbuf(code)));
286 return code;
287 }
288
289 /* Trace 'const char*' return-values */
290 NCURSES_EXPORT(const char *)
_nc_retrace_cptr(const char * code)291 _nc_retrace_cptr(const char *code)
292 {
293 T((T_RETURN("%s"), _nc_visbuf(code)));
294 return code;
295 }
296
297 /* Trace 'NCURSES_CONST void*' return-values */
298 NCURSES_EXPORT(NCURSES_CONST void *)
_nc_retrace_cvoid_ptr(NCURSES_CONST void * code)299 _nc_retrace_cvoid_ptr(NCURSES_CONST void *code)
300 {
301 T((T_RETURN("%p"), code));
302 return code;
303 }
304
305 /* Trace 'void*' return-values */
306 NCURSES_EXPORT(void *)
_nc_retrace_void_ptr(void * code)307 _nc_retrace_void_ptr(void *code)
308 {
309 T((T_RETURN("%p"), code));
310 return code;
311 }
312
313 /* Trace 'SCREEN *' return-values */
314 NCURSES_EXPORT(SCREEN *)
_nc_retrace_sp(SCREEN * code)315 _nc_retrace_sp(SCREEN *code)
316 {
317 T((T_RETURN("%p"), (void *) code));
318 return code;
319 }
320
321 /* Trace 'WINDOW *' return-values */
322 NCURSES_EXPORT(WINDOW *)
_nc_retrace_win(WINDOW * code)323 _nc_retrace_win(WINDOW *code)
324 {
325 T((T_RETURN("%p"), (void *) code));
326 return code;
327 }
328
329 NCURSES_EXPORT(char *)
_nc_fmt_funcptr(char * target,const char * source,size_t size)330 _nc_fmt_funcptr(char *target, const char *source, size_t size)
331 {
332 size_t n;
333 char *dst = target;
334 bool leading = TRUE;
335
336 union {
337 int value;
338 char bytes[sizeof(int)];
339 } byteorder;
340
341 byteorder.value = 0x1234;
342
343 *dst++ = '0';
344 *dst++ = 'x';
345
346 for (n = 0; n < size; ++n) {
347 unsigned ch = ((byteorder.bytes[0] == 0x34)
348 ? UChar(source[size - n - 1])
349 : UChar(source[n]));
350 if (ch != 0 || (n + 1) >= size)
351 leading = FALSE;
352 if (!leading) {
353 _nc_SPRINTF(dst, _nc_SLIMIT(TR_FUNC_LEN - (size_t) (dst - target))
354 "%02x", ch & 0xff);
355 dst += 2;
356 }
357 }
358 *dst = '\0';
359 return target;
360 }
361
362 #if USE_REENTRANT
363 /*
364 * Check if the given trace-mask is enabled.
365 *
366 * This function may be called from within one of the functions that fills
367 * in parameters for _tracef(), but in that case we do not want to lock the
368 * mutex, since it is already locked.
369 */
370 NCURSES_EXPORT(int)
_nc_use_tracef(unsigned mask)371 _nc_use_tracef(unsigned mask)
372 {
373 bool result = FALSE;
374
375 _nc_lock_global(tst_tracef);
376 if (!MyNested++) {
377 if ((result = (_nc_tracing & (mask))) != 0
378 && _nc_try_global(tracef) == 0) {
379 /* we will call _nc_locked_tracef(), no nesting so far */
380 } else {
381 /* we will not call _nc_locked_tracef() */
382 MyNested = 0;
383 }
384 } else {
385 /* we may call _nc_locked_tracef(), but with nested_tracef > 0 */
386 result = (_nc_tracing & (mask));
387 }
388 _nc_unlock_global(tst_tracef);
389 return result;
390 }
391
392 /*
393 * We call this if _nc_use_tracef() returns true, which means we must unlock
394 * the tracef mutex.
395 */
396 NCURSES_EXPORT(void)
_nc_locked_tracef(const char * fmt,...)397 _nc_locked_tracef(const char *fmt, ...)
398 {
399 va_list ap;
400
401 va_start(ap, fmt);
402 _nc_va_tracef(fmt, ap);
403 va_end(ap);
404
405 if (--(MyNested) == 0) {
406 _nc_unlock_global(tracef);
407 }
408 }
409 #endif /* USE_REENTRANT */
410
411 #endif /* TRACE */
412