blob: 52dc20150bd86dcc7472f7f14a5af6e736e77c61 [file] [log] [blame]
Juan Cespedes3268a161997-08-25 16:45:22 +02001#include <stdio.h>
Juan Cespedes5e4455b1997-08-24 01:48:26 +02002#include <stdarg.h>
Juan Cespedes5e0acdb1998-04-04 08:34:07 +02003#include <time.h>
4#include <sys/time.h>
5#include <unistd.h>
Juan Cespedes5e4455b1997-08-24 01:48:26 +02006
7#include "ltrace.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +01008#include "options.h"
9#include "output.h"
Juan Cespedes5e4455b1997-08-24 01:48:26 +020010
Juan Cespedes5e01f651998-03-08 22:31:44 +010011static pid_t current_pid = 0;
12static int current_column = 0;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020013
Juan Cespedes5e01f651998-03-08 22:31:44 +010014static void begin_of_line(enum tof type, struct process * proc)
Juan Cespedes5e4455b1997-08-24 01:48:26 +020015{
Juan Cespedes5e01f651998-03-08 22:31:44 +010016 current_column = 0;
17 if (!proc) {
18 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020019 }
Juan Cespedes273ea6d1998-03-14 23:02:40 +010020 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 Cespedes5e01f651998-03-08 22:31:44 +010024 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020025 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 Cespedes5e01f651998-03-08 22:31:44 +010043 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 Cespedes5e4455b1997-08-24 01:48:26 +020052}
53
Juan Cespedes5e01f651998-03-08 22:31:44 +010054static 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
68void output_line(struct process * proc, char *fmt, ...)
Juan Cespedes5e4455b1997-08-24 01:48:26 +020069{
70 va_list args;
71
Juan Cespedes5e01f651998-03-08 22:31:44 +010072 if (current_pid) {
73 fprintf(output, " <unfinished ...>\n");
74 }
Juan Cespedes28f60191998-04-12 00:04:39 +020075 current_pid=0;
76 if (!fmt) {
77 return;
78 }
Juan Cespedes5e01f651998-03-08 22:31:44 +010079 begin_of_line(LT_TOF_NONE, proc);
80
81 va_start(args, fmt);
82 vfprintf(output, fmt, args);
83 fprintf(output, "\n");
84 va_end(args);
Juan Cespedes5e01f651998-03-08 22:31:44 +010085 current_column=0;
86}
87
88static void tabto(int col)
89{
90 if (current_column < col) {
91 fprintf(output, "%*s", col-current_column, "");
92 }
93}
94
95void output_left(enum tof type, struct process * proc, char * function_name)
96{
97 struct function * func;
98
99 if (current_pid) {
Juan Cespedes81690ef1998-03-13 19:31:29 +0100100#if 0 /* FIXME: should I do this? */
Juan Cespedes5e01f651998-03-08 22:31:44 +0100101 if (current_pid == proc->pid
102 && proc->type_being_displayed == LT_TOF_FUNCTION
103 && proc->type_being_displayed == type) {
104 tabto(opt_a);
105 fprintf(output, "= ???\n");
106 } else
107#endif
108 fprintf(output, " <unfinished ...>\n");
109 current_pid=0;
110 current_column=0;
111 }
112 current_pid=proc->pid;
113 proc->type_being_displayed = type;
114 begin_of_line(type, proc);
115 current_column += fprintf(output, "%s(", function_name);
116
117 func = name2func(function_name);
118 if (!func) {
119 int i;
120 for(i=0; i<4; i++) {
121 current_column += display_arg(type, proc, i, LT_PT_UNKNOWN);
122 current_column += fprintf(output, ", ");
123 }
124 current_column += display_arg(type, proc, 4, LT_PT_UNKNOWN);
125 return;
126 } else {
127 int i;
128 for(i=0; i< func->num_params - func->params_right - 1; i++) {
129 current_column += display_arg(type, proc, i, func->param_types[i]);
130 current_column += fprintf(output, ", ");
131 }
132 if (func->num_params>func->params_right) {
133 current_column += display_arg(type, proc, i, func->param_types[i]);
134 if (func->params_right) {
135 current_column += fprintf(output, ", ");
136 }
137 }
138 if (!func->params_right && func->return_type == LT_PT_VOID) {
139 current_column += fprintf(output, ") ");
140 tabto(opt_a);
141 fprintf(output, "= <void>\n");
142 current_pid = 0;
143 current_column = 0;
144 }
145 }
146}
147
148void output_right(enum tof type, struct process * proc, char * function_name)
149{
150 struct function * func = name2func(function_name);
151
152 if (func && func->params_right==0 && func->return_type == LT_PT_VOID) {
153 return;
154 }
155
156 if (current_pid && current_pid!=proc->pid) {
157 fprintf(output, " <unfinished ...>\n");
158 begin_of_line(type, proc);
159 current_column += fprintf(output, "<... %s resumed> ", function_name);
160 } else if (!current_pid) {
161 begin_of_line(type, proc);
162 current_column += fprintf(output, "<... %s resumed> ", function_name);
163 }
164
165 if (!func) {
166 current_column += fprintf(output, ") ");
167 tabto(opt_a);
168 fprintf(output, "= ");
169 display_arg(type, proc, -1, LT_PT_UNKNOWN);
Juan Cespedes3268a161997-08-25 16:45:22 +0200170 fprintf(output, "\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100171 } else {
172 int i;
173 for(i=func->num_params-func->params_right; i<func->num_params-1; i++) {
174 current_column += display_arg(type, proc, i, func->param_types[i]);
175 current_column += fprintf(output, ", ");
176 }
177 if (func->params_right) {
178 current_column += display_arg(type, proc, i, func->param_types[i]);
179 }
180 current_column += fprintf(output, ") ");
181 tabto(opt_a);
182 fprintf(output, "= ");
183 if (func->return_type == LT_PT_VOID) {
184 fprintf(output, "<void>");
185 } else {
186 display_arg(type, proc, -1, func->return_type);
187 }
188 fprintf(output, "\n");
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200189 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100190 current_pid=0;
191 current_column=0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100192}