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
|
/*
* Environment handling routines
*
* Copyright 2000 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 http://dmalloc.com/
*
* $Id: env.c,v 1.37 2007/05/14 17:09:20 gray Exp $
*/
/*
* This file contains short routine(s) to process the environment
* variable(s) used by the library to get the runtime option(s).
*/
#define DMALLOC_DISABLE
#if HAVE_STDLIB_H
# include <stdlib.h>
#endif
#if HAVE_STRING_H
# include <string.h>
#endif
#if HAVE_UNISTD_H
# include <unistd.h> /* for getpid */
#endif
#include "conf.h"
#include "dmalloc.h"
#include "compat.h"
#include "dmalloc_loc.h"
#include "debug_tok.h"
#include "env.h"
#include "error.h"
/*
* Environmental labels.
*
* NOTE: the decision has been made _not_ to do the sizeof() hack for
* portability reasons.
*/
#define ADDRESS_LABEL "addr"
#define DEBUG_LABEL "debug"
#define INTERVAL_LABEL "inter"
#define LOCK_ON_LABEL "lockon"
#define LOGFILE_LABEL "log"
#define START_LABEL "start"
#define LIMIT_LABEL "limit"
#define ASSIGNMENT_CHAR '='
/* local variables */
static char log_path[512] = { '\0' }; /* storage for env path */
static char start_file[512] = { '\0' }; /* file to start at */
/****************************** local utilities ******************************/
/*
* Hexadecimal STR to int translation
*/
static long hex_to_long(const char *str)
{
long ret;
/* strip off spaces */
for (; *str == ' ' || *str == '\t'; str++) {
}
/* skip a leading 0[xX] */
if (*str == '0' && (*(str + 1) == 'x' || *(str + 1) == 'X')) {
str += 2;
}
for (ret = 0;; str++) {
if (*str >= '0' && *str <= '9') {
ret = ret * 16 + (*str - '0');
}
else if (*str >= 'a' && *str <= 'f') {
ret = ret * 16 + (*str - 'a' + 10);
}
else if (*str >= 'A' && *str <= 'F') {
ret = ret * 16 + (*str - 'A' + 10);
}
else {
break;
}
}
return ret;
}
/***************************** exported routines *****************************/
/*
* Break up ADDR_ALL into ADDR_P and ADDR_COUNT_P
*/
void _dmalloc_address_break(const char *addr_all, DMALLOC_PNT *addr_p,
unsigned long *addr_count_p)
{
char *colon_p;
SET_POINTER(addr_p, (DMALLOC_PNT)hex_to_long(addr_all));
if (addr_count_p != NULL) {
colon_p = strchr(addr_all, ':');
if (colon_p != NULL) {
*addr_count_p = loc_atoul(colon_p + 1);
}
}
}
/*
* Break up START_ALL into SFILE_P, SLINE_P, and SCOUNT_P
*/
void _dmalloc_start_break(char *start_all, char **start_file_p,
int *start_line_p, unsigned long *start_iter_p,
unsigned long *start_size_p)
{
char *start_p;
start_p = strchr(start_all, ':');
if (start_p != NULL) {
(void)strncpy(start_file, start_all, sizeof(start_file));
start_file[sizeof(start_file) - 1] = '\0';
SET_POINTER(start_file_p, start_file);
start_p = start_file + (start_p - start_all);
*start_p = '\0';
SET_POINTER(start_line_p, atoi(start_p + 1));
SET_POINTER(start_iter_p, 0);
SET_POINTER(start_size_p, 0);
}
else if (start_all[0] == 's') {
SET_POINTER(start_file_p, NULL);
SET_POINTER(start_line_p, 0);
SET_POINTER(start_iter_p, 0);
SET_POINTER(start_size_p, loc_atoul(start_all + 1));
}
else {
SET_POINTER(start_file_p, NULL);
SET_POINTER(start_line_p, 0);
if (start_all[0] == 'c') {
SET_POINTER(start_iter_p, loc_atoul(start_all + 1));
}
else {
SET_POINTER(start_iter_p, loc_atoul(start_all));
}
SET_POINTER(start_size_p, 0);
}
}
/*
* Process the values of dmalloc environ variable(s) from ENVIRON
* string.
*/
void _dmalloc_environ_process(const char *env_str, DMALLOC_PNT *addr_p,
unsigned long *addr_count_p,
unsigned int *debug_p,
unsigned long *interval_p, int *lock_on_p,
char **logpath_p, char **start_file_p,
int *start_line_p,
unsigned long *start_iter_p,
unsigned long *start_size_p,
unsigned long *limit_p)
{
char *env_p, *this_p;
char buf[1024];
int len, done_b = 0;
unsigned int flags = 0;
attr_t *attr_p;
SET_POINTER(addr_p, NULL);
SET_POINTER(addr_count_p, 0);
SET_POINTER(debug_p, 0);
SET_POINTER(interval_p, 0);
SET_POINTER(lock_on_p, 0);
SET_POINTER(logpath_p, NULL);
SET_POINTER(start_file_p, NULL);
SET_POINTER(start_line_p, 0);
SET_POINTER(start_iter_p, 0);
SET_POINTER(start_size_p, 0);
SET_POINTER(limit_p, 0);
/* make a copy */
(void)strncpy(buf, env_str, sizeof(buf));
buf[sizeof(buf) - 1] = '\0';
/* handle each of tokens, in turn */
for (env_p = buf, this_p = buf; ! done_b; env_p++, this_p = env_p) {
/* find the comma of end */
for (;; env_p++) {
if (*env_p == '\0') {
done_b = 1;
break;
}
if (*env_p == ',' && (env_p == buf || *(env_p - 1) != '\\')) {
break;
}
}
/* should we strip ' ' or '\t' here? */
if (this_p == env_p) {
continue;
}
*env_p = '\0';
len = strlen(ADDRESS_LABEL);
if (strncmp(this_p, ADDRESS_LABEL, len) == 0
&& *(this_p + len) == ASSIGNMENT_CHAR) {
this_p += len + 1;
_dmalloc_address_break(this_p, addr_p, addr_count_p);
continue;
}
len = strlen(DEBUG_LABEL);
if (strncmp(this_p, DEBUG_LABEL, len) == 0
&& *(this_p + len) == ASSIGNMENT_CHAR) {
this_p += len + 1;
SET_POINTER(debug_p, hex_to_long(this_p));
continue;
}
len = strlen(INTERVAL_LABEL);
if (strncmp(this_p, INTERVAL_LABEL, len) == 0
&& *(this_p + len) == ASSIGNMENT_CHAR) {
this_p += len + 1;
SET_POINTER(interval_p, loc_atoul(this_p));
continue;
}
len = strlen(LOCK_ON_LABEL);
if (strncmp(this_p, LOCK_ON_LABEL, len) == 0
&& *(this_p + len) == ASSIGNMENT_CHAR) {
this_p += len + 1;
SET_POINTER(lock_on_p, atoi(this_p));
continue;
}
/* get the dmalloc logfile name into a holding variable */
len = strlen(LOGFILE_LABEL);
if (strncmp(this_p, LOGFILE_LABEL, len) == 0
&& *(this_p + len) == ASSIGNMENT_CHAR) {
this_p += len + 1;
(void)strncpy(log_path, this_p, sizeof(log_path));
log_path[sizeof(log_path) - 1] = '\0';
SET_POINTER(logpath_p, log_path);
continue;
}
/*
* start checking the heap after X iterations OR
* start at a file:line combination
*/
len = strlen(START_LABEL);
if (strncmp(this_p, START_LABEL, len) == 0
&& *(this_p + len) == ASSIGNMENT_CHAR) {
this_p += len + 1;
_dmalloc_start_break(this_p, start_file_p, start_line_p, start_iter_p,
start_size_p);
continue;
}
/* set the memory limit to the library */
len = strlen(LIMIT_LABEL);
if (strncmp(this_p, LIMIT_LABEL, len) == 0
&& *(this_p + len) == ASSIGNMENT_CHAR) {
this_p += len + 1;
SET_POINTER(limit_p, loc_atoul(this_p));
continue;
}
/* need to check the short/long debug options */
for (attr_p = attributes; attr_p->at_string != NULL; attr_p++) {
if (strcmp(this_p, attr_p->at_string) == 0) {
flags |= attr_p->at_value;
break;
}
}
if (attr_p->at_string != NULL) {
continue;
}
}
/* append the token settings to the debug setting */
if (debug_p != NULL) {
if (*debug_p == 0) {
*debug_p = flags;
}
else {
*debug_p |= flags;
}
}
}
/*
* Set dmalloc environ variable(s) with the values (maybe SHORT debug
* info) into BUF.
*/
void _dmalloc_environ_set(char *buf, const int buf_size,
const int long_tokens_b,
const DMALLOC_PNT address,
const unsigned long addr_count,
const unsigned int debug,
const unsigned long interval, const int lock_on,
const char *logpath, const char *start_file_p,
const int start_line,
const unsigned long start_iter,
const unsigned long start_size,
const unsigned long limit_val)
{
char *buf_p = buf, *bounds_p = buf + buf_size;
if (debug > 0) {
if (long_tokens_b) {
attr_t *attr_p;
for (attr_p = attributes; attr_p->at_string != NULL; attr_p++) {
if (debug & attr_p->at_value) {
buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s,",
attr_p->at_string);
}
}
}
else {
buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%#x,",
DEBUG_LABEL, ASSIGNMENT_CHAR, debug);
}
}
if (address != NULL) {
if (addr_count > 0) {
buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%#lx:%lu,",
ADDRESS_LABEL, ASSIGNMENT_CHAR, (long)address,
addr_count);
}
else {
buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%#lx,",
ADDRESS_LABEL, ASSIGNMENT_CHAR, (long)address);
}
}
if (interval > 0) {
buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%lu,",
INTERVAL_LABEL, ASSIGNMENT_CHAR, interval);
}
if (lock_on > 0) {
buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%d,",
LOCK_ON_LABEL, ASSIGNMENT_CHAR, lock_on);
}
if (logpath != NULL) {
buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%s,",
LOGFILE_LABEL, ASSIGNMENT_CHAR, logpath);
}
if (start_file_p != NULL) {
if (start_line > 0) {
buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%s:%d,",
START_LABEL, ASSIGNMENT_CHAR, start_file_p,
start_line);
}
else {
buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%s,",
START_LABEL, ASSIGNMENT_CHAR, start_file_p);
}
}
else if (start_iter > 0) {
/* NOTE: there is an 'c' (for count) before the iter variable here */
buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%cc%lu,",
START_LABEL, ASSIGNMENT_CHAR, start_iter);
}
else if (start_size > 0) {
/* NOTE: there is an 's' before the size variable here */
buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%cs%lu,",
START_LABEL, ASSIGNMENT_CHAR, start_size);
}
if (limit_val > 0) {
buf_p += loc_snprintf(buf_p, bounds_p - buf_p, "%s%c%lu,",
LIMIT_LABEL, ASSIGNMENT_CHAR, limit_val);
}
/* cut off the last comma */
if (buf_p > buf) {
buf_p--;
}
*buf_p = '\0';
}
|