1
/******************************************************************************
5
* Copyright Abandoned, 1987, Fred Fish *
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. *
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. *
20
* I would appreciate it, as a courtesy, if this notice is left in *
21
* all copies and derivative works. Thank you. *
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. *
27
******************************************************************************
33
* dbug.c runtime support routines for dbug package
37
* @(#)dbug.c 1.25 7/25/89
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.
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.
52
* Fred Fish (base code)
53
* Enhanced Software Technologies, Tempe, AZ
54
* asuvax!mcdphx!estinc!fnf
56
* Binayak Banerjee (profiling enhancements)
57
* seismo!bpa!sjuvax!bbanerje
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")
68
* incremental mode (-#+t:-d,info ...)
69
* DBUG_SET, _db_explain_
70
* thread-local settings
75
#include <my_global.h>
78
#if defined(MSDOS) || defined(__WIN__)
87
* Manifest constants which may be "tuned" if desired.
90
#define PRINTBUF 1024 /* Print buffer size */
91
#define INDENT 2 /* Indentation per trace level */
92
#define MAXDEPTH 200 /* Maximum trace depth default */
95
* The following flags are used to determine which
96
* capabilities the user has enabled with the settings
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 */
114
#define TRACING (cs->stack->flags & TRACE_ON)
115
#define DEBUGGING (cs->stack->flags & DEBUG_ON)
116
#define PROFILING (cs->stack->flags & PROFILE_ON)
119
* Typedefs to make things more obvious.
129
* Make it easy to change storage classes if necessary.
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 */
138
* The default file for profiling. Could also add another flag
139
* (G?) which allowed the user to specify this.
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.
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"
155
#ifdef M_I386 /* predefined by xenix 386 compiler */
156
#define AUTOS_REVERSE 1
160
* Externally supplied functions.
164
static void perror(); /* Fake system/library error print routine */
167
IMPORT int _sanity(const char *file,uint line); /* safemalloc sanity checker */
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.
176
struct link *next_link; /* Pointer to the next link */
177
char str[1]; /* Pointer to link's contents */
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.
187
* Note: if out_file is NULL, the other fields are not initialized at all!
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 */
205
#define is_shared(S, V) ((S)->next && (S)->next->V == (S)->V)
208
* Local variables not seen by user.
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] */
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() */
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.
234
* These variables are currently used only by _db_pargs_() and
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 */
244
The test below is so we could call functions with DBUG_ENTER before
247
#define get_code_state_or_return if (!cs && !((cs=code_state()))) return
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);
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);
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 */
273
static void DoPrefix(CODE_STATE *cs, uint line);
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);
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);
293
* Miscellaneous printf format strings.
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\": "
303
* Macros and defines for testing file accessibility under UNIX and MSDOS.
307
#if !defined(HAVE_ACCESS) || defined(MSDOS)
308
#define EXISTS(pathname) (FALSE) /* Assume no existance */
309
#define Writable(name) (TRUE)
311
#define EXISTS(pathname) (access(pathname, F_OK) == 0)
312
#define WRITABLE(pathname) (access(pathname, W_OK) == 0)
315
#define ChangeOwner(cs,name)
320
** Macros to allow dbugging with threads
324
#include <my_pthread.h>
325
pthread_mutex_t THR_LOCK_dbug;
327
static CODE_STATE *code_state(void)
330
struct st_my_thread_var *tmp;
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;
341
if ((tmp=my_thread_var))
343
if (!(cs=(CODE_STATE *) tmp->dbug))
345
cs=(CODE_STATE*) DbugMalloc(sizeof(*cs));
346
bzero((uchar*) cs,sizeof(*cs));
347
cs->process= db_process ? db_process : "dbug";
350
cs->stack=&init_settings;
351
tmp->dbug= (void*) cs;
359
static CODE_STATE static_code_state=
361
"dbug", "?func", "?file", NULL, &init_settings,
362
NullS, NullS, 0,0,0,0,0,NullS
365
static CODE_STATE *code_state(void)
369
bzero(&init_settings, sizeof(init_settings));
370
init_settings.out_file=stderr;
371
init_settings.flags=OPEN_APPEND;
374
return &static_code_state;
377
#define pthread_mutex_lock(A) {}
378
#define pthread_mutex_unlock(A) {}
382
* Translate some calls among different systems.
386
/* sleep() wants seconds */
387
#define Delay(A) sleep(((uint) A)/10)
395
* _db_process_ give the name to the current process/thread
399
* VOID _process_(name)
404
void _db_process_(const char *name)
411
get_code_state_or_return;
419
* DbugParse parse control string and set current debugger setting
423
* Given pointer to a debug control string in "control",
424
* parses the control string, and sets
425
* up a current debug settings.
427
* The debug control string is a sequence of colon separated fields
430
* [+]<field_1>:<field_2>:...:<field_N>
432
* Each field consists of a mandatory flag character followed by
433
* an optional "," and comma separated list of modifiers:
435
* [sign]flag[,modifier,modifier,...,modifier]
437
* See the manual for the list of supported signs, flags, and modifiers
439
* For convenience, any leading "-#" is stripped off.
443
static void DbugParse(CODE_STATE *cs, const char *control)
447
struct settings *stack;
449
get_code_state_or_return;
452
if (control[0] == '-' && control[1] == '#')
455
rel= control[0] == '+' || control[0] == '-';
456
if ((!rel || (!stack->out_file && !stack->next)))
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;
469
else if (!stack->out_file)
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)
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);
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;
495
end= DbugStrTok(control);
496
while (control < end)
498
int c, sign= (*control == '+') ? 1 : (*control == '-') ? -1 : 0;
502
if (*control == ',') control++;
503
/* XXX when adding new cases here, don't forget _db_explain_ ! */
506
if (sign < 0 && control == end)
508
if (!is_shared(stack, keywords))
509
FreeList(stack->keywords);
510
stack->keywords=NULL;
511
stack->flags &= ~DEBUG_ON;
514
if (rel && is_shared(stack, keywords))
515
stack->keywords= ListCopy(stack->keywords);
519
stack->keywords= ListDel(stack->keywords, control, end);
522
stack->keywords= ListAdd(stack->keywords, control, end);
523
stack->flags |= DEBUG_ON;
526
stack->delay= atoi(control);
529
if (sign < 0 && control == end)
531
if (!is_shared(stack,functions))
532
FreeList(stack->functions);
533
stack->functions=NULL;
536
if (rel && is_shared(stack,functions))
537
stack->functions= ListCopy(stack->functions);
539
stack->functions= ListDel(stack->functions, control, end);
541
stack->functions= ListAdd(stack->functions, control, end);
545
stack->flags &= ~FILE_ON;
547
stack->flags |= FILE_ON;
551
stack->flags &= ~PID_ON;
553
stack->flags |= PID_ON;
557
if (OpenProfile(cs, PROF_FILE))
559
stack->flags |= PROFILE_ON;
560
stack->p_functions= ListAdd(stack->p_functions, control, end);
566
stack->flags &= ~LINE_ON;
568
stack->flags |= LINE_ON;
572
stack->flags &= ~DEPTH_ON;
574
stack->flags |= DEPTH_ON;
578
stack->flags &= ~NUMBER_ON;
580
stack->flags |= NUMBER_ON;
584
stack->flags |= FLUSH_ON_WRITE;
590
if (!is_shared(stack, out_file))
591
DBUGCloseFile(cs, stack->out_file);
592
stack->flags &= ~FLUSH_ON_WRITE;
593
stack->out_file= stderr;
596
if (c == 'a' || c == 'A')
597
stack->flags |= OPEN_APPEND;
599
stack->flags &= ~OPEN_APPEND;
601
DBUGOpenFile(cs, control, end, stack->flags & OPEN_APPEND);
603
DBUGOpenFile(cs, "-",0,0);
606
if (sign < 0 && control == end)
608
if (!is_shared(stack,processes))
609
FreeList(stack->processes);
610
stack->processes=NULL;
613
if (rel && is_shared(stack, processes))
614
stack->processes= ListCopy(stack->processes);
616
stack->processes= ListDel(stack->processes, control, end);
618
stack->processes= ListAdd(stack->processes, control, end);
622
stack->flags &= ~PROCESS_ON;
624
stack->flags |= PROCESS_ON;
627
stack->sub_level= cs->level;
633
stack->maxdepth-= atoi(control);
640
stack->maxdepth+= atoi(control);
642
stack->maxdepth= MAXDEPTH;
644
if (stack->maxdepth > 0)
645
stack->flags |= TRACE_ON;
647
stack->flags &= ~TRACE_ON;
651
stack->flags &= ~TIMESTAMP_ON;
653
stack->flags |= TIMESTAMP_ON;
657
stack->flags &= ~SANITY_CHECK_ON;
659
stack->flags |= SANITY_CHECK_ON;
665
end= DbugStrTok(control);
673
* _db_set_ set current debugger settings
677
* VOID _db_set_(control)
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.
688
void _db_set_(CODE_STATE *cs, const char *control)
690
get_code_state_or_return;
692
if (cs->stack == &init_settings)
695
DbugParse(cs, control);
702
* _db_push_ push current debugger settings and set up new one
706
* VOID _db_push_(control)
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()
717
void _db_push_(const char *control)
720
get_code_state_or_return;
722
DbugParse(cs, control);
728
* _db_set_init_ set initial debugger settings
732
* VOID _db_set_init_(control)
740
void _db_set_init_(const char *control)
743
bzero((uchar*) &tmp_cs, sizeof(tmp_cs));
744
tmp_cs.stack= &init_settings;
745
DbugParse(&tmp_cs, control);
751
* _db_pop_ pop the debug stack
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.
767
struct settings *discard;
770
get_code_state_or_return;
773
if (discard->next != NULL)
775
cs->stack= discard->next;
776
FreeState(cs, discard, 1);
783
* _db_explain_ generates 'control' string for the current settings
787
* 1 - buffer too short, output truncated
792
#define char_to_buf(C) do { \
794
if (buf >= end) goto overflow; \
796
#define str_to_buf(S) do { \
798
buf=strnmov(buf, (S), len+1); \
799
if (buf >= end) goto overflow; \
801
#define list_to_buf(l) do { \
802
struct link *listp=(l); \
805
str_to_buf(listp->str); \
806
listp=listp->next_link; \
809
#define int_to_buf(i) do { \
811
int10_to_str((i), b, 10); \
814
#define colon_to_buf do { \
815
if (buf != start) char_to_buf(':'); \
817
#define op_int_to_buf(C, val, def) do { \
818
if ((val) != (def)) \
825
#define op_intf_to_buf(C, val, def, cond) do { \
830
if ((val) != (def)) int_to_buf(val); \
833
#define op_str_to_buf(C, val, cond) do { \
839
if (*s) str_to_buf(s); \
842
#define op_list_to_buf(C, val, cond) do { \
850
#define op_bool_to_buf(C, cond) do { \
858
int _db_explain_ (CODE_STATE *cs, char *buf, size_t len)
860
char *start=buf, *end=buf+len-4;
862
get_code_state_or_return *buf=0;
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);
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);
901
#undef op_intf_to_buf
903
#undef op_list_to_buf
904
#undef op_bool_to_buf
909
* _db_explain_init_ explain initial debugger settings
915
int _db_explain_init_(char *buf, size_t len)
918
bzero((uchar*) &cs,sizeof(cs));
919
cs.stack=&init_settings;
920
return _db_explain_(&cs, buf, len);
926
* _db_enter_ process entry point to user function
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
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.
952
* Also prints a trace line if tracing is enabled and
953
* increments the current function nesting depth.
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.
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)))
965
int save_errno=errno;
967
get_code_state_or_return;
973
*_slevel_= ++cs->level;
975
*_sframep_= cs->framep;
976
cs->framep= (char **) _sframep_;
980
if (*cs->framep == NULL)
984
stackused= ((long)(*cs->framep)) - ((long)(cs->framep));
985
stackused= stackused > 0 ? stackused : -stackused;
987
(void) fprintf(cs->stack->prof_file, PROF_EFMT , Clock(), cs->func);
989
(void) fprintf(cs->stack->prof_file, PROF_SFMT, cs->framep, stackused, *_sfunc_);
991
(void) fprintf(cs->stack->prof_file, PROF_SFMT, (ulong) cs->framep, stackused,
994
(void) fflush(cs->stack->prof_file);
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 */
1007
if (cs->stack->flags & SANITY_CHECK_ON)
1008
if (_sanity(_file_,_line_)) /* Check of safemalloc */
1009
cs->stack->flags &= ~SANITY_CHECK_ON;
1017
* _db_return_ process exit from user function
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
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.
1037
void _db_return_(uint _line_, const char **_sfunc_,
1038
const char **_sfile_, uint *_slevel_)
1040
int save_errno=errno;
1042
get_code_state_or_return;
1044
if (cs->level != (int) *_slevel_)
1047
pthread_mutex_lock(&THR_LOCK_dbug);
1048
(void) fprintf(cs->stack->out_file, ERR_MISSING_RETURN, cs->process,
1055
if (cs->stack->flags & SANITY_CHECK_ON)
1057
if (_sanity(*_sfile_,_line_))
1058
cs->stack->flags &= ~SANITY_CHECK_ON;
1063
(void) fprintf(cs->stack->prof_file, PROF_XFMT, Clock(), cs->func);
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);
1075
cs->level= *_slevel_-1;
1079
if (cs->framep != NULL)
1080
cs->framep= (char **) *cs->framep;
1089
* _db_pargs_ log arguments for subsequent use by _db_doprnt_()
1093
* VOID _db_pargs_(_line_, keyword)
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_().
1106
void _db_pargs_(uint _line_, const char *keyword)
1109
get_code_state_or_return;
1111
cs->u_keyword= keyword;
1118
* _db_doprnt_ handle print of debug lines
1122
* VOID _db_doprnt_(format, va_alist)
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.
1134
* Note that the format string SHOULD NOT include a terminating
1135
* newline, this is supplied automatically.
1141
void _db_doprnt_(const char *format,...)
1146
get_code_state_or_return;
1148
va_start(args,format);
1150
if (_db_keyword_(cs, cs->u_keyword))
1152
int save_errno=errno;
1154
pthread_mutex_lock(&THR_LOCK_dbug);
1155
DoPrefix(cs, cs->u_line);
1157
Indent(cs, cs->level + 1);
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);
1173
* _db_dump_ dump a string in hex
1177
* void _db_dump_(_line_,keyword,memory,length)
1178
* int _line_; current source line number
1180
* char *memory; Memory to print
1181
* int length; Bytes to print
1184
* Dump N characters in a binary array.
1185
* Is used to examine corrputed memory or arrays.
1188
void _db_dump_(uint _line_, const char *keyword,
1189
const unsigned char *memory, size_t length)
1195
get_code_state_or_return;
1197
if (_db_keyword_(cs, keyword))
1200
pthread_mutex_lock(&THR_LOCK_dbug);
1201
DoPrefix(cs, _line_);
1204
Indent(cs, cs->level + 1);
1205
pos= min(max(cs->level-cs->stack->sub_level,0)*INDENT,80);
1209
fprintf(cs->stack->out_file, "%s: ", cs->func);
1211
sprintf(dbuff,"%s: Memory: 0x%lx Bytes: (%ld)\n",
1212
keyword, (ulong) memory, (long) length);
1213
(void) fputs(dbuff,cs->stack->out_file);
1216
while (length-- > 0)
1218
uint tmp= *((unsigned char*) memory++);
1221
fputc('\n',cs->stack->out_file);
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);
1228
(void) fputc('\n',cs->stack->out_file);
1237
* ListAdd add to the list modifiers from debug control string
1241
* static struct link *ListAdd(listp, ctlp, end)
1242
* struct link *listp;
1248
* Given pointer to a comma separated list of strings in "cltp",
1249
* parses the list, and adds it to listp, returning a pointer
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.
1258
static struct link *ListAdd(struct link *head,
1259
const char *ctlp, const char *end)
1262
struct link *new_malloc;
1268
while (ctlp < end && *ctlp != ',')
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;
1284
* ListDel remove from the list modifiers in debug control string
1288
* static struct link *ListDel(listp, ctlp, end)
1289
* struct link *listp;
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.
1301
static struct link *ListDel(struct link *head,
1302
const char *ctlp, const char *end)
1311
while (ctlp < end && *ctlp != ',')
1317
while (*cur && !strncmp((*cur)->str, start, len))
1319
struct link *delme=*cur;
1320
*cur=(*cur)->next_link;
1321
free((void*) delme);
1323
} while (*cur && *(cur=&((*cur)->next_link)));
1331
* ListCopy make a copy of the list
1335
* static struct link *ListCopy(orig)
1336
* struct link *orig;
1340
* Given pointer to list, which contains a copy of every element from
1341
* the original list.
1343
* the orig pointer can be NULL
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.
1351
static struct link *ListCopy(struct link *orig)
1353
struct link *new_malloc;
1358
while (orig != NULL)
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;
1366
orig= orig->next_link;
1374
* InList test a given string for member of a given list
1378
* static BOOLEAN InList(linkp, cp)
1379
* struct link *linkp;
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.
1395
static BOOLEAN InList(struct link *linkp, const char *cp)
1397
REGISTER struct link *scan;
1398
REGISTER BOOLEAN result;
1405
for (scan= linkp; scan != NULL; scan= scan->next_link)
1407
if (!strcmp(scan->str, cp))
1421
* PushState push current settings onto stack and set up new one
1425
* static VOID PushState()
1429
* Pushes the current settings on the settings stack, and creates
1430
* a new settings. The new settings is NOT initialized
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.
1438
static void PushState(CODE_STATE *cs)
1440
struct settings *new_malloc;
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;
1451
* FreeState Free memory associated with a struct state.
1455
* static void FreeState (state)
1456
* struct state *state;
1461
* Deallocates the memory allocated for various information in a
1462
* state. If free_state is set, also free 'state'
1465
static void FreeState(CODE_STATE *cs, struct settings *state, int free_state)
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);
1481
free((void*) state);
1488
* _db_end_ End debugging, freeing state stack memory.
1492
* static VOID _db_end_ ()
1496
* Ends debugging, de-allocating the memory allocated to the
1499
* To be called at the very end of the program.
1504
struct settings *discard;
1505
static struct settings tmp;
1508
get_code_state_or_return;
1510
while ((discard= cs->stack))
1512
if (discard == &init_settings)
1514
cs->stack= discard->next;
1515
FreeState(cs, discard, 1);
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);
1539
* DoTrace check to see if tracing is current enabled
1543
* static BOOLEAN DoTrace(stack)
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.
1555
static BOOLEAN DoTrace(CODE_STATE *cs)
1557
return (TRACING && cs->level <= cs->stack->maxdepth &&
1558
InList(cs->stack->functions, cs->func) &&
1559
InList(cs->stack->processes, cs->process));
1566
* DoProfile check to see if profiling is current enabled
1570
* static BOOLEAN DoProfile()
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.
1583
static BOOLEAN DoProfile(CODE_STATE *cs)
1586
cs->level <= cs->stack->maxdepth &&
1587
InList(cs->stack->p_functions, cs->func) &&
1588
InList(cs->stack->processes, cs->process);
1595
get_code_state_or_return NULL;
1596
return cs->stack->out_file;
1603
* _db_strict_keyword_ test keyword for member of keyword list
1607
* BOOLEAN _db_strict_keyword_(keyword)
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.
1616
* Returns TRUE if keyword accepted, FALSE otherwise.
1620
BOOLEAN _db_strict_keyword_(const char *keyword)
1623
get_code_state_or_return FALSE;
1624
if (!DEBUGGING || cs->stack->keywords == NULL)
1626
return _db_keyword_(cs, keyword);
1632
* _db_keyword_ test keyword for member of keyword list
1636
* BOOLEAN _db_keyword_(keyword)
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.
1650
* Returns TRUE if keyword accepted, FALSE otherwise.
1654
BOOLEAN _db_keyword_(CODE_STATE *cs, const char *keyword)
1656
get_code_state_or_return FALSE;
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));
1668
* Indent indent a line to the given indentation level
1672
* static VOID Indent(indent)
1677
* Indent a line to the given level. Note that this is
1678
* a simple minded but portable implementation.
1679
* There are better ways.
1681
* Also, the indent must be scaled by the compile time option
1682
* of character positions per nesting level.
1686
static void Indent(CODE_STATE *cs, int indent)
1690
indent= max(indent-1-cs->stack->sub_level,0)*INDENT;
1691
for (count= 0; count < indent ; count++)
1693
if ((count % INDENT) == 0)
1694
fputc('|',cs->stack->out_file);
1696
fputc(' ',cs->stack->out_file);
1704
* FreeList free all memory associated with a linked list
1708
* static VOID FreeList(linkp)
1709
* struct link *linkp;
1713
* Given pointer to the head of a linked list, frees all
1714
* memory held by the list and the members of the list.
1718
static void FreeList(struct link *linkp)
1720
REGISTER struct link *old;
1722
while (linkp != NULL)
1725
linkp= linkp->next_link;
1734
* DoPrefix print debugger line prefix prior to indentation
1738
* static VOID DoPrefix(_line_)
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.
1750
static void DoPrefix(CODE_STATE *cs, uint _line_)
1753
if (cs->stack->flags & PID_ON)
1756
(void) fprintf(cs->stack->out_file, "%-7s: ", my_thread_name());
1758
(void) fprintf(cs->stack->out_file, "%5d: ", (int) getpid());
1761
if (cs->stack->flags & NUMBER_ON)
1762
(void) fprintf(cs->stack->out_file, "%5d: ", cs->lineno);
1763
if (cs->stack->flags & TIMESTAMP_ON)
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 */
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);
1778
if (gettimeofday(&tv, NULL) != -1)
1780
if ((tm_p= localtime((const time_t *)&tv.tv_sec)))
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));
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);
1806
* DBUGOpenFile open new output stream for debugger output
1810
* static VOID DBUGOpenFile(name)
1815
* Given name of a new file (or "-" for stdout) opens the file
1816
* and sets the output stream to the new file.
1820
static void DBUGOpenFile(CODE_STATE *cs,
1821
const char *name,const char *end,int append)
1824
REGISTER BOOLEAN newfile;
1831
memcpy(cs->stack->name, name, len);
1832
cs->stack->name[len]=0;
1835
strmov(cs->stack->name,name);
1836
name=cs->stack->name;
1837
if (strcmp(name, "-") == 0)
1839
cs->stack->out_file= stdout;
1840
cs->stack->flags |= FLUSH_ON_WRITE;
1841
cs->stack->name[0]=0;
1845
if (!Writable(name))
1847
(void) fprintf(stderr, ERR_OPEN, cs->process, name);
1853
newfile= !EXISTS(name);
1854
if (!(fp= fopen(name, append ? "a+" : "w")))
1856
(void) fprintf(stderr, ERR_OPEN, cs->process, name);
1862
cs->stack->out_file= fp;
1865
ChangeOwner(cs, name);
1877
* OpenProfile open new output stream for profiler output
1881
* static FILE *OpenProfile(name)
1886
* Given name of a new file, opens the file
1887
* and sets the profiler output stream to the new file.
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].
1901
static FILE *OpenProfile(CODE_STATE *cs, const char *name)
1904
REGISTER BOOLEAN newfile;
1907
if (!Writable(name))
1909
(void) fprintf(cs->stack->out_file, ERR_OPEN, cs->process, name);
1911
(void) Delay(cs->stack->delay);
1915
newfile= !EXISTS(name);
1916
if (!(fp= fopen(name, "w")))
1918
(void) fprintf(cs->stack->out_file, ERR_OPEN, cs->process, name);
1923
cs->stack->prof_file= fp;
1926
ChangeOwner(cs, name);
1937
* DBUGCloseFile close the debug output stream
1941
* static VOID DBUGCloseFile(fp)
1946
* Closes the debug output stream unless it is standard output
1947
* or standard error.
1951
static void DBUGCloseFile(CODE_STATE *cs, FILE *fp)
1953
if (fp != stderr && fp != stdout && fclose(fp) == EOF)
1955
pthread_mutex_lock(&THR_LOCK_dbug);
1956
(void) fprintf(cs->stack->out_file, ERR_CLOSE, cs->process);
1966
* DbugExit print error message and exit
1970
* static VOID DbugExit(why)
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.
1982
static void DbugExit(const char *why)
1984
CODE_STATE *cs=code_state();
1985
(void) fprintf(stderr, ERR_ABORT, cs ? cs->process : "(null)", why);
1986
(void) fflush(stderr);
1994
* DbugMalloc allocate memory for debugger runtime support
1998
* static long *DbugMalloc(size)
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.
2012
static char *DbugMalloc(size_t size)
2014
register char *new_malloc;
2016
if (!(new_malloc= (char*) malloc(size)))
2017
DbugExit("out of memory");
2023
* strtok lookalike - splits on ':', magically handles ::, :\ and :/
2026
static const char *DbugStrTok(const char *s)
2028
while (s[0] && (s[0] != ':' ||
2029
(s[1] == '\\' || s[1] == '/' || (s[1] == ':' && s++))))
2038
* BaseName strip leading pathname components from name
2042
* static char *BaseName(pathname)
2047
* Given pointer to a complete pathname, locates the base file
2048
* name at the end of the pathname and returns a pointer to
2053
static const char *BaseName(const char *pathname)
2055
register const char *base;
2057
base= strrchr(pathname, FN_LIBCHAR);
2058
if (base++ == NullS)
2067
* Writable test to see if a pathname is writable/creatable
2071
* static BOOLEAN Writable(pathname)
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.
2082
* Returns TRUE if the user would normally be allowed write or
2083
* create access to the named file. Returns FALSE otherwise.
2090
static BOOLEAN Writable(const char *pathname)
2092
REGISTER BOOLEAN granted;
2093
REGISTER char *lastslash;
2096
if (EXISTS(pathname))
2098
if (WRITABLE(pathname))
2103
lastslash= strrchr(pathname, '/');
2104
if (lastslash != NULL)
2108
if (WRITABLE(pathname))
2110
if (lastslash != NULL)
2121
* ChangeOwner change owner to real user for suid programs
2125
* static VOID ChangeOwner(pathname)
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.
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
2142
static void ChangeOwner(CODE_STATE *cs, char *pathname)
2144
if (chown(pathname, getuid(), getgid()) == -1)
2146
(void) fprintf(stderr, ERR_CHOWN, cs->process, pathname);
2148
(void) fflush(stderr);
2157
* _db_setjmp_ save debugger environment
2161
* VOID _db_setjmp_()
2165
* Invoked as part of the user's DBUG_SETJMP macro to save
2166
* the debugger environment in parallel with saving the user's
2173
EXPORT void _db_setjmp_()
2176
get_code_state_or_return;
2178
cs->jmplevel= cs->level;
2179
cs->jmpfunc= cs->func;
2180
cs->jmpfile= cs->file;
2186
* _db_longjmp_ restore previously saved debugger environment
2190
* VOID _db_longjmp_()
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.
2200
EXPORT void _db_longjmp_()
2203
get_code_state_or_return;
2205
cs->level= cs->jmplevel;
2207
cs->func= cs->jmpfunc;
2209
cs->file= cs->jmpfile;
2216
* perror perror simulation for systems that don't have it
2220
* static VOID perror(s)
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.
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.
2234
* This version just complains about an "unknown system error".
2239
static void perror(s)
2242
if (s && *s != '\0')
2243
(void) fprintf(stderr, "%s: ", s);
2244
(void) fprintf(stderr, "<unknown system error>\n");
2246
#endif /* HAVE_PERROR */
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 !! */
2253
static void dbug_flush(CODE_STATE *cs)
2256
if (cs->stack->flags & FLUSH_ON_WRITE)
2259
(void) fflush(cs->stack->out_file);
2260
if (cs->stack->delay)
2261
(void) Delay(cs->stack->delay);
2264
pthread_mutex_unlock(&THR_LOCK_dbug);
2268
void _db_lock_file_()
2271
get_code_state_or_return;
2272
pthread_mutex_lock(&THR_LOCK_dbug);
2276
void _db_unlock_file_()
2279
get_code_state_or_return;
2281
pthread_mutex_unlock(&THR_LOCK_dbug);
2285
* Here we need the definitions of the clock routine. Add your
2286
* own for whatever system that you have.
2290
#if defined(HAVE_GETRUSAGE)
2292
#include <sys/param.h>
2293
#include <sys/resource.h>
2295
/* extern int getrusage(int, struct rusage *); */
2298
* Returns the user time in milliseconds used by this process so
2302
static unsigned long Clock()
2306
(void) getrusage(RUSAGE_SELF, &ru);
2307
return ru.ru_utime.tv_sec*1000 + ru.ru_utime.tv_usec/1000;
2310
#elif defined(MSDOS) || defined(__WIN__)
2312
static ulong Clock()
2314
return clock()*(1000/CLOCKS_PER_SEC);
2316
#elif defined(amiga)
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 */
2324
static int first_clock= TRUE;
2325
static struct DateStamp begin;
2326
static struct DateStamp elapsed;
2328
static unsigned long Clock()
2330
register struct DateStamp *now;
2331
register unsigned long millisec= 0;
2332
extern VOID *AllocMem();
2334
now= (struct DateStamp *) AllocMem((long) sizeof(struct DateStamp), 0L);
2337
if (first_clock == TRUE)
2340
(void) DateStamp(now);
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));
2352
static unsigned long Clock()
2357
#endif /* THREADS */
2362
* Fake vfprintf for systems that don't support it. If this
2363
* doesn't work, you are probably SOL...
2366
static int vfprintf(stream, format, ap)
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);
2388
#endif /* NO_VARARGS */
2393
* Dummy function, workaround for MySQL bug#14420 related
2394
* build failure on a platform where linking with an empty
2397
* This block can be removed as soon as a fix for bug#14420
2400
int i_am_a_dummy_function() {