]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/trace/lib_trace.c
ncurses 6.0 - patch 20171118
[ncurses.git] / ncurses / trace / lib_trace.c
1 /****************************************************************************
2  * Copyright (c) 1998-2016,2017 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
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  *     and: Juergen Pfeifer                                                 *
34  ****************************************************************************/
35
36 /*
37  *      lib_trace.c - Tracing/Debugging routines
38  *
39  * The _tracef() function is originally from pcurses (by Pavel Curtis) in 1982. 
40  * pcurses allowed one to enable/disable tracing using traceon() and traceoff()
41  * functions.  ncurses provides a trace() function which allows one to
42  * selectively enable or disable several tracing features.
43  */
44
45 #include <curses.priv.h>
46 #include <tic.h>
47
48 #include <ctype.h>
49
50 MODULE_ID("$Id: lib_trace.c,v 1.86 2017/01/14 17:53:42 tom Exp $")
51
52 NCURSES_EXPORT_VAR(unsigned) _nc_tracing = 0; /* always define this */
53
54 #ifdef TRACE
55
56 #if USE_REENTRANT
57 NCURSES_EXPORT(const char *)
58 NCURSES_PUBLIC_VAR(_nc_tputs_trace) (void)
59 {
60     return CURRENT_SCREEN ? CURRENT_SCREEN->_tputs_trace : _nc_prescreen._tputs_trace;
61 }
62 NCURSES_EXPORT(long)
63 NCURSES_PUBLIC_VAR(_nc_outchars) (void)
64 {
65     return CURRENT_SCREEN ? CURRENT_SCREEN->_outchars : _nc_prescreen._outchars;
66 }
67 NCURSES_EXPORT(void)
68 _nc_set_tputs_trace(const char *s)
69 {
70     if (CURRENT_SCREEN)
71         CURRENT_SCREEN->_tputs_trace = s;
72     else
73         _nc_prescreen._tputs_trace = s;
74 }
75 NCURSES_EXPORT(void)
76 _nc_count_outchars(long increment)
77 {
78     if (CURRENT_SCREEN)
79         CURRENT_SCREEN->_outchars += increment;
80     else
81         _nc_prescreen._outchars += increment;
82 }
83 #else
84 NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace = "";
85 NCURSES_EXPORT_VAR(long) _nc_outchars = 0;
86 #endif
87
88 #define MyFP            _nc_globals.trace_fp
89 #define MyFD            _nc_globals.trace_fd
90 #define MyInit          _nc_globals.trace_opened
91 #define MyPath          _nc_globals.trace_fname
92 #define MyLevel         _nc_globals.trace_level
93 #define MyNested        _nc_globals.nested_tracef
94
95 NCURSES_EXPORT(void)
96 trace(const unsigned int tracelevel)
97 {
98     if ((MyFP == 0) && tracelevel) {
99         MyInit = TRUE;
100         if (MyFD >= 0) {
101             MyFP = fdopen(MyFD, "wb");
102         } else {
103             if (MyPath[0] == '\0') {
104                 size_t size = sizeof(MyPath) - 12;
105                 if (getcwd(MyPath, size) == 0) {
106                     perror("curses: Can't get working directory");
107                     exit(EXIT_FAILURE);
108                 }
109                 MyPath[size] = '\0';
110                 assert(strlen(MyPath) <= size);
111                 _nc_STRCAT(MyPath, "/trace", sizeof(MyPath));
112                 if (_nc_is_dir_path(MyPath)) {
113                     _nc_STRCAT(MyPath, ".log", sizeof(MyPath));
114                 }
115             }
116             if (_nc_access(MyPath, W_OK) < 0
117                 || (MyFD = open(MyPath, O_CREAT | O_EXCL | O_RDWR, 0600)) < 0
118                 || (MyFP = fdopen(MyFD, "wb")) == 0) {
119                 ;               /* EMPTY */
120             }
121         }
122         _nc_tracing = tracelevel;
123         /* Try to set line-buffered mode, or (failing that) unbuffered,
124          * so that the trace-output gets flushed automatically at the
125          * end of each line.  This is useful in case the program dies. 
126          */
127         if (MyFP != 0) {
128 #if HAVE_SETVBUF                /* ANSI */
129             (void) setvbuf(MyFP, (char *) 0, _IOLBF, (size_t) 0);
130 #elif HAVE_SETBUF /* POSIX */
131             (void) setbuffer(MyFP, (char *) 0);
132 #endif
133         }
134         _tracef("TRACING NCURSES version %s.%d (tracelevel=%#x)",
135                 NCURSES_VERSION,
136                 NCURSES_VERSION_PATCH,
137                 tracelevel);
138     } else if (tracelevel == 0) {
139         if (MyFP != 0) {
140             MyFD = dup(MyFD);   /* allow reopen of same file */
141             fclose(MyFP);
142             MyFP = 0;
143         }
144         _nc_tracing = tracelevel;
145     } else if (_nc_tracing != tracelevel) {
146         _nc_tracing = tracelevel;
147         _tracef("tracelevel=%#x", tracelevel);
148     }
149 }
150
151 static void
152 _nc_va_tracef(const char *fmt, va_list ap)
153 {
154     static const char Called[] = T_CALLED("");
155     static const char Return[] = T_RETURN("");
156
157     bool before = FALSE;
158     bool after = FALSE;
159     unsigned doit = _nc_tracing;
160     int save_err = errno;
161     FILE *fp = MyFP;
162
163 #ifdef TRACE
164     /* verbose-trace in the command-line utilities relies on this */
165     if (fp == 0 && !MyInit && _nc_tracing >= DEBUG_LEVEL(1))
166         fp = stderr;
167 #endif
168
169     if (strlen(fmt) >= sizeof(Called) - 1) {
170         if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
171             before = TRUE;
172             MyLevel++;
173         } else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
174             after = TRUE;
175         }
176         if (before || after) {
177             if ((MyLevel <= 1)
178                 || (doit & TRACE_ICALLS) != 0)
179                 doit &= (TRACE_CALLS | TRACE_CCALLS);
180             else
181                 doit = 0;
182         }
183     }
184
185     if (doit != 0 && fp != 0) {
186 #ifdef USE_PTHREADS
187         /*
188          * TRACE_ICALLS is "really" needed to show normal use with threaded
189          * applications, since anything can be running during a napms(),
190          * making it appear in the hierarchical trace as it other functions
191          * are being called.
192          *
193          * Rather than add the complication of a per-thread stack, just
194          * show the thread-id in each line of the trace.
195          */
196 # if USE_WEAK_SYMBOLS
197         if ((pthread_self))
198 # endif
199 #ifdef __MINGW32__
200             fprintf(fp, "%#lx:", (long) (intptr_t) pthread_self().p);
201 #else
202             fprintf(fp, "%#lx:", (long) (intptr_t) pthread_self());
203 #endif
204 #endif
205         if (before || after) {
206             int n;
207             for (n = 1; n < MyLevel; n++)
208                 fputs("+ ", fp);
209         }
210         vfprintf(fp, fmt, ap);
211         fputc('\n', fp);
212         fflush(fp);
213     }
214
215     if (after && MyLevel)
216         MyLevel--;
217
218     errno = save_err;
219 }
220
221 NCURSES_EXPORT(void)
222 _tracef(const char *fmt,...)
223 {
224     va_list ap;
225
226     va_start(ap, fmt);
227     _nc_va_tracef(fmt, ap);
228     va_end(ap);
229 }
230
231 /* Trace 'bool' return-values */
232 NCURSES_EXPORT(NCURSES_BOOL)
233 _nc_retrace_bool(int code)
234 {
235     T((T_RETURN("%s"), code ? "TRUE" : "FALSE"));
236     return code;
237 }
238
239 /* Trace 'char' return-values */
240 NCURSES_EXPORT(char)
241 _nc_retrace_char(int code)
242 {
243     T((T_RETURN("%c"), code));
244     return (char) code;
245 }
246
247 /* Trace 'int' return-values */
248 NCURSES_EXPORT(int)
249 _nc_retrace_int(int code)
250 {
251     T((T_RETURN("%d"), code));
252     return code;
253 }
254
255 /* Trace 'unsigned' return-values */
256 NCURSES_EXPORT(unsigned)
257 _nc_retrace_unsigned(unsigned code)
258 {
259     T((T_RETURN("%#x"), code));
260     return code;
261 }
262
263 /* Trace 'char*' return-values */
264 NCURSES_EXPORT(char *)
265 _nc_retrace_ptr(char *code)
266 {
267     T((T_RETURN("%s"), _nc_visbuf(code)));
268     return code;
269 }
270
271 /* Trace 'const char*' return-values */
272 NCURSES_EXPORT(const char *)
273 _nc_retrace_cptr(const char *code)
274 {
275     T((T_RETURN("%s"), _nc_visbuf(code)));
276     return code;
277 }
278
279 /* Trace 'NCURSES_CONST void*' return-values */
280 NCURSES_EXPORT(NCURSES_CONST void *)
281 _nc_retrace_cvoid_ptr(NCURSES_CONST void *code)
282 {
283     T((T_RETURN("%p"), code));
284     return code;
285 }
286
287 /* Trace 'void*' return-values */
288 NCURSES_EXPORT(void *)
289 _nc_retrace_void_ptr(void *code)
290 {
291     T((T_RETURN("%p"), code));
292     return code;
293 }
294
295 /* Trace 'SCREEN *' return-values */
296 NCURSES_EXPORT(SCREEN *)
297 _nc_retrace_sp(SCREEN *code)
298 {
299     T((T_RETURN("%p"), (void *) code));
300     return code;
301 }
302
303 /* Trace 'WINDOW *' return-values */
304 NCURSES_EXPORT(WINDOW *)
305 _nc_retrace_win(WINDOW *code)
306 {
307     T((T_RETURN("%p"), (void *) code));
308     return code;
309 }
310
311 #if USE_REENTRANT
312 /*
313  * Check if the given trace-mask is enabled.
314  *
315  * This function may be called from within one of the functions that fills
316  * in parameters for _tracef(), but in that case we do not want to lock the
317  * mutex, since it is already locked.
318  */
319 NCURSES_EXPORT(int)
320 _nc_use_tracef(unsigned mask)
321 {
322     bool result = FALSE;
323
324     _nc_lock_global(tst_tracef);
325     if (!MyNested++) {
326         if ((result = (_nc_tracing & (mask))) != 0
327             && _nc_try_global(tracef) == 0) {
328             /* we will call _nc_locked_tracef(), no nesting so far */
329         } else {
330             /* we will not call _nc_locked_tracef() */
331             MyNested = 0;
332         }
333     } else {
334         /* we may call _nc_locked_tracef(), but with nested_tracef > 0 */
335         result = (_nc_tracing & (mask));
336     }
337     _nc_unlock_global(tst_tracef);
338     return result;
339 }
340
341 /*
342  * We call this if _nc_use_tracef() returns true, which means we must unlock
343  * the tracef mutex.
344  */
345 NCURSES_EXPORT(void)
346 _nc_locked_tracef(const char *fmt,...)
347 {
348     va_list ap;
349
350     va_start(ap, fmt);
351     _nc_va_tracef(fmt, ap);
352     va_end(ap);
353
354     if (--(MyNested) == 0) {
355         _nc_unlock_global(tracef);
356     }
357 }
358 #endif /* USE_REENTRANT */
359
360 #endif /* TRACE */