| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of ltrace. |
| 3 | * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc. |
| 4 | * Copyright (C) 2010 Joe Damato |
| 5 | * Copyright (C) 1997,1998,1999,2001,2002,2003,2004,2007,2008,2009 Juan Cespedes |
| 6 | * Copyright (C) 2006 Paul Gilliam |
| 7 | * Copyright (C) 2006 Ian Wienand |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of the |
| 12 | * License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 22 | * 02110-1301 USA |
| 23 | */ |
| 24 | |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 25 | #include "config.h" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 26 | |
| Juan Cespedes | 3268a16 | 1997-08-25 16:45:22 +0200 | [diff] [blame] | 27 | #include <stdio.h> |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 28 | #include <stdlib.h> |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 29 | #include <stdarg.h> |
| Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 30 | #include <string.h> |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 31 | #include <time.h> |
| 32 | #include <sys/time.h> |
| 33 | #include <unistd.h> |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 34 | #include <errno.h> |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame^] | 35 | #include <assert.h> |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 36 | |
| Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 37 | #include "common.h" |
| Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 38 | #include "proc.h" |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 39 | #include "library.h" |
| Petr Machata | 000e311 | 2012-01-03 17:03:39 +0100 | [diff] [blame] | 40 | #include "type.h" |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 41 | #include "value.h" |
| 42 | #include "value_dict.h" |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame^] | 43 | #include "param.h" |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 44 | #include "fetch.h" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 45 | |
| Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 46 | /* TODO FIXME XXX: include in common.h: */ |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 47 | extern struct timeval current_time_spent; |
| 48 | |
| Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 49 | Dict *dict_opt_c = NULL; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 50 | |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 51 | static Process *current_proc = 0; |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 52 | static int current_depth = 0; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 53 | static int current_column = 0; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 54 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 55 | static void |
| Petr Machata | 5400475 | 2012-05-03 18:36:48 +0200 | [diff] [blame] | 56 | output_indent(struct Process *proc) |
| 57 | { |
| 58 | int d = options.indent * (proc->callstack_depth - 1); |
| 59 | current_column += fprintf(options.output, "%*s", d, ""); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 60 | } |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 61 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 62 | static void |
| Petr Machata | 37b73c0 | 2012-01-06 16:00:25 +0100 | [diff] [blame] | 63 | begin_of_line(Process *proc, int is_func, int indent) |
| 64 | { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 65 | current_column = 0; |
| 66 | if (!proc) { |
| 67 | return; |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 68 | } |
| Juan Cespedes | c693f02 | 2009-05-21 18:59:41 +0200 | [diff] [blame] | 69 | if ((options.output != stderr) && (opt_p || options.follow)) { |
| 70 | current_column += fprintf(options.output, "%u ", proc->pid); |
| Juan Cespedes | e12df4c | 2009-05-28 19:06:35 +0200 | [diff] [blame] | 71 | } else if (options.follow) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 72 | current_column += fprintf(options.output, "[pid %u] ", proc->pid); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 73 | } |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 74 | if (opt_r) { |
| 75 | struct timeval tv; |
| 76 | struct timezone tz; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 77 | static struct timeval old_tv = { 0, 0 }; |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 78 | struct timeval diff; |
| 79 | |
| 80 | gettimeofday(&tv, &tz); |
| 81 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 82 | if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) { |
| 83 | old_tv.tv_sec = tv.tv_sec; |
| 84 | old_tv.tv_usec = tv.tv_usec; |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 85 | } |
| 86 | diff.tv_sec = tv.tv_sec - old_tv.tv_sec; |
| 87 | if (tv.tv_usec >= old_tv.tv_usec) { |
| 88 | diff.tv_usec = tv.tv_usec - old_tv.tv_usec; |
| 89 | } else { |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 90 | diff.tv_sec--; |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 91 | diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec; |
| 92 | } |
| 93 | old_tv.tv_sec = tv.tv_sec; |
| 94 | old_tv.tv_usec = tv.tv_usec; |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 95 | current_column += fprintf(options.output, "%3lu.%06d ", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 96 | diff.tv_sec, (int)diff.tv_usec); |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 97 | } |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 98 | if (opt_t) { |
| 99 | struct timeval tv; |
| 100 | struct timezone tz; |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 101 | |
| 102 | gettimeofday(&tv, &tz); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 103 | if (opt_t > 2) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 104 | current_column += fprintf(options.output, "%lu.%06d ", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 105 | tv.tv_sec, (int)tv.tv_usec); |
| 106 | } else if (opt_t > 1) { |
| 107 | struct tm *tmp = localtime(&tv.tv_sec); |
| 108 | current_column += |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 109 | fprintf(options.output, "%02d:%02d:%02d.%06d ", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 110 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec, |
| 111 | (int)tv.tv_usec); |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 112 | } else { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 113 | struct tm *tmp = localtime(&tv.tv_sec); |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 114 | current_column += fprintf(options.output, "%02d:%02d:%02d ", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 115 | tmp->tm_hour, tmp->tm_min, |
| 116 | tmp->tm_sec); |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 117 | } |
| 118 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 119 | if (opt_i) { |
| Petr Machata | 37b73c0 | 2012-01-06 16:00:25 +0100 | [diff] [blame] | 120 | if (is_func) |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 121 | current_column += fprintf(options.output, "[%p] ", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 122 | proc->return_addr); |
| Petr Machata | 37b73c0 | 2012-01-06 16:00:25 +0100 | [diff] [blame] | 123 | else |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 124 | current_column += fprintf(options.output, "[%p] ", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 125 | proc->instruction_pointer); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 126 | } |
| Petr Machata | 37b73c0 | 2012-01-06 16:00:25 +0100 | [diff] [blame] | 127 | if (options.indent > 0 && indent) { |
| Juan Cespedes | 3f0b62e | 2001-07-09 01:02:52 +0200 | [diff] [blame] | 128 | output_indent(proc); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 129 | } |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 132 | /* The default prototype is: long X(long, long, long, long). */ |
| 133 | static Function * |
| 134 | build_default_prototype(void) |
| 135 | { |
| 136 | Function *ret = malloc(sizeof(*ret)); |
| 137 | size_t i = 0; |
| 138 | if (ret == NULL) |
| 139 | goto err; |
| 140 | memset(ret, 0, sizeof(*ret)); |
| 141 | |
| 142 | struct arg_type_info *unknown_type = type_get_simple(ARGTYPE_UNKNOWN); |
| 143 | |
| 144 | ret->return_info = unknown_type; |
| 145 | |
| 146 | ret->num_params = 4; |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame^] | 147 | ret->params = malloc(sizeof(*ret->params) * ret->num_params); |
| 148 | if (ret->params == NULL) |
| 149 | goto err; |
| 150 | |
| 151 | for (i = 0; i < ret->num_params; ++i) |
| 152 | param_init_type(&ret->params[i], unknown_type, 0); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 153 | |
| 154 | return ret; |
| 155 | |
| 156 | err: |
| 157 | report_global_error("malloc: %s", strerror(errno)); |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame^] | 158 | if (ret->params != NULL) { |
| 159 | while (i-- > 0) |
| 160 | param_destroy(&ret->params[i]); |
| 161 | free(ret->params); |
| 162 | } |
| 163 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 164 | free(ret); |
| 165 | |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| Juan Cespedes | cde5826 | 2009-05-07 11:09:00 +0200 | [diff] [blame] | 169 | static Function * |
| Petr Machata | 9a7f232 | 2011-07-08 19:00:37 +0200 | [diff] [blame] | 170 | name2func(char const *name) { |
| Juan Cespedes | cde5826 | 2009-05-07 11:09:00 +0200 | [diff] [blame] | 171 | Function *tmp; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 172 | const char *str1, *str2; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 173 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 174 | for (tmp = list_of_functions; tmp != NULL; tmp = tmp->next) { |
| Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 175 | str1 = tmp->name; |
| 176 | str2 = name; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 177 | if (!strcmp(str1, str2)) |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 178 | return tmp; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 179 | } |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 180 | |
| 181 | static Function *def = NULL; |
| 182 | if (def == NULL) |
| 183 | def = build_default_prototype(); |
| 184 | |
| 185 | return def; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 188 | void |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 189 | output_line(Process *proc, char *fmt, ...) { |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 190 | va_list args; |
| 191 | |
| Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 192 | if (options.summary) { |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 193 | return; |
| 194 | } |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 195 | if (current_proc) { |
| 196 | if (current_proc->callstack[current_depth].return_addr) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 197 | fprintf(options.output, " <unfinished ...>\n"); |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 198 | } else { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 199 | fprintf(options.output, " <no return ...>\n"); |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 200 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 201 | } |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 202 | current_proc = 0; |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 203 | if (!fmt) { |
| 204 | return; |
| 205 | } |
| Petr Machata | 37b73c0 | 2012-01-06 16:00:25 +0100 | [diff] [blame] | 206 | begin_of_line(proc, 0, 0); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 207 | |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 208 | va_start(args, fmt); |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 209 | vfprintf(options.output, fmt, args); |
| 210 | fprintf(options.output, "\n"); |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 211 | va_end(args); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 212 | current_column = 0; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 215 | static void |
| 216 | tabto(int col) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 217 | if (current_column < col) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 218 | fprintf(options.output, "%*s", col - current_column, ""); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 222 | static int |
| 223 | account_output(int o) |
| 224 | { |
| 225 | if (o < 0) |
| 226 | return -1; |
| 227 | current_column += o; |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int |
| 232 | output_error(void) |
| 233 | { |
| 234 | return account_output(fprintf(options.output, "?")); |
| 235 | } |
| 236 | |
| 237 | static int |
| 238 | fetch_simple_param(enum tof type, Process *proc, struct fetch_context *context, |
| 239 | struct value_dict *arguments, struct arg_type_info *info, |
| 240 | struct value *valuep) |
| 241 | { |
| 242 | /* Arrays decay into pointers per C standard. We check for |
| 243 | * this here, because here we also capture arrays that come |
| 244 | * from parameter packs. */ |
| 245 | int own = 0; |
| 246 | if (info->type == ARGTYPE_ARRAY) { |
| 247 | struct arg_type_info *tmp = malloc(sizeof(*tmp)); |
| 248 | if (tmp != NULL) { |
| 249 | type_init_pointer(tmp, info, 0); |
| 250 | info = tmp; |
| 251 | own = 1; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | struct value value; |
| 256 | value_init(&value, proc, NULL, info, own); |
| 257 | if (fetch_arg_next(context, type, proc, info, &value) < 0) |
| 258 | return -1; |
| 259 | |
| 260 | if (val_dict_push_next(arguments, &value) < 0) { |
| 261 | value_destroy(&value); |
| 262 | return -1; |
| 263 | } |
| 264 | |
| 265 | if (valuep != NULL) |
| 266 | *valuep = value; |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static void |
| 272 | fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp) |
| 273 | { |
| 274 | if (*params_leftp == -1) |
| 275 | *params_leftp = val_dict_count(arguments); |
| 276 | } |
| 277 | |
| 278 | static int |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame^] | 279 | fetch_param_pack(enum tof type, Process *proc, struct fetch_context *context, |
| 280 | struct value_dict *arguments, struct param *param, |
| 281 | ssize_t *params_leftp) |
| 282 | { |
| 283 | struct param_enum *e = param_pack_init(param, arguments); |
| 284 | if (e == NULL) |
| 285 | return -1; |
| 286 | |
| 287 | int ret = 0; |
| 288 | while (1) { |
| 289 | int insert_stop = 0; |
| 290 | struct arg_type_info *info = malloc(sizeof(*info)); |
| 291 | if (info == NULL |
| 292 | || param_pack_next(param, e, info, &insert_stop) < 0) { |
| 293 | fail: |
| 294 | free(info); |
| 295 | ret = -1; |
| 296 | break; |
| 297 | } |
| 298 | |
| 299 | if (insert_stop) |
| 300 | fetch_param_stop(arguments, params_leftp); |
| 301 | |
| 302 | if (info->type == ARGTYPE_VOID) |
| 303 | break; |
| 304 | |
| 305 | struct value val; |
| 306 | if (fetch_simple_param(type, proc, context, arguments, |
| 307 | info, &val) < 0) |
| 308 | goto fail; |
| 309 | |
| 310 | int stop = 0; |
| 311 | switch (param_pack_stop(param, e, &val)) { |
| 312 | case PPCB_ERR: |
| 313 | goto fail; |
| 314 | case PPCB_STOP: |
| 315 | stop = 1; |
| 316 | case PPCB_CONT: |
| 317 | break; |
| 318 | } |
| 319 | |
| 320 | if (stop) |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | param_pack_done(param, e); |
| 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | static int |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 329 | fetch_one_param(enum tof type, Process *proc, struct fetch_context *context, |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame^] | 330 | struct value_dict *arguments, struct param *param, |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 331 | ssize_t *params_leftp) |
| 332 | { |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame^] | 333 | switch (param->flavor) { |
| 334 | case PARAM_FLAVOR_TYPE: |
| 335 | return fetch_simple_param(type, proc, context, arguments, |
| 336 | param->u.type.type, NULL); |
| 337 | |
| 338 | case PARAM_FLAVOR_PACK: |
| 339 | return fetch_param_pack(type, proc, context, arguments, |
| 340 | param, params_leftp); |
| 341 | |
| 342 | case PARAM_FLAVOR_STOP: |
| 343 | fetch_param_stop(arguments, params_leftp); |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | assert(!"Invalid param flavor!"); |
| 348 | abort(); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static int |
| 352 | fetch_params(enum tof type, Process *proc, struct fetch_context *context, |
| 353 | struct value_dict *arguments, Function *func, ssize_t *params_leftp) |
| 354 | { |
| 355 | size_t i; |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame^] | 356 | for (i = 0; i < func->num_params; ++i) |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 357 | if (fetch_one_param(type, proc, context, arguments, |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame^] | 358 | &func->params[i], params_leftp) < 0) |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 359 | return -1; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 360 | |
| 361 | /* Implicit stop at the end of parameter list. */ |
| 362 | fetch_param_stop(arguments, params_leftp); |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame^] | 363 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | static int |
| 368 | output_one(struct value *val, struct value_dict *arguments) |
| 369 | { |
| 370 | int o = format_argument(options.output, val, arguments); |
| 371 | if (account_output(o) < 0) { |
| 372 | if (output_error() < 0) |
| 373 | return -1; |
| 374 | o = 1; |
| 375 | } |
| 376 | return o; |
| 377 | } |
| 378 | |
| 379 | static int |
| 380 | output_params(struct value_dict *arguments, size_t start, size_t end, |
| 381 | int *need_delimp) |
| 382 | { |
| 383 | size_t i; |
| 384 | int need_delim = *need_delimp; |
| 385 | for (i = start; i < end; ++i) { |
| 386 | if (need_delim |
| 387 | && account_output(fprintf(options.output, ", ")) < 0) |
| 388 | return -1; |
| 389 | struct value *value = val_dict_get_num(arguments, i); |
| 390 | if (value == NULL) |
| 391 | return -1; |
| 392 | need_delim = output_one(value, arguments); |
| 393 | if (need_delim < 0) |
| 394 | return -1; |
| 395 | } |
| 396 | *need_delimp = need_delim; |
| 397 | return 0; |
| 398 | } |
| 399 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 400 | void |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 401 | output_left(enum tof type, struct Process *proc, |
| 402 | struct library_symbol *libsym) |
| 403 | { |
| 404 | const char *function_name = libsym->name; |
| Juan Cespedes | cde5826 | 2009-05-07 11:09:00 +0200 | [diff] [blame] | 405 | Function *func; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 406 | |
| Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 407 | if (options.summary) { |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 408 | return; |
| 409 | } |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 410 | if (current_proc) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 411 | fprintf(options.output, " <unfinished ...>\n"); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 412 | current_column = 0; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 413 | } |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 414 | current_proc = proc; |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 415 | current_depth = proc->callstack_depth; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 416 | begin_of_line(proc, type == LT_TOF_FUNCTION, 1); |
| Petr Machata | 53bc49b | 2012-04-25 17:23:02 +0200 | [diff] [blame] | 417 | if (!options.hide_caller && libsym->lib != NULL |
| 418 | && libsym->plt_type != LS_TOPLT_NONE) |
| Petr Machata | b89c256 | 2012-04-03 17:00:28 +0200 | [diff] [blame] | 419 | current_column += fprintf(options.output, "%s->", |
| 420 | libsym->lib->soname); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 421 | |
| 422 | const char *name = function_name; |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 423 | #ifdef USE_DEMANGLE |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 424 | if (options.demangle) |
| 425 | name = my_demangle(function_name); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 426 | #endif |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 427 | if (account_output(fprintf(options.output, "%s(", name)) < 0) |
| 428 | return; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 429 | |
| 430 | func = name2func(function_name); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 431 | if (func == NULL) |
| 432 | return; |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 433 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 434 | struct fetch_context *context = fetch_arg_init(type, proc, |
| 435 | func->return_info); |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 436 | struct value_dict *arguments = malloc(sizeof(*arguments)); |
| 437 | if (arguments == NULL) |
| 438 | return; |
| 439 | val_dict_init(arguments); |
| 440 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 441 | ssize_t params_left = -1; |
| 442 | int need_delim = 0; |
| 443 | if (fetch_params(type, proc, context, arguments, func, ¶ms_left) < 0 |
| 444 | || output_params(arguments, 0, params_left, &need_delim) < 0) { |
| 445 | val_dict_destroy(arguments); |
| 446 | fetch_arg_done(context); |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame^] | 447 | arguments = NULL; |
| 448 | context = NULL; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 449 | } |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 450 | |
| 451 | struct callstack_element *stel |
| 452 | = &proc->callstack[proc->callstack_depth - 1]; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 453 | stel->fetch_context = context; |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 454 | stel->arguments = arguments; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 455 | stel->out.params_left = params_left; |
| 456 | stel->out.need_delim = need_delim; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 457 | } |
| 458 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 459 | void |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 460 | output_right(enum tof type, struct Process *proc, struct library_symbol *libsym) |
| Petr Machata | 14184b3 | 2012-02-10 13:10:26 +0100 | [diff] [blame] | 461 | { |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 462 | const char *function_name = libsym->name; |
| Juan Cespedes | cde5826 | 2009-05-07 11:09:00 +0200 | [diff] [blame] | 463 | Function *func = name2func(function_name); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 464 | if (func == NULL) |
| 465 | return; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 466 | |
| Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 467 | if (options.summary) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 468 | struct opt_c_struct *st; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 469 | if (!dict_opt_c) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 470 | dict_opt_c = |
| 471 | dict_init(dict_key2hash_string, |
| 472 | dict_key_cmp_string); |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 473 | } |
| 474 | st = dict_find_entry(dict_opt_c, function_name); |
| 475 | if (!st) { |
| 476 | char *na; |
| 477 | st = malloc(sizeof(struct opt_c_struct)); |
| 478 | na = strdup(function_name); |
| 479 | if (!st || !na) { |
| 480 | perror("malloc()"); |
| 481 | exit(1); |
| 482 | } |
| 483 | st->count = 0; |
| 484 | st->tv.tv_sec = st->tv.tv_usec = 0; |
| 485 | dict_enter(dict_opt_c, na, st); |
| 486 | } |
| 487 | if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) { |
| 488 | st->tv.tv_usec += current_time_spent.tv_usec - 1000000; |
| 489 | st->tv.tv_sec++; |
| 490 | } else { |
| 491 | st->tv.tv_usec += current_time_spent.tv_usec; |
| 492 | } |
| 493 | st->count++; |
| 494 | st->tv.tv_sec += current_time_spent.tv_sec; |
| 495 | |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 496 | // fprintf(options.output, "%s <%lu.%06d>\n", function_name, |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 497 | // current_time_spent.tv_sec, (int)current_time_spent.tv_usec); |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 498 | return; |
| 499 | } |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 500 | if (current_proc && (current_proc != proc || |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 501 | current_depth != proc->callstack_depth)) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 502 | fprintf(options.output, " <unfinished ...>\n"); |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 503 | current_proc = 0; |
| Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 504 | } |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 505 | if (current_proc != proc) { |
| Petr Machata | 37b73c0 | 2012-01-06 16:00:25 +0100 | [diff] [blame] | 506 | begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 507 | #ifdef USE_DEMANGLE |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 508 | current_column += |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 509 | fprintf(options.output, "<... %s resumed> ", |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 510 | options.demangle ? my_demangle(function_name) : function_name); |
| Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 511 | #else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 512 | current_column += |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 513 | fprintf(options.output, "<... %s resumed> ", function_name); |
| Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 514 | #endif |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 515 | } |
| 516 | |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 517 | struct callstack_element *stel |
| 518 | = &proc->callstack[proc->callstack_depth - 1]; |
| 519 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 520 | struct fetch_context *context = stel->fetch_context; |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 521 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 522 | /* Fetch & enter into dictionary the retval first, so that |
| 523 | * other values can use it in expressions. */ |
| 524 | struct value retval; |
| 525 | int own_retval = 0; |
| 526 | if (context != NULL) { |
| 527 | value_init(&retval, proc, NULL, func->return_info, 0); |
| 528 | own_retval = 1; |
| 529 | if (fetch_retval(context, type, proc, func->return_info, |
| 530 | &retval) == 0) { |
| 531 | if (stel->arguments != NULL |
| 532 | && val_dict_push_named(stel->arguments, &retval, |
| 533 | "retval", 0) == 0) |
| 534 | own_retval = 0; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 535 | } |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 536 | } |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 537 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 538 | if (stel->arguments != NULL) |
| 539 | output_params(stel->arguments, stel->out.params_left, |
| 540 | val_dict_count(stel->arguments), |
| 541 | &stel->out.need_delim); |
| 542 | |
| 543 | current_column += fprintf(options.output, ") "); |
| 544 | tabto(options.align - 1); |
| 545 | fprintf(options.output, "= "); |
| 546 | |
| 547 | output_one(&retval, stel->arguments); |
| 548 | |
| 549 | if (own_retval) |
| 550 | value_destroy(&retval); |
| 551 | |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame^] | 552 | if (stel->arguments != NULL) { |
| 553 | val_dict_destroy(stel->arguments); |
| 554 | free(stel->arguments); |
| 555 | } |
| 556 | if (context != NULL) |
| 557 | fetch_arg_done(context); |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 558 | |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 559 | if (opt_T) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 560 | fprintf(options.output, " <%lu.%06d>", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 561 | current_time_spent.tv_sec, |
| 562 | (int)current_time_spent.tv_usec); |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 563 | } |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 564 | fprintf(options.output, "\n"); |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 565 | |
| 566 | #if defined(HAVE_LIBUNWIND) |
| 567 | if (options.bt_depth > 0) { |
| 568 | unw_cursor_t cursor; |
| 569 | unw_word_t ip, sp; |
| 570 | int unwind_depth = options.bt_depth; |
| 571 | char fn_name[100]; |
| 572 | |
| 573 | unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv); |
| 574 | while (unwind_depth) { |
| 575 | unw_get_reg(&cursor, UNW_REG_IP, &ip); |
| 576 | unw_get_reg(&cursor, UNW_REG_SP, &sp); |
| 577 | unw_get_proc_name(&cursor, fn_name, 100, NULL); |
| 578 | fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip); |
| 579 | if (unw_step(&cursor) <= 0) |
| 580 | break; |
| 581 | unwind_depth--; |
| 582 | } |
| 583 | fprintf(options.output, "\n"); |
| 584 | } |
| 585 | #endif /* defined(HAVE_LIBUNWIND) */ |
| 586 | |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 587 | current_proc = 0; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 588 | current_column = 0; |
| Juan Cespedes | c40e64a | 1997-10-26 20:34:00 +0100 | [diff] [blame] | 589 | } |
| Petr Machata | d71cf2d | 2012-01-05 18:28:31 +0100 | [diff] [blame] | 590 | |
| 591 | static void |
| 592 | do_report(const char *filename, unsigned line_no, const char *severity, |
| 593 | const char *fmt, va_list args) |
| 594 | { |
| 595 | char buf[128]; |
| 596 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 597 | buf[sizeof(buf) - 1] = 0; |
| 598 | if (filename != NULL) |
| 599 | output_line(0, "%s:%d: %s: %s", |
| 600 | filename, line_no, severity, buf); |
| 601 | else |
| 602 | output_line(0, "%s: %s", severity, buf); |
| 603 | } |
| 604 | |
| 605 | void |
| 606 | report_error(const char *filename, unsigned line_no, char *fmt, ...) |
| 607 | { |
| 608 | va_list args; |
| 609 | va_start(args, fmt); |
| 610 | do_report(filename, line_no, "error", fmt, args); |
| 611 | va_end(args); |
| 612 | } |
| 613 | |
| 614 | void |
| 615 | report_warning(const char *filename, unsigned line_no, char *fmt, ...) |
| 616 | { |
| 617 | va_list args; |
| 618 | va_start(args, fmt); |
| 619 | do_report(filename, line_no, "warning", fmt, args); |
| 620 | va_end(args); |
| 621 | } |
| 622 | |
| 623 | void |
| 624 | report_global_error(char *fmt, ...) |
| 625 | { |
| 626 | va_list args; |
| 627 | va_start(args, fmt); |
| 628 | do_report(NULL, 0, "error", fmt, args); |
| 629 | va_end(args); |
| 630 | } |