1 /****************************************************************************
2 * Copyright 2018-2019,2020 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
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 ****************************************************************************/
38 * lib_trace.c - Tracing/Debugging routines
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.
46 #include <curses.priv.h>
51 MODULE_ID("$Id: lib_trace.c,v 1.97 2020/08/29 16:22:03 juergen Exp $")
53 NCURSES_EXPORT_VAR(unsigned) _nc_tracing = 0; /* always define this */
58 NCURSES_EXPORT(const char *)
59 NCURSES_PUBLIC_VAR(_nc_tputs_trace) (void)
61 return CURRENT_SCREEN ? CURRENT_SCREEN->_tputs_trace : _nc_prescreen._tputs_trace;
64 NCURSES_PUBLIC_VAR(_nc_outchars) (void)
66 return CURRENT_SCREEN ? CURRENT_SCREEN->_outchars : _nc_prescreen._outchars;
69 _nc_set_tputs_trace(const char *s)
72 CURRENT_SCREEN->_tputs_trace = s;
74 _nc_prescreen._tputs_trace = s;
77 _nc_count_outchars(long increment)
80 CURRENT_SCREEN->_outchars += increment;
82 _nc_prescreen._outchars += increment;
85 NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace = "";
86 NCURSES_EXPORT_VAR(long) _nc_outchars = 0;
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
97 NCURSES_EXPORT(unsigned)
98 curses_trace(unsigned tracelevel)
102 result = _nc_tracing;
103 if ((MyFP == 0) && tracelevel) {
106 MyFP = fdopen(MyFD, BIN_W);
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");
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));
121 if (_nc_access(MyPath, W_OK) < 0
122 || (MyFD = open(MyPath, O_CREAT | O_EXCL | O_RDWR, 0600)) < 0
123 || (MyFP = fdopen(MyFD, BIN_W)) == 0) {
127 _nc_tracing = tracelevel;
128 /* Try to set line-buffered mode, or (failing that) unbuffered,
129 * so that the trace-output gets flushed automatically at the
130 * end of each line. This is useful in case the program dies.
133 #if HAVE_SETVBUF /* ANSI */
134 (void) setvbuf(MyFP, (char *) 0, _IOLBF, (size_t) 0);
135 #elif HAVE_SETBUF /* POSIX */
136 (void) setbuffer(MyFP, (char *) 0);
139 _tracef("TRACING NCURSES version %s.%d (tracelevel=%#x)",
141 NCURSES_VERSION_PATCH,
143 } else if (tracelevel == 0) {
145 MyFD = dup(MyFD); /* allow reopen of same file */
149 _nc_tracing = tracelevel;
150 } else if (_nc_tracing != tracelevel) {
151 _nc_tracing = tracelevel;
152 _tracef("tracelevel=%#x", tracelevel);
163 trace(const unsigned int tracelevel)
165 curses_trace(tracelevel);
169 _nc_va_tracef(const char *fmt, va_list ap)
171 static const char Called[] = T_CALLED("");
172 static const char Return[] = T_RETURN("");
176 unsigned doit = _nc_tracing;
177 int save_err = errno;
181 /* verbose-trace in the command-line utilities relies on this */
182 if (fp == 0 && !MyInit && _nc_tracing >= DEBUG_LEVEL(1))
186 if (strlen(fmt) >= sizeof(Called) - 1) {
187 if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
190 } else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
193 if (before || after) {
195 || (doit & TRACE_ICALLS) != 0)
196 doit &= (TRACE_CALLS | TRACE_CCALLS);
202 if (doit != 0 && fp != 0) {
205 * TRACE_ICALLS is "really" needed to show normal use with threaded
206 * applications, since anything can be running during a napms(),
207 * making it appear in the hierarchical trace as it other functions
210 * Rather than add the complication of a per-thread stack, just
211 * show the thread-id in each line of the trace.
213 # if USE_WEAK_SYMBOLS
217 fprintf(fp, "%#lx:", (long) (intptr_t) pthread_self().p);
219 fprintf(fp, "%#lx:", (long) (intptr_t) pthread_self());
222 if (before || after) {
224 for (n = 1; n < MyLevel; n++)
227 vfprintf(fp, fmt, ap);
232 if (after && MyLevel)
239 _tracef(const char *fmt, ...)
244 _nc_va_tracef(fmt, ap);
248 /* Trace 'bool' return-values */
249 NCURSES_EXPORT(NCURSES_BOOL)
250 _nc_retrace_bool(int code)
252 T((T_RETURN("%s"), code ? "TRUE" : "FALSE"));
256 /* Trace 'char' return-values */
258 _nc_retrace_char(int code)
260 T((T_RETURN("%c"), code));
264 /* Trace 'int' return-values */
266 _nc_retrace_int(int code)
268 T((T_RETURN("%d"), code));
272 /* Trace 'unsigned' return-values */
273 NCURSES_EXPORT(unsigned)
274 _nc_retrace_unsigned(unsigned code)
276 T((T_RETURN("%#x"), code));
280 /* Trace 'char*' return-values */
281 NCURSES_EXPORT(char *)
282 _nc_retrace_ptr(char *code)
284 T((T_RETURN("%s"), _nc_visbuf(code)));
288 /* Trace 'const char*' return-values */
289 NCURSES_EXPORT(const char *)
290 _nc_retrace_cptr(const char *code)
292 T((T_RETURN("%s"), _nc_visbuf(code)));
296 /* Trace 'NCURSES_CONST void*' return-values */
297 NCURSES_EXPORT(NCURSES_CONST void *)
298 _nc_retrace_cvoid_ptr(NCURSES_CONST void *code)
300 T((T_RETURN("%p"), code));
304 /* Trace 'void*' return-values */
305 NCURSES_EXPORT(void *)
306 _nc_retrace_void_ptr(void *code)
308 T((T_RETURN("%p"), code));
312 /* Trace 'SCREEN *' return-values */
313 NCURSES_EXPORT(SCREEN *)
314 _nc_retrace_sp(SCREEN *code)
316 T((T_RETURN("%p"), (void *) code));
320 /* Trace 'WINDOW *' return-values */
321 NCURSES_EXPORT(WINDOW *)
322 _nc_retrace_win(WINDOW *code)
324 T((T_RETURN("%p"), (void *) code));
328 NCURSES_EXPORT(char *)
329 _nc_fmt_funcptr(char *target, const char *source, size_t size)
337 char bytes[sizeof(int)];
340 byteorder.value = 0x1234;
345 for (n = 0; n < size; ++n) {
346 unsigned ch = ((byteorder.bytes[0] == 0x34)
347 ? UChar(source[size - n - 1])
349 if (ch != 0 || (n + 1) >= size)
352 _nc_SPRINTF(dst, _nc_SLIMIT(TR_FUNC_LEN - (size_t) (dst - target))
363 * Check if the given trace-mask is enabled.
365 * This function may be called from within one of the functions that fills
366 * in parameters for _tracef(), but in that case we do not want to lock the
367 * mutex, since it is already locked.
370 _nc_use_tracef(unsigned mask)
374 _nc_lock_global(tst_tracef);
376 if ((result = (_nc_tracing & (mask))) != 0
377 && _nc_try_global(tracef) == 0) {
378 /* we will call _nc_locked_tracef(), no nesting so far */
380 /* we will not call _nc_locked_tracef() */
384 /* we may call _nc_locked_tracef(), but with nested_tracef > 0 */
385 result = (_nc_tracing & (mask));
387 _nc_unlock_global(tst_tracef);
392 * We call this if _nc_use_tracef() returns true, which means we must unlock
396 _nc_locked_tracef(const char *fmt, ...)
401 _nc_va_tracef(fmt, ap);
404 if (--(MyNested) == 0) {
405 _nc_unlock_global(tracef);
408 #endif /* USE_REENTRANT */