~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to dbug/dbug.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 *                                                                            *
 
3
 *                                 N O T I C E                                *
 
4
 *                                                                            *
 
5
 *                    Copyright Abandoned, 1987, Fred Fish                    *
 
6
 *                                                                            *
 
7
 *                                                                            *
 
8
 *      This previously copyrighted work has been placed into the  public     *
 
9
 *      domain  by  the  author  and  may be freely used for any purpose,     *
 
10
 *      private or commercial.                                                *
 
11
 *                                                                            *
 
12
 *      Because of the number of inquiries I was receiving about the  use     *
 
13
 *      of this product in commercially developed works I have decided to     *
 
14
 *      simply make it public domain to further its unrestricted use.   I     *
 
15
 *      specifically  would  be  most happy to see this material become a     *
 
16
 *      part of the standard Unix distributions by AT&T and the  Berkeley     *
 
17
 *      Computer  Science  Research Group, and a standard part of the GNU     *
 
18
 *      system from the Free Software Foundation.                             *
 
19
 *                                                                            *
 
20
 *      I would appreciate it, as a courtesy, if this notice is  left  in     *
 
21
 *      all copies and derivative works.  Thank you.                          *
 
22
 *                                                                            *
 
23
 *      The author makes no warranty of any kind  with  respect  to  this     *
 
24
 *      product  and  explicitly disclaims any implied warranties of mer-     *
 
25
 *      chantability or fitness for any particular purpose.                   *
 
26
 *                                                                            *
 
27
 ******************************************************************************
 
28
 */
 
29
 
 
30
/*
 
31
 *  FILE
 
32
 *
 
33
 *      dbug.c   runtime support routines for dbug package
 
34
 *
 
35
 *  SCCS
 
36
 *
 
37
 *      @(#)dbug.c      1.25    7/25/89
 
38
 *
 
39
 *  DESCRIPTION
 
40
 *
 
41
 *      These are the runtime support routines for the dbug package.
 
42
 *      The dbug package has two main components; the user include
 
43
 *      file containing various macro definitions, and the runtime
 
44
 *      support routines which are called from the macro expansions.
 
45
 *
 
46
 *      Externally visible functions in the runtime support module
 
47
 *      use the naming convention pattern "_db_xx...xx_", thus
 
48
 *      they are unlikely to collide with user defined function names.
 
49
 *
 
50
 *  AUTHOR(S)
 
51
 *
 
52
 *      Fred Fish               (base code)
 
53
 *      Enhanced Software Technologies, Tempe, AZ
 
54
 *      asuvax!mcdphx!estinc!fnf
 
55
 *
 
56
 *      Binayak Banerjee        (profiling enhancements)
 
57
 *      seismo!bpa!sjuvax!bbanerje
 
58
 *
 
59
 *      Michael Widenius:
 
60
 *      DBUG_DUMP       - To dump a block of memory.
 
61
 *      PUSH_FLAG "O"   - To be used insted of "o" if we
 
62
 *                        want flushing after each write
 
63
 *      PUSH_FLAG "A"   - as 'O', but we will append to the out file instead
 
64
 *                        of creating a new one.
 
65
 *      Check of malloc on entry/exit (option "S")
 
66
 *
 
67
 *      DBUG_EXECUTE_IF
 
68
 *      incremental mode (-#+t:-d,info ...)
 
69
 *      DBUG_SET, _db_explain_
 
70
 *      thread-local settings
 
71
 *
 
72
 */
 
73
 
 
74
 
 
75
#include <my_global.h>
 
76
#include <m_string.h>
 
77
#include <errno.h>
 
78
#if defined(MSDOS) || defined(__WIN__)
 
79
#include <process.h>
 
80
#endif
 
81
 
 
82
 
 
83
#ifndef DBUG_OFF
 
84
 
 
85
 
 
86
/*
 
87
 *            Manifest constants which may be "tuned" if desired.
 
88
 */
 
89
 
 
90
#define PRINTBUF              1024    /* Print buffer size */
 
91
#define INDENT                2       /* Indentation per trace level */
 
92
#define MAXDEPTH              200     /* Maximum trace depth default */
 
93
 
 
94
/*
 
95
 *      The following flags are used to determine which
 
96
 *      capabilities the user has enabled with the settings
 
97
 *      push macro.
 
98
 */
 
99
 
 
100
#define TRACE_ON        000001  /* Trace enabled */
 
101
#define DEBUG_ON        000002  /* Debug enabled */
 
102
#define FILE_ON         000004  /* File name print enabled */
 
103
#define LINE_ON         000010  /* Line number print enabled */
 
104
#define DEPTH_ON        000020  /* Function nest level print enabled */
 
105
#define PROCESS_ON      000040  /* Process name print enabled */
 
106
#define NUMBER_ON       000100  /* Number each line of output */
 
107
#define PROFILE_ON      000200  /* Print out profiling code */
 
108
#define PID_ON          000400  /* Identify each line with process id */
 
109
#define TIMESTAMP_ON    001000  /* timestamp every line of output */
 
110
#define SANITY_CHECK_ON 002000  /* Check safemalloc on DBUG_ENTER */
 
111
#define FLUSH_ON_WRITE  004000  /* Flush on every write */
 
112
#define OPEN_APPEND     010000  /* Open for append      */
 
113
 
 
114
#define TRACING (cs->stack->flags & TRACE_ON)
 
115
#define DEBUGGING (cs->stack->flags & DEBUG_ON)
 
116
#define PROFILING (cs->stack->flags & PROFILE_ON)
 
117
 
 
118
/*
 
119
 *      Typedefs to make things more obvious.
 
120
 */
 
121
 
 
122
#ifndef __WIN__
 
123
typedef int BOOLEAN;
 
124
#else
 
125
#define BOOLEAN BOOL
 
126
#endif
 
127
 
 
128
/*
 
129
 *      Make it easy to change storage classes if necessary.
 
130
 */
 
131
 
 
132
#define IMPORT extern           /* Names defined externally */
 
133
#define EXPORT                  /* Allocated here, available globally */
 
134
#define AUTO auto               /* Names to be allocated on stack */
 
135
#define REGISTER register       /* Names to be placed in registers */
 
136
 
 
137
/*
 
138
 * The default file for profiling.  Could also add another flag
 
139
 * (G?) which allowed the user to specify this.
 
140
 *
 
141
 * If the automatic variables get allocated on the stack in
 
142
 * reverse order from their declarations, then define AUTOS_REVERSE.
 
143
 * This is used by the code that keeps track of stack usage.  For
 
144
 * forward allocation, the difference in the dbug frame pointers
 
145
 * represents stack used by the callee function.  For reverse allocation,
 
146
 * the difference represents stack used by the caller function.
 
147
 *
 
148
 */
 
149
 
 
150
#define PROF_FILE       "dbugmon.out"
 
151
#define PROF_EFMT       "E\t%ld\t%s\n"
 
152
#define PROF_SFMT       "S\t%lx\t%lx\t%s\n"
 
153
#define PROF_XFMT       "X\t%ld\t%s\n"
 
154
 
 
155
#ifdef M_I386           /* predefined by xenix 386 compiler */
 
156
#define AUTOS_REVERSE 1
 
157
#endif
 
158
 
 
159
/*
 
160
 *      Externally supplied functions.
 
161
 */
 
162
 
 
163
#ifndef HAVE_PERROR
 
164
static void perror();          /* Fake system/library error print routine */
 
165
#endif
 
166
 
 
167
IMPORT int _sanity(const char *file,uint line); /* safemalloc sanity checker */
 
168
 
 
169
/*
 
170
 *      The user may specify a list of functions to trace or
 
171
 *      debug.  These lists are kept in a linear linked list,
 
172
 *      a very simple implementation.
 
173
 */
 
174
 
 
175
struct link {
 
176
    struct link *next_link;   /* Pointer to the next link */
 
177
    char   str[1];        /* Pointer to link's contents */
 
178
};
 
179
 
 
180
/*
 
181
 *      Debugging settings can be pushed or popped off of a
 
182
 *      stack which is implemented as a linked list.  Note
 
183
 *      that the head of the list is the current settings and the
 
184
 *      stack is pushed by adding a new settings to the head of the
 
185
 *      list or popped by removing the first link.
 
186
 *
 
187
 *      Note: if out_file is NULL, the other fields are not initialized at all!
 
188
 */
 
189
 
 
190
struct settings {
 
191
  int flags;                    /* Current settings flags */
 
192
  int maxdepth;                 /* Current maximum trace depth */
 
193
  uint delay;                   /* Delay after each output line */
 
194
  int sub_level;                /* Sub this from code_state->level */
 
195
  FILE *out_file;               /* Current output stream */
 
196
  FILE *prof_file;              /* Current profiling stream */
 
197
  char name[FN_REFLEN];         /* Name of output file */
 
198
  struct link *functions;       /* List of functions */
 
199
  struct link *p_functions;     /* List of profiled functions */
 
200
  struct link *keywords;        /* List of debug keywords */
 
201
  struct link *processes;       /* List of process names */
 
202
  struct settings *next;        /* Next settings in the list */
 
203
};
 
204
 
 
205
#define is_shared(S, V) ((S)->next && (S)->next->V == (S)->V)
 
206
 
 
207
/*
 
208
 *      Local variables not seen by user.
 
209
 */
 
210
 
 
211
 
 
212
static BOOLEAN init_done= FALSE; /* Set to TRUE when initialization done */
 
213
static struct settings init_settings;
 
214
static const char *db_process= 0;/* Pointer to process name; argv[0] */
 
215
 
 
216
typedef struct _db_code_state_ {
 
217
  const char *process;          /* Pointer to process name; usually argv[0] */
 
218
  const char *func;             /* Name of current user function */
 
219
  const char *file;             /* Name of current user file */
 
220
  char **framep;                /* Pointer to current frame */
 
221
  struct settings *stack;       /* debugging settings */
 
222
  const char *jmpfunc;          /* Remember current function for setjmp */
 
223
  const char *jmpfile;          /* Remember current file for setjmp */
 
224
  int lineno;                   /* Current debugger output line number */
 
225
  int level;                    /* Current function nesting level */
 
226
  int jmplevel;                 /* Remember nesting level at setjmp() */
 
227
 
 
228
/*
 
229
 *      The following variables are used to hold the state information
 
230
 *      between the call to _db_pargs_() and _db_doprnt_(), during
 
231
 *      expansion of the DBUG_PRINT macro.  This is the only macro
 
232
 *      that currently uses these variables.
 
233
 *
 
234
 *      These variables are currently used only by _db_pargs_() and
 
235
 *      _db_doprnt_().
 
236
 */
 
237
 
 
238
  uint u_line;                  /* User source code line number */
 
239
  int  locked;                  /* If locked with _db_lock_file_ */
 
240
  const char *u_keyword;        /* Keyword for current macro */
 
241
} CODE_STATE;
 
242
 
 
243
/*
 
244
  The test below is so we could call functions with DBUG_ENTER before
 
245
  my_thread_init().
 
246
*/
 
247
#define get_code_state_or_return if (!cs && !((cs=code_state()))) return
 
248
 
 
249
        /* Handling lists */
 
250
static struct link *ListAdd(struct link *, const char *, const char *);
 
251
static struct link *ListDel(struct link *, const char *, const char *);
 
252
static struct link *ListCopy(struct link *);
 
253
static void FreeList(struct link *linkp);
 
254
 
 
255
        /* OpenClose debug output stream */
 
256
static void DBUGOpenFile(CODE_STATE *,const char *, const char *, int);
 
257
static void DBUGCloseFile(CODE_STATE *cs, FILE *fp);
 
258
        /* Push current debug settings */
 
259
static void PushState(CODE_STATE *cs);
 
260
        /* Free memory associated with debug state. */
 
261
static void FreeState (CODE_STATE *cs, struct settings *state, int free_state);
 
262
        /* Test for tracing enabled */
 
263
static BOOLEAN DoTrace(CODE_STATE *cs);
 
