1998-03-01 12:21:12 +08:00
|
|
|
/****************************************************************************
|
2010-01-17 09:52:00 +08:00
|
|
|
* Copyright (c) 1998-2009,2010 Free Software Foundation, Inc. *
|
1998-03-01 12:21:12 +08:00
|
|
|
* *
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a *
|
|
|
|
* copy of this software and associated documentation files (the *
|
|
|
|
* "Software"), to deal in the Software without restriction, including *
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, *
|
|
|
|
* distribute, distribute with modifications, sublicense, and/or sell *
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is *
|
|
|
|
* furnished to do so, subject to the following conditions: *
|
|
|
|
* *
|
|
|
|
* The above copyright notice and this permission notice shall be included *
|
|
|
|
* in all copies or substantial portions of the Software. *
|
|
|
|
* *
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
|
|
|
|
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
|
|
|
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
|
|
|
|
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
|
|
|
|
* *
|
|
|
|
* Except as contained in this notice, the name(s) of the above copyright *
|
|
|
|
* holders shall not be used in advertising or otherwise to promote the *
|
|
|
|
* sale, use or other dealings in this Software without prior written *
|
|
|
|
* authorization. *
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
|
|
|
|
* and: Eric S. Raymond <esr@snark.thyrsus.com> *
|
2005-10-10 02:41:57 +08:00
|
|
|
* and: Thomas E. Dickey 1996-on *
|
2009-03-01 07:06:36 +08:00
|
|
|
* and: Juergen Pfeifer *
|
1998-03-01 12:21:12 +08:00
|
|
|
****************************************************************************/
|
1997-05-15 12:00:00 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* lib_trace.c - Tracing/Debugging routines
|
2006-12-18 12:32:42 +08:00
|
|
|
*
|
|
|
|
* The _tracef() function is originally from pcurses (by Pavel Curtis) in 1982.
|
|
|
|
* pcurses allowed one to enable/disable tracing using traceon() and traceoff()
|
|
|
|
* functions. ncurses provides a trace() function which allows one to
|
|
|
|
* selectively enable or disable several tracing features.
|
1997-05-15 12:00:00 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <curses.priv.h>
|
1999-10-24 12:32:42 +08:00
|
|
|
#include <tic.h>
|
1997-05-15 12:00:00 +08:00
|
|
|
|
|
|
|
#include <ctype.h>
|
2000-07-09 10:46:08 +08:00
|
|
|
|
2010-12-26 09:22:11 +08:00
|
|
|
MODULE_ID("$Id: lib_trace.c,v 1.76 2010/12/19 01:21:19 tom Exp $")
|
1997-05-15 12:00:00 +08:00
|
|
|
|
2005-10-10 02:41:57 +08:00
|
|
|
NCURSES_EXPORT_VAR(unsigned) _nc_tracing = 0; /* always define this */
|
1999-10-24 12:32:42 +08:00
|
|
|
|
|
|
|
#ifdef TRACE
|
2007-09-30 07:30:37 +08:00
|
|
|
|
|
|
|
#if USE_REENTRANT
|
|
|
|
NCURSES_EXPORT(const char *)
|
|
|
|
NCURSES_PUBLIC_VAR(_nc_tputs_trace) (void)
|
|
|
|
{
|
2009-03-01 07:06:36 +08:00
|
|
|
return CURRENT_SCREEN ? CURRENT_SCREEN->_tputs_trace : _nc_prescreen._tputs_trace;
|
2007-09-30 07:30:37 +08:00
|
|
|
}
|
|
|
|
NCURSES_EXPORT(long)
|
|
|
|
NCURSES_PUBLIC_VAR(_nc_outchars) (void)
|
|
|
|
{
|
2009-03-01 07:06:36 +08:00
|
|
|
return CURRENT_SCREEN ? CURRENT_SCREEN->_outchars : _nc_prescreen._outchars;
|
2007-09-30 07:30:37 +08:00
|
|
|
}
|
|
|
|
NCURSES_EXPORT(void)
|
|
|
|
_nc_set_tputs_trace(const char *s)
|
|
|
|
{
|
2009-03-01 07:06:36 +08:00
|
|
|
if (CURRENT_SCREEN)
|
|
|
|
CURRENT_SCREEN->_tputs_trace = s;
|
2007-09-30 07:30:37 +08:00
|
|
|
else
|
|
|
|
_nc_prescreen._tputs_trace = s;
|
|
|
|
}
|
|
|
|
NCURSES_EXPORT(void)
|
|
|
|
_nc_count_outchars(long increment)
|
|
|
|
{
|
2009-03-01 07:06:36 +08:00
|
|
|
if (CURRENT_SCREEN)
|
|
|
|
CURRENT_SCREEN->_outchars += increment;
|
2007-09-30 07:30:37 +08:00
|
|
|
else
|
|
|
|
_nc_prescreen._outchars += increment;
|
|
|
|
}
|
|
|
|
#else
|
2002-10-13 11:35:53 +08:00
|
|
|
NCURSES_EXPORT_VAR(const char *) _nc_tputs_trace = "";
|
|
|
|
NCURSES_EXPORT_VAR(long) _nc_outchars = 0;
|
2007-09-30 07:30:37 +08:00
|
|
|
#endif
|
1997-05-15 12:00:00 +08:00
|
|
|
|
2007-04-22 08:57:08 +08:00
|
|
|
#define TraceFP _nc_globals.trace_fp
|
|
|
|
#define TracePath _nc_globals.trace_fname
|
|
|
|
#define TraceLevel _nc_globals.trace_level
|
1997-05-15 12:00:00 +08:00
|
|
|
|
2002-10-13 11:35:53 +08:00
|
|
|
NCURSES_EXPORT(void)
|
2004-02-09 10:15:26 +08:00
|
|
|
trace(const unsigned int tracelevel)
|
1997-05-15 12:00:00 +08:00
|
|
|
{
|
2007-04-22 08:57:08 +08:00
|
|
|
if ((TraceFP == 0) && tracelevel) {
|
|
|
|
const char *mode = _nc_globals.init_trace ? "ab" : "wb";
|
2000-07-09 10:46:08 +08:00
|
|
|
|
2007-04-22 08:57:08 +08:00
|
|
|
if (TracePath[0] == '\0') {
|
2010-12-26 09:22:11 +08:00
|
|
|
size_t size = sizeof(TracePath) - 12;
|
2008-08-05 09:06:16 +08:00
|
|
|
if (getcwd(TracePath, size) == 0) {
|
2004-02-09 10:15:26 +08:00
|
|
|
perror("curses: Can't get working directory");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2008-08-05 09:06:16 +08:00
|
|
|
TracePath[size] = '\0';
|
|
|
|
assert(strlen(TracePath) <= size);
|
2007-04-22 08:57:08 +08:00
|
|
|
strcat(TracePath, "/trace");
|
2007-07-15 08:11:01 +08:00
|
|
|
if (_nc_is_dir_path(TracePath)) {
|
|
|
|
strcat(TracePath, ".log");
|
|
|
|
}
|
2004-02-09 10:15:26 +08:00
|
|
|
}
|
2000-07-09 10:46:08 +08:00
|
|
|
|
2007-04-22 08:57:08 +08:00
|
|
|
_nc_globals.init_trace = TRUE;
|
2002-10-13 11:35:53 +08:00
|
|
|
_nc_tracing = tracelevel;
|
2007-04-22 08:57:08 +08:00
|
|
|
if (_nc_access(TracePath, W_OK) < 0
|
|
|
|
|| (TraceFP = fopen(TracePath, mode)) == 0) {
|
2004-02-09 10:15:26 +08:00
|
|
|
perror("curses: Can't open 'trace' file");
|
2000-07-09 10:46:08 +08:00
|
|
|
exit(EXIT_FAILURE);
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|
2000-07-09 10:46:08 +08:00
|
|
|
/* Try to set line-buffered mode, or (failing that) unbuffered,
|
|
|
|
* so that the trace-output gets flushed automatically at the
|
|
|
|
* end of each line. This is useful in case the program dies.
|
|
|
|
*/
|
|
|
|
#if HAVE_SETVBUF /* ANSI */
|
2007-04-22 08:57:08 +08:00
|
|
|
(void) setvbuf(TraceFP, (char *) 0, _IOLBF, 0);
|
2010-12-26 09:22:11 +08:00
|
|
|
#elif HAVE_SETBUF /* POSIX */
|
2007-04-22 08:57:08 +08:00
|
|
|
(void) setbuffer(TraceFP, (char *) 0);
|
1999-10-24 12:32:42 +08:00
|
|
|
#endif
|
2004-02-09 10:15:26 +08:00
|
|
|
_tracef("TRACING NCURSES version %s.%d (tracelevel=%#x)",
|
|
|
|
NCURSES_VERSION,
|
|
|
|
NCURSES_VERSION_PATCH,
|
|
|
|
tracelevel);
|
|
|
|
} else if (tracelevel == 0) {
|
2007-04-22 08:57:08 +08:00
|
|
|
if (TraceFP != 0) {
|
|
|
|
fclose(TraceFP);
|
|
|
|
TraceFP = 0;
|
2004-02-09 10:15:26 +08:00
|
|
|
}
|
|
|
|
_nc_tracing = tracelevel;
|
2002-10-13 11:35:53 +08:00
|
|
|
} else if (_nc_tracing != tracelevel) {
|
|
|
|
_nc_tracing = tracelevel;
|
|
|
|
_tracef("tracelevel=%#x", tracelevel);
|
2000-07-09 10:46:08 +08:00
|
|
|
}
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|
|
|
|
|
2007-09-30 07:30:37 +08:00
|
|
|
static void
|
|
|
|
_nc_va_tracef(const char *fmt, va_list ap)
|
1997-05-15 12:00:00 +08:00
|
|
|
{
|
2000-07-09 10:46:08 +08:00
|
|
|
static const char Called[] = T_CALLED("");
|
|
|
|
static const char Return[] = T_RETURN("");
|
2007-09-30 07:30:37 +08:00
|
|
|
|
2000-07-09 10:46:08 +08:00
|
|
|
bool before = FALSE;
|
|
|
|
bool after = FALSE;
|
2005-10-10 02:41:57 +08:00
|
|
|
unsigned doit = _nc_tracing;
|
2000-07-09 10:46:08 +08:00
|
|
|
int save_err = errno;
|
|
|
|
|
|
|
|
if (strlen(fmt) >= sizeof(Called) - 1) {
|
|
|
|
if (!strncmp(fmt, Called, sizeof(Called) - 1)) {
|
|
|
|
before = TRUE;
|
2007-04-22 08:57:08 +08:00
|
|
|
TraceLevel++;
|
2000-07-09 10:46:08 +08:00
|
|
|
} else if (!strncmp(fmt, Return, sizeof(Return) - 1)) {
|
|
|
|
after = TRUE;
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|
2000-07-09 10:46:08 +08:00
|
|
|
if (before || after) {
|
2007-04-22 08:57:08 +08:00
|
|
|
if ((TraceLevel <= 1)
|
2000-07-09 10:46:08 +08:00
|
|
|
|| (doit & TRACE_ICALLS) != 0)
|
|
|
|
doit &= (TRACE_CALLS | TRACE_CCALLS);
|
|
|
|
else
|
|
|
|
doit = 0;
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|
2000-07-09 10:46:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (doit != 0) {
|
2007-04-22 08:57:08 +08:00
|
|
|
if (TraceFP == 0)
|
|
|
|
TraceFP = stderr;
|
2008-03-23 09:02:37 +08:00
|
|
|
#ifdef USE_PTHREADS
|
|
|
|
/*
|
|
|
|
* TRACE_ICALLS is "really" needed to show normal use with threaded
|
|
|
|
* applications, since anything can be running during a napms(),
|
|
|
|
* making it appear in the hierarchical trace as it other functions
|
|
|
|
* are being called.
|
|
|
|
*
|
|
|
|
* Rather than add the complication of a per-thread stack, just
|
|
|
|
* show the thread-id in each line of the trace.
|
|
|
|
*/
|
2008-08-17 09:51:55 +08:00
|
|
|
# if USE_WEAK_SYMBOLS
|
|
|
|
if ((pthread_self))
|
|
|
|
# endif
|
2010-09-26 09:21:45 +08:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
fprintf(TraceFP, "%#lx:", (long) (void *) pthread_self().p);
|
|
|
|
#else
|
2008-08-24 08:26:02 +08:00
|
|
|
fprintf(TraceFP, "%#lx:", (long) (void *) pthread_self());
|
2010-09-26 09:21:45 +08:00
|
|
|
#endif
|
2008-03-23 09:02:37 +08:00
|
|
|
#endif
|
2000-07-09 10:46:08 +08:00
|
|
|
if (before || after) {
|
|
|
|
int n;
|
2007-04-22 08:57:08 +08:00
|
|
|
for (n = 1; n < TraceLevel; n++)
|
|
|
|
fputs("+ ", TraceFP);
|
2000-07-09 10:46:08 +08:00
|
|
|
}
|
2007-04-22 08:57:08 +08:00
|
|
|
vfprintf(TraceFP, fmt, ap);
|
|
|
|
fputc('\n', TraceFP);
|
|
|
|
fflush(TraceFP);
|
2000-07-09 10:46:08 +08:00
|
|
|
}
|
|
|
|
|
2007-04-22 08:57:08 +08:00
|
|
|
if (after && TraceLevel)
|
|
|
|
TraceLevel--;
|
2007-09-30 07:30:37 +08:00
|
|
|
|
2000-07-09 10:46:08 +08:00
|
|
|
errno = save_err;
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|
|
|
|
|
2007-09-30 07:30:37 +08:00
|
|
|
NCURSES_EXPORT(void)
|
|
|
|
_tracef(const char *fmt,...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
_nc_va_tracef(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2002-10-13 11:35:53 +08:00
|
|
|
/* Trace 'bool' return-values */
|
|
|
|
NCURSES_EXPORT(NCURSES_BOOL)
|
|
|
|
_nc_retrace_bool(NCURSES_BOOL code)
|
|
|
|
{
|
|
|
|
T((T_RETURN("%s"), code ? "TRUE" : "FALSE"));
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2010-01-17 09:52:00 +08:00
|
|
|
/* Trace 'char' return-values */
|
|
|
|
NCURSES_EXPORT(char)
|
|
|
|
_nc_retrace_char(char code)
|
|
|
|
{
|
|
|
|
T((T_RETURN("%c"), code));
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
1997-05-15 12:00:00 +08:00
|
|
|
/* Trace 'int' return-values */
|
2002-10-13 11:35:53 +08:00
|
|
|
NCURSES_EXPORT(int)
|
2000-07-09 10:46:08 +08:00
|
|
|
_nc_retrace_int(int code)
|
1997-05-15 12:00:00 +08:00
|
|
|
{
|
2000-07-09 10:46:08 +08:00
|
|
|
T((T_RETURN("%d"), code));
|
|
|
|
return code;
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|
|
|
|
|
2002-10-13 11:35:53 +08:00
|
|
|
/* Trace 'unsigned' return-values */
|
|
|
|
NCURSES_EXPORT(unsigned)
|
|
|
|
_nc_retrace_unsigned(unsigned code)
|
|
|
|
{
|
|
|
|
T((T_RETURN("%#x"), code));
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
1997-05-15 12:00:00 +08:00
|
|
|
/* Trace 'char*' return-values */
|
2002-10-13 11:35:53 +08:00
|
|
|
NCURSES_EXPORT(char *)
|
2000-07-09 10:46:08 +08:00
|
|
|
_nc_retrace_ptr(char *code)
|
1997-05-15 12:00:00 +08:00
|
|
|
{
|
2000-07-09 10:46:08 +08:00
|
|
|
T((T_RETURN("%s"), _nc_visbuf(code)));
|
|
|
|
return code;
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|
|
|
|
|
2005-10-10 02:41:57 +08:00
|
|
|
/* Trace 'const char*' return-values */
|
|
|
|
NCURSES_EXPORT(const char *)
|
|
|
|
_nc_retrace_cptr(const char *code)
|
|
|
|
{
|
|
|
|
T((T_RETURN("%s"), _nc_visbuf(code)));
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Trace 'NCURSES_CONST void*' return-values */
|
|
|
|
NCURSES_EXPORT(NCURSES_CONST void *)
|
|
|
|
_nc_retrace_cvoid_ptr(NCURSES_CONST void *code)
|
|
|
|
{
|
|
|
|
T((T_RETURN("%p"), code));
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Trace 'void*' return-values */
|
|
|
|
NCURSES_EXPORT(void *)
|
|
|
|
_nc_retrace_void_ptr(void *code)
|
|
|
|
{
|
|
|
|
T((T_RETURN("%p"), code));
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2002-10-13 11:35:53 +08:00
|
|
|
/* Trace 'SCREEN *' return-values */
|
|
|
|
NCURSES_EXPORT(SCREEN *)
|
2005-10-10 02:41:57 +08:00
|
|
|
_nc_retrace_sp(SCREEN *code)
|
2002-10-13 11:35:53 +08:00
|
|
|
{
|
2009-10-25 09:06:00 +08:00
|
|
|
T((T_RETURN("%p"), (void *) code));
|
2002-10-13 11:35:53 +08:00
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
1997-05-15 12:00:00 +08:00
|
|
|
/* Trace 'WINDOW *' return-values */
|
2002-10-13 11:35:53 +08:00
|
|
|
NCURSES_EXPORT(WINDOW *)
|
2000-07-09 10:46:08 +08:00
|
|
|
_nc_retrace_win(WINDOW *code)
|
1997-05-15 12:00:00 +08:00
|
|
|
{
|
2009-10-25 09:06:00 +08:00
|
|
|
T((T_RETURN("%p"), (void *) code));
|
2000-07-09 10:46:08 +08:00
|
|
|
return code;
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|
2007-09-30 07:30:37 +08:00
|
|
|
|
|
|
|
#if USE_REENTRANT
|
|
|
|
/*
|
|
|
|
* Check if the given trace-mask is enabled.
|
|
|
|
*
|
|
|
|
* This function may be called from within one of the functions that fills
|
|
|
|
* in parameters for _tracef(), but in that case we do not want to lock the
|
|
|
|
* mutex, since it is already locked.
|
|
|
|
*/
|
|
|
|
NCURSES_EXPORT(int)
|
|
|
|
_nc_use_tracef(unsigned mask)
|
|
|
|
{
|
|
|
|
bool result = FALSE;
|
|
|
|
|
|
|
|
_nc_lock_global(tst_tracef);
|
|
|
|
if (!_nc_globals.nested_tracef++) {
|
2008-06-08 08:40:16 +08:00
|
|
|
if ((result = (_nc_tracing & (mask))) != 0
|
|
|
|
&& _nc_try_global(tracef) == 0) {
|
2007-09-30 07:30:37 +08:00
|
|
|
/* we will call _nc_locked_tracef(), no nesting so far */
|
|
|
|
} else {
|
|
|
|
/* we will not call _nc_locked_tracef() */
|
|
|
|
_nc_globals.nested_tracef = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* we may call _nc_locked_tracef(), but with nested_tracef > 0 */
|
|
|
|
result = (_nc_tracing & (mask));
|
|
|
|
}
|
|
|
|
_nc_unlock_global(tst_tracef);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We call this if _nc_use_tracef() returns true, which means we must unlock
|
|
|
|
* the tracef mutex.
|
|
|
|
*/
|
|
|
|
NCURSES_EXPORT(void)
|
|
|
|
_nc_locked_tracef(const char *fmt,...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
_nc_va_tracef(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (--(_nc_globals.nested_tracef) == 0)
|
|
|
|
_nc_unlock_global(tracef);
|
|
|
|
}
|
|
|
|
#endif /* USE_REENTRANT */
|
|
|
|
|
1999-10-24 12:32:42 +08:00
|
|
|
#endif /* TRACE */
|