blob: 1ced9221e29848020177929be055b7f907b1f5b9 [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>
3
4#include "ltrace.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +01005#include "options.h"
6#include "output.h"
Juan Cespedes5e4455b1997-08-24 01:48:26 +02007
Juan Cespedes5e01f651998-03-08 22:31:44 +01008static pid_t current_pid = 0;
9static int current_column = 0;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020010
Juan Cespedes5e01f651998-03-08 22:31:44 +010011static void begin_of_line(enum tof type, struct process * proc)
Juan Cespedes5e4455b1997-08-24 01:48:26 +020012{
Juan Cespedes5e01f651998-03-08 22:31:44 +010013 current_column = 0;
14 if (!proc) {
15 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020016 }
Juan Cespedes5e01f651998-03-08 22:31:44 +010017 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 Cespedes5e4455b1997-08-24 01:48:26 +020029}
30
Juan Cespedes5e01f651998-03-08 22:31:44 +010031static 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, ...)
Juan Cespedes5e4455b1997-08-24 01:48:26 +020046{
47 va_list args;
48
Juan Cespedes5e01f651998-03-08 22:31:44 +010049 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) {
Juan Cespedes81690ef1998-03-13 19:31:29 +010074#if 0 /* FIXME: should I do this? */
Juan Cespedes5e01f651998-03-08 22:31:44 +010075 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);
Juan Cespedes3268a161997-08-25 16:45:22 +0200144 fprintf(output, "\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100145 } 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 Cespedes5e4455b1997-08-24 01:48:26 +0200163 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100164 current_pid=0;
165 current_column=0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100166}