264
 
 
265
        /* Test to see if file is writable */
 
266
#if !(!defined(HAVE_ACCESS) || defined(MSDOS))
 
267
static BOOLEAN Writable(const char *pathname);
 
268
        /* Change file owner and group */
 
269
static void ChangeOwner(CODE_STATE *cs, char *pathname);
 
270
        /* Allocate memory for runtime support */
 
271
#endif
 
272
 
 
273
static void DoPrefix(CODE_STATE *cs, uint line);
 
274
 
 
275
static char *DbugMalloc(size_t size);
 
276
static const char *BaseName(const char *pathname);
 
277
static void Indent(CODE_STATE *cs, int indent);
 
278
static BOOLEAN InList(struct link *linkp,const char *cp);
 
279
static void dbug_flush(CODE_STATE *);
 
280
static void DbugExit(const char *why);
 
281
static const char *DbugStrTok(const char *s);
 
282
 
 
283
#ifndef THREAD
 
284
        /* Open profile output stream */
 
285
static FILE *OpenProfile(CODE_STATE *cs, const char *name);
 
286
        /* Profile if asked for it */
 
287
static BOOLEAN DoProfile(CODE_STATE *);
 
288
        /* Return current user time (ms) */
 
289
static unsigned long Clock(void);
 
290
#endif
 
291
 
 
292
/*
 
293
 *      Miscellaneous printf format strings.
 
294
 */
 
295
 
 
296
#define ERR_MISSING_RETURN "%s: missing DBUG_RETURN or DBUG_VOID_RETURN macro in function \"%s\"\n"
 
297
#define ERR_OPEN "%s: can't open debug output stream \"%s\": "
 
298
#define ERR_CLOSE "%s: can't close debug file: "
 
299
#define ERR_ABORT "%s: debugger aborting because %s\n"
 
300
#define ERR_CHOWN "%s: can't change owner/group of \"%s\": "
 
301
 
 
302
/*
 
303
 *      Macros and defines for testing file accessibility under UNIX and MSDOS.
 
304
 */
 
305
 
 
306
#undef EXISTS
 
307
#if !defined(HAVE_ACCESS) || defined(MSDOS)
 
308
#define EXISTS(pathname) (FALSE)        /* Assume no existance */
 
309
#define Writable(name) (TRUE)
 
310
#else
 
311
#define EXISTS(pathname)         (access(pathname, F_OK) == 0)
 
312
#define WRITABLE(pathname)       (access(pathname, W_OK) == 0)
 
313
#endif
 
314
#ifndef MSDOS
 
315
#define ChangeOwner(cs,name)
 
316
#endif
 
317
 
 
318
 
 
319
/*
 
320
** Macros to allow dbugging with threads
 
321
*/
 
322
 
 
323
#ifdef THREAD
 
324
#include <my_pthread.h>
 
325
pthread_mutex_t THR_LOCK_dbug;
 
326
 
 
327
static CODE_STATE *code_state(void)
 
328
{
 
329
  CODE_STATE *cs=0;
 
330
  struct st_my_thread_var *tmp;
 
331
 
 
332
  if (!init_done)
 
333
  {
 
334
    pthread_mutex_init(&THR_LOCK_dbug,MY_MUTEX_INIT_FAST);
 
335
    bzero(&init_settings, sizeof(init_settings));
 
336
    init_settings.out_file=stderr;
 
337
    init_settings.flags=OPEN_APPEND;
 
338
    init_done=TRUE;
 
339
  }
 
340
 
 
341
  if ((tmp=my_thread_var))
 
342
  {
 
343
    if (!(cs=(CODE_STATE *) tmp->dbug))
 
344
    {
 
345
      cs=(CODE_STATE*) DbugMalloc(sizeof(*cs));
 
346
      bzero((uchar*) cs,sizeof(*cs));
 
347
      cs->process= db_process ? db_process : "dbug";
 
348
      cs->func="?func";
 
349
      cs->file="?file";
 
350
      cs->stack=&init_settings;
 
351
      tmp->dbug= (void*) cs;
 
352
    }
 
353
  }
 
354
  return cs;
 
355
}
 
356
 
 
357
#else /* !THREAD */
 
358
 
 
359
static CODE_STATE static_code_state=
 
360
{
 
361
  "dbug", "?func", "?file", NULL, &init_settings,
 
362
  NullS, NullS, 0,0,0,0,0,NullS
 
363
};
 
364
 
 
365
static CODE_STATE *code_state(void)
 
366
{
 
367
  if (!init_done)
 
368
  {
 
369
    bzero(&init_settings, sizeof(init_settings));
 
370
    init_settings.out_file=stderr;
 
371
    init_settings.flags=OPEN_APPEND;
 
372
    init_done=TRUE;
 
373
  }
 
374
  return &static_code_state;
 
375
}
 
376
 
 
377
#define pthread_mutex_lock(A) {}
 
378
#define pthread_mutex_unlock(A) {}
 
379
#endif
 
380
 
 
381
/*
 
382
 *      Translate some calls among different systems.
 
383
 */
 
384
 
 
385
#ifdef HAVE_SLEEP
 
386
/* sleep() wants seconds */
 
387
#define Delay(A) sleep(((uint) A)/10)
 
388
#else
 
389
#define Delay(A) (0)
 
390
#endif
 
391
 
 
392
/*
 
393
 *  FUNCTION
 
394
 *
 
395
 *      _db_process_       give the name to the current process/thread
 
396
 *
 
397
 *  SYNOPSIS
 
398
 *
 
399
 *      VOID _process_(name)
 
400
 *      char *name;
 
401
 *
 
402
 */
 
403
 
 
404
void _db_process_(const char *name)
 
405
{
 
406
  CODE_STATE *cs=0;
 
407
 
 
408
  if (!db_process)
 
409
    db_process= name;
 
410
  
 
411
  get_code_state_or_return;
 
412
  cs->process= name;
 
413
}
 
414
 
 
415
 
 
416
/*
 
417
 *  FUNCTION
 
418
 *
 
419
 *      DbugParse       parse control string and set current debugger setting
 
420
 *
 
421
 *  DESCRIPTION
 
422
 *
 
423
 *      Given pointer to a debug control string in "control",
 
424
 *      parses the control string, and sets
 
425
 *      up a current debug settings.
 
426
 *
 
427
 *      The debug control string is a sequence of colon separated fields
 
428
 *      as follows:
 
429
 *
 
430
 *              [+]<field_1>:<field_2>:...:<field_N>
 
431
 *
 
432
 *      Each field consists of a mandatory flag character followed by
 
433
 *      an optional "," and comma separated list of modifiers:
 
434
 *
 
435
 *              [sign]flag[,modifier,modifier,...,modifier]
 
436
 *
 
437
 *      See the manual for the list of supported signs, flags, and modifiers
 
438
 *
 
439
 *      For convenience, any leading "-#" is stripped off.
 
440
 *
 
441
 */
 
442
 
 
443
static void DbugParse(CODE_STATE *cs, const char *control)
 
444
{
 
445
  const char *end;
 
446
  int rel=0;
 
447
  struct settings *stack;
 
448
 
 
449
  get_code_state_or_return;
 
450
  stack= cs->stack;
 
451
 
 
452
  if (control[0] == '-' && control[1] == '#')
 
453
    control+=2;
 
454
 
 
455
  rel= control[0] == '+' || control[0] == '-';
 
456
  if ((!rel || (!stack->out_file && !stack->next)))
 
457
  {
 
458
    stack->flags= 0;
 
459
    stack->delay= 0;
 
460
    stack->maxdepth= 0;
 
461
    stack->sub_level= 0;
 
462
    stack->out_file= stderr;
 
463
    stack->prof_file= NULL;
 
464
    stack->functions= NULL;
 
465
    stack->p_functions= NULL;
 
466
    stack->keywords= NULL;
 
467
    stack->processes= NULL;
 
468
  }
 
469
  else if (!stack->out_file)
 
470
  {
 
471
    stack->flags= stack->next->flags;
 
472
    stack->delay= stack->next->delay;
 
473
    stack->maxdepth= stack->next->maxdepth;
 
474
    stack->sub_level= stack->next->sub_level;
 
475
    strcpy(stack->name, stack->next->name);
 
476
    stack->out_file= stack->next->out_file;
 
477
    stack->prof_file= stack->next->prof_file;
 
478
    if (stack->next == &init_settings)
 
479
    {
 
480
      /* never share with the global parent - it can change under your feet */
 
481
      stack->functions= ListCopy(init_settings.functions);
 
482
      stack->p_functions= ListCopy(init_settings.p_functions);
 
483
      stack->keywords= ListCopy(init_settings.keywords);
 
484
      stack->processes= ListCopy(init_settings.processes);
 
485
    }
 
486
    else
 
487
    {
 
488
      stack->functions= stack->next->functions;
 
489
      stack->p_functions= stack->next->p_functions;
 
490
      stack->keywords= stack->next->keywords;
 
491
      stack->processes= stack->next->processes;
 
492
    }
 
493
  }
 
494
 
 
495
  end= DbugStrTok(control);
 
496
  while (control < end)
 
497
  {
 
498
    int c, sign= (*control == '+') ? 1 : (*control == '-') ? -1 : 0;
 
499
    if (sign) control++;
 
500
    if (!rel) sign=0;
 
501
    c= *control++;
 
502
    if (*control == ',') control++;
 
503
    /* XXX when adding new cases here, don't forget _db_explain_ ! */
 
504
    switch (c) {
 
505
    case 'd':
 
506
      if (sign < 0 && control == end)
 
507
      {
 
508
        if (!is_shared(stack, keywords))
 
509
          FreeList(stack->keywords);
 
510
        stack->keywords=NULL;
 
511
        stack->flags &= ~DEBUG_ON;
 
512
        break;
 
513
      }
 
514
      if (rel && is_shared(stack, keywords))
 
515
        stack->keywords= ListCopy(stack->keywords);
 
516
      if (sign < 0)
 
517
      {
 
518
        if (DEBUGGING)
 
519
          stack->keywords= ListDel(stack->keywords, control, end);
 
520
      break;
 
521
      }
 
522
      stack->keywords= ListAdd(stack->keywords, control, end);
 
523
      stack->flags |= DEBUG_ON;
 
524
      break;
 
525
    case 'D':
 
526
      stack->delay= atoi(control);
 
527
      break;
 
528
    case 'f':
 
529
      if (sign < 0 && control == end)
 
530
      {
 
531
        if (!is_shared(stack,functions))
 
532
          FreeList(stack->functions);
 
533
        stack->functions=NULL;
 
534
        break;
 
535
      }
 
536
      if (rel && is_shared(stack,functions))
 
537
        stack->functions= ListCopy(stack->functions);
 
538
      if (sign < 0)
 
539
        stack->functions= ListDel(stack->functions, control, end);
 
540
      else
 
541
        stack->functions= ListAdd(stack->functions, control, end);
 
542
      break;
 
543
    case 'F':
 
544
      if (sign < 0)
 
545
        stack->flags &= ~FILE_ON;
 
546
      else
 
547
        stack->flags |= FILE_ON;
 
548
      break;
 
549
    case 'i':
 
550
      if (sign < 0)
 
551
        stack->flags &= ~PID_ON;
 
552
      else
 
553
        stack->flags |= PID_ON;
 
554
      break;
 
555
#ifndef THREAD
 
556
    case 'g':
 
557
      if (OpenProfile(cs, PROF_FILE))
 
558
      {
 
559
        stack->flags |= PROFILE_ON;
 
560
        stack->p_functions= ListAdd(stack->p_functions, control, end);
 
561
      }
 
562
      break;
 
563
#endif
 
564
    case 'L':
 
565
      if (sign < 0)
 
566
        stack->flags &= ~LINE_ON;
 
567
      else
 
568
        stack->flags |= LINE_ON;
 
569
      break;
 
570
    case 'n':
 
571
      if (sign < 0)
 
572
        stack->flags &= ~DEPTH_ON;
 
573
      else
 
574
        stack->flags |= DEPTH_ON;
 
575
      break;
 
576
    case 'N':
 
577
      if (sign < 0)
 
578
        stack->flags &= ~NUMBER_ON;
 
579
      else
 
580
        stack->flags |= NUMBER_ON;
 
581
      break;
 
582
    case 'A':
 
583
    case 'O':
 
584
      stack->flags |= FLUSH_ON_WRITE;
 
585
      /* fall through */
 
586
    case 'a':
 
587
    case 'o':
 
588
      if (sign < 0)
 
589
      {
 
590
        if (!is_shared(stack, out_file))
 
591
          DBUGCloseFile(cs, stack->out_file);
 
592
        stack->flags &= ~FLUSH_ON_WRITE;
 
593
        stack->out_file= stderr;
 
594
        break;
 
595
      }
 
596
      if (c == 'a' || c == 'A')
 
597
        stack->flags |= OPEN_APPEND;
 
598
      else
 
599
        stack->flags &= ~OPEN_APPEND;
 
600
      if (control != end)
 
601
        DBUGOpenFile(cs, control, end, stack->flags & OPEN_APPEND);
 
602
      else
 
603
        DBUGOpenFile(cs, "-",0,0);
 
604
      break;
 
605
    case 'p':
 
606
      if (sign < 0 && control == end)
 
607
      {
 
608
        if (!is_shared(stack,processes))
 
609
          FreeList(stack->processes);
 
610
        stack->processes=NULL;
 
611
        break;
 
612
      }
 
613
      if (rel && is_shared(stack, processes))
 
614
        stack->processes= ListCopy(stack->processes);
 
615
      if (sign < 0)
 
616
        stack->processes= ListDel(stack->processes, control, end);
 
617
      else
 
618
        stack->processes= ListAdd(stack->processes, control, end);
 
619
      break;
 
620
    case 'P':
 
621
      if (sign < 0)
 
622
        stack->flags &= ~PROCESS_ON;
 
623
      else
 
624
        stack->flags |= PROCESS_ON;
 
625
      break;
 
626
    case 'r':
 
627
      stack->sub_level= cs->level;
 
628
      break;
 
629
    case 't':
 
630
      if (sign < 0)
 
631
      {
 
632
        if (control != end)
 
633
          stack->maxdepth-= atoi(control);
 
634
        else
 
635
          stack->maxdepth= 0;
 
636
      }
 
637
      else
 
638
      {
 
639
        if (control != end)
 
640
          stack->maxdepth+= atoi(control);
 
641
        else
 
642
          stack->maxdepth= MAXDEPTH;
 
643
      }
 
644
      if (stack->maxdepth > 0)
 
645
        stack->flags |= TRACE_ON;
 
646
      else
 
647
        stack->flags &= ~TRACE_ON;
 
648
      break;
 
649
    case 'T':
 
650
      if (sign < 0)
 
651
        stack->flags &= ~TIMESTAMP_ON;
 
652
      else
 
653
        stack->flags |= TIMESTAMP_ON;
 
654
      break;
 
655
    case 'S':
 
656
      if (sign < 0)
 
657
        stack->flags &= ~SANITY_CHECK_ON;
 
658
      else
 
659
        stack->flags |= SANITY_CHECK_ON;
 
660
      break;
 
661
    }
 
662
    if (!*end)
 
663
      break;
 
664
    control=end+1;
 
665
    end= DbugStrTok(control);
 
666
  }
 
667
}
 
