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