-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
dmalloc.c
1076 lines (953 loc) · 28.4 KB
/
dmalloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* program that handles the dmalloc variables.
*
* Copyright 2020 by Gray Watson
*
* This file is part of the dmalloc package.
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies, and that the name of Gray Watson not be used in advertising
* or publicity pertaining to distribution of the document or software
* without specific, written prior permission.
*
* Gray Watson makes no representations about the suitability of the
* software described herein for any purpose. It is provided "as is"
* without express or implied warranty.
*
* The author may be contacted via https://dmalloc.com/
*/
/*
* This is the dmalloc program which is designed to enable the user to
* easily set the environmental variables that control the dmalloc
* library capabilities.
*/
/*
* NOTE: all standard-output from this program is designed to be run
* through a shell evaluation command by default. Any messages for
* the user should therefore be send to stderr.
*/
#define DMALLOC_DISABLE
#if HAVE_STRING_H
# include <string.h>
#endif
#if HAVE_STDIO_H
# include <stdio.h>
#endif
#if HAVE_STDLIB_H
# include <stdlib.h>
#endif
#include "conf.h"
#include "dmalloc_argv.h" /* for argument processing */
#include "dmalloc.h"
#include "append.h"
#include "compat.h"
#include "debug_tok.h"
#include "env.h"
#include "error_val.h"
#include "dmalloc_loc.h"
#include "version.h"
#define HOME_ENVIRON "HOME" /* home directory */
#define SHELL_ENVIRON "SHELL" /* for the type of shell */
#define DEFAULT_CONFIG ".dmallocrc" /* default config file */
#define TOKENIZE_EQUALS " \t=" /* for tag lines */
#define TOKENIZE_CHARS " \t," /* for tag lines */
#define DEBUG_ARG 'd' /* debug argument */
#define INTERVAL_ARG 'i' /* interval argument */
#define THREAD_LOCK_ON_ARG 'o' /* lock-on argument */
#define LIMIT_ARG 'M' /* memory-limit argument */
#define LINE_WIDTH 75 /* num debug toks per line */
#define FILE_NOT_FOUND 1
#define FILE_FOUND 2
#define TOKEN_FOUND 3
/*
* default flag information
*/
typedef struct {
char *de_string; /* default name */
long de_flags; /* default settings */
} default_t;
#define RUNTIME_FLAGS (DMALLOC_DEBUG_LOG_STATS | DMALLOC_DEBUG_LOG_NONFREE | \
DMALLOC_DEBUG_LOG_BAD_SPACE | \
DMALLOC_DEBUG_CHECK_FENCE | \
DMALLOC_DEBUG_CATCH_NULL)
#define LOW_FLAGS (RUNTIME_FLAGS | \
DMALLOC_DEBUG_LOG_ELAPSED_TIME | \
DMALLOC_DEBUG_CHECK_SHUTDOWN | \
DMALLOC_DEBUG_FREE_BLANK | DMALLOC_DEBUG_ERROR_ABORT | \
DMALLOC_DEBUG_ALLOC_BLANK)
#define MEDIUM_FLAGS (LOW_FLAGS | \
DMALLOC_DEBUG_CHECK_HEAP | \
DMALLOC_DEBUG_REALLOC_COPY)
#define HIGH_FLAGS (MEDIUM_FLAGS | \
DMALLOC_DEBUG_CHECK_BLANK | DMALLOC_DEBUG_CHECK_FUNCS)
#define ALL_FLAGS (HIGH_FLAGS | \
DMALLOC_DEBUG_LOG_TRANS | DMALLOC_DEBUG_LOG_ADMIN | \
DMALLOC_DEBUG_NEVER_REUSE)
/* NOTE: print-messages is not in this list because it is special */
static const default_t defaults[] = {
{ "none", 0 },
{ "runtime", RUNTIME_FLAGS },
{ "run", RUNTIME_FLAGS },
{ "low", LOW_FLAGS },
{ "med", MEDIUM_FLAGS },
{ "medium", MEDIUM_FLAGS },
{ "high", HIGH_FLAGS },
{ "all", ALL_FLAGS },
{ NULL }
};
/* argument variables */
static int bourne_b = 0; /* set bourne shell output */
static int cshell_b = 0; /* set c-shell output */
static int gdb_b = 0; /* set gdb output */
static int rcshell_b = 0; /* set rc shell output */
static char *address = NULL; /* for ADDRESS */
static int clear_b = 0; /* clear variables */
static int debug = 0; /* for DEBUG */
static int errno_to_print = 0; /* to print the error string */
static int help_b = 0; /* print help message */
static char *inpath = NULL; /* for config-file path */
static unsigned long interval = 0; /* for setting INTERVAL */
static int thread_lock_on = 0; /* for setting LOCK_ON */
static int keep_b = 0; /* keep settings override -r */
static int list_tags_b = 0; /* list rc tags */
static int debug_tokens_b = 0; /* list debug tokens */
static char *logpath = NULL; /* for LOGFILE setting */
static int long_tokens_b = 0; /* long-tok output */
static argv_array_t minus; /* tokens to remove */
static unsigned long limit_arg = 0; /* memory limit */
static int make_changes_b = 1; /* make no changes to env */
static argv_array_t plus; /* tokens to add */
static int remove_auto_b = 0; /* auto-remove settings */
static char *start_file = NULL; /* for START settings */
static unsigned long start_iter = 0; /* for START settings */
static unsigned long start_size = 0; /* for START settings */
static int usage_b = 0; /* usage messages */
static int verbose_b = 0; /* verbose flag */
static int very_verbose_b = 0; /* very-verbose flag */
static int version_b = 0; /* print version string */
static char *tag = NULL; /* maybe a tag argument */
static argv_t args[] = {
{ 'b', "bourne-shell", ARGV_BOOL_INT, &bourne_b,
NULL, "set output for bourne shells" },
{ ARGV_OR },
{ 'C', "c-shell", ARGV_BOOL_INT, &cshell_b,
NULL, "set output for C-type shells" },
{ ARGV_OR },
{ 'g', "gdb", ARGV_BOOL_INT, &gdb_b,
NULL, "set output for gdb parsing" },
{ ARGV_OR },
{ 'R', "rc-shell", ARGV_BOOL_INT, &rcshell_b,
NULL, "set output for rc shell" },
{ 'a', "address", ARGV_CHAR_P, &address,
"address:#", "stop when malloc sees address" },
{ 'c', "clear", ARGV_BOOL_INT, &clear_b,
NULL, "clear all variables not set" },
{ DEBUG_ARG, "debug-mask", ARGV_HEX, &debug,
"value", "hex flag to set debug mask" },
{ 'D', "debug-tokens", ARGV_BOOL_INT, &debug_tokens_b,
NULL, "list debug tokens" },
{ 'e', "errno", ARGV_INT, &errno_to_print,
"errno", "print error string for errno" },
{ 'f', "file", ARGV_CHAR_P, &inpath,
"path", "config if not $HOME/.dmallocrc" },
{ 'h', "help", ARGV_BOOL_INT, &help_b,
NULL, "print help message" },
{ INTERVAL_ARG, "interval", ARGV_U_LONG, &interval,
"value", "check heap every number times" },
{ 'k', "keep", ARGV_BOOL_INT, &keep_b,
NULL, "keep settings (override -r)" },
{ 'l', "logfile", ARGV_CHAR_P, &logpath,
"path", "file to log messages to" },
{ 'L', "long-tokens", ARGV_BOOL_INT, &long_tokens_b,
NULL, "set env with tokens not 0x..." },
{ 'm', "minus", ARGV_CHAR_P | ARGV_FLAG_ARRAY, &minus,
"token(s)", "del tokens from current debug" },
{ LIMIT_ARG, "memory-limit", ARGV_U_SIZE, &limit_arg,
"value", "limit allocations to this amount" },
{ 'n', "no-changes", ARGV_BOOL_NEG, &make_changes_b,
NULL, "make no changes to the env" },
{ THREAD_LOCK_ON_ARG, "lock-on", ARGV_INT, &thread_lock_on,
"number", "number of times to not lock" },
{ 'p', "plus", ARGV_CHAR_P | ARGV_FLAG_ARRAY, &plus,
"token(s)", "add tokens to current debug" },
{ 'r', "remove", ARGV_BOOL_INT, &remove_auto_b,
NULL, "remove other settings if tag" },
{ 's', "start-file", ARGV_CHAR_P, &start_file,
"file:line", "check heap after this location" },
{ ARGV_OR },
{ 'S', "start-iter", ARGV_U_LONG, &start_iter,
"number", "check heap after this iteration" },
{ ARGV_OR },
{ '\0', "start-size", ARGV_U_SIZE, &start_size,
"size", "check heap after this mem size" },
{ 't', "list-tags", ARGV_BOOL_INT, &list_tags_b,
NULL, "list tags in rc file" },
{ 'u', "usage", ARGV_BOOL_INT, &usage_b,
NULL, "print usage messages" },
{ 'v', "verbose", ARGV_BOOL_INT, &verbose_b,
NULL, "turn on verbose output" },
{ 'V', "very-verbose", ARGV_BOOL_INT, &very_verbose_b,
NULL, "turn on very-verbose output" },
{ '\0', "version", ARGV_BOOL_INT, &version_b,
NULL, "display version string" },
{ ARGV_MAYBE, NULL, ARGV_CHAR_P, &tag,
"tag", "debug token, internal or from rc file" },
{ ARGV_LAST }
};
/*
* list of bourne shells
*/
static char * const sh_shells[] = { "sh", "ash", "bash", "ksh", "zsh", NULL };
/*
* try a check out the shell env variable to see what form of shell
* commands we should output
*/
static void choose_shell(void)
{
const char *shell, *shell_p;
int shell_c;
char env_buf[256];
shell = loc_getenv(SHELL_ENVIRON, env_buf, sizeof(env_buf), 0);
if (shell == NULL) {
/* oh well, we just guess on c-shell */
cshell_b = 1;
return;
}
shell_p = strrchr(shell, '/');
if (shell_p == NULL) {
shell_p = shell;
}
else {
shell_p++;
}
/* look for the rc shell specifically */
if (strcmp("rc", shell_p) == 0) {
rcshell_b = 1;
return;
}
/* look for the shells which are bourne-shell compatible */
for (shell_c = 0; sh_shells[shell_c] != NULL; shell_c++) {
if (strcmp(sh_shells[shell_c], shell_p) == 0) {
bourne_b = 1;
return;
}
}
/* otherwise set to c-shell */
cshell_b = 1;
}
/*
* dump the current flags set in the debug variable VAL
*/
static void dump_debug(const unsigned long val)
{
const attr_t *attr_p;
char *str;
unsigned long work = val;
int col_c = 0, len;
for (attr_p = attributes; attr_p->at_string != NULL; attr_p++) {
/* the below test for work == 0 is necessary to handle the 'none' token */
if ((work == 0 && attr_p->at_value == 0)
|| (attr_p->at_value != 0 && BIT_IS_SET(work, attr_p->at_value))) {
BIT_CLEAR(work, attr_p->at_value);
if (col_c == 0) {
loc_fprintf(stderr, " ");
col_c += 3;
}
if (very_verbose_b) {
loc_fprintf(stderr, "%s -- %s (%#lx)\n",
attr_p->at_string, attr_p->at_desc, attr_p->at_value);
col_c = 0;
}
else {
str = attr_p->at_string;
len = strlen(str);
if (col_c + len + 2 > LINE_WIDTH) {
loc_fprintf(stderr, "\n");
loc_fprintf(stderr, " ");
col_c = 3;
}
loc_fprintf(stderr, "%s", str);
col_c += len;
/* if we've got more to go then print the , */
if (work != 0) {
loc_fprintf(stderr, ", ");
col_c += 2;
}
}
if (work == 0) {
break;
}
}
}
if (col_c != 0) {
loc_fprintf(stderr, "\n");
}
if (work != 0) {
loc_fprintf(stderr, "%s: warning, unknown debug flag(s): %#lx\n",
argv_program, work);
}
}
/*
* translate TOK into its proper value which is returned
*/
static long token_to_value(const char *tok)
{
const attr_t *attr_p;
/* find the matching attribute string */
for (attr_p = attributes; attr_p->at_string != NULL; attr_p++) {
if (strcmp(tok, attr_p->at_string) == 0) {
break;
}
}
if (attr_p->at_string == NULL) {
loc_fprintf(stderr, "%s: unknown token '%s'\n", argv_program, tok);
return 0;
}
/* if we have a 0 value and not none then this is a disabled token */
if (attr_p->at_value == 0 && strcmp(tok, "none") != 0) {
loc_fprintf(stderr, "%s: token '%s' has been disabled: %s\n",
argv_program, tok, attr_p->at_desc);
return 0;
}
return attr_p->at_value;
}
/*
* Read in the next token from INFILE. It passes back the returned
* debug value in DEBUG_P. Passes back the matching TOKEN of
* TOKEN_SIZE. Returns 1 if there was a next else 0.
*/
static int read_next_token(FILE *infile, long *debug_p,
char *token, const int token_size)
{
int cont_b = 0, found_b = 0;
long new_debug = 0;
char buf[1024], *tok_p, *buf_p;
while (fgets(buf, sizeof(buf), infile) != NULL) {
/* ignore comments and empty lines */
if (buf[0] == '#' || buf[0] == '\n') {
continue;
}
/* chop off the ending \n */
buf_p = strrchr(buf, '\n');
SET_POINTER(buf_p, '\0');
buf_p = buf;
/* if we're not continuing then we need to process a tag */
if (! cont_b) {
/* get the first token on the line */
tok_p = strsep(&buf_p, TOKENIZE_EQUALS);
if (tok_p == NULL) {
continue;
}
if (*tok_p == '\0') {
loc_fprintf(stderr, "Invalid start of line: %s\n", buf_p);
continue;
}
/* save the token */
if (token != NULL) {
(void)strncpy(token, tok_p, token_size);
token[token_size - 1] = '\0';
}
found_b = 1;
}
cont_b = 0;
while (1) {
/* get the next token */
tok_p = strsep(&buf_p, TOKENIZE_CHARS);
if (tok_p == NULL) {
break;
}
if (*tok_p == '\0') {
continue;
}
/* do we have a continuation character? */
if (strcmp(tok_p, "\\") == 0) {
cont_b = 1;
break;
}
new_debug |= token_to_value(tok_p);
}
/* are we done? */
if (! cont_b) {
break;
}
}
SET_POINTER(debug_p, new_debug);
if (found_b) {
return 1;
}
else {
return 0;
}
}
/*
* Read in a rc file from PATH and process it looking for the
* specified DEBUG_VALUE or TAG_FIND token. It passes back the
* returned debug value in DEBUG_P. Passes back the matching TOKEN of
* TOKEN_SIZE.
*
* Returns FILE_NOT_FOUND, FILE_FOUND, or TOKEN_FOUND.
*/
static int read_rc_file(const char *path, const long debug_value,
const char *tag_find, long *debug_p,
char *token, const int token_size)
{
FILE *infile;
int found_b = 0;
char next_token[64];
long new_debug;
/* open the path */
infile = fopen(path, "r");
if (infile == NULL) {
return FILE_NOT_FOUND;
}
/* run through the tokens, looking for a match */
while (read_next_token(infile, &new_debug, next_token,
sizeof(next_token)) == 1) {
/* are we looking for a tag? */
if (tag_find != NULL && strcmp(tag_find, next_token) == 0) {
found_b = 1;
break;
}
/* are we looking for a debug-value? */
if (debug_value > 0 && debug_value == new_debug) {
found_b = 1;
break;
}
}
(void)fclose(infile);
SET_POINTER(debug_p, new_debug);
if (token != NULL) {
(void)loc_snprintf(token, token_size, "config file token: %s",
next_token);
}
if (found_b) {
return TOKEN_FOUND;
}
else {
return FILE_FOUND;
}
}
/*
* Process the user configuration looking for the TAG_FIND. If it is
* null then look for DEBUG_VALUE in the file and copy the token found
* into TOKEN of TOKEN_SIZE. Routine returns the new debug value
* matching tag.
*/
static long find_tag(const long debug_value, const char *tag_find,
char *token, const int token_size)
{
char path[1024], *path_p;
const default_t *def_p;
const char *home_p;
int ret;
long new_debug = 0;
char env_buf[256];
/* do we need to have a home variable? */
if (inpath == NULL) {
/* first we try to read the RC file from the current directory */
ret = read_rc_file(DEFAULT_CONFIG, debug_value, tag_find, &new_debug,
token, token_size);
/* did we find the correct value in the file? */
if (ret == TOKEN_FOUND) {
return new_debug;
}
/* if we did not find the file, check the home directory */
if (ret == FILE_FOUND) {
path_p = DEFAULT_CONFIG;
}
else {
/* find our home directory */
home_p = loc_getenv(HOME_ENVIRON, env_buf, sizeof(env_buf), 0);
if (home_p == NULL) {
loc_fprintf(stderr, "%s: could not find variable '%s'\n",
argv_program, HOME_ENVIRON);
exit(1);
}
(void)loc_snprintf(path, sizeof(path), "%s/%s", home_p, DEFAULT_CONFIG);
/* read in the file from our home directory */
ret = read_rc_file(path, debug_value, tag_find, &new_debug,
token, token_size);
/* did we find the correct value in the file? */
if (ret == TOKEN_FOUND) {
return new_debug;
}
if (ret == FILE_FOUND) {
path_p = path;
}
else {
path_p = NULL;
}
}
}
else {
/* read in the specified file */
ret = read_rc_file(inpath, debug_value, tag_find, &new_debug,
token, token_size);
/* did we find the correct value in the file? */
if (ret == TOKEN_FOUND) {
return new_debug;
}
/* if the specified was not found, return error */
if (ret != FILE_FOUND) {
loc_fprintf(stderr, "%s: could not read '%s': ",
argv_program, inpath);
perror("");
exit(1);
}
path_p = inpath;
}
/* if tag-find is NULL we assume we are looking for a debug-value */
if (tag_find == NULL) {
/* now look for the value in the default token list */
if (token != NULL) {
for (def_p = defaults; def_p->de_string != NULL; def_p++) {
if (def_p->de_flags == debug_value) {
(void)loc_snprintf(token, token_size, "internal token: %s",
def_p->de_string);
new_debug = def_p->de_flags;
break;
}
}
if (def_p->de_string == NULL) {
(void)loc_snprintf(token, token_size, "unknown token");
new_debug = 0;
}
}
}
else {
/* now look for the token in the default token list */
for (def_p = defaults; def_p->de_string != NULL; def_p++) {
if (strcmp(tag_find, def_p->de_string) == 0) {
new_debug = def_p->de_flags;
break;
}
}
/* did we not find the token? */
if (def_p->de_string == NULL) {
if (path_p == NULL) {
loc_fprintf(stderr, "%s: unknown tag '%s'\n", argv_program, tag_find);
}
else {
loc_fprintf(stderr, "%s: could not find tag '%s' in '%s'\n",
argv_program, tag_find, path_p);
}
exit(1);
}
}
return new_debug;
}
/*
* List the tags that in the files.
*/
static void list_tags(void)
{
char path[1024], *path_p, token[80];
const default_t *def_p;
const char *home_p;
long new_debug = 0;
FILE *rc_file;
char env_buf[256];
/* do we need to have a home variable? */
if (inpath == NULL) {
/* first we try to read the RC file from the current directory */
rc_file = fopen(DEFAULT_CONFIG, "r");
if (rc_file == NULL) {
/* if no file in current directory, try home directory */
home_p = loc_getenv(HOME_ENVIRON, env_buf, sizeof(env_buf), 0);
if (home_p == NULL) {
loc_fprintf(stderr, "%s: could not find variable '%s'\n",
argv_program, HOME_ENVIRON);
exit(1);
}
(void)loc_snprintf(path, sizeof(path), "%s/%s", home_p, DEFAULT_CONFIG);
path_p = path;
rc_file = fopen(path, "r");
/* we don't check for error right here */
}
else {
path_p = DEFAULT_CONFIG;
}
}
else {
/* open the specified file */
rc_file = fopen(inpath, "r");
/* we assume that if the file was specified, it must be there */
if (rc_file == NULL) {
loc_fprintf(stderr, "%s: could not read '%s': ",
argv_program, inpath);
perror("");
exit(1);
}
path_p = inpath;
}
if (rc_file != NULL) {
loc_fprintf(stderr, "Tags available from '%s':\n", path_p);
while (read_next_token(rc_file, &new_debug, token, sizeof(token)) == 1) {
if (verbose_b) {
loc_fprintf(stderr, "%s (%#lx):\n", token, new_debug);
dump_debug(new_debug);
}
else {
loc_fprintf(stderr, "%s\n", token);
}
}
(void)fclose(rc_file);
}
loc_fprintf(stderr, "\n");
loc_fprintf(stderr, "Tags available by default:\n");
for (def_p = defaults; def_p->de_string != NULL; def_p++) {
if (verbose_b) {
loc_fprintf(stderr, "%s (%#lx):\n", def_p->de_string, def_p->de_flags);
dump_debug(def_p->de_flags);
}
else {
loc_fprintf(stderr, "%s\n", def_p->de_string);
}
}
}
/*
* dump the current settings of the malloc variables
*/
static void dump_current(void)
{
char *log_path, *loc_start_file, token[64];
const char *env_str;
DMALLOC_PNT addr;
unsigned long inter, limit_val, loc_start_size, loc_start_iter;
unsigned long addr_count;
int lock_on, loc_start_line;
unsigned int flags;
char env_buf[256];
/* get the options flag */
env_str = loc_getenv(OPTIONS_ENVIRON, env_buf, sizeof(env_buf), 0);
if (env_str == NULL) {
env_str = "";
}
_dmalloc_environ_process(env_str, &addr, &addr_count, &flags,
&inter, &lock_on, &log_path,
&loc_start_file, &loc_start_line, &loc_start_iter,
&loc_start_size, &limit_val);
if (flags == 0) {
loc_fprintf(stderr, "Debug-Flags not-set\n");
}
else {
(void)find_tag(flags, NULL, token, sizeof(token));
loc_fprintf(stderr, "Debug-Flags %#x (%u) (%s)\n", flags, flags, token);
if (verbose_b) {
dump_debug(flags);
}
}
if (addr == NULL) {
loc_fprintf(stderr, "Address not-set\n");
}
else {
if (addr_count == 0) {
loc_fprintf(stderr, "Address %p\n", addr);
}
else {
loc_fprintf(stderr, "Address %p, count = %lu\n", addr, addr_count);
}
}
if (inter == 0) {
loc_fprintf(stderr, "Interval not-set\n");
}
else {
loc_fprintf(stderr, "Interval %lu\n", inter);
}
if (lock_on == 0) {
loc_fprintf(stderr, "Lock-On not-set\n");
}
else {
loc_fprintf(stderr, "Lock-On %d\n", lock_on);
}
if (log_path == NULL) {
loc_fprintf(stderr, "Logpath not-set\n");
}
else {
loc_fprintf(stderr, "Logpath '%s'\n", log_path);
}
if (limit_val == 0) {
loc_fprintf(stderr, "Mem-Limit not-set\n");
}
else {
loc_fprintf(stderr, "Mem-Limit %lu\n", limit_val);
}
if (loc_start_file != NULL) {
loc_fprintf(stderr, "Start-File '%s', line = %d\n", loc_start_file, loc_start_line);
}
else if (loc_start_iter > 0) {
loc_fprintf(stderr, "Start-Count %lu\n", loc_start_iter);
}
else if (loc_start_size > 0) {
loc_fprintf(stderr, "Start-Size %lu\n", loc_start_size);
}
else {
loc_fprintf(stderr, "Start not-set\n");
}
loc_fprintf(stderr, "\n");
loc_fprintf(stderr, "Debug Malloc Utility: https://dmalloc.com/\n");
loc_fprintf(stderr, " For a list of the command-line options enter: %s --usage\n",
argv_argv[0]);
}
/*
* output the code to set env VAR to VALUE
*/
static void set_variable(const char *var, const char *value)
{
char comm[1024];
if (value == NULL || *value == '\0') {
(void)loc_snprintf(comm, sizeof(comm), "unset %s\n", var);
}
else if (bourne_b) {
(void)loc_snprintf(comm, sizeof(comm), "export %s=%s\n",
var, value, var);
}
else if (rcshell_b) {
(void)loc_snprintf(comm, sizeof(comm), "%s='%s'\n", var, value);
}
else if (gdb_b) {
(void)loc_snprintf(comm, sizeof(comm), "set env %s %s\n", var, value);
}
else {
(void)loc_snprintf(comm, sizeof(comm), "setenv %s %s\n", var, value);
}
if (make_changes_b) {
(void)printf("%s", comm);
}
if ((! make_changes_b) || verbose_b) {
loc_fprintf(stderr, "Outputed:\n");
loc_fprintf(stderr, "%s", comm);
}
}
/*
* Returns the string for ERROR_NUM.
*/
static char *local_strerror(const int error_num)
{
const error_str_t *err_p;
for (err_p = error_list; err_p->es_error != 0; err_p++) {
if (err_p->es_error == error_num) {
return err_p->es_string;
}
}
return INVALID_ERROR;
}
/*
* static void header
*
* Print out a little usage header to the user.
*/
static void header(void)
{
loc_fprintf(stderr, "Debug Malloc Utility: https://dmalloc.com/\n");
loc_fprintf(stderr,
" This utility helps set the Debug Malloc environment variables.\n");
}
int main(int argc, char **argv)
{
char buf[1024];
int set_b = 0;
char *log_path, *loc_start_file;
const char *env_str;
DMALLOC_PNT addr;
unsigned long inter, limit_val, loc_start_size, loc_start_iter;
unsigned long addr_count;
int lock_on;
int loc_start_line;
unsigned int flags;
char env_buf[256];
argv_help_string = "Sets dmalloc library env variables. Also try --usage.";
argv_version_string = dmalloc_version;
argv_usage_type = ARGV_USAGE_LONG;
argv_error_type = ARGV_USAGE_SHORT_REM;
argv_process(args, argc, argv);
if (help_b) {
header();
loc_fprintf(stderr, " For a list of the command-line options enter: %s --usage\n",
argv_argv[0]);
exit(0);
}
if (usage_b) {
header();
argv_usage(args, argv_usage_type);
exit(0);
}
if (version_b) {
loc_fprintf(stderr, "Dmalloc utility version string is: %s\n", argv_version_string);
loc_fprintf(stderr,
" NOTE: Library linked with your application may be a different version.\n");
loc_fprintf(stderr,
" Check top of logfile after application is run for library version.\n");
exit(0);
}
if (very_verbose_b) {
verbose_b = 1;
}
/* try to figure out the shell we are using */
if ((! bourne_b) && (! cshell_b) && (! gdb_b) && (! rcshell_b)) {
choose_shell();
}
/* get the current debug information from the env variable */
env_str = loc_getenv(OPTIONS_ENVIRON, env_buf, sizeof(env_buf), 0);
if (env_str == NULL) {
env_str = "";
}
_dmalloc_environ_process(env_str, &addr, &addr_count, &flags, &inter,
&lock_on, &log_path, &loc_start_file,
&loc_start_line, &loc_start_iter, &loc_start_size,
&limit_val);
/*
* So, if a tag was specified on the command line then we set the
* debug from it. If it was not then we see if the debug flags were
* set as a hex value from the -d. If this was not used then take
* the current value.
*/
if (tag == NULL) {
if (argv_was_used(args, DEBUG_ARG)) {
set_b = 1;
/* should we clear the rest? */
if (remove_auto_b && (! keep_b)) {
clear_b = 1;
}
}
else {
debug = flags;
}
}
else {
if (argv_was_used(args, DEBUG_ARG)) {
loc_fprintf(stderr, "%s: warning -d ignored, processing tag '%s'\n",
argv_program, tag);
}
set_b = 1;
debug = find_tag(0L, tag, NULL, 0);
/* should we clear the rest? */
if (remove_auto_b && (! keep_b)) {
clear_b = 1;
}
}
if (plus.aa_entry_n > 0) {
int plus_c;
for (plus_c = 0; plus_c < plus.aa_entry_n; plus_c++) {
BIT_SET(debug, token_to_value(ARGV_ARRAY_ENTRY(plus, char *, plus_c)));
set_b = 1;
}
}
if (minus.aa_entry_n > 0) {
int minus_c;
for (minus_c = 0; minus_c < minus.aa_entry_n; minus_c++) {
BIT_CLEAR(debug,
token_to_value(ARGV_ARRAY_ENTRY(minus, char *, minus_c)));
set_b = 1;
}
}
if (address != NULL) {
_dmalloc_address_break(address, &addr, &addr_count);
set_b = 1;
}
else if (clear_b) {
addr = NULL;
}
if (argv_was_used(args, INTERVAL_ARG)) {
inter = interval;
set_b = 1;
}
else if (clear_b) {
inter = 0;
}
/*
* NOTE: this should be after the debug setting which this tests.
*/
if (argv_was_used(args, THREAD_LOCK_ON_ARG)) {
lock_on = thread_lock_on;
set_b = 1;
}
else if (clear_b) {
lock_on = 0;
}
if (logpath != NULL) {
log_path = logpath;
set_b = 1;
}
else if (clear_b) {
log_path = NULL;
}
if (start_file != NULL) {
_dmalloc_start_break(start_file, &loc_start_file, &loc_start_line,
&loc_start_iter, &loc_start_size);
set_b = 1;
}
else if (start_iter > 0) {
loc_start_file = NULL;
loc_start_line = 0;
loc_start_iter = start_iter;