668
 
 
669
 
 
670
/*
 
671
 *  FUNCTION
 
672
 *
 
673
 *      _db_set_       set current debugger settings
 
674
 *
 
675
 *  SYNOPSIS
 
676
 *
 
677
 *      VOID _db_set_(control)
 
678
 *      char *control;
 
679
 *
 
680
 *  DESCRIPTION
 
681
 *
 
682
 *      Given pointer to a debug control string in "control",
 
683
 *      parses the control string, and sets up a current debug
 
684
 *      settings. Pushes a new debug settings if the current is
 
685
 *      set to the initial debugger settings.
 
686
 */
 
687
 
 
688
void _db_set_(CODE_STATE *cs, const char *control)
 
689
{
 
690
  get_code_state_or_return;
 
691
 
 
692
  if (cs->stack == &init_settings)
 
693
    PushState(cs);
 
694
 
 
695
  DbugParse(cs, control);
 
696
}
 
697
 
 
698
 
 
699
/*
 
700
 *  FUNCTION
 
701
 *
 
702
 *      _db_push_       push current debugger settings and set up new one
 
703
 *
 
704
 *  SYNOPSIS
 
705
 *
 
706
 *      VOID _db_push_(control)
 
707
 *      char *control;
 
708
 *
 
709
 *  DESCRIPTION
 
710
 *
 
711
 *      Given pointer to a debug control string in "control", pushes
 
712
 *      the current debug settings, parses the control string, and sets
 
713
 *      up a new debug settings with DbugParse()
 
714
 *
 
715
 */
 
716
 
 
717
void _db_push_(const char *control)
 
718
{
 
719
  CODE_STATE *cs=0;
 
720
  get_code_state_or_return;
 
721
  PushState(cs);
 
722
  DbugParse(cs, control);
 
723
}
 
724
 
 
725
/*
 
726
 *  FUNCTION
 
727
 *
 
728
 *      _db_set_init_       set initial debugger settings
 
729
 *
 
730
 *  SYNOPSIS
 
731
 *
 
732
 *      VOID _db_set_init_(control)
 
733
 *      char *control;
 
734
 *
 
735
 *  DESCRIPTION
 
736
 *      see _db_set_
 
737
 *
 
738
 */
 
739
 
 
740
void _db_set_init_(const char *control)
 
741
{
 
742
  CODE_STATE tmp_cs;
 
743
  bzero((uchar*) &tmp_cs, sizeof(tmp_cs));
 
744
  tmp_cs.stack= &init_settings;
 
745
  DbugParse(&tmp_cs, control);
 
746
}
 
747
 
 
748
/*
 
749
 *  FUNCTION
 
750
 *
 
751
 *      _db_pop_    pop the debug stack
 
752
 *
 
753
 *  DESCRIPTION
 
754
 *
 
755
 *      Pops the debug stack, returning the debug settings to its
 
756
 *      condition prior to the most recent _db_push_ invocation.
 
757
 *      Note that the pop will fail if it would remove the last
 
758
 *      valid settings from the stack.  This prevents user errors
 
759
 *      in the push/pop sequence from screwing up the debugger.
 
760
 *      Maybe there should be some kind of warning printed if the
 
761
 *      user tries to pop too many states.
 
762
 *
 
763
 */
 
764
 
 
765
void _db_pop_()
 
766
{
 
767
  struct settings *discard;
 
768
  CODE_STATE *cs=0;
 
769
 
 
770
  get_code_state_or_return;
 
771
 
 
772
  discard= cs->stack;
 
773
  if (discard->next != NULL)
 
774
  {
 
775
    cs->stack= discard->next;
 
776
    FreeState(cs, discard, 1);
 
777
  }
 
778
}
 
779
 
 
780
/*
 
781
 *  FUNCTION
 
782
 *
 
783
 *      _db_explain_    generates 'control' string for the current settings
 
784
 *
 
785
 *  RETURN
 
786
 *      0 - ok
 
787
 *      1  - buffer too short, output truncated
 
788
 *
 
789
 */
 
790
 
 
791
/* helper macros */
 
792
#define char_to_buf(C)    do {                  \
 
793
        *buf++=(C);                             \
 
794
        if (buf >= end) goto overflow;          \
 
795
      } while (0)
 
796
#define str_to_buf(S)    do {                   \
 
797
        char_to_buf(',');                       \
 
798
        buf=strnmov(buf, (S), len+1);           \
 
799
        if (buf >= end) goto overflow;          \
 
800
      } while (0)
 
801
#define list_to_buf(l)  do {                    \
 
802
        struct link *listp=(l);                 \
 
803
        while (listp)                           \
 
804
        {                                       \
 
805
          str_to_buf(listp->str);               \
 
806
          listp=listp->next_link;               \
 
807
        }                                       \
 
808
      } while (0)
 
809
#define int_to_buf(i)  do {                     \
 
810
        char b[50];                             \
 
811
        int10_to_str((i), b, 10);               \
 
812
        str_to_buf(b);                          \
 
813
      } while (0)
 
814
#define colon_to_buf   do {                     \
 
815
        if (buf != start) char_to_buf(':');     \
 
816
      } while(0)
 
817
#define op_int_to_buf(C, val, def) do {         \
 
818
        if ((val) != (def))                     \
 
819
        {                                       \
 
820
          colon_to_buf;                         \
 
821
          char_to_buf((C));                     \
 
822
          int_to_buf(val);                      \
 
823
        }                                       \
 
824
      } while (0)
 
825
#define op_intf_to_buf(C, val, def, cond) do {  \
 
826
        if ((cond))                             \
 
827
        {                                       \
 
828
          colon_to_buf;                         \
 
829
          char_to_buf((C));                     \
 
830
          if ((val) != (def)) int_to_buf(val);  \
 
831
        }                                       \
 
832
      } while (0)
 
833
#define op_str_to_buf(C, val, cond) do {        \
 
834
        if ((cond))                             \
 
835
        {                                       \
 
836
          char *s=(val);                        \
 
837
          colon_to_buf;                         \
 
838
          char_to_buf((C));                     \
 
839
          if (*s) str_to_buf(s);                \
 
840
        }                                       \
 
841
      } while (0)
 
842
#define op_list_to_buf(C, val, cond) do {       \
 
843
        if ((cond))                             \
 
844
        {                                       \
 
845
          colon_to_buf;                         \
 
846
          char_to_buf((C));                     \
 
847
          list_to_buf(val);                     \
 
848
        }                                       \
 
849
      } while (0)
 
850
#define op_bool_to_buf(C, cond) do {            \
 
851
        if ((cond))                             \
 
852
        {                                       \
 
853
          colon_to_buf;                         \
 
854
          char_to_buf((C));                     \
 
855
        }                                       \
 
856
      } while (0)
 
857
 
 
858
int _db_explain_ (CODE_STATE *cs, char *buf, size_t len)
 
859
{
 
860
  char *start=buf, *end=buf+len-4;
 
861
 
 
862
  get_code_state_or_return *buf=0;
 
863
 
 
864
  op_list_to_buf('d', cs->stack->keywords, DEBUGGING);
 
865
  op_int_to_buf ('D', cs->stack->delay, 0);
 
866
  op_list_to_buf('f', cs->stack->functions, cs->stack->functions);
 
867
  op_bool_to_buf('F', cs->stack->flags & FILE_ON);
 
868
  op_bool_to_buf('i', cs->stack->flags & PID_ON);
 
869
  op_list_to_buf('g', cs->stack->p_functions, PROFILING);
 
870
  op_bool_to_buf('L', cs->stack->flags & LINE_ON);
 
871
  op_bool_to_buf('n', cs->stack->flags & DEPTH_ON);
 
872
  op_bool_to_buf('N', cs->stack->flags & NUMBER_ON);
 
873
  op_str_to_buf(
 
874
    ((cs->stack->flags & FLUSH_ON_WRITE ? 0 : 32) |
 
875
     (cs->stack->flags & OPEN_APPEND ? 'A' : 'O')),
 
876
    cs->stack->name, cs->stack->out_file != stderr);
 
877
  op_list_to_buf('p', cs->stack->processes, cs->stack->processes);
 
878
  op_bool_to_buf('P', cs->stack->flags & PROCESS_ON);
 
879
  op_bool_to_buf('r', cs->stack->sub_level != 0);
 
880
  op_intf_to_buf('t', cs->stack->maxdepth, MAXDEPTH, TRACING);
 
881
  op_bool_to_buf('T', cs->stack->flags & TIMESTAMP_ON);
 
882
  op_bool_to_buf('S', cs->stack->flags & SANITY_CHECK_ON);
 
883
 
 
884
  *buf= '\0';
 
885
  return 0;
 
886
 
 
887
overflow:
 
888
  *end++= '.';
 
889
  *end++= '.';
 
890
  *end++= '.';
 
891
  *end=   '\0';
 
892
  return 1;
 
893
}
 
