blob: 0904c132fee12d85c0fec9687e529f6d78191055 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2018-2023,2024 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
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 *
Steve Kondikae271bc2015-11-15 02:50:53 +010034 * and: Juergen Pfeifer *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053035 ****************************************************************************/
36
37/*
38 * lib_trace.c - Tracing/Debugging routines
39 *
micky3879b9f5e72025-07-08 18:04:53 -040040 * The _tracef() function is originally from pcurses (by Pavel Curtis) in 1982.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053041 * 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
micky3879b9f5e72025-07-08 18:04:53 -040051MODULE_ID("$Id: lib_trace.c,v 1.106 2024/02/24 18:28:19 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053052
53NCURSES_EXPORT_VAR(unsigned) _nc_tracing = 0; /* always define this */
54
55#ifdef TRACE
56
57#if USE_REENTRANT
58NCURSES_EXPORT(const char *)
59NCURSES_PUBLIC_VAR(_nc_tputs_trace) (void)
60{
Steve Kondikae271bc2015-11-15 02:50:53 +010061 return CURRENT_SCREEN ? CURRENT_SCREEN->_tputs_trace : _nc_prescreen._tputs_trace;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053062}
63NCURSES_EXPORT(long)
64NCURSES_PUBLIC_VAR(_nc_outchars) (void)
65{
Steve Kondikae271bc2015-11-15 02:50:53 +010066 return CURRENT_SCREEN ? CURRENT_SCREEN->_outchars : _nc_prescreen._outchars;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053067}
68NCURSES_EXPORT(void)
69_nc_set_tputs_trace(const char *s)
70{
Steve Kondikae271bc2015-11-15 02:50:53 +010071 if (CURRENT_SCREEN)
72 CURRENT_SCREEN->_tputs_trace = s;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053073 else
74 _nc_prescreen._tputs_trace = s;
75}
76NCURSES_EXPORT(void)
77_nc_count_outchars(long increment)
78{
Steve Kondikae271bc2015-11-15 02:50:53 +010079 if (CURRENT_SCREEN)
80 CURRENT_SCREEN->_outchars += increment;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053081 else
82 _nc_prescreen._outchars += increment;
83}
84#else
85NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace = "";
86NCURSES_EXPORT_VAR(long) _nc_outchars = 0;
87#endif
88
micky3879b9f5e72025-07-08 18:04:53 -040089#define MyFP _nc_globals.trace_fp
90#define MyFD _nc_globals.trace_fd
91#define MyInit _nc_globals.trace_opened
92#define MyLevel _nc_globals.trace_level
93#define MyNested _nc_globals.nested_tracef
94#endif /* TRACE */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053095
micky3879b9f5e72025-07-08 18:04:53 -040096#if USE_REENTRANT
97#define Locked(statement) \
98 do { \
99 _nc_lock_global(tst_tracef); \
100 statement; \
101 _nc_unlock_global(tst_tracef); \
102 } while (0)
103#else
104#define Locked(statement) statement
105#endif
106
107NCURSES_EXPORT(unsigned)
108curses_trace(unsigned tracelevel)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530109{
micky3879b9f5e72025-07-08 18:04:53 -0400110 unsigned result;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530111
micky3879b9f5e72025-07-08 18:04:53 -0400112#if defined(TRACE)
113 int bit;
114
115#define DATA(name) { name, #name }
116 static struct {
117 unsigned mask;
118 const char *name;
119 } trace_names[] = {
120 DATA(TRACE_TIMES),
121 DATA(TRACE_TPUTS),
122 DATA(TRACE_UPDATE),
123 DATA(TRACE_MOVE),
124 DATA(TRACE_CHARPUT),
125 DATA(TRACE_CALLS),
126 DATA(TRACE_VIRTPUT),
127 DATA(TRACE_IEVENT),
128 DATA(TRACE_BITS),
129 DATA(TRACE_ICALLS),
130 DATA(TRACE_CCALLS),
131 DATA(TRACE_DATABASE),
132 DATA(TRACE_ATTRS)
133 };
134#undef DATA
135
136 Locked(result = _nc_tracing);
137
138 if ((MyFP == 0) && tracelevel) {
139 MyInit = TRUE;
140 if (MyFD >= 0) {
141 MyFP = fdopen(MyFD, BIN_W);
142 } else {
143 char myFile[80];
144
145 _nc_STRCPY(myFile, "trace", sizeof(myFile));
146 if (_nc_is_dir_path(myFile)) {
147 _nc_STRCAT(myFile, ".log", sizeof(myFile));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530148 }
micky3879b9f5e72025-07-08 18:04:53 -0400149#define SAFE_MODE (O_CREAT | O_EXCL | O_RDWR)
150 if (_nc_access(myFile, W_OK) < 0
151 || (MyFD = safe_open3(myFile, SAFE_MODE, 0600)) < 0
152 || (MyFP = fdopen(MyFD, BIN_W)) == 0) {
153 ; /* EMPTY */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530154 }
155 }
micky3879b9f5e72025-07-08 18:04:53 -0400156 Locked(_nc_tracing = tracelevel);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530157 /* Try to set line-buffered mode, or (failing that) unbuffered,
158 * so that the trace-output gets flushed automatically at the
micky3879b9f5e72025-07-08 18:04:53 -0400159 * end of each line. This is useful in case the program dies.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530160 */
micky3879b9f5e72025-07-08 18:04:53 -0400161 if (MyFP != 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530162#if HAVE_SETVBUF /* ANSI */
micky3879b9f5e72025-07-08 18:04:53 -0400163 (void) setvbuf(MyFP, (char *) 0, _IOLBF, (size_t) 0);
Steve Kondikae271bc2015-11-15 02:50:53 +0100164#elif HAVE_SETBUF /* POSIX */
micky3879b9f5e72025-07-08 18:04:53 -0400165 (void) setbuffer(MyFP, (char *) 0);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530166#endif
micky3879b9f5e72025-07-08 18:04:53 -0400167 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530168 _tracef("TRACING NCURSES version %s.%d (tracelevel=%#x)",
169 NCURSES_VERSION,
170 NCURSES_VERSION_PATCH,
171 tracelevel);
micky3879b9f5e72025-07-08 18:04:53 -0400172
173#define SPECIAL_MASK(mask) \
174 if ((tracelevel & mask) == mask) \
175 _tracef("- %s (%u)", #mask, mask)
176
177 for (bit = 0; bit < TRACE_SHIFT; ++bit) {
178 unsigned mask = (1U << bit) & tracelevel;
179 if ((mask & trace_names[bit].mask) != 0) {
180 _tracef("- %s (%u)", trace_names[bit].name, mask);
181 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530182 }
micky3879b9f5e72025-07-08 18:04:53 -0400183 SPECIAL_MASK(TRACE_MAXIMUM);
184 else
185 SPECIAL_MASK(TRACE_ORDINARY);
186
187 if (tracelevel > TRACE_MAXIMUM) {
188 _tracef("- DEBUG_LEVEL(%u)", tracelevel >> TRACE_SHIFT);
189 }
190 } else if (tracelevel == 0) {
191 if (MyFP != 0) {
192 MyFD = dup(MyFD); /* allow reopen of same file */
193 fclose(MyFP);
194 MyFP = 0;
195 }
196 Locked(_nc_tracing = tracelevel);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530197 } else if (_nc_tracing != tracelevel) {
micky3879b9f5e72025-07-08 18:04:53 -0400198 Locked(_nc_tracing = tracelevel);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530199 _tracef("tracelevel=%#x", tracelevel);
200 }
micky3879b9f5e72025-07-08 18:04:53 -0400201#else
202 (void) tracelevel;
203 result = 0;
204#endif
205 return result;
206}
207
208#if defined(TRACE)
209NCURSES_EXPORT(void)
210trace(const unsigned int tracelevel)
211{
212 curses_trace(tracelevel);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530213}
214
215static void
216_nc_va_tracef(const char *fmt, va_list ap)
217{
218 static const char Called[] = T_CALLED("");
219 static const char Return[] = T_RETURN("");
220
221 bool before = FALSE;
222 bool after = FALSE;
223 unsigned doit = _nc_tracing;
224 int save_err = errno;
micky3879b9f5e72025-07-08 18:04:53 -0400225 FILE *fp = MyFP;
226
227#ifdef TRACE
228 /* verbose-trace in the command-line utilities relies on this */
229 if (fp == 0 && !MyInit && _nc_tracing >= DEBUG_LEVEL(1))
230 fp = stderr;
231#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530232
233 if (strlen(fmt) >= sizeof(Called) - 1) {
234 if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
235 before = TRUE;
micky3879b9f5e72025-07-08 18:04:53 -0400236 MyLevel++;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530237 } else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
238 after = TRUE;
239 }
240 if (before || after) {
micky3879b9f5e72025-07-08 18:04:53 -0400241 if ((MyLevel <= 1)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530242 || (doit & TRACE_ICALLS) != 0)
243 doit &= (TRACE_CALLS | TRACE_CCALLS);
244 else
245 doit = 0;
246 }
247 }
248
micky3879b9f5e72025-07-08 18:04:53 -0400249 if (doit != 0 && fp != 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530250#ifdef USE_PTHREADS
251 /*
252 * TRACE_ICALLS is "really" needed to show normal use with threaded
253 * applications, since anything can be running during a napms(),
254 * making it appear in the hierarchical trace as it other functions
255 * are being called.
256 *
257 * Rather than add the complication of a per-thread stack, just
258 * show the thread-id in each line of the trace.
259 */
260# if USE_WEAK_SYMBOLS
261 if ((pthread_self))
262# endif
micky3879b9f5e72025-07-08 18:04:53 -0400263 fprintf(fp, "%#" PRIxPTR ":",
264#ifdef _NC_WINDOWS
265 CASTxPTR(pthread_self().p)
Steve Kondikae271bc2015-11-15 02:50:53 +0100266#else
micky3879b9f5e72025-07-08 18:04:53 -0400267 CASTxPTR(pthread_self())
Steve Kondikae271bc2015-11-15 02:50:53 +0100268#endif
micky3879b9f5e72025-07-08 18:04:53 -0400269 );
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530270#endif
271 if (before || after) {
272 int n;
micky3879b9f5e72025-07-08 18:04:53 -0400273 for (n = 1; n < MyLevel; n++)
274 fputs("+ ", fp);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530275 }
micky3879b9f5e72025-07-08 18:04:53 -0400276 vfprintf(fp, fmt, ap);
277 fputc('\n', fp);
278 fflush(fp);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530279 }
280
micky3879b9f5e72025-07-08 18:04:53 -0400281 if (after && MyLevel)
282 MyLevel--;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530283
284 errno = save_err;
285}
286
287NCURSES_EXPORT(void)
micky3879b9f5e72025-07-08 18:04:53 -0400288_tracef(const char *fmt, ...)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530289{
290 va_list ap;
291
292 va_start(ap, fmt);
293 _nc_va_tracef(fmt, ap);
294 va_end(ap);
295}
296
297/* Trace 'bool' return-values */
298NCURSES_EXPORT(NCURSES_BOOL)
Steve Kondikae271bc2015-11-15 02:50:53 +0100299_nc_retrace_bool(int code)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530300{
301 T((T_RETURN("%s"), code ? "TRUE" : "FALSE"));
302 return code;
303}
304
Steve Kondikae271bc2015-11-15 02:50:53 +0100305/* Trace 'char' return-values */
306NCURSES_EXPORT(char)
307_nc_retrace_char(int code)
308{
309 T((T_RETURN("%c"), code));
310 return (char) code;
311}
312
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530313/* Trace 'int' return-values */
314NCURSES_EXPORT(int)
315_nc_retrace_int(int code)
316{
317 T((T_RETURN("%d"), code));
318 return code;
319}
320
321/* Trace 'unsigned' return-values */
322NCURSES_EXPORT(unsigned)
323_nc_retrace_unsigned(unsigned code)
324{
325 T((T_RETURN("%#x"), code));
326 return code;
327}
328
329/* Trace 'char*' return-values */
330NCURSES_EXPORT(char *)
331_nc_retrace_ptr(char *code)
332{
333 T((T_RETURN("%s"), _nc_visbuf(code)));
334 return code;
335}
336
337/* Trace 'const char*' return-values */
338NCURSES_EXPORT(const char *)
339_nc_retrace_cptr(const char *code)
340{
341 T((T_RETURN("%s"), _nc_visbuf(code)));
342 return code;
343}
344
345/* Trace 'NCURSES_CONST void*' return-values */
346NCURSES_EXPORT(NCURSES_CONST void *)
347_nc_retrace_cvoid_ptr(NCURSES_CONST void *code)
348{
349 T((T_RETURN("%p"), code));
350 return code;
351}
352
353/* Trace 'void*' return-values */
354NCURSES_EXPORT(void *)
355_nc_retrace_void_ptr(void *code)
356{
357 T((T_RETURN("%p"), code));
358 return code;
359}
360
361/* Trace 'SCREEN *' return-values */
362NCURSES_EXPORT(SCREEN *)
363_nc_retrace_sp(SCREEN *code)
364{
Steve Kondikae271bc2015-11-15 02:50:53 +0100365 T((T_RETURN("%p"), (void *) code));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530366 return code;
367}
368
369/* Trace 'WINDOW *' return-values */
370NCURSES_EXPORT(WINDOW *)
371_nc_retrace_win(WINDOW *code)
372{
Steve Kondikae271bc2015-11-15 02:50:53 +0100373 T((T_RETURN("%p"), (void *) code));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530374 return code;
375}
376
micky3879b9f5e72025-07-08 18:04:53 -0400377NCURSES_EXPORT(char *)
378_nc_fmt_funcptr(char *target, const char *source, size_t size)
379{
380 size_t n;
381 char *dst = target;
382 bool leading = TRUE;
383
384 union {
385 int value;
386 char bytes[sizeof(int)];
387 } byteorder;
388
389 byteorder.value = 0x1234;
390
391 *dst++ = '0';
392 *dst++ = 'x';
393
394 for (n = 0; n < size; ++n) {
395 unsigned ch = ((byteorder.bytes[0] == 0x34)
396 ? UChar(source[size - n - 1])
397 : UChar(source[n]));
398 if (ch != 0 || (n + 1) >= size)
399 leading = FALSE;
400 if (!leading) {
401 _nc_SPRINTF(dst, _nc_SLIMIT(TR_FUNC_LEN - (size_t) (dst - target))
402 "%02x", ch & 0xff);
403 dst += 2;
404 }
405 }
406 *dst = '\0';
407 return target;
408}
409
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530410#if USE_REENTRANT
411/*
412 * Check if the given trace-mask is enabled.
413 *
414 * This function may be called from within one of the functions that fills
415 * in parameters for _tracef(), but in that case we do not want to lock the
416 * mutex, since it is already locked.
417 */
418NCURSES_EXPORT(int)
419_nc_use_tracef(unsigned mask)
420{
421 bool result = FALSE;
422
423 _nc_lock_global(tst_tracef);
micky3879b9f5e72025-07-08 18:04:53 -0400424 if (!MyNested++) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530425 if ((result = (_nc_tracing & (mask))) != 0
426 && _nc_try_global(tracef) == 0) {
427 /* we will call _nc_locked_tracef(), no nesting so far */
428 } else {
429 /* we will not call _nc_locked_tracef() */
micky3879b9f5e72025-07-08 18:04:53 -0400430 MyNested = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530431 }
432 } else {
433 /* we may call _nc_locked_tracef(), but with nested_tracef > 0 */
434 result = (_nc_tracing & (mask));
435 }
436 _nc_unlock_global(tst_tracef);
437 return result;
438}
439
440/*
441 * We call this if _nc_use_tracef() returns true, which means we must unlock
442 * the tracef mutex.
443 */
444NCURSES_EXPORT(void)
micky3879b9f5e72025-07-08 18:04:53 -0400445_nc_locked_tracef(const char *fmt, ...)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530446{
447 va_list ap;
448
449 va_start(ap, fmt);
450 _nc_va_tracef(fmt, ap);
451 va_end(ap);
452
micky3879b9f5e72025-07-08 18:04:53 -0400453 if (--(MyNested) == 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530454 _nc_unlock_global(tracef);
Steve Kondikae271bc2015-11-15 02:50:53 +0100455 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530456}
457#endif /* USE_REENTRANT */
458
459#endif /* TRACE */