| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 1 | #include <string.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | #include <errno.h> |
| 5 | #include <limits.h> |
| 6 | |
| 7 | #include "ltrace.h" |
| 8 | #include "options.h" |
| 9 | #include "defs.h" |
| 10 | |
| 11 | FILE * output; |
| 12 | int opt_a = DEFAULT_ACOLUMN; /* default alignment column for results */ |
| 13 | int opt_d = 0; /* debug */ |
| 14 | int opt_i = 0; /* instruction pointer */ |
| 15 | int opt_s = DEFAULT_STRLEN; /* default maximum # of bytes printed in strings */ |
| 16 | int opt_S = 0; /* display syscalls */ |
| 17 | int opt_L = 1; /* display library calls */ |
| 18 | int opt_f = 0; /* trace child processes as they are created */ |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 19 | char * opt_u = NULL; /* username to run command as */ |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 20 | |
| 21 | /* List of pids given to option -p: */ |
| 22 | struct opt_p_t * opt_p = NULL; /* attach to process with a given pid */ |
| 23 | |
| 24 | static void usage(void) |
| 25 | { |
| 26 | fprintf(stderr, "Usage: ltrace [-dfiLS] [-a column] [-s strlen] [-o filename]\n" |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 27 | " [-u username] [-p pid] ... [command [arg ...]]\n\n"); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static char * search_for_command(char * filename) |
| 31 | { |
| 32 | static char pathname[PATH_MAX]; |
| 33 | char *path; |
| 34 | int m, n; |
| 35 | |
| 36 | if (strchr(filename, '/')) { |
| 37 | return filename; |
| 38 | } |
| 39 | for (path = getenv("PATH"); path && *path; path += m) { |
| 40 | if (strchr(path, ':')) { |
| 41 | n = strchr(path, ':') - path; |
| 42 | m = n + 1; |
| 43 | } else { |
| 44 | m = n = strlen(path); |
| 45 | } |
| 46 | strncpy(pathname, path, n); |
| 47 | if (n && pathname[n - 1] != '/') { |
| 48 | pathname[n++] = '/'; |
| 49 | } |
| 50 | strcpy(pathname + n, filename); |
| 51 | if (!access(pathname, X_OK)) { |
| 52 | return pathname; |
| 53 | } |
| 54 | } |
| 55 | return filename; |
| 56 | } |
| 57 | |
| 58 | char ** process_options(int argc, char **argv) |
| 59 | { |
| 60 | char *nextchar = NULL; |
| 61 | |
| 62 | output = stderr; |
| 63 | |
| 64 | while(1) { |
| 65 | if (!nextchar || !(*nextchar)) { |
| 66 | if (!argv[1] || argv[1][0] != '-' || !argv[1][1]) { |
| 67 | break; |
| 68 | } |
| 69 | nextchar = &argv[1][1]; |
| 70 | argc--; argv++; |
| 71 | } |
| 72 | switch (*nextchar++) { |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame^] | 73 | case 'a': if (!argv[1]) { usage(); exit(1); } |
| 74 | opt_a = atoi(argv[1]); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 75 | argc--; argv++; |
| 76 | break; |
| 77 | case 'd': opt_d++; |
| 78 | break; |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame^] | 79 | case 'o': if (!argv[1]) { usage(); exit(1); } |
| 80 | output = fopen(argv[1], "w"); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 81 | if (!output) { |
| 82 | fprintf(stderr, "Can't open %s for output: %s\n", argv[1], strerror(errno)); |
| 83 | exit(1); |
| 84 | } |
| 85 | argc--; argv++; |
| 86 | break; |
| 87 | case 'i': opt_i++; |
| 88 | break; |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame^] | 89 | case 's': if (!argv[1]) { usage(); exit(1); } |
| 90 | opt_s = atoi(argv[1]); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 91 | argc--; argv++; |
| 92 | break; |
| 93 | case 'L': opt_L = 0; |
| 94 | break; |
| 95 | case 'S': opt_S = 1; |
| 96 | break; |
| 97 | case 'f': opt_f = 1; |
| 98 | break; |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame^] | 99 | case 'u': if (!argv[1]) { usage(); exit(1); } |
| 100 | opt_u = argv[1]; |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 101 | argc--; argv++; |
| 102 | break; |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame^] | 103 | case 'p': if (!argv[1]) { usage(); exit(1); } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 104 | { |
| 105 | struct opt_p_t * tmp = malloc(sizeof(struct opt_p_t)); |
| 106 | if (!tmp) { |
| 107 | perror("malloc"); |
| 108 | exit(1); |
| 109 | } |
| 110 | tmp->pid = atoi(argv[1]); |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame^] | 111 | tmp->next = opt_p; |
| 112 | opt_p = tmp; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 113 | argc--; argv++; |
| 114 | break; |
| 115 | } |
| 116 | default: fprintf(stderr, "Unknown option '%c'\n", *(nextchar-1)); |
| 117 | usage(); |
| 118 | exit(1); |
| 119 | } |
| 120 | } |
| 121 | |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame^] | 122 | if (!opt_p && argc<2) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 123 | usage(); |
| 124 | exit(1); |
| 125 | } |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame^] | 126 | if (argc>1) { |
| 127 | command = search_for_command(argv[1]); |
| 128 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 129 | return &argv[1]; |
| 130 | } |