1 /****************************************************************************
2 * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
36 * lib_trace.c - Tracing/Debugging routines
38 * The _tracef() function is originally from pcurses (by Pavel Curtis) in 1982.
39 * pcurses allowed one to enable/disable tracing using traceon() and traceoff()
40 * functions. ncurses provides a trace() function which allows one to
41 * selectively enable or disable several tracing features.
44 #include <curses.priv.h>
49 MODULE_ID("$Id: lib_trace.c,v 1.66 2008/03/22 16:56:48 tom Exp $")
51 NCURSES_EXPORT_VAR(unsigned) _nc_tracing = 0; /* always define this */
56 NCURSES_EXPORT(const char *)
57 NCURSES_PUBLIC_VAR(_nc_tputs_trace) (void)
59 return SP ? SP->_tputs_trace : _nc_prescreen._tputs_trace;
62 NCURSES_PUBLIC_VAR(_nc_outchars) (void)
64 return SP ? SP->_outchars : _nc_prescreen._outchars;
67 _nc_set_tputs_trace(const char *s)
72 _nc_prescreen._tputs_trace = s;
75 _nc_count_outchars(long increment)
78 SP->_outchars += increment;
80 _nc_prescreen._outchars += increment;
83 NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace = "";
84 NCURSES_EXPORT_VAR(long) _nc_outchars = 0;
87 #define TraceFP _nc_globals.trace_fp
88 #define TracePath _nc_globals.trace_fname
89 #define TraceLevel _nc_globals.trace_level
92 trace(const unsigned int tracelevel)
94 if ((TraceFP == 0) && tracelevel) {
95 const char *mode = _nc_globals.init_trace ? "ab" : "wb";
97 if (TracePath[0] == '\0') {
98 if (getcwd(TracePath, sizeof(TracePath) - 12) == 0) {
99 perror("curses: Can't get working directory");
102 strcat(TracePath, "/trace");
103 if (_nc_is_dir_path(TracePath)) {
104 strcat(TracePath, ".log");
108 _nc_globals.init_trace = TRUE;
109 _nc_tracing = tracelevel;
110 if (_nc_access(TracePath, W_OK) < 0
111 || (TraceFP = fopen(TracePath, mode)) == 0) {
112 perror("curses: Can't open 'trace' file");
115 /* Try to set line-buffered mode, or (failing that) unbuffered,
116 * so that the trace-output gets flushed automatically at the
117 * end of each line. This is useful in case the program dies.
119 #if HAVE_SETVBUF /* ANSI */
120 (void) setvbuf(TraceFP, (char *) 0, _IOLBF, 0);
121 #elif HAVE_SETBUF /* POSIX */
122 (void) setbuffer(TraceFP, (char *) 0);
124 _tracef("TRACING NCURSES version %s.%d (tracelevel=%#x)",
126 NCURSES_VERSION_PATCH,
128 } else if (tracelevel == 0) {
133 _nc_tracing = tracelevel;
134 } else if (_nc_tracing != tracelevel) {
135 _nc_tracing = tracelevel;
136 _tracef("tracelevel=%#x", tracelevel);
141 _nc_va_tracef(const char *fmt, va_list ap)
143 static const char Called[] = T_CALLED("");
144 static const char Return[] = T_RETURN("");
148 unsigned doit = _nc_tracing;
149 int save_err = errno;
151 if (strlen(fmt) >= sizeof(Called) - 1) {
152 if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
155 } else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
158 if (before || after) {
159 if ((TraceLevel <= 1)
160 || (doit & TRACE_ICALLS) != 0)
161 doit &= (TRACE_CALLS | TRACE_CCALLS);
172 * TRACE_ICALLS is "really" needed to show normal use with threaded
173 * applications, since anything can be running during a napms(),
174 * making it appear in the hierarchical trace as it other functions
177 * Rather than add the complication of a per-thread stack, just
178 * show the thread-id in each line of the trace.
180 fprintf(TraceFP, "%#lx:", (long) pthread_self());
182 if (before || after) {
184 for (n = 1; n < TraceLevel; n++)
185 fputs("+ ", TraceFP);
187 vfprintf(TraceFP, fmt, ap);
188 fputc('\n', TraceFP);
192 if (after && TraceLevel)
199 _tracef(const char *fmt,...)
204 _nc_va_tracef(fmt, ap);
208 /* Trace 'bool' return-values */
209 NCURSES_EXPORT(NCURSES_BOOL)
210 _nc_retrace_bool(NCURSES_BOOL code)
212 T((T_RETURN("%s"), code ? "TRUE" : "FALSE"));
216 /* Trace 'int' return-values */
218 _nc_retrace_int(int code)
220 T((T_RETURN("%d"), code));
224 /* Trace 'unsigned' return-values */
225 NCURSES_EXPORT(unsigned)
226 _nc_retrace_unsigned(unsigned code)
228 T((T_RETURN("%#x"), code));
232 /* Trace 'char*' return-values */
233 NCURSES_EXPORT(char *)
234 _nc_retrace_ptr(char *code)
236 T((T_RETURN("%s"), _nc_visbuf(code)));
240 /* Trace 'const char*' return-values */
241 NCURSES_EXPORT(const char *)
242 _nc_retrace_cptr(const char *code)
244 T((T_RETURN("%s"), _nc_visbuf(code)));
248 /* Trace 'NCURSES_CONST void*' return-values */
249 NCURSES_EXPORT(NCURSES_CONST void *)
250 _nc_retrace_cvoid_ptr(NCURSES_CONST void *code)
252 T((T_RETURN("%p"), code));
256 /* Trace 'void*' return-values */
257 NCURSES_EXPORT(void *)
258 _nc_retrace_void_ptr(void *code)
260 T((T_RETURN("%p"), code));
264 /* Trace 'SCREEN *' return-values */
265 NCURSES_EXPORT(SCREEN *)
266 _nc_retrace_sp(SCREEN *code)
268 T((T_RETURN("%p"), code));
272 /* Trace 'WINDOW *' return-values */
273 NCURSES_EXPORT(WINDOW *)
274 _nc_retrace_win(WINDOW *code)
276 T((T_RETURN("%p"), code));
282 * Check if the given trace-mask is enabled.
284 * This function may be called from within one of the functions that fills
285 * in parameters for _tracef(), but in that case we do not want to lock the
286 * mutex, since it is already locked.
289 _nc_use_tracef(unsigned mask)
293 _nc_lock_global(tst_tracef);
294 if (!_nc_globals.nested_tracef++) {
295 if ((result = (_nc_tracing & (mask))) != 0) {
296 /* we will call _nc_locked_tracef(), no nesting so far */
297 _nc_lock_global(tracef);
299 /* we will not call _nc_locked_tracef() */
300 _nc_globals.nested_tracef = 0;
303 /* we may call _nc_locked_tracef(), but with nested_tracef > 0 */
304 result = (_nc_tracing & (mask));
306 _nc_unlock_global(tst_tracef);
311 * We call this if _nc_use_tracef() returns true, which means we must unlock
315 _nc_locked_tracef(const char *fmt,...)
320 _nc_va_tracef(fmt, ap);
323 if (--(_nc_globals.nested_tracef) == 0)
324 _nc_unlock_global(tracef);
326 #endif /* USE_REENTRANT */