]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/trace/lib_trace.c
4567054a9fd30120b47309d576e258d41033c9d1
[ncurses.git] / ncurses / trace / lib_trace.c
1 /****************************************************************************
2  * Copyright (c) 1998-2013,2016 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.85 2016/12/31 13:50:06 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 MyPath          _nc_globals.trace_fname
91 #define MyLevel         _nc_globals.trace_level
92 #define MyNested        _nc_globals.nested_tracef
93
94 NCURSES_EXPORT(void)
95 trace(const unsigned int tracelevel)
96 {
97     if ((MyFP == 0) && tracelevel) {
98         if (MyFD >= 0) {
99             MyFP = fdopen(MyFD, "wb");
100         } else {
101             if (MyPath[0] == '\0') {
102                 size_t size = sizeof(MyPath) - 12;
103                 if (getcwd(MyPath, size) == 0) {
104                     perror("curses: Can't get working directory");
105                     exit(EXIT_FAILURE);
106                 }
107                 MyPath[size] = '\0';
108                 assert(strlen(MyPath) <= size);
109                 _nc_STRCAT(MyPath, "/trace", sizeof(MyPath));
110                 if (_nc_is_dir_path(MyPath)) {
111                     _nc_STRCAT(MyPath, ".log", sizeof(MyPath));
112                 }
113             }
114             if (_nc_access(MyPath, W_OK) < 0
115                 || (MyFD = open(MyPath, O_CREAT | O_EXCL | O_RDWR, 0600)) < 0
116                 || (MyFP = fdopen(MyFD, "wb")) == 0) {
117                 ;               /* EMPTY */
118             }
119         }
120         _nc_tracing = tracelevel;
121         /* Try to set line-buffered mode, or (failing that) unbuffered,
122          * so that the trace-output gets flushed automatically at the
123          * end of each line.  This is useful in case the program dies. 
124          */
125         if (MyFP != 0) {
126 #if HAVE_SETVBUF                /* ANSI */
127             (void) setvbuf(MyFP, (char *) 0, _IOLBF, (size_t) 0);
128 #elif HAVE_SETBUF /* POSIX */
129             (void) setbuffer(MyFP, (char *) 0);
130 #endif
131         }
132         _tracef("TRACING NCURSES version %s.%d (tracelevel=%#x)",
133                 NCURSES_VERSION,
134                 NCURSES_VERSION_PATCH,
135                 tracelevel);
136     } else if (tracelevel == 0) {
137         if (MyFP != 0) {
138             MyFD = dup(MyFD);   /* allow reopen of same file */
139             fclose(MyFP);
140             MyFP = 0;
141         }
142         _nc_tracing = tracelevel;
143     } else if (_nc_tracing != tracelevel) {
144         _nc_tracing = tracelevel;
145         _tracef("tracelevel=%#x", tracelevel);
146     }
147 }
148
149 static void
150 _nc_va_tracef(const char *fmt, va_list ap)
151 {
152     static const char Called[] = T_CALLED("");
153     static const char Return[] = T_RETURN("");
154
155     bool before = FALSE;
156     bool after = FALSE;
157     unsigned doit = _nc_tracing;
158     int save_err = errno;
159     FILE *fp = MyFP;
160
161 #ifdef TRACE
162     /* verbose-trace in the command-line utilities relies on this */
163     if (fp == 0 && _nc_tracing >= DEBUG_LEVEL(1))
164         fp = stderr;
165 #endif
166
167     if (strlen(fmt) >= sizeof(Called) - 1) {
168         if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
169             before = TRUE;
170             MyLevel++;
171         } else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
172             after = TRUE;
173         }
174         if (before || after) {
175             if ((MyLevel <= 1)
176                 || (doit & TRACE_ICALLS) != 0)
177                 doit &= (TRACE_CALLS | TRACE_CCALLS);
178             else
179                 doit = 0;
180         }
181     }
182
183     if (doit != 0 && fp != 0) {
184 #ifdef USE_PTHREADS
185         /*
186          * TRACE_ICALLS is "really" needed to show normal use with threaded
187          * applications, since anything can be running during a napms(),
188          * making it appear in the hierarchical trace as it other functions
189          * are being called.
190          *
191          * Rather than add the complication of a per-thread stack, just
192          * show the thread-id in each line of the trace.
193          */
194 # if USE_WEAK_SYMBOLS
195         if ((pthread_self))
196 # endif
197 #ifdef __MINGW32__
198             fprintf(fp, "%#lx:", (long) (intptr_t) pthread_self().p);
199 #else
200             fprintf(fp, "%#lx:", (long) (intptr_t) pthread_self());
201 #endif
202 #endif
203         if (before || after) {
204             int n;
205             for (n = 1; n < MyLevel; n++)
206                 fputs("+ ", fp);
207         }
208         vfprintf(fp, fmt, ap);
209         fputc('\n', fp);
210         fflush(fp);
211     }
212
213     if (after && MyLevel)
214         MyLevel--;
215
216     errno = save_err;
217 }
218
219 NCURSES_EXPORT(void)
220 _tracef(const char *fmt,...)
221 {
222     va_list ap;
223
224     va_start(ap, fmt);
225     _nc_va_tracef(fmt, ap);
226     va_end(ap);
227 }
228
229 /* Trace 'bool' return-values */
230 NCURSES_EXPORT(NCURSES_BOOL)
231 _nc_retrace_bool(int code)
232 {
233     T((T_RETURN("%s"), code ? "TRUE" : "FALSE"));
234     return code;
235 }
236
237 /* Trace 'char' return-values */
238 NCURSES_EXPORT(char)
239 _nc_retrace_char(int code)
240 {
241     T((T_RETURN("%c"), code));
242     return (char) code;
243 }
244
245 /* Trace 'int' return-values */
246 NCURSES_EXPORT(int)
247 _nc_retrace_int(int code)
248 {
249     T((T_RETURN("%d"), code));
250     return code;
251 }
252
253 /* Trace 'unsigned' return-values */
254 NCURSES_EXPORT(unsigned)
255 _nc_retrace_unsigned(unsigned code)
256 {
257     T((T_RETURN("%#x"), code));
258     return code;
259 }
260
261 /* Trace 'char*' return-values */
262 NCURSES_EXPORT(char *)
263 _nc_retrace_ptr(char *code)
264 {
265     T((T_RETURN("%s"), _nc_visbuf(code)));
266     return code;
267 }
268
269 /* Trace 'const char*' return-values */
270 NCURSES_EXPORT(const char *)
271 _nc_retrace_cptr(const char *code)
272 {
273     T((T_RETURN("%s"), _nc_visbuf(code)));
274     return code;
275 }
276
277 /* Trace 'NCURSES_CONST void*' return-values */
278 NCURSES_EXPORT(NCURSES_CONST void *)
279 _nc_retrace_cvoid_ptr(NCURSES_CONST void *code)
280 {
281     T((T_RETURN("%p"), code));
282     return code;
283 }
284
285 /* Trace 'void*' return-values */
286 NCURSES_EXPORT(void *)
287 _nc_retrace_void_ptr(void *code)
288 {
289     T((T_RETURN("%p"), code));
290     return code;
291 }
292
293 /* Trace 'SCREEN *' return-values */
294 NCURSES_EXPORT(SCREEN *)
295 _nc_retrace_sp(SCREEN *code)
296 {
297     T((T_RETURN("%p"), (void *) code));
298     return code;
299 }
300
301 /* Trace 'WINDOW *' return-values */
302 NCURSES_EXPORT(WINDOW *)
303 _nc_retrace_win(WINDOW *code)
304 {
305     T((T_RETURN("%p"), (void *) code));
306     return code;
307 }
308
309 #if USE_REENTRANT
310 /*
311  * Check if the given trace-mask is enabled.
312  *
313  * This function may be called from within one of the functions that fills
314  * in parameters for _tracef(), but in that case we do not want to lock the
315  * mutex, since it is already locked.
316  */
317 NCURSES_EXPORT(int)
318 _nc_use_tracef(unsigned mask)
319 {
320     bool result = FALSE;
321
322     _nc_lock_global(tst_tracef);
323     if (!MyNested++) {
324         if ((result = (_nc_tracing & (mask))) != 0
325             && _nc_try_global(tracef) == 0) {
326             /* we will call _nc_locked_tracef(), no nesting so far */
327         } else {
328             /* we will not call _nc_locked_tracef() */
329             MyNested = 0;
330         }
331     } else {
332         /* we may call _nc_locked_tracef(), but with nested_tracef > 0 */
333         result = (_nc_tracing & (mask));
334     }
335     _nc_unlock_global(tst_tracef);
336     return result;
337 }
338
339 /*
340  * We call this if _nc_use_tracef() returns true, which means we must unlock
341  * the tracef mutex.
342  */
343 NCURSES_EXPORT(void)
344 _nc_locked_tracef(const char *fmt,...)
345 {
346     va_list ap;
347
348     va_start(ap, fmt);
349     _nc_va_tracef(fmt, ap);
350     va_end(ap);
351
352     if (--(MyNested) == 0) {
353         _nc_unlock_global(tracef);
354     }
355 }
356 #endif /* USE_REENTRANT */
357
358 #endif /* TRACE */