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