894
 
 
895
#undef char_to_buf
 
896
#undef str_to_buf
 
897
#undef list_to_buf
 
898
#undef int_to_buf
 
899
#undef colon_to_buf
 
900
#undef op_int_to_buf
 
901
#undef op_intf_to_buf
 
902
#undef op_str_to_buf
 
903
#undef op_list_to_buf
 
904
#undef op_bool_to_buf
 
905
 
 
906
/*
 
907
 *  FUNCTION
 
908
 *
 
909
 *      _db_explain_init_       explain initial debugger settings
 
910
 *
 
911
 *  DESCRIPTION
 
912
 *      see _db_explain_
 
913
 */
 
914
 
 
915
int _db_explain_init_(char *buf, size_t len)
 
916
{
 
917
  CODE_STATE cs;
 
918
  bzero((uchar*) &cs,sizeof(cs));
 
919
  cs.stack=&init_settings;
 
920
  return _db_explain_(&cs, buf, len);
 
921
}
 
922
 
 
923
/*
 
924
 *  FUNCTION
 
925
 *
 
926
 *      _db_enter_    process entry point to user function
 
927
 *
 
928
 *  SYNOPSIS
 
929
 *
 
930
 *      VOID _db_enter_(_func_, _file_, _line_,
 
931
 *                       _sfunc_, _sfile_, _slevel_, _sframep_)
 
932
 *      char *_func_;           points to current function name
 
933
 *      char *_file_;           points to current file name
 
934
 *      int _line_;             called from source line number
 
935
 *      char **_sfunc_;         save previous _func_
 
936
 *      char **_sfile_;         save previous _file_
 
937
 *      int *_slevel_;          save previous nesting level
 
938
 *      char ***_sframep_;      save previous frame pointer
 
939
 *
 
940
 *  DESCRIPTION
 
941
 *
 
942
 *      Called at the beginning of each user function to tell
 
943
 *      the debugger that a new function has been entered.
 
944
 *      Note that the pointers to the previous user function
 
945
 *      name and previous user file name are stored on the
 
946
 *      caller's stack (this is why the ENTER macro must be
 
947
 *      the first "executable" code in a function, since it
 
948
 *      allocates these storage locations).  The previous nesting
 
949
 *      level is also stored on the callers stack for internal
 
950
 *      self consistency checks.
 
951
 *
 
952
 *      Also prints a trace line if tracing is enabled and
 
953
 *      increments the current function nesting depth.
 
954
 *
 
955
 *      Note that this mechanism allows the debugger to know
 
956
 *      what the current user function is at all times, without
 
957
 *      maintaining an internal stack for the function names.
 
958
 *
 
959
 */
 
960
 
 
961
void _db_enter_(const char *_func_, const char *_file_,
 
962
                uint _line_, const char **_sfunc_, const char **_sfile_,
 
963
                uint *_slevel_, char ***_sframep_ __attribute__((unused)))
 
964
{
 
965
  int save_errno=errno;
 
966
  CODE_STATE *cs=0;
 
967
  get_code_state_or_return;
 
968
 
 
969
  *_sfunc_= cs->func;
 
970
  *_sfile_= cs->file;
 
971
  cs->func=  _func_;
 
972
  cs->file=  _file_;
 
973
  *_slevel_=  ++cs->level;
 
974
#ifndef THREAD
 
975
  *_sframep_= cs->framep;
 
976
  cs->framep= (char **) _sframep_;
 
977
  if (DoProfile(cs))
 
978
  {
 
979
    long stackused;
 
980
    if (*cs->framep == NULL)
 
981
      stackused= 0;
 
982
    else
 
983
    {
 
984
      stackused= ((long)(*cs->framep)) - ((long)(cs->framep));
 
985
      stackused= stackused > 0 ? stackused : -stackused;
 
986
    }
 
987
    (void) fprintf(cs->stack->prof_file, PROF_EFMT , Clock(), cs->func);
 
988
#ifdef AUTOS_REVERSE
 
989
    (void) fprintf(cs->stack->prof_file, PROF_SFMT, cs->framep, stackused, *_sfunc_);
 
990
#else
 
991
    (void) fprintf(cs->stack->prof_file, PROF_SFMT, (ulong) cs->framep, stackused,
 
992
                    cs->func);
 
993
#endif
 
994
    (void) fflush(cs->stack->prof_file);
 
995
  }
 
996
#endif
 
997
  if (DoTrace(cs))
 
998
  {
 
999
    if (!cs->locked)
 
1000
      pthread_mutex_lock(&THR_LOCK_dbug);
 
1001
    DoPrefix(cs, _line_);
 
1002
    Indent(cs, cs->level);
 
1003
    (void) fprintf(cs->stack->out_file, ">%s\n", cs->func);
 
1004
    dbug_flush(cs);                       /* This does a unlock */
 
1005
  }
 
1006
#ifdef SAFEMALLOC
 
1007
  if (cs->stack->flags & SANITY_CHECK_ON)
 
1008
    if (_sanity(_file_,_line_))               /* Check of safemalloc */
 
1009
      cs->stack->flags &= ~SANITY_CHECK_ON;
 
1010
#endif
 
1011
  errno=save_errno;
 
1012
}
 
1013
 
 
1014
/*
 
1015
 *  FUNCTION
 
1016
 *
 
1017
 *      _db_return_    process exit from user function
 
1018
 *
 
1019
 *  SYNOPSIS
 
1020
 *
 
1021
 *      VOID _db_return_(_line_, _sfunc_, _sfile_, _slevel_)
 
1022
 *      int _line_;             current source line number
 
1023
 *      char **_sfunc_;         where previous _func_ is to be retrieved
 
1024
 *      char **_sfile_;         where previous _file_ is to be retrieved
 
1025
 *      int *_slevel_;          where previous level was stashed
 
1026
 *
 
1027
 *  DESCRIPTION
 
1028
 *
 
1029
 *      Called just before user function executes an explicit or implicit
 
1030
 *      return.  Prints a trace line if trace is enabled, decrements
 
1031
 *      the current nesting level, and restores the current function and
 
1032
 *      file names from the defunct function's stack.
 
1033
 *
 
1034
 */
 
1035
 
 
1036
/* helper macro */
 
1037
void _db_return_(uint _line_, const char **_sfunc_,
 
1038
                 const char **_sfile_, uint *_slevel_)
 
1039
{
 
1040
  int save_errno=errno;
 
1041
  CODE_STATE *cs=0;
 
1042
  get_code_state_or_return;
 
1043
 
 
1044
  if (cs->level != (int) *_slevel_)
 
1045
  {
 
1046
    if (!cs->locked)
 
1047
      pthread_mutex_lock(&THR_LOCK_dbug);
 
1048
    (void) fprintf(cs->stack->out_file, ERR_MISSING_RETURN, cs->process,
 
1049
                   cs->func);
 
1050
    dbug_flush(cs);
 
1051
  }
 
1052
  else
 
1053
  {
 
1054
#ifdef SAFEMALLOC
 
1055
    if (cs->stack->flags & SANITY_CHECK_ON)
 
1056
    {
 
1057
      if (_sanity(*_sfile_,_line_))
 
1058
        cs->stack->flags &= ~SANITY_CHECK_ON;
 
1059
    }
 
1060
#endif
 
1061
#ifndef THREAD
 
1062
    if (DoProfile(cs))
 
1063
      (void) fprintf(cs->stack->prof_file, PROF_XFMT, Clock(), cs->func);
 
1064
#endif
 
1065
    if (DoTrace(cs))
 
1066
    {
 
1067
      if (!cs->locked)
 
1068
        pthread_mutex_lock(&THR_LOCK_dbug);
 
1069
      DoPrefix(cs, _line_);
 
1070
      Indent(cs, cs->level);
 
1071
      (void) fprintf(cs->stack->out_file, "<%s\n", cs->func);
 
1072
      dbug_flush(cs);
 
1073
    }
 
1074
  }
 
1075
  cs->level= *_slevel_-1;
 
1076
  cs->func= *_sfunc_;
 
1077
  cs->file= *_sfile_;
 
1078
#ifndef THREAD
 
1079
  if (cs->framep != NULL)
 
1080
    cs->framep= (char **) *cs->framep;
 
1081
#endif
 
1082
  errno=save_errno;
 
1083
}
 
1084
 
 
1085
 
 
1086
/*
 
1087
 *  FUNCTION
 
1088
 *
 
1089
 *      _db_pargs_    log arguments for subsequent use by _db_doprnt_()
 
1090
 *
 
1091
 *  SYNOPSIS
 
1092
 *
 
1093
 *      VOID _db_pargs_(_line_, keyword)
 
1094
 *      int _line_;
 
1095
 *      char *keyword;
 
1096
 *
 
1097
 *  DESCRIPTION
 
1098
 *
 
1099
 *      The new universal printing macro DBUG_PRINT, which replaces
 
1100
 *      all forms of the DBUG_N macros, needs two calls to runtime
 
1101
 *      support routines.  The first, this function, remembers arguments
 
1102
 *      that are used by the subsequent call to _db_doprnt_().
 
1103
 *
 
1104
 */
 
1105
 
 
1106
void _db_pargs_(uint _line_, const char *keyword)
 
1107
{
 
1108
  CODE_STATE *cs=0;
 
1109
  get_code_state_or_return;
 
1110
  cs->u_line= _line_;
 
1111
  cs->u_keyword= keyword;
 
1112
}
 
1113
 
 
1114
 
 
1115
/*
 
1116
 *  FUNCTION
 
1117
 *
 
1118
 *      _db_doprnt_    handle print of debug lines
 
1119
 *
 
1120
 *  SYNOPSIS
 
1121
 *
 
1122
 *      VOID _db_doprnt_(format, va_alist)
 
1123
 *      char *format;
 
1124
 *      va_dcl;
 
1125
 *
 
1126
 *  DESCRIPTION
 
1127
 *
 
1128
 *      When invoked via one of the DBUG macros, tests the current keyword
 
1129
 *      set by calling _db_pargs_() to see if that macro has been selected
 
1130
 *      for processing via the debugger control string, and if so, handles
 
1131
 *      printing of the arguments via the format string.  The line number
 
1132
 *      of the DBUG macro in the source is found in u_line.
 
1133
 *
 
1134
 *      Note that the format string SHOULD NOT include a terminating
 
1135
 *      newline, this is supplied automatically.
 
1136
 *
 
1137
 */
 
1138
 
 
1139
#include <stdarg.h>
 
1140
 
 
1141
void _db_doprnt_(const char *format,...)
 
1142
{
 
1143
  va_list args;
 
1144
 
 
1145
  CODE_STATE *cs=0;
 
1146
  get_code_state_or_return;
 
1147
 
 
1148
  va_start(args,format);
 
1149
 
 
1150
  if (_db_keyword_(cs, cs->u_keyword))
 
1151
  {
 
1152
    int save_errno=errno;
 
1153
    if (!cs->locked)
 
1154
      pthread_mutex_lock(&THR_LOCK_dbug);
 
1155
    DoPrefix(cs, cs->u_line);
 
1156
    if (TRACING)
 
1157
      Indent(cs, cs->level + 1);
 
1158
    else
 
1159
      (void) fprintf(cs->stack->out_file, "%s: ", cs->func);
 
1160
    (void) fprintf(cs->stack->out_file, "%s: ", cs->u_keyword);
 
1161
    (void) vfprintf(cs->stack->out_file, format, args);
 
1162
    (void) fputc('\n',cs->stack->out_file);
 
1163
    dbug_flush(cs);
 
1164
    errno=save_errno;
 
1165
  }
 
1166
  va_end(args);
 
1167
}
 
