Added global struct "options"
Substituted opt_* with options.align, options.user, options.syscalls,
options.libcalls, options.demangle
diff --git a/breakpoints.c b/breakpoints.c
index 92e48e3..ad1cacf 100644
--- a/breakpoints.c
+++ b/breakpoints.c
@@ -88,7 +88,7 @@
* If the dynamic linker hasn't populated the PLT then
* dont enable the breakpoints
*/
- if (opt_L) {
+ if (options.libcalls) {
a = ptrace(PTRACE_PEEKTEXT, proc->pid,
sym2addr(proc, proc->list_of_symbols),
0);
@@ -167,7 +167,7 @@
}
proc->breakpoints = dict_init(dict_key2hash_int, dict_key_cmp_int);
- if (opt_L && proc->filename) {
+ if (options.libcalls && proc->filename) {
proc->list_of_symbols = read_elf(proc);
if (opt_e) {
struct library_symbol **tmp1 = &(proc->list_of_symbols);
diff --git a/defs.h b/defs.h
index e59ebca..1eadb47 100644
--- a/defs.h
+++ b/defs.h
@@ -1,6 +1,6 @@
-#ifndef DEFAULT_ACOLUMN
-#define DEFAULT_ACOLUMN 50 /* default alignment column for results */
+#ifndef DEFAULT_ALIGN
+#define DEFAULT_ALIGN 50 /* default alignment column for results */
#endif /* (-a switch) */
#ifndef MAX_ARGS
diff --git a/execute_program.c b/execute_program.c
index dfc27f4..e8f801a 100644
--- a/execute_program.c
+++ b/execute_program.c
@@ -22,7 +22,7 @@
uid_t run_uid, run_euid;
gid_t run_gid, run_egid;
- if (opt_u) {
+ if (options.user) {
struct passwd *pent;
if (getuid() != 0 || geteuid() != 0) {
@@ -30,14 +30,14 @@
"you must be root to use the -u option\n");
exit(1);
}
- if ((pent = getpwnam(opt_u)) == NULL) {
- fprintf(stderr, "cannot find user `%s'\n", opt_u);
+ if ((pent = getpwnam(options.user)) == NULL) {
+ fprintf(stderr, "cannot find user `%s'\n", options.user);
exit(1);
}
run_uid = pent->pw_uid;
run_gid = pent->pw_gid;
- if (initgroups(opt_u, run_gid) < 0) {
+ if (initgroups(options.user, run_gid) < 0) {
perror("ltrace: initgroups");
exit(1);
}
@@ -45,7 +45,7 @@
run_uid = getuid();
run_gid = getgid();
}
- if (opt_u || !geteuid()) {
+ if (options.user || !geteuid()) {
struct stat statbuf;
run_euid = run_uid;
run_egid = run_gid;
diff --git a/ltrace.c b/ltrace.c
index 33b573b..8441a18 100644
--- a/ltrace.c
+++ b/ltrace.c
@@ -10,7 +10,6 @@
#include <sys/param.h>
#include <signal.h>
#include <sys/wait.h>
-#include <sys/ioctl.h>
#include "ltrace.h"
#include "output.h"
@@ -81,25 +80,6 @@
}
}
-static void
-guess_cols(void) {
- struct winsize ws;
- char *c;
-
- opt_a = DEFAULT_ACOLUMN;
- c = getenv("COLUMNS");
- if (c && *c) {
- char *endptr;
- int cols;
- cols = strtol(c, &endptr, 0);
- if (cols > 0 && !*endptr) {
- opt_a = cols * 5 / 8;
- }
- } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
- opt_a = ws.ws_col * 5 / 8;
- }
-}
-
int
main(int argc, char **argv) {
struct opt_p_t *opt_p_tmp;
@@ -108,7 +88,6 @@
signal(SIGINT, signal_exit); /* Detach processes when interrupted */
signal(SIGTERM, signal_exit); /* ... or killed */
- guess_cols();
argv = process_options(argc, argv);
while (opt_F) {
/* If filename begins with ~, expand it to the user's home */
diff --git a/options.c b/options.c
index 3d3b11c..1d402d0 100644
--- a/options.c
+++ b/options.c
@@ -8,6 +8,7 @@
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
+#include <sys/ioctl.h>
#if HAVE_GETOPT_H
#include <getopt.h>
@@ -24,26 +25,29 @@
#define SYSTEM_CONFIG_FILE SYSCONFDIR "/ltrace.conf"
#define USER_CONFIG_FILE "~/.ltrace.conf"
+struct options_t options = {
+ .align = DEFAULT_ALIGN, /* alignment column for results */
+ .user = NULL, /* username to run command as */
+ .syscalls = 0, /* display syscalls */
+ .libcalls = 1, /* display library calls */
+#ifdef USE_DEMANGLE
+ .demangle = 0, /* Demangle low-level symbol names */
+#endif
+};
+
#define MAX_LIBRARY 30
char *library[MAX_LIBRARY];
int library_num = 0;
static char *progname; /* Program name (`ltrace') */
FILE *output;
-int opt_a = DEFAULT_ACOLUMN; /* alignment column for results */
int opt_A = DEFAULT_ARRAYLEN; /* maximum # array elements to print */
int opt_c = 0; /* Report a summary on program exit */
int opt_d = 0; /* debug */
int opt_i = 0; /* instruction pointer */
int opt_s = DEFAULT_STRLEN; /* maximum # of bytes printed in strings */
-int opt_S = 0; /* display syscalls */
-int opt_L = 1; /* display library calls */
int opt_f = 0; /* trace child processes as they are created */
-char *opt_u = NULL; /* username to run command as */
int opt_r = 0; /* print relative timestamp */
int opt_t = 0; /* print absolute timestamp */
-#ifdef USE_DEMANGLE
-int opt_C = 0; /* Demangle low-level symbol names */
-#endif
int opt_n = 0; /* indent output according to program flow */
int opt_T = 0; /* show the time spent inside each call */
int opt_o = 0; /* output to a specific file */
@@ -97,11 +101,10 @@
" -e expr modify which events to trace.\n"
" -f follow forks.\n"
# if HAVE_GETOPT_LONG
- " -F, --config=FILE load alternate configuration file\n"
+ " -F, --config=FILE load alternate configuration file (can be repeated).\n"
# else
- " -F FILE load alternate configuration file\n"
+ " -F FILE load alternate configuration file (can be repeated).\n"
# endif
- " (can be repeated).\n"
# if HAVE_GETOPT_LONG
" -h, --help display this help and exit.\n"
# else
@@ -177,11 +180,32 @@
return filename;
}
+static void
+guess_cols(void) {
+ struct winsize ws;
+ char *c;
+
+ options.align = DEFAULT_ALIGN;
+ c = getenv("COLUMNS");
+ if (c && *c) {
+ char *endptr;
+ int cols;
+ cols = strtol(c, &endptr, 0);
+ if (cols > 0 && !*endptr) {
+ options.align = cols * 5 / 8;
+ }
+ } else if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
+ options.align = ws.ws_col * 5 / 8;
+ }
+}
+
char **
process_options(int argc, char **argv) {
progname = argv[0];
output = stderr;
+ guess_cols();
+
#if HAVE_GETOPT || HAVE_GETOPT_LONG
while (1) {
int c;
@@ -219,7 +243,7 @@
}
switch (c) {
case 'a':
- opt_a = atoi(optarg);
+ options.align = atoi(optarg);
break;
case 'A':
opt_A = atoi(optarg);
@@ -229,7 +253,7 @@
break;
#ifdef USE_DEMANGLE
case 'C':
- opt_C++;
+ options.demangle++;
break;
#endif
case 'd':
@@ -299,7 +323,7 @@
library[library_num++] = optarg;
break;
case 'L':
- opt_L = 0;
+ options.libcalls = 0;
break;
case 'n':
opt_n = atoi(optarg);
@@ -335,7 +359,7 @@
opt_s = atoi(optarg);
break;
case 'S':
- opt_S = 1;
+ options.syscalls = 1;
break;
case 't':
opt_t++;
@@ -344,7 +368,7 @@
opt_T++;
break;
case 'u':
- opt_u = optarg;
+ options.user = optarg;
break;
case 'V':
printf("ltrace version " PACKAGE_VERSION ".\n"
diff --git a/options.h b/options.h
index da5c5aa..78d271c 100644
--- a/options.h
+++ b/options.h
@@ -5,20 +5,24 @@
#include <stdio.h>
#include <sys/types.h>
+struct options_t {
+ int align; /* -a: default alignment column for results */
+ char * user; /* -u: username to run command as */
+ int syscalls; /* -S: display system calls */
+ int libcalls; /* -L: display library calls */
+ int demangle; /* -C: demangle low-level names into user-level names */
+};
+extern struct options_t options;
+
extern FILE *output;
-extern int opt_a; /* default alignment column for results */
extern int opt_A; /* default maximum # of array elements printed */
extern int opt_c; /* count time, calls, and report a summary on program exit */
extern int opt_d; /* debug */
extern int opt_i; /* instruction pointer */
extern int opt_s; /* default maximum # of bytes printed in strings */
-extern int opt_L; /* display library calls */
-extern int opt_S; /* display system calls */
extern int opt_f; /* trace child processes */
-extern char *opt_u; /* username to run command as */
extern int opt_r; /* print relative timestamp */
extern int opt_t; /* print absolute timestamp */
-extern int opt_C; /* Demanglelow-level symbol names into user-level names */
extern int opt_n; /* indent trace output according to program flow */
extern int opt_T; /* show the time spent inside each call */
extern int opt_o; /* output to a specific file */
diff --git a/output.c b/output.c
index 5e089b7..31d87da 100644
--- a/output.c
+++ b/output.c
@@ -112,8 +112,8 @@
tmp = list_of_functions;
while (tmp) {
#ifdef USE_DEMANGLE
- str1 = opt_C ? my_demangle(tmp->name) : tmp->name;
- str2 = opt_C ? my_demangle(name) : name;
+ str1 = options.demangle ? my_demangle(tmp->name) : tmp->name;
+ str2 = options.demangle ? my_demangle(name) : name;
#else
str1 = tmp->name;
str2 = name;
@@ -183,7 +183,7 @@
#ifdef USE_DEMANGLE
current_column +=
fprintf(output, "%s(",
- opt_C ? my_demangle(function_name) : function_name);
+ options.demangle ? my_demangle(function_name) : function_name);
#else
current_column += fprintf(output, "%s(", function_name);
#endif
@@ -268,7 +268,7 @@
#ifdef USE_DEMANGLE
current_column +=
fprintf(output, "<... %s resumed> ",
- opt_C ? my_demangle(function_name) : function_name);
+ options.demangle ? my_demangle(function_name) : function_name);
#else
current_column +=
fprintf(output, "<... %s resumed> ", function_name);
@@ -277,7 +277,7 @@
if (!func) {
current_column += fprintf(output, ") ");
- tabto(opt_a - 1);
+ tabto(options.align - 1);
fprintf(output, "= ");
display_arg(type, proc, -1, arg_unknown);
} else {
@@ -293,7 +293,7 @@
display_arg(type, proc, i, func->arg_info[i]);
}
current_column += fprintf(output, ") ");
- tabto(opt_a - 1);
+ tabto(options.align - 1);
fprintf(output, "= ");
if (func->return_info->type == ARGTYPE_VOID) {
fprintf(output, "<void>");
diff --git a/process_event.c b/process_event.c
index 7c8a725..f3c966d 100644
--- a/process_event.c
+++ b/process_event.c
@@ -213,7 +213,7 @@
static void
process_syscall(struct event *event) {
- if (opt_S) {
+ if (options.syscalls) {
output_left(LT_TOF_SYSCALL, event->proc,
sysname(event->proc, event->e_un.sysnum));
}
@@ -228,7 +228,7 @@
static void
process_arch_syscall(struct event *event) {
- if (opt_S) {
+ if (options.syscalls) {
output_left(LT_TOF_SYSCALL, event->proc,
arch_sysname(event->proc, event->e_un.sysnum));
}
@@ -280,7 +280,7 @@
enable_all_breakpoints(event->proc);
}
callstack_pop(event->proc);
- if (opt_S) {
+ if (options.syscalls) {
output_right(LT_TOF_SYSCALLR, event->proc,
sysname(event->proc, event->e_un.sysnum));
}
@@ -293,7 +293,7 @@
calc_time_spent(event->proc);
}
callstack_pop(event->proc);
- if (opt_S) {
+ if (options.syscalls) {
output_right(LT_TOF_SYSCALLR, event->proc,
arch_sysname(event->proc, event->e_un.sysnum));
}
diff --git a/summary.c b/summary.c
index 4865597..4a6af70 100644
--- a/summary.c
+++ b/summary.c
@@ -83,7 +83,7 @@
(unsigned long int)(c / entries[i].count),
entries[i].count,
#ifdef USE_DEMANGLE
- opt_C ? my_demangle(entries[i].name) :
+ options.demangle ? my_demangle(entries[i].name) :
#endif
entries[i].name);
}