blob: 9beab40ac028440ba3918e4bd52ced32d52bcdeb [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 }
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
85static void tabto(int col)
86{
87 if (current_column < col) {
88 fprintf(output, "%*s", col-current_column, "");
89 }
90}
91
92void output_left(enum tof type, struct process * proc, char * function_name)
93{
94 struct function * func;
95
96 if (current_pid) {
Juan Cespedes81690ef1998-03-13 19:31:29 +010097#if 0 /* FIXME: should I do this? */
Juan Cespedes5e01f651998-03-08 22:31:44 +010098 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
145void 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 Cespedes3268a161997-08-25 16:45:22 +0200167 fprintf(output, "\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100168 } 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 Cespedes5e4455b1997-08-24 01:48:26 +0200186 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100187 current_pid=0;
188 current_column=0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100189}