1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
/*******************************************************************************
*
* MODULE: debug.c
*
********************************************************************************
*
* DESCRIPTION: C::B::C debugging stuff
*
********************************************************************************
*
* $Project: /Convert-Binary-C $
* $Author: mhx $
* $Date: 2009/03/15 04:10:52 +0100 $
* $Revision: 12 $
* $Source: /cbc/debug.c $
*
********************************************************************************
*
* Copyright (c) 2002-2009 Marcus Holland-Moritz. All rights reserved.
* This program is free software; you can redistribute it and/or modify
* it under the same terms as Perl itself.
*
*******************************************************************************/
#ifdef CBC_DEBUGGING
/*===== GLOBAL INCLUDES ======================================================*/
#define PERL_NO_GET_CONTEXT
#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h>
#include "ppport.h"
/*===== LOCAL INCLUDES =======================================================*/
#include "ctlib/ctdebug.h"
#include "util/hash.h"
#include "util/memalloc.h"
#include "cbc/cbc.h"
#include "cbc/debug.h"
#include "cbc/util.h"
/*===== DEFINES ==============================================================*/
#ifndef PERLIO_IS_STDIO
# ifdef fprintf
# undef fprintf
# endif
# define fprintf PerlIO_printf
# ifdef vfprintf
# undef vfprintf
# endif
# define vfprintf PerlIO_vprintf
# ifdef stderr
# undef stderr
# endif
# define stderr PerlIO_stderr()
# ifdef fopen
# undef fopen
# endif
# define fopen PerlIO_open
# ifdef fclose
# undef fclose
# endif
# define fclose PerlIO_close
#endif
/*===== TYPEDEFS =============================================================*/
#ifdef PerlIO
typedef PerlIO * DebugStream;
#else
typedef FILE * DebugStream;
#endif
/*===== STATIC FUNCTION PROTOTYPES ===========================================*/
static void debug_vprintf(const char *f, va_list *l);
static void debug_printf(const char *f, ...);
static void debug_printf_ctlib(const char *f, ...);
/*===== EXTERNAL VARIABLES ===================================================*/
/*===== GLOBAL VARIABLES =====================================================*/
/*===== STATIC VARIABLES =====================================================*/
static DebugStream gs_DB_stream;
/*===== STATIC FUNCTIONS =====================================================*/
/*******************************************************************************
*
* ROUTINE: debug_*
*
* WRITTEN BY: Marcus Holland-Moritz ON: Mar 2002
* CHANGED BY: ON:
*
********************************************************************************
*
* DESCRIPTION: Debug output routines.
*
* ARGUMENTS:
*
* RETURNS:
*
*******************************************************************************/
static void debug_vprintf(const char *f, va_list *l)
{
dTHX;
vfprintf(gs_DB_stream, f, *l);
}
static void debug_printf(const char *f, ...)
{
dTHX;
va_list l;
va_start(l, f);
vfprintf(gs_DB_stream, f, l);
va_end(l);
}
static void debug_printf_ctlib(const char *f, ...)
{
dTHX;
va_list l;
va_start(l, f);
debug_printf("DBG: ");
vfprintf(gs_DB_stream, f, l);
debug_printf("\n");
va_end(l);
}
/*===== FUNCTIONS ============================================================*/
/*******************************************************************************
*
* ROUTINE: set_debug_options
*
* WRITTEN BY: Marcus Holland-Moritz ON: Mar 2002
* CHANGED BY: ON:
*
********************************************************************************
*
* DESCRIPTION:
*
* ARGUMENTS:
*
* RETURNS:
*
*******************************************************************************/
void set_debug_options(pTHX_ const char *dbopts)
{
unsigned long memflags, hashflags, dbgflags;
if (strEQ(dbopts, "all"))
{
memflags = hashflags = dbgflags = 0xFFFFFFFF;
}
else
{
memflags = hashflags = dbgflags = 0;
while (*dbopts)
{
switch (*dbopts)
{
case 'm': memflags |= DB_MEMALLOC_TRACE; break;
case 'M': memflags |= DB_MEMALLOC_TRACE
| DB_MEMALLOC_ASSERT; break;
case 'h': hashflags |= DB_HASH_MAIN; break;
case 'd': dbgflags |= DB_CTLIB_MAIN; break;
case 'p': dbgflags |= DB_CTLIB_PARSER; break;
case 'l': dbgflags |= DB_CTLIB_CLEXER; break;
case 'y': dbgflags |= DB_CTLIB_YACC; break;
case 'r': dbgflags |= DB_CTLIB_PRAGMA; break;
case 'c': dbgflags |= DB_CTLIB_CTLIB; break;
case 'H': dbgflags |= DB_CTLIB_HASH; break;
case 't': dbgflags |= DB_CTLIB_TYPE; break;
case 'P': dbgflags |= DB_CTLIB_PREPROC; break;
default:
Perl_croak(aTHX_ "Unknown debug option '%c'", *dbopts);
break;
}
dbopts++;
}
}
if (!SetDebugMemAlloc(debug_printf, memflags))
fatal("Cannot enable memory debugging");
if (!SetDebugHash(debug_printf, hashflags))
fatal("Cannot enable hash debugging");
if (!SetDebugCType(debug_printf_ctlib, debug_vprintf, dbgflags))
fatal("Cannot enable debugging");
}
/*******************************************************************************
*
* ROUTINE: set_debug_file
*
* WRITTEN BY: Marcus Holland-Moritz ON: Mar 2002
* CHANGED BY: ON:
*
********************************************************************************
*
* DESCRIPTION:
*
* ARGUMENTS:
*
* RETURNS:
*
*******************************************************************************/
void set_debug_file(pTHX_ const char *dbfile)
{
if (gs_DB_stream != stderr && gs_DB_stream != NULL)
{
fclose(gs_DB_stream);
gs_DB_stream = NULL;
}
gs_DB_stream = dbfile ? fopen(dbfile, "w") : stderr;
if (gs_DB_stream == NULL)
{
WARN((aTHX_ "Cannot open '%s', defaulting to stderr", dbfile));
gs_DB_stream = stderr;
}
}
/*******************************************************************************
*
* ROUTINE: init_debugging
*
* WRITTEN BY: Marcus Holland-Moritz ON: Dec 2004
* CHANGED BY: ON:
*
********************************************************************************
*
* DESCRIPTION:
*
* ARGUMENTS:
*
* RETURNS:
*
*******************************************************************************/
void init_debugging(pTHX)
{
gs_DB_stream = stderr;
}
#endif /* CBC_DEBUGGING */
|