| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 1 | #if HAVE_CONFIG_H |
| 2 | #include "config.h" |
| 3 | #endif |
| 4 | |
| Juan Cespedes | 3268a16 | 1997-08-25 16:45:22 +0200 | [diff] [blame] | 5 | #include <stdio.h> |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 6 | #include <stdarg.h> |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 7 | #include <time.h> |
| 8 | #include <sys/time.h> |
| 9 | #include <unistd.h> |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 10 | |
| 11 | #include "ltrace.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 12 | #include "options.h" |
| 13 | #include "output.h" |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 14 | |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 15 | #if HAVE_LIBIBERTY |
| 16 | #include "demangle.h" |
| 17 | #endif |
| 18 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 19 | static pid_t current_pid = 0; |
| 20 | static int current_column = 0; |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 21 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 22 | static void begin_of_line(enum tof type, struct process * proc) |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 23 | { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 24 | current_column = 0; |
| 25 | if (!proc) { |
| 26 | return; |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 27 | } |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 28 | if ((output!=stderr) && (opt_p || opt_f)) { |
| 29 | current_column += fprintf(output, "%u ", proc->pid); |
| 30 | } else if (list_of_processes->next) { |
| 31 | current_column += fprintf(output, "[pid %u] ", proc->pid); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 32 | } |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 33 | if (opt_t) { |
| 34 | struct timeval tv; |
| 35 | struct timezone tz; |
| 36 | struct tm * tmp; |
| 37 | |
| 38 | gettimeofday(&tv, &tz); |
| 39 | tmp = localtime(&tv.tv_sec); |
| 40 | if (opt_t>2) { |
| 41 | current_column += fprintf(output, "%lu.%06d ", |
| 42 | tv.tv_sec, (int)tv.tv_usec); |
| 43 | } else if (opt_t>1) { |
| 44 | current_column += fprintf(output, "%02d:%02d:%02d.%06d ", |
| 45 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec, (int)tv.tv_usec); |
| 46 | } else { |
| 47 | current_column += fprintf(output, "%02d:%02d:%02d ", |
| 48 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 49 | } |
| 50 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 51 | if (opt_i) { |
| 52 | if (type==LT_TOF_FUNCTION) { |
| 53 | current_column += fprintf(output, "[%08x] ", |
| 54 | (unsigned)proc->return_addr); |
| 55 | } else { |
| 56 | current_column += fprintf(output, "[%08x] ", |
| 57 | (unsigned)proc->instruction_pointer); |
| 58 | } |
| 59 | } |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 62 | static struct function * name2func(char * name) |
| 63 | { |
| 64 | struct function * tmp; |
| 65 | |
| 66 | tmp = list_of_functions; |
| 67 | while(tmp) { |
| 68 | if (!strcmp(tmp->name, name)) { |
| 69 | return tmp; |
| 70 | } |
| 71 | tmp = tmp->next; |
| 72 | } |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | void output_line(struct process * proc, char *fmt, ...) |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 77 | { |
| 78 | va_list args; |
| 79 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 80 | if (current_pid) { |
| 81 | fprintf(output, " <unfinished ...>\n"); |
| 82 | } |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 83 | current_pid=0; |
| 84 | if (!fmt) { |
| 85 | return; |
| 86 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 87 | begin_of_line(LT_TOF_NONE, proc); |
| 88 | |
| 89 | va_start(args, fmt); |
| 90 | vfprintf(output, fmt, args); |
| 91 | fprintf(output, "\n"); |
| 92 | va_end(args); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 93 | current_column=0; |
| 94 | } |
| 95 | |
| 96 | static void tabto(int col) |
| 97 | { |
| 98 | if (current_column < col) { |
| 99 | fprintf(output, "%*s", col-current_column, ""); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void output_left(enum tof type, struct process * proc, char * function_name) |
| 104 | { |
| 105 | struct function * func; |
| 106 | |
| 107 | if (current_pid) { |
| Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 108 | #if 0 /* FIXME: should I do this? */ |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 109 | if (current_pid == proc->pid |
| 110 | && proc->type_being_displayed == LT_TOF_FUNCTION |
| 111 | && proc->type_being_displayed == type) { |
| 112 | tabto(opt_a); |
| 113 | fprintf(output, "= ???\n"); |
| 114 | } else |
| 115 | #endif |
| 116 | fprintf(output, " <unfinished ...>\n"); |
| 117 | current_pid=0; |
| 118 | current_column=0; |
| 119 | } |
| 120 | current_pid=proc->pid; |
| 121 | proc->type_being_displayed = type; |
| 122 | begin_of_line(type, proc); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 123 | #if HAVE_LIBIBERTY |
| 124 | current_column += fprintf(output, "%s(", my_demangle(function_name)); |
| 125 | #else |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 126 | current_column += fprintf(output, "%s(", function_name); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 127 | #endif |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 128 | |
| 129 | func = name2func(function_name); |
| 130 | if (!func) { |
| 131 | int i; |
| 132 | for(i=0; i<4; i++) { |
| 133 | current_column += display_arg(type, proc, i, LT_PT_UNKNOWN); |
| 134 | current_column += fprintf(output, ", "); |
| 135 | } |
| 136 | current_column += display_arg(type, proc, 4, LT_PT_UNKNOWN); |
| 137 | return; |
| 138 | } else { |
| 139 | int i; |
| 140 | for(i=0; i< func->num_params - func->params_right - 1; i++) { |
| 141 | current_column += display_arg(type, proc, i, func->param_types[i]); |
| 142 | current_column += fprintf(output, ", "); |
| 143 | } |
| 144 | if (func->num_params>func->params_right) { |
| 145 | current_column += display_arg(type, proc, i, func->param_types[i]); |
| 146 | if (func->params_right) { |
| 147 | current_column += fprintf(output, ", "); |
| 148 | } |
| 149 | } |
| 150 | if (!func->params_right && func->return_type == LT_PT_VOID) { |
| 151 | current_column += fprintf(output, ") "); |
| 152 | tabto(opt_a); |
| 153 | fprintf(output, "= <void>\n"); |
| 154 | current_pid = 0; |
| 155 | current_column = 0; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void output_right(enum tof type, struct process * proc, char * function_name) |
| 161 | { |
| 162 | struct function * func = name2func(function_name); |
| 163 | |
| 164 | if (func && func->params_right==0 && func->return_type == LT_PT_VOID) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | if (current_pid && current_pid!=proc->pid) { |
| 169 | fprintf(output, " <unfinished ...>\n"); |
| 170 | begin_of_line(type, proc); |
| 171 | current_column += fprintf(output, "<... %s resumed> ", function_name); |
| 172 | } else if (!current_pid) { |
| 173 | begin_of_line(type, proc); |
| 174 | current_column += fprintf(output, "<... %s resumed> ", function_name); |
| 175 | } |
| 176 | |
| 177 | if (!func) { |
| 178 | current_column += fprintf(output, ") "); |
| 179 | tabto(opt_a); |
| 180 | fprintf(output, "= "); |
| 181 | display_arg(type, proc, -1, LT_PT_UNKNOWN); |
| Juan Cespedes | 3268a16 | 1997-08-25 16:45:22 +0200 | [diff] [blame] | 182 | fprintf(output, "\n"); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 183 | } else { |
| 184 | int i; |
| 185 | for(i=func->num_params-func->params_right; i<func->num_params-1; i++) { |
| 186 | current_column += display_arg(type, proc, i, func->param_types[i]); |
| 187 | current_column += fprintf(output, ", "); |
| 188 | } |
| 189 | if (func->params_right) { |
| 190 | current_column += display_arg(type, proc, i, func->param_types[i]); |
| 191 | } |
| 192 | current_column += fprintf(output, ") "); |
| 193 | tabto(opt_a); |
| 194 | fprintf(output, "= "); |
| 195 | if (func->return_type == LT_PT_VOID) { |
| 196 | fprintf(output, "<void>"); |
| 197 | } else { |
| 198 | display_arg(type, proc, -1, func->return_type); |
| 199 | } |
| 200 | fprintf(output, "\n"); |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 201 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 202 | current_pid=0; |
| 203 | current_column=0; |
| Juan Cespedes | c40e64a | 1997-10-26 20:34:00 +0100 | [diff] [blame] | 204 | } |