1168
 
 
1169
 
 
1170
/*
 
1171
 *  FUNCTION
 
1172
 *
 
1173
 *            _db_dump_    dump a string in hex
 
1174
 *
 
1175
 *  SYNOPSIS
 
1176
 *
 
1177
 *            void _db_dump_(_line_,keyword,memory,length)
 
1178
 *            int _line_;               current source line number
 
1179
 *            char *keyword;
 
1180
 *            char *memory;             Memory to print
 
1181
 *            int length;               Bytes to print
 
1182
 *
 
1183
 *  DESCRIPTION
 
1184
 *  Dump N characters in a binary array.
 
1185
 *  Is used to examine corrputed memory or arrays.
 
1186
 */
 
1187
 
 
1188
void _db_dump_(uint _line_, const char *keyword,
 
1189
               const unsigned char *memory, size_t length)
 
1190
{
 
1191
  int pos;
 
1192
  char dbuff[90];
 
1193
 
 
1194
  CODE_STATE *cs=0;
 
1195
  get_code_state_or_return;
 
1196
 
 
1197
  if (_db_keyword_(cs, keyword))
 
1198
  {
 
1199
    if (!cs->locked)
 
1200
      pthread_mutex_lock(&THR_LOCK_dbug);
 
1201
    DoPrefix(cs, _line_);
 
1202
    if (TRACING)
 
1203
    {
 
1204
      Indent(cs, cs->level + 1);
 
1205
      pos= min(max(cs->level-cs->stack->sub_level,0)*INDENT,80);
 
1206
    }
 
1207
    else
 
1208
    {
 
1209
      fprintf(cs->stack->out_file, "%s: ", cs->func);
 
1210
    }
 
1211
    sprintf(dbuff,"%s: Memory: 0x%lx  Bytes: (%ld)\n",
 
1212
            keyword, (ulong) memory, (long) length);
 
1213
    (void) fputs(dbuff,cs->stack->out_file);
 
1214
 
 
1215
    pos=0;
 
1216
    while (length-- > 0)
 
1217
    {
 
1218
      uint tmp= *((unsigned char*) memory++);
 
1219
      if ((pos+=3) >= 80)
 
1220
      {
 
1221
        fputc('\n',cs->stack->out_file);
 
1222
        pos=3;
 
1223
      }
 
1224
      fputc(_dig_vec_upper[((tmp >> 4) & 15)], cs->stack->out_file);
 
1225
      fputc(_dig_vec_upper[tmp & 15], cs->stack->out_file);
 
1226
      fputc(' ',cs->stack->out_file);
 
1227
    }
 
1228
    (void) fputc('\n',cs->stack->out_file);
 
1229
    dbug_flush(cs);
 
1230
  }
 
1231
}
 
1232
 
 
1233
 
 
1234
/*
 
1235
 *  FUNCTION
 
1236
 *
 
1237
 *      ListAdd    add to the list modifiers from debug control string
 
1238
 *
 
1239
 *  SYNOPSIS
 
1240
 *
 
1241
 *      static struct link *ListAdd(listp, ctlp, end)
 
1242
 *      struct link *listp;
 
1243
 *      char *ctlp;
 
1244
 *      char *end;
 
1245
 *
 
1246
 *  DESCRIPTION
 
1247
 *
 
1248
 *      Given pointer to a comma separated list of strings in "cltp",
 
1249
 *      parses the list, and adds it to listp, returning a pointer
 
1250
 *      to the new list
 
1251
 *
 
1252
 *      Note that since each link is added at the head of the list,
 
1253
 *      the final list will be in "reverse order", which is not
 
1254
 *      significant for our usage here.
 
1255
 *
 
1256
 */
 
1257
 
 
1258
static struct link *ListAdd(struct link *head,
 
1259
                             const char *ctlp, const char *end)
 
1260
{
 
1261
  const char *start;
 
1262
  struct link *new_malloc;
 
1263
  int len;
 
1264
 
 
1265
  while (ctlp < end)
 
1266
  {
 
1267
    start= ctlp;
 
1268
    while (ctlp < end && *ctlp != ',')
 
1269
      ctlp++;
 
1270
    len=ctlp-start;
 
1271
    new_malloc= (struct link *) DbugMalloc(sizeof(struct link)+len);
 
1272
    memcpy(new_malloc->str, start, len);
 
1273
    new_malloc->str[len]=0;
 
1274
    new_malloc->next_link= head;
 
1275
    head= new_malloc;
 
1276
    ctlp++;
 
1277
  }
 
1278
  return head;
 
1279
}
 
1280
 
 
1281
/*
 
1282
 *  FUNCTION
 
1283
 *
 
1284
 *      ListDel    remove from the list modifiers in debug control string
 
1285
 *
 
1286
 *  SYNOPSIS
 
1287
 *
 
1288
 *      static struct link *ListDel(listp, ctlp, end)
 
1289
 *      struct link *listp;
 
1290
 *      char *ctlp;
 
1291
 *      char *end;
 
1292
 *
 
1293
 *  DESCRIPTION
 
1294
 *
 
1295
 *      Given pointer to a comma separated list of strings in "cltp",
 
1296
 *      parses the list, and removes these strings from the listp,
 
1297
 *      returning a pointer to the new list.
 
1298
 *
 
1299
 */
 
1300
 
 
1301
static struct link *ListDel(struct link *head,
 
1302
                             const char *ctlp, const char *end)
 
1303
{
 
1304
  const char *start;
 
1305
  struct link **cur;
 
1306
  int len;
 
1307
 
 
1308
  while (ctlp < end)
 
1309
  {
 
1310
    start= ctlp;
 
1311
    while (ctlp < end && *ctlp != ',')
 
1312
      ctlp++;
 
1313
    len=ctlp-start;
 
1314
    cur=&head;
 
1315
    do
 
1316
    {
 
1317
      while (*cur && !strncmp((*cur)->str, start, len))
 
1318
      {
 
1319
        struct link *delme=*cur;
 
1320
        *cur=(*cur)->next_link;
 
1321
        free((void*) delme);
 
1322
      }
 
1323
    } while (*cur && *(cur=&((*cur)->next_link)));
 
1324
  }
 
1325
  return head;
 
1326
}
 
1327
 
 
1328
/*
 
1329
 *  FUNCTION
 
1330
 *
 
1331
 *      ListCopy    make a copy of the list
 
1332
 *
 
1333
 *  SYNOPSIS
 
1334
 *
 
1335
 *      static struct link *ListCopy(orig)
 
1336
 *      struct link *orig;
 
1337
 *
 
1338
 *  DESCRIPTION
 
1339
 *
 
1340
 *      Given pointer to list, which contains a copy of every element from
 
1341
 *      the original list.
 
1342
 *
 
1343
 *      the orig pointer can be NULL
 
1344
 *
 
1345
 *      Note that since each link is added at the head of the list,
 
1346
 *      the final list will be in "reverse order", which is not
 
1347
 *      significant for our usage here.
 
1348
 *
 
1349
 */
 
1350
 
 
1351
static struct link *ListCopy(struct link *orig)
 
1352
{
 
1353
  struct link *new_malloc;
 
1354
  struct link *head;
 
1355
  int len;
 
1356
 
 
1357
  head= NULL;
 
1358
  while (orig != NULL)
 
1359
  {
 
1360
    len= strlen(orig->str);
 
1361
    new_malloc= (struct link *) DbugMalloc(sizeof(struct link)+len);
 
1362
    memcpy(new_malloc->str, orig->str, len);
 
1363
    new_malloc->str[len]= 0;
 
1364
    new_malloc->next_link= head;
 
1365
    head= new_malloc;
 
1366
    orig= orig->next_link;
 
1367
  }
 
1368
  return head;
 
1369
}
 
1370
 
 
1371
/*
 
1372
 *  FUNCTION
 
1373
 *
 
1374
 *      InList    test a given string for member of a given list
 
1375
 *
 
1376
 *  SYNOPSIS
 
1377
 *
 
1378
 *      static BOOLEAN InList(linkp, cp)
 
1379
 *      struct link *linkp;
 
1380
 *      char *cp;
 
1381
 *
 
1382
 *  DESCRIPTION
 
1383
 *
 
1384
 *      Tests the string pointed to by "cp" to determine if it is in
 
1385
 *      the list pointed to by "linkp".  Linkp points to the first
 
1386
 *      link in the list.  If linkp is NULL then the string is treated
 
1387
 *      as if it is in the list (I.E all strings are in the null list).
 
1388
 *      This may seem rather strange at first but leads to the desired
 
1389
 *      operation if no list is given.  The net effect is that all
 
1390
 *      strings will be accepted when there is no list, and when there
 
1391
 *      is a list, only those strings in the list will be accepted.
 
1392
 *
 
1393
 */
 
1394
 
 
1395
static BOOLEAN InList(struct link *linkp, const char *cp)
 
1396
{
 
1397
  REGISTER struct link *scan;
 
1398
  REGISTER BOOLEAN result;
 
1399
 
 
1400
  if (linkp == NULL)
 
1401
    result= TRUE;
 
1402
  else
 
1403
  {
 
1404
    result= FALSE;
 
1405
    for (scan= linkp; scan != NULL; scan= scan->next_link)
 
1406
    {
 
1407
      if (!strcmp(scan->str, cp))
 
1408
      {
 
1409
        result= TRUE;
 
1410
        break;
 
1411
      }
 
1412
    }
 
1413
  }
 
1414
  return result;
 
1415
}
 
1416
 
 
1417
 
 
1418
/*
 
1419
 *  FUNCTION
 
1420
 *
 
1421
 *      PushState    push current settings onto stack and set up new one
 
1422
 *
 
1423
 *  SYNOPSIS
 
1424
 *
 
1425
 *      static VOID PushState()
 
1426
 *
 
1427
 *  DESCRIPTION
 
1428
 *
 
1429
 *      Pushes the current settings on the settings stack, and creates
 
1430
 *      a new settings. The new settings is NOT initialized
 
1431
 *
 
1432
 *      The settings stack is a linked list of settings, with the new
 
1433
 *      settings added at the head.  This allows the stack to grow
 
1434
 *      to the limits of memory if necessary.
 
1435
 *
 
1436
 */
 
1437
 
 
1438
static void PushState(CODE_STATE *cs)
 
1439
{
 
1440
  struct settings *new_malloc;
 
1441
 
 
1442
  new_malloc= (struct settings *) DbugMalloc(sizeof(struct settings));
 
1443
  new_malloc->next= cs->stack;
 
1444
  new_malloc->out_file= NULL;
 
1445
  cs->stack= new_malloc;
 
1446
}
 
1447
 
 
1448
/*
 
1449
 *  FUNCTION
 
1450
 *
 
1451
 *      FreeState    Free memory associated with a struct state.
 
1452
 *
 
1453
 *  SYNOPSIS
 
1454
 *
 
1455
 *      static void FreeState (state)
 
1456
 *      struct state *state;
 
1457
 *      int free_state;
 
1458
 *
 
1459
 *  DESCRIPTION
 
1460
 *
 
1461
 *      Deallocates the memory allocated for various information in a
 
1462
 *      state. If free_state is set, also free 'state'
 
1463
 *
 
1464
 */
 
1465
static void FreeState(CODE_STATE *cs, struct settings *state, int free_state)
 
1466
{
 
1467
  if (!is_shared(state, keywords))
 
1468
    FreeList(state->keywords);
 
1469
  if (!is_shared(state, functions))
 
1470
    FreeList(state->functions);
 
1471
  if (!is_shared(state, processes))
 
1472
    FreeList(state->processes);
 
1473
  if (!is_shared(state, p_functions))
 
1474
    FreeList(state->p_functions);
 
1475
  if (!is_shared(state, out_file))
 
1476
    DBUGCloseFile(cs, state->out_file);
 
1477
  (void) fflush(cs->stack->out_file);
 
1478
  if (state->prof_file)
 
1479
    DBUGCloseFile(cs, state->prof_file);
 
1480
  if (free_state)
 
1481
    free((void*) state);
 
1482
}
 
