output.c revision 81690ef1a83d7a093daaebc1a87cdb9ec6f74589
1#include <stdio.h> 2#include <stdarg.h> 3 4#include "ltrace.h" 5#include "options.h" 6#include "output.h" 7 8static pid_t current_pid = 0; 9static int current_column = 0; 10 11static void begin_of_line(enum tof type, struct process * proc) 12{ 13 current_column = 0; 14 if (!proc) { 15 return; 16 } 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 } 29} 30 31static 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 45void output_line(struct process * proc, char *fmt, ...) 46{ 47 va_list args; 48 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 62static void tabto(int col) 63{ 64 if (current_column < col) { 65 fprintf(output, "%*s", col-current_column, ""); 66 } 67} 68 69void output_left(enum tof type, struct process * proc, char * function_name) 70{ 71 struct function * func; 72 73 if (current_pid) { 74#if 0 /* FIXME: should I do this? */ 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 122void 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); 144 fprintf(output, "\n"); 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"); 163 } 164 current_pid=0; 165 current_column=0; 166} 167