1483
 
 
1484
 
 
1485
/*
 
1486
 *  FUNCTION
 
1487
 *
 
1488
 *      _db_end_    End debugging, freeing state stack memory.
 
1489
 *
 
1490
 *  SYNOPSIS
 
1491
 *
 
1492
 *      static VOID _db_end_ ()
 
1493
 *
 
1494
 *  DESCRIPTION
 
1495
 *
 
1496
 *      Ends debugging, de-allocating the memory allocated to the
 
1497
 *      state stack.
 
1498
 *
 
1499
 *      To be called at the very end of the program.
 
1500
 *
 
1501
 */
 
1502
void _db_end_()
 
1503
{
 
1504
  struct settings *discard;
 
1505
  static struct settings tmp;
 
1506
  CODE_STATE *cs=0;
 
1507
 
 
1508
  get_code_state_or_return;
 
1509
 
 
1510
  while ((discard= cs->stack))
 
1511
  {
 
1512
    if (discard == &init_settings)
 
1513
      break;
 
1514
    cs->stack= discard->next;
 
1515
    FreeState(cs, discard, 1);
 
1516
  }
 
1517
  tmp= init_settings;
 
1518
 
 
1519
  /* Use mutex lock to make it less likely anyone access out_file */
 
1520
  pthread_mutex_lock(&THR_LOCK_dbug);
 
1521
  init_settings.flags=    OPEN_APPEND;
 
1522
  init_settings.out_file= stderr;
 
1523
  init_settings.prof_file= stderr;
 
1524
  init_settings.maxdepth= 0;
 
1525
  init_settings.delay= 0;
 
1526
  init_settings.sub_level= 0;
 
1527
  init_settings.functions= 0;
 
1528
  init_settings.p_functions= 0;
 
1529
  init_settings.keywords= 0;
 
1530
  init_settings.processes= 0;
 
1531
  pthread_mutex_unlock(&THR_LOCK_dbug);
 
1532
  FreeState(cs, &tmp, 0);
 
1533
}
 
1534
 
 
1535
 
 
1536
/*
 
1537
 *  FUNCTION
 
1538
 *
 
1539
 *      DoTrace    check to see if tracing is current enabled
 
1540
 *
 
1541
 *  SYNOPSIS
 
1542
 *
 
1543
 *      static BOOLEAN DoTrace(stack)
 
1544
 *
 
1545
 *  DESCRIPTION
 
1546
 *
 
1547
 *      Checks to see if tracing is enabled based on whether the
 
1548
 *      user has specified tracing, the maximum trace depth has
 
1549
 *      not yet been reached, the current function is selected,
 
1550
 *      and the current process is selected.  Returns TRUE if
 
1551
 *      tracing is enabled, FALSE otherwise.
 
1552
 *
 
1553
 */
 
1554
 
 
1555
static BOOLEAN DoTrace(CODE_STATE *cs)
 
1556
{
 
1557
  return (TRACING && cs->level <= cs->stack->maxdepth &&
 
1558
          InList(cs->stack->functions, cs->func) &&
 
1559
          InList(cs->stack->processes, cs->process));
 
1560
}
 
1561
 
 
1562
 
 
1563
/*
 
1564
 *  FUNCTION
 
1565
 *
 
1566
 *      DoProfile    check to see if profiling is current enabled
 
1567
 *
 
1568
 *  SYNOPSIS
 
1569
 *
 
1570
 *      static BOOLEAN DoProfile()
 
1571
 *
 
1572
 *  DESCRIPTION
 
1573
 *
 
1574
 *      Checks to see if profiling is enabled based on whether the
 
1575
 *      user has specified profiling, the maximum trace depth has
 
1576
 *      not yet been reached, the current function is selected,
 
1577
 *      and the current process is selected.  Returns TRUE if
 
1578
 *      profiling is enabled, FALSE otherwise.
 
1579
 *
 
1580
 */
 
1581
 
 
1582
#ifndef THREAD
 
1583
static BOOLEAN DoProfile(CODE_STATE *cs)
 
1584
{
 
1585
  return PROFILING &&
 
1586
         cs->level <= cs->stack->maxdepth &&
 
1587
         InList(cs->stack->p_functions, cs->func) &&
 
1588
         InList(cs->stack->processes, cs->process);
 
1589
}
 
1590
#endif
 
1591
 
 
1592
FILE *_db_fp_(void)
 
1593
{
 
1594
  CODE_STATE *cs=0;
 
1595
  get_code_state_or_return NULL;
 
1596
  return cs->stack->out_file;
 
1597
}
 
1598
 
 
1599
 
 
1600
/*
 
1601
 *  FUNCTION
 
1602
 *
 
1603
 *      _db_strict_keyword_     test keyword for member of keyword list
 
1604
 *
 
1605
 *  SYNOPSIS
 
1606
 *
 
1607
 *      BOOLEAN _db_strict_keyword_(keyword)
 
1608
 *      char *keyword;
 
1609
 *
 
1610
 *  DESCRIPTION
 
1611
 *
 
1612
 *      Similar to _db_keyword_, but keyword is NOT accepted if keyword list
 
1613
 *      is empty. Used in DBUG_EXECUTE_IF() - for actions that must not be
 
1614
 *      executed by default.
 
1615
 *
 
1616
 *      Returns TRUE if keyword accepted, FALSE otherwise.
 
1617
 *
 
1618
 */
 
1619
 
 
1620
BOOLEAN _db_strict_keyword_(const char *keyword)
 
1621
{
 
1622
  CODE_STATE *cs=0;
 
1623
  get_code_state_or_return FALSE;
 
1624
  if (!DEBUGGING || cs->stack->keywords == NULL)
 
1625
    return FALSE;
 
1626
  return _db_keyword_(cs, keyword);
 
1627
}
 
1628
 
 
1629
/*
 
1630
 *  FUNCTION
 
1631
 *
 
1632
 *      _db_keyword_    test keyword for member of keyword list
 
1633
 *
 
1634
 *  SYNOPSIS
 
1635
 *
 
1636
 *      BOOLEAN _db_keyword_(keyword)
 
1637
 *      char *keyword;
 
1638
 *
 
1639
 *  DESCRIPTION
 
1640
 *
 
1641
 *      Test a keyword to determine if it is in the currently active
 
1642
 *      keyword list.  As with the function list, a keyword is accepted
 
1643
 *      if the list is null, otherwise it must match one of the list
 
1644
 *      members.  When debugging is not on, no keywords are accepted.
 
1645
 *      After the maximum trace level is exceeded, no keywords are
 
1646
 *      accepted (this behavior subject to change).  Additionally,
 
1647
 *      the current function and process must be accepted based on
 
1648
 *      their respective lists.
 
1649
 *
 
1650
 *      Returns TRUE if keyword accepted, FALSE otherwise.
 
1651
 *
 
1652
 */
 
1653
 
 
1654
BOOLEAN _db_keyword_(CODE_STATE *cs, const char *keyword)
 
1655
{
 
1656
  get_code_state_or_return FALSE;
 
1657
 
 
1658
  return (DEBUGGING &&
 
1659
          (!TRACING || cs->level <= cs->stack->maxdepth) &&
 
1660
          InList(cs->stack->functions, cs->func) &&
 
1661
          InList(cs->stack->keywords, keyword) &&
 
1662
          InList(cs->stack->processes, cs->process));
 
1663
}
 
1664
 
 
1665
/*
 
1666
 *  FUNCTION
 
1667
 *
 
1668
 *      Indent    indent a line to the given indentation level
 
1669
 *
 
1670
 *  SYNOPSIS
 
1671
 *
 
1672
 *      static VOID Indent(indent)
 
1673
 *      int indent;
 
1674
 *
 
1675
 *  DESCRIPTION
 
1676
 *
 
1677
 *      Indent a line to the given level.  Note that this is
 
1678
 *      a simple minded but portable implementation.
 
1679
 *      There are better ways.
 
1680
 *
 
1681
 *      Also, the indent must be scaled by the compile time option
 
1682
 *      of character positions per nesting level.
 
1683
 *
 
1684
 */
 
1685
 
 
1686
static void Indent(CODE_STATE *cs, int indent)
 
1687
{
 
1688
  REGISTER int count;
 
1689
 
 
1690
  indent= max(indent-1-cs->stack->sub_level,0)*INDENT;
 
1691
  for (count= 0; count < indent ; count++)
 
1692
  {
 
1693
    if ((count % INDENT) == 0)
 
1694
      fputc('|',cs->stack->out_file);
 
1695
    else
 
1696
      fputc(' ',cs->stack->out_file);
 
1697
  }
 
1698
}
 
1699
 
 
1700
 
 
1701
/*
 
1702
 *  FUNCTION
 
1703
 *
 
1704
 *      FreeList    free all memory associated with a linked list
 
1705
 *
 
1706
 *  SYNOPSIS
 
1707
 *
 
1708
 *      static VOID FreeList(linkp)
 
1709
 *      struct link *linkp;
 
1710
 *
 
1711
 *  DESCRIPTION
 
1712
 *
 
1713
 *      Given pointer to the head of a linked list, frees all
 
1714
 *      memory held by the list and the members of the list.
 
1715
 *
 
1716
 */
 
1717
 
 
1718
static void FreeList(struct link *linkp)
 
1719
{
 
1720
  REGISTER struct link *old;
 
1721
 
 
1722
  while (linkp != NULL)
 
1723
  {
 
1724
    old= linkp;
 
1725
    linkp= linkp->next_link;
 
1726
    free((void*) old);
 
1727
  }
 
1728
}
 
1729
 
 
1730
 
 
1731
/*
 
1732
 *  FUNCTION
 
1733
 *
 
1734
 *      DoPrefix    print debugger line prefix prior to indentation
 
1735
 *
 
1736
 *  SYNOPSIS
 
1737
 *
 
1738
 *      static VOID DoPrefix(_line_)
 
1739
 *      int _line_;
 
1740
 *
 
1741
 *  DESCRIPTION
 
1742
 *
 
1743
 *      Print prefix common to all debugger output lines, prior to
 
1744
 *      doing indentation if necessary.  Print such information as
 
1745
 *      current process name, current source file name and line number,
 
1746
 *      and current function nesting depth.
 
1747
 *
 
1748
 */
 
1749
 
 
1750
static void DoPrefix(CODE_STATE *cs, uint _line_)
 
1751
{
 
1752
  cs->lineno++;
 
1753
  if (cs->stack->flags & PID_ON)
 
1754
  {
 
1755
#ifdef THREAD
 
1756
    (void) fprintf(cs->stack->out_file, "%-7s: ", my_thread_name());
 
1757
#else
 
1758
    (void) fprintf(cs->stack->out_file, "%5d: ", (int) getpid());
 
1759
#endif
 
1760
  }
 
1761
  if (cs->stack->flags & NUMBER_ON)
 
1762
    (void) fprintf(cs->stack->out_file, "%5d: ", cs->lineno);
 
1763
  if (cs->stack->flags & TIMESTAMP_ON)
 
1764
  {
 
1765
#ifdef __WIN__
 
1766
    /* FIXME This doesn't give microseconds as in Unix case, and the resolution is
 
1767
       in system ticks, 10 ms intervals. See my_getsystime.c for high res */
 
1768
    SYSTEMTIME loc_t;
 
1769
    GetLocalTime(&loc_t);
 
1770
    (void) fprintf (cs->stack->out_file,
 
1771
                    /* "%04d-%02d-%02d " */
 
1772
                    "%02d:%02d:%02d.%06d ",
 
1773
                    /*tm_p->tm_year + 1900, tm_p->tm_mon + 1, tm_p->tm_mday,*/
 
1774
                    loc_t.wHour, loc_t.wMinute, loc_t.wSecond, loc_t.wMilliseconds);
 
1775
#else
 
1776
    struct timeval tv;
 
1777
    struct tm *tm_p;
 
1778
    if (gettimeofday(&tv, NULL) != -1)
 
1779
    {
 
1780
      if ((tm_p= localtime((const time_t *)&tv.tv_sec)))
 
1781
      {
 
1782
        (void) fprintf (cs->stack->out_file,
 
1783
                        /* "%04d-%02d-%02d " */
 
1784
                        "%02d:%02d:%02d.%06d ",
 
1785
                        /*tm_p->tm_year + 1900, tm_p->tm_mon + 1, tm_p->tm_mday,*/
 
1786
                        tm_p->tm_hour, tm_p->tm_min, tm_p->tm_sec,
 
1787
                        (int) (tv.tv_usec));
 
1788
      }
 
1789
    }
 
1790
#endif
 
1791
  }
 
1792
  if (cs->stack->flags & PROCESS_ON)
 
1793
    (void) fprintf(cs->stack->out_file, "%s: ", cs->process);
 
1794
  if (cs->stack->flags & FILE_ON)
 
1795
    (void) fprintf(cs->stack->out_file, "%14s: ", BaseName(cs->file));
 
1796
  if (cs->stack->flags & LINE_ON)
 
1797
    (void) fprintf(cs->stack->out_file, "%5d: ", _line_);
 
1798
  if (cs->stack->flags & DEPTH_ON)
 
1799
    (void) fprintf(cs->stack->out_file, "%4d: ", cs->level);
 
1800
}
 
1801
 
 
1802
 
 
1803
/*
 
1804
 *  FUNCTION
 
1805
 *
 
1806
 *      DBUGOpenFile    open new output stream for debugger output
 
1807
 *
 
1808
 *  SYNOPSIS
 
1809
 *
 
1810
 *      static VOID DBUGOpenFile(name)
 
1811
 *      char *name;
 
1812
 *
 
1813
 *  DESCRIPTION
 
1814
 *
 
1815
 *      Given name of a new file (or "-" for stdout) opens the file
 
1816
 *      and sets the output stream to the new file.
 
1817
 *
 
1818
 */
 
1819
 
 
1820
static void DBUGOpenFile(CODE_STATE *cs,
 
1821
                         const char *name,const char *end,int append)
 
1822
{
 
1823
  REGISTER FILE *fp;
 
1824
  REGISTER BOOLEAN newfile;
 
1825
 
 
1826
  if (name != NULL)
 
1827
  {
 
1828
    if (end)
 
1829
    {
 
1830
      int len=end-name;
 
1831
      memcpy(cs->stack->name, name, len);
 
1832
      cs->stack->name[len]=0;
 
1833
    }
 
1834
    else
 
1835
    strmov(cs->stack->name,name);
 
1836
    name=cs->stack->name;
 
1837
    if (strcmp(name, "-") == 0)
 
1838
    {
 
1839
      cs->stack->out_file= stdout;
 
1840
      cs->stack->flags |= FLUSH_ON_WRITE;
 
1841
      cs->stack->name[0]=0;
 
1842
    }
 
1843
    else
 
1844
    {
 
1845
      if (!Writable(name))
 
1846
      {
 
1847
        (void) fprintf(stderr, ERR_OPEN, cs->process, name);
 
1848
        perror("");
 
1849
        fflush(stderr);
 
1850
      }
 
1851
      else
 
1852
      {
 
1853
        newfile= !EXISTS(name);
 
1854
        if (!(fp= fopen(name, append ? "a+" : "w")))
 
1855
        {
 
1856
          (void) fprintf(stderr, ERR_OPEN, cs->process, name);
 
1857
          perror("");
 
1858
          fflush(stderr);
 
1859
        }
 
1860
        else
 
1861
        {
 
1862
          cs->stack->out_file= fp;
 
1863
          if (newfile)
 
1864
          {
 
1865
            ChangeOwner(cs, name);
 
1866
          }
 
1867
        }
 
1868
      }
 
1869
    }
 
1870
  }
 
1871
}
 
1872
 
 
1873
 
 
1874
/*
 
1875
 *  FUNCTION
 
1876
 *
 
1877
 *      OpenProfile    open new output stream for profiler output
 
1878
 *
 
1879
 *  SYNOPSIS
 
1880
 *
 
1881
 *      static FILE *OpenProfile(name)
 
1882
 *      char *name;
 
1883
 *
 
1884
 *  DESCRIPTION
 
1885
 *
 
1886
 *      Given name of a new file, opens the file
 
1887
 *      and sets the profiler output stream to the new file.
 
1888
 *
 
1889
 *      It is currently unclear whether the prefered behavior is
 
1890
 *      to truncate any existing file, or simply append to it.
 
1891
 *      The latter behavior would be desirable for collecting
 
1892
 *      accumulated runtime history over a number of separate
 
1893
 *      runs.  It might take some changes to the analyzer program
 
1894
 *      though, and the notes that Binayak sent with the profiling
 
1895
 *      diffs indicated that append was the normal mode, but this
 
1896
 *      does not appear to agree with the actual code. I haven't
 
1897
 *      investigated at this time [fnf; 24-Jul-87].
 
1898
 */
 
1899
 
 
1900
#ifndef THREAD
 
1901
static FILE *OpenProfile(CODE_STATE *cs, const char *name)
 
1902
{
 
1903
  REGISTER FILE *fp;
 
1904
  REGISTER BOOLEAN newfile;
 
1905
 
 
1906
  fp=0;
 
1907
  if (!Writable(name))
 
1908
  {
 
1909
    (void) fprintf(cs->stack->out_file, ERR_OPEN, cs->process, name);
 
1910
    perror("");
 
1911
    (void) Delay(cs->stack->delay);
 
1912
  }
 
1913
  else
 
1914
  {
 
1915
    newfile= !EXISTS(name);
 
1916
    if (!(fp= fopen(name, "w")))
 
1917
    {
 
1918
      (void) fprintf(cs->stack->out_file, ERR_OPEN, cs->process, name);
 
1919
      perror("");
 
1920
    }
 
1921
    else
 
1922
    {
 
1923
      cs->stack->prof_file= fp;
 
1924
      if (newfile)
 
1925
      {
 
1926
        ChangeOwner(cs, name);
 
1927
      }
 
1928
    }
 
1929
  }
 
1930
  return fp;
 
1931
}
 
1932
#endif
 
1933
 
 
1934
/*
 
1935
 *  FUNCTION
 
1936
 *
 
1937
 *      DBUGCloseFile    close the debug output stream
 
1938
 *
 
1939
 *  SYNOPSIS
 
1940
 *
 
1941
 *      static VOID DBUGCloseFile(fp)
 
1942
 *      FILE *fp;
 
1943
 *
 
1944
 *  DESCRIPTION
 
1945
 *
 
1946
 *      Closes the debug output stream unless it is standard output
 
1947
 *      or standard error.
 
1948
 *
 
1949
 */
 
1950
 
 
1951
static void DBUGCloseFile(CODE_STATE *cs, FILE *fp)
 
1952
{
 
1953
  if (fp != stderr && fp != stdout && fclose(fp) == EOF)
 
1954
  {
 
1955
    pthread_mutex_lock(&THR_LOCK_dbug);
 
1956
    (void) fprintf(cs->stack->out_file, ERR_CLOSE, cs->process);
 
1957
    perror("");
 
1958
    dbug_flush(cs);
 
1959
  }
 
1960
}
 
1961
 
 
1962
 
 
1963
/*
 
1964
 *  FUNCTION
 
1965
 *
 
1966
 *      DbugExit    print error message and exit
 
1967
 *
 
1968
 *  SYNOPSIS
 
1969
 *
 
1970
 *      static VOID DbugExit(why)
 
1971
 *      char *why;
 
1972
 *
 
1973
 *  DESCRIPTION
 
1974
 *
 
1975
 *      Prints error message using current process name, the reason for
 
1976
 *      aborting (typically out of memory), and exits with status 1.
 
1977
 *      This should probably be changed to use a status code
 
1978
 *      defined in the user's debugger include file.
 
1979
 *
 
1980
 */
 
1981
 
 
1982
static void DbugExit(const char *why)
 
1983
{
 
1984
  CODE_STATE *cs=code_state();
 
1985
  (void) fprintf(stderr, ERR_ABORT, cs ? cs->process : "(null)", why);
 
1986
  (void) fflush(stderr);
 
1987
  exit(1);
 
1988
}
 
1989
 
 
1990
 
 
1991
/*
 
1992
 *  FUNCTION
 
1993
 *
 
1994
 *      DbugMalloc    allocate memory for debugger runtime support
 
1995
 *
 
1996
 *  SYNOPSIS
 
1997
 *
 
1998
 *      static long *DbugMalloc(size)
 
1999
 *      int size;
 
2000
 *
 
2001
 *  DESCRIPTION
 
2002
 *
 
2003
 *      Allocate more memory for debugger runtime support functions.
 
2004
 *      Failure to to allocate the requested number of bytes is
 
2005
 *      immediately fatal to the current process.  This may be
 
2006
 *      rather unfriendly behavior.  It might be better to simply
 
2007
 *      print a warning message, freeze the current debugger cs,
 
2008
 *      and continue execution.
 
2009
 *
 
2010
 */
 
2011
 
 
2012
static char *DbugMalloc(size_t size)
 
2013
{
 
2014
  register char *new_malloc;
 
2015
 
 
2016
  if (!(new_malloc= (char*) malloc(size)))
 
2017
    DbugExit("out of memory");
 
2018
  return new_malloc;
 
2019
}
 
2020
 
 
2021
 
 
2022
/*
 
2023
 *     strtok lookalike - splits on ':', magically handles ::, :\ and :/
 
2024
 */
 
2025
 
 
2026
static const char *DbugStrTok(const char *s)
 
2027
{
 
2028
  while (s[0] && (s[0] != ':' ||
 
2029
                  (s[1] == '\\' || s[1] == '/' || (s[1] == ':' && s++))))
 
2030
    s++;
 
2031
  return s;
 
2032
}
 
2033
 
 
2034
 
 
2035
/*
 
2036
 *  FUNCTION
 
2037
 *
 
2038
 *      BaseName    strip leading pathname components from name
 
2039
 *
 
2040
 *  SYNOPSIS
 
2041
 *
 
2042
 *      static char *BaseName(pathname)
 
2043
 *      char *pathname;
 
2044
 *
 
2045
 *  DESCRIPTION
 
2046
 *
 
2047
 *      Given pointer to a complete pathname, locates the base file
 
2048
 *      name at the end of the pathname and returns a pointer to
 
2049
 *      it.
 
2050
 *
 
2051
 */
 
2052
 
 
2053
static const char *BaseName(const char *pathname)
 
2054
{
 
2055
  register const char *base;
 
2056
 
 
2057
  base= strrchr(pathname, FN_LIBCHAR);
 
2058
  if (base++ == NullS)
 
2059
    base= pathname;
 
2060
  return base;
 
2061
}
 
2062
 
 
2063
 
 
2064
/*
 
2065
 *  FUNCTION
 
2066
 *
 
2067
 *      Writable    test to see if a pathname is writable/creatable
 
2068
 *
 
2069
 *  SYNOPSIS
 
2070
 *
 
2071
 *      static BOOLEAN Writable(pathname)
 
2072
 *      char *pathname;
 
2073
 *
 
2074
 *  DESCRIPTION
 
2075
 *
 
2076
 *      Because the debugger might be linked in with a program that
 
2077
 *      runs with the set-uid-bit (suid) set, we have to be careful
 
2078
 *      about opening a user named file for debug output.  This consists
 
2079
 *      of checking the file for write access with the real user id,
 
2080
 *      or checking the directory where the file will be created.
 
2081
 *
 
2082
 *      Returns TRUE if the user would normally be allowed write or
 
2083
 *      create access to the named file.  Returns FALSE otherwise.
 
2084
 *
 
2085
 */
 
2086
 
 
2087
 
 
2088
#ifndef Writable
 
2089
 
 
2090
static BOOLEAN Writable(const char *pathname)
 
2091
{
 
2092
  REGISTER BOOLEAN granted;
 
2093
  REGISTER char *lastslash;
 
2094
 
 
2095
  granted= FALSE;
 
2096
  if (EXISTS(pathname))
 
2097
  {
 
2098
    if (WRITABLE(pathname))
 
2099
      granted= TRUE;
 
2100
  }
 
2101
  else
 
2102
  {
 
2103
    lastslash= strrchr(pathname, '/');
 
2104
    if (lastslash != NULL)
 
2105
      *lastslash= '\0';
 
2106
    else
 
2107
      pathname= ".";
 
2108
    if (WRITABLE(pathname))
 
2109
      granted= TRUE;
 
2110
    if (lastslash != NULL)
 
2111
      *lastslash= '/';
 
2112
  }
 
2113
  return granted;
 
2114
}
 
2115
#endif
 
2116
 
 
2117
 
 
2118
/*
 
2119
 *  FUNCTION
 
2120
 *
 
2121
 *      ChangeOwner    change owner to real user for suid programs
 
2122
 *
 
2123
 *  SYNOPSIS
 
2124
 *
 
2125
 *      static VOID ChangeOwner(pathname)
 
2126
 *
 
2127
 *  DESCRIPTION
 
2128
 *
 
2129
 *      For unix systems, change the owner of the newly created debug
 
2130
 *      file to the real owner.  This is strictly for the benefit of
 
2131
 *      programs that are running with the set-user-id bit set.
 
2132
 *
 
2133
 *      Note that at this point, the fact that pathname represents
 
2134
 *      a newly created file has already been established.  If the
 
2135
 *      program that the debugger is linked to is not running with
 
2136
 *      the suid bit set, then this operation is redundant (but
 
2137
 *      harmless).
 
2138
 *
 
2139
 */
 
2140
 
 
2141
#ifndef ChangeOwner
 
2142
static void ChangeOwner(CODE_STATE *cs, char *pathname)
 
2143
{
 
2144
  if (chown(pathname, getuid(), getgid()) == -1)
 
2145
  {
 
2146
    (void) fprintf(stderr, ERR_CHOWN, cs->process, pathname);
 
2147
    perror("");
 
2148
    (void) fflush(stderr);
 
2149
  }
 
2150
}
 
2151
#endif
 
2152
 
 
2153
 
 
2154
/*
 
2155
 *  FUNCTION
 
2156
 *
 
2157
 *      _db_setjmp_    save debugger environment
 
2158
 *
 
2159
 *  SYNOPSIS
 
2160
 *
 
2161
 *      VOID _db_setjmp_()
 
2162
 *
 
2163
 *  DESCRIPTION
 
2164
 *
 
2165
 *      Invoked as part of the user's DBUG_SETJMP macro to save
 
2166
 *      the debugger environment in parallel with saving the user's
 
2167
 *      environment.
 
2168
 *
 
2169
 */
 
2170
 
 
2171
#ifdef HAVE_LONGJMP
 
2172
 
 
2173
EXPORT void _db_setjmp_()
 
2174
{
 
2175
  CODE_STATE *cs=0;
 
2176
  get_code_state_or_return;
 
2177
 
 
2178
  cs->jmplevel= cs->level;
 
2179
  cs->jmpfunc= cs->func;
 
2180
  cs->jmpfile= cs->file;
 
2181
}
 
2182
 
 
2183
/*
 
2184
 *  FUNCTION
 
2185
 *
 
2186
 *      _db_longjmp_    restore previously saved debugger environment
 
2187
 *
 
2188
 *  SYNOPSIS
 
2189
 *
 
2190
 *      VOID _db_longjmp_()
 
2191
 *
 
2192
 *  DESCRIPTION
 
2193
 *
 
2194
 *      Invoked as part of the user's DBUG_LONGJMP macro to restore
 
2195
 *      the debugger environment in parallel with restoring the user's
 
2196
 *      previously saved environment.
 
2197
 *
 
2198
 */
 
2199
 
 
2200
EXPORT void _db_longjmp_()
 
2201
{
 
2202
  CODE_STATE *cs=0;
 
2203
  get_code_state_or_return;
 
2204
 
 
2205
  cs->level= cs->jmplevel;
 
2206
  if (cs->jmpfunc)
 
2207
    cs->func= cs->jmpfunc;
 
2208
  if (cs->jmpfile)
 
2209
    cs->file= cs->jmpfile;
 
2210
}
 
2211
#endif
 
2212
 
 
2213
/*
 
2214
 *  FUNCTION
 
2215
 *
 
2216
 *      perror    perror simulation for systems that don't have it
 
2217
 *
 
2218
 *  SYNOPSIS
 
2219
 *
 
2220
 *      static VOID perror(s)
 
2221
 *      char *s;
 
2222
 *
 
2223
 *  DESCRIPTION
 
2224
 *
 
2225
 *      Perror produces a message on the standard error stream which
 
2226
 *      provides more information about the library or system error
 
2227
 *      just encountered.  The argument string s is printed, followed
 
2228
 *      by a ':', a blank, and then a message and a newline.
 
2229
 *
 
2230
 *      An undocumented feature of the unix perror is that if the string
 
2231
 *      's' is a null string (NOT a NULL pointer!), then the ':' and
 
2232
 *      blank are not printed.
 
2233
 *
 
2234
 *      This version just complains about an "unknown system error".
 
2235
 *
 
2236
 */
 
2237
 
 
2238
#ifndef HAVE_PERROR
 
2239
static void perror(s)
 
2240
char *s;
 
2241
{
 
2242
  if (s && *s != '\0')
 
2243
    (void) fprintf(stderr, "%s: ", s);
 
2244
  (void) fprintf(stderr, "<unknown system error>\n");
 
2245
}
 
2246
#endif /* HAVE_PERROR */
 
2247
 
 
2248
 
 
2249
        /* flush dbug-stream, free mutex lock & wait delay */
 
2250
        /* This is because some systems (MSDOS!!) dosn't flush fileheader */
 
2251
        /* and dbug-file isn't readable after a system crash !! */
 
2252
 
 
2253
static void dbug_flush(CODE_STATE *cs)
 
2254
{
 
2255
#ifndef THREAD
 
2256
  if (cs->stack->flags & FLUSH_ON_WRITE)
 
2257
#endif
 
2258
  {
 
2259
    (void) fflush(cs->stack->out_file);
 
2260
    if (cs->stack->delay)
 
2261
      (void) Delay(cs->stack->delay);
 
2262
  }
 
2263
  if (!cs->locked)
 
2264
    pthread_mutex_unlock(&THR_LOCK_dbug);
 
2265
} /* dbug_flush */
 
2266
 
 
2267
 
 
2268
void _db_lock_file_()
 
2269
{
 
2270
  CODE_STATE *cs=0;
 
2271
  get_code_state_or_return;
 
2272
  pthread_mutex_lock(&THR_LOCK_dbug);
 
2273
  cs->locked=1;
 
2274
}
 
2275
 
 
2276
void _db_unlock_file_()
 
2277
{
 
2278
  CODE_STATE *cs=0;
 
2279
  get_code_state_or_return;
 
2280
  cs->locked=0;
 
2281
  pthread_mutex_unlock(&THR_LOCK_dbug);
 
2282
}
 
2283
 
 
2284
/*
 
2285
 * Here we need the definitions of the clock routine.  Add your
 
2286
 * own for whatever system that you have.
 
2287
 */
 
2288
 
 
2289
#ifndef THREAD
 
2290
#if defined(HAVE_GETRUSAGE)
 
2291
 
 
2292
#include <sys/param.h>
 
2293
#include <sys/resource.h>
 
2294
 
 
2295
/* extern int getrusage(int, struct rusage *); */
 
2296
 
 
2297
/*
 
2298
 * Returns the user time in milliseconds used by this process so
 
2299
 * far.
 
2300
 */
 
2301
 
 
2302
static unsigned long Clock()
 
2303
{
 
2304
    struct rusage ru;
 
2305
 
 
2306
    (void) getrusage(RUSAGE_SELF, &ru);
 
2307
    return ru.ru_utime.tv_sec*1000 + ru.ru_utime.tv_usec/1000;
 
2308
}
 
2309
 
 
2310
#elif defined(MSDOS) || defined(__WIN__)
 
2311
 
 
2312
static ulong Clock()
 
2313
{
 
2314
  return clock()*(1000/CLOCKS_PER_SEC);
 
2315
}
 
2316
#elif defined(amiga)
 
2317
 
 
2318
struct DateStamp {              /* Yes, this is a hack, but doing it right */
 
2319
        long ds_Days;           /* is incredibly ugly without splitting this */
 
2320
        long ds_Minute;         /* off into a separate file */
 
2321
        long ds_Tick;
 
2322
};
 
2323
 
 
2324
static int first_clock= TRUE;
 
2325
static struct DateStamp begin;
 
2326
static struct DateStamp elapsed;
 
2327
 
 
2328
static unsigned long Clock()
 
2329
{
 
2330
    register struct DateStamp *now;
 
2331
    register unsigned long millisec= 0;
 
2332
    extern VOID *AllocMem();
 
2333
 
 
2334
    now= (struct DateStamp *) AllocMem((long) sizeof(struct DateStamp), 0L);
 
2335
    if (now != NULL)
 
2336
    {
 
2337
        if (first_clock == TRUE)
 
2338
        {
 
2339
            first_clock= FALSE;
 
2340
            (void) DateStamp(now);
 
2341
            begin= *now;
 
2342
        }
 
2343
        (void) DateStamp(now);
 
2344
        millisec= 24 * 3600 * (1000 / HZ) * (now->ds_Days - begin.ds_Days);
 
2345
        millisec += 60 * (1000 / HZ) * (now->ds_Minute - begin.ds_Minute);
 
2346
        millisec += (1000 / HZ) * (now->ds_Tick - begin.ds_Tick);
 
2347
        (void) FreeMem(now, (long) sizeof(struct DateStamp));
 
2348
    }
 
2349
    return millisec;
 
2350
}
 
2351
#else
 
2352
static unsigned long Clock()
 
2353
{
 
2354
    return 0;
 
2355
}
 
2356
#endif /* RUSAGE */
 
2357
#endif /* THREADS */
 
2358
 
 
2359
#ifdef NO_VARARGS
 
2360
 
 
2361
/*
 
2362
 *      Fake vfprintf for systems that don't support it.  If this
 
2363
 *      doesn't work, you are probably SOL...
 
2364
 */
 
2365
 
 
2366
static int vfprintf(stream, format, ap)
 
2367
FILE *stream;
 
2368
char *format;
 
2369
va_list ap;
 
2370
{
 
2371
    int rtnval;
 
2372
    ARGS_DCL;
 
2373
 
 
2374
    ARG0=  va_arg(ap, ARGS_TYPE);
 
2375
    ARG1=  va_arg(ap, ARGS_TYPE);
 
2376
    ARG2=  va_arg(ap, ARGS_TYPE);
 
2377
    ARG3=  va_arg(ap, ARGS_TYPE);
 
2378
    ARG4=  va_arg(ap, ARGS_TYPE);
 
2379
    ARG5=  va_arg(ap, ARGS_TYPE);
 
2380
    ARG6=  va_arg(ap, ARGS_TYPE);
 
2381
    ARG7=  va_arg(ap, ARGS_TYPE);
 
2382
    ARG8=  va_arg(ap, ARGS_TYPE);
 
2383
    ARG9=  va_arg(ap, ARGS_TYPE);
 
2384
    rtnval= fprintf(stream, format, ARGS_LIST);
 
2385
    return rtnval;
 
2386
}
 
2387
 
 
2388
#endif  /* NO_VARARGS */
 
2389
 
 
2390
#else
 
2391
 
 
2392
/*
 
2393
 * Dummy function, workaround for MySQL bug#14420 related
 
2394
 * build failure on a platform where linking with an empty
 
2395
 * archive fails.
 
2396
 *
 
2397
 * This block can be removed as soon as a fix for bug#14420
 
2398
 * is implemented.
 
2399
 */
 
2400
int i_am_a_dummy_function() {
 
2401
       return 0;
 
2402
}
 
2403
 
 
2404
#endif