| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of ltrace. |
| Petr Machata | 98ff309 | 2013-03-08 22:11:36 +0100 | [diff] [blame] | 3 | * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc. |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 4 | * Copyright (C) 2010 Joe Damato |
| 5 | * Copyright (C) 1997,1998,1999,2001,2002,2003,2004,2007,2008,2009 Juan Cespedes |
| Petr Machata | e99af27 | 2012-10-26 00:29:52 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006 Paul Gilliam, IBM Corporation |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 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 | |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 37 | #include "output.h" |
| Petr Machata | 3ac8db6 | 2012-11-23 18:43:10 +0100 | [diff] [blame] | 38 | #include "demangle.h" |
| Petr Machata | 9bc290b | 2012-11-23 19:08:22 +0100 | [diff] [blame] | 39 | #include "fetch.h" |
| 40 | #include "lens_default.h" |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 41 | #include "library.h" |
| Petr Machata | 9bc290b | 2012-11-23 19:08:22 +0100 | [diff] [blame] | 42 | #include "memstream.h" |
| Petr Machata | 8bf82d0 | 2012-11-23 18:44:31 +0100 | [diff] [blame] | 43 | #include "options.h" |
| Petr Machata | 9bc290b | 2012-11-23 19:08:22 +0100 | [diff] [blame] | 44 | #include "param.h" |
| 45 | #include "proc.h" |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 46 | #include "prototype.h" |
| Petr Machata | 000e311 | 2012-01-03 17:03:39 +0100 | [diff] [blame] | 47 | #include "type.h" |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 48 | #include "value.h" |
| 49 | #include "value_dict.h" |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 50 | |
| Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 51 | /* TODO FIXME XXX: include in common.h: */ |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 52 | extern struct timeval current_time_spent; |
| 53 | |
| Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 54 | struct dict *dict_opt_c = NULL; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 55 | |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 56 | static struct process *current_proc = 0; |
| Petr Machata | ba1664b | 2012-04-28 14:59:05 +0200 | [diff] [blame] | 57 | static size_t current_depth = 0; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 58 | static int current_column = 0; |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 59 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 60 | static void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 61 | output_indent(struct process *proc) |
| Petr Machata | 5400475 | 2012-05-03 18:36:48 +0200 | [diff] [blame] | 62 | { |
| 63 | int d = options.indent * (proc->callstack_depth - 1); |
| 64 | current_column += fprintf(options.output, "%*s", d, ""); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 65 | } |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 66 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 67 | static void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 68 | begin_of_line(struct process *proc, int is_func, int indent) |
| Petr Machata | 37b73c0 | 2012-01-06 16:00:25 +0100 | [diff] [blame] | 69 | { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 70 | current_column = 0; |
| 71 | if (!proc) { |
| 72 | return; |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 73 | } |
| Juan Cespedes | c693f02 | 2009-05-21 18:59:41 +0200 | [diff] [blame] | 74 | if ((options.output != stderr) && (opt_p || options.follow)) { |
| 75 | current_column += fprintf(options.output, "%u ", proc->pid); |
| Juan Cespedes | e12df4c | 2009-05-28 19:06:35 +0200 | [diff] [blame] | 76 | } else if (options.follow) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 77 | current_column += fprintf(options.output, "[pid %u] ", proc->pid); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 78 | } |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 79 | if (opt_r) { |
| 80 | struct timeval tv; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 81 | static struct timeval old_tv = { 0, 0 }; |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 82 | struct timeval diff; |
| 83 | |
| Petr Machata | 7564227 | 2012-11-23 19:02:13 +0100 | [diff] [blame] | 84 | gettimeofday(&tv, NULL); |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 85 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 86 | if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) { |
| 87 | old_tv.tv_sec = tv.tv_sec; |
| 88 | old_tv.tv_usec = tv.tv_usec; |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 89 | } |
| 90 | diff.tv_sec = tv.tv_sec - old_tv.tv_sec; |
| 91 | if (tv.tv_usec >= old_tv.tv_usec) { |
| 92 | diff.tv_usec = tv.tv_usec - old_tv.tv_usec; |
| 93 | } else { |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 94 | diff.tv_sec--; |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 95 | diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec; |
| 96 | } |
| 97 | old_tv.tv_sec = tv.tv_sec; |
| 98 | old_tv.tv_usec = tv.tv_usec; |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 99 | current_column += fprintf(options.output, "%3lu.%06d ", |
| Andrey Zonov | 6bb4201 | 2013-02-14 12:32:06 +0100 | [diff] [blame] | 100 | (unsigned long)diff.tv_sec, |
| 101 | (int)diff.tv_usec); |
| Juan Cespedes | f666d19 | 1998-09-20 23:04:34 +0200 | [diff] [blame] | 102 | } |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 103 | if (opt_t) { |
| 104 | struct timeval tv; |
| Petr Machata | 7564227 | 2012-11-23 19:02:13 +0100 | [diff] [blame] | 105 | gettimeofday(&tv, NULL); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 106 | if (opt_t > 2) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 107 | current_column += fprintf(options.output, "%lu.%06d ", |
| Andrey Zonov | 6bb4201 | 2013-02-14 12:32:06 +0100 | [diff] [blame] | 108 | (unsigned long)tv.tv_sec, |
| 109 | (int)tv.tv_usec); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 110 | } else if (opt_t > 1) { |
| 111 | struct tm *tmp = localtime(&tv.tv_sec); |
| 112 | current_column += |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 113 | fprintf(options.output, "%02d:%02d:%02d.%06d ", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 114 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec, |
| 115 | (int)tv.tv_usec); |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 116 | } else { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 117 | struct tm *tmp = localtime(&tv.tv_sec); |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 118 | current_column += fprintf(options.output, "%02d:%02d:%02d ", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 119 | tmp->tm_hour, tmp->tm_min, |
| 120 | tmp->tm_sec); |
| Juan Cespedes | 5e0acdb | 1998-04-04 08:34:07 +0200 | [diff] [blame] | 121 | } |
| 122 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 123 | if (opt_i) { |
| Petr Machata | 4dcc389 | 2013-01-30 23:11:35 +0100 | [diff] [blame] | 124 | if (is_func) { |
| 125 | struct callstack_element *stel |
| 126 | = &proc->callstack[proc->callstack_depth - 1]; |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 127 | current_column += fprintf(options.output, "[%p] ", |
| Petr Machata | 4dcc389 | 2013-01-30 23:11:35 +0100 | [diff] [blame] | 128 | stel->return_addr); |
| 129 | } else { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 130 | current_column += fprintf(options.output, "[%p] ", |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 131 | proc->instruction_pointer); |
| Petr Machata | 4dcc389 | 2013-01-30 23:11:35 +0100 | [diff] [blame] | 132 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 133 | } |
| Petr Machata | 37b73c0 | 2012-01-06 16:00:25 +0100 | [diff] [blame] | 134 | if (options.indent > 0 && indent) { |
| Juan Cespedes | 3f0b62e | 2001-07-09 01:02:52 +0200 | [diff] [blame] | 135 | output_indent(proc); |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 136 | } |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| Petr Machata | b781916 | 2012-01-09 04:26:15 +0100 | [diff] [blame] | 139 | static struct arg_type_info * |
| 140 | get_unknown_type(void) |
| 141 | { |
| Petr Machata | f1bd48b | 2012-12-25 05:15:47 +0100 | [diff] [blame] | 142 | static struct arg_type_info *ret = NULL; |
| 143 | if (ret != NULL) |
| 144 | return ret; |
| 145 | |
| 146 | static struct arg_type_info info; |
| 147 | info = *type_get_simple(ARGTYPE_LONG); |
| 148 | info.lens = &guess_lens; |
| 149 | ret = &info; |
| 150 | return ret; |
| Petr Machata | b781916 | 2012-01-09 04:26:15 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 153 | /* The default prototype is: long X(long, long, long, long). */ |
| Petr Machata | 3d083b6 | 2012-11-22 15:14:45 +0100 | [diff] [blame] | 154 | static struct prototype * |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 155 | build_default_prototype(void) |
| 156 | { |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 157 | static struct prototype *ret = NULL; |
| 158 | if (ret != NULL) |
| 159 | return ret; |
| 160 | |
| 161 | static struct prototype proto; |
| 162 | prototype_init(&proto); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 163 | |
| Petr Machata | b781916 | 2012-01-09 04:26:15 +0100 | [diff] [blame] | 164 | struct arg_type_info *unknown_type = get_unknown_type(); |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 165 | assert(unknown_type != NULL); |
| 166 | proto.return_info = unknown_type; |
| 167 | proto.own_return_info = 0; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 168 | |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 169 | struct param unknown_param; |
| 170 | param_init_type(&unknown_param, unknown_type, 0); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 171 | |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 172 | size_t i; |
| 173 | for (i = 0; i < 4; ++i) |
| 174 | if (prototype_push_param(&proto, &unknown_param) < 0) { |
| 175 | report_global_error("build_default_prototype: %s", |
| 176 | strerror(errno)); |
| 177 | prototype_destroy(&proto); |
| 178 | return NULL; |
| 179 | } |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 180 | |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 181 | ret = &proto; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 182 | return ret; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| Petr Machata | 98a7dce | 2013-10-23 17:37:47 +0200 | [diff] [blame^] | 185 | static bool |
| 186 | snip_period(char *buf) |
| 187 | { |
| 188 | char *period = strrchr(buf, '.'); |
| 189 | if (period != NULL && strcmp(period, ".so") != 0) { |
| 190 | *period = 0; |
| 191 | return true; |
| 192 | } else { |
| 193 | return false; |
| 194 | } |
| 195 | } |
| 196 | |
| Petr Machata | 3d083b6 | 2012-11-22 15:14:45 +0100 | [diff] [blame] | 197 | static struct prototype * |
| Petr Machata | a94d9df | 2012-12-17 04:41:16 +0100 | [diff] [blame] | 198 | library_get_prototype(struct library *lib, const char *name) |
| 199 | { |
| Petr Machata | 98a7dce | 2013-10-23 17:37:47 +0200 | [diff] [blame^] | 200 | if (lib->protolib == NULL) { |
| 201 | size_t sz = strlen(lib->soname); |
| 202 | char buf[sz + 1]; |
| 203 | memcpy(buf, lib->soname, sz); |
| 204 | |
| 205 | do { |
| 206 | if (protolib_cache_maybe_load(&g_protocache, buf, 0, |
| 207 | true, &lib->protolib) < 0) |
| 208 | return NULL; |
| 209 | } while (lib->protolib == NULL |
| 210 | && lib->type == LT_LIBTYPE_DSO |
| 211 | && snip_period(buf)); |
| 212 | |
| 213 | if (lib->protolib == NULL) |
| 214 | lib->protolib = protolib_cache_default(&g_protocache, |
| 215 | buf, 0); |
| 216 | } |
| Petr Machata | a94d9df | 2012-12-17 04:41:16 +0100 | [diff] [blame] | 217 | if (lib->protolib == NULL) |
| 218 | return NULL; |
| 219 | |
| Petr Machata | 82f748d | 2013-10-23 00:39:23 +0200 | [diff] [blame] | 220 | return protolib_lookup_prototype(lib->protolib, name, |
| 221 | lib->type != LT_LIBTYPE_SYSCALL); |
| Petr Machata | a94d9df | 2012-12-17 04:41:16 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | struct find_proto_data { |
| 225 | const char *name; |
| 226 | struct prototype *ret; |
| 227 | }; |
| 228 | |
| 229 | static enum callback_status |
| 230 | find_proto_cb(struct process *proc, struct library *lib, void *d) |
| 231 | { |
| 232 | struct find_proto_data *data = d; |
| 233 | data->ret = library_get_prototype(lib, data->name); |
| 234 | return CBS_STOP_IF(data->ret != NULL); |
| 235 | } |
| 236 | |
| 237 | static struct prototype * |
| 238 | lookup_symbol_prototype(struct process *proc, struct library_symbol *libsym) |
| Petr Machata | 3d083b6 | 2012-11-22 15:14:45 +0100 | [diff] [blame] | 239 | { |
| Petr Machata | b8f0d8b | 2013-10-16 14:25:45 +0200 | [diff] [blame] | 240 | if (libsym->proto != NULL) |
| 241 | return libsym->proto; |
| 242 | |
| Petr Machata | 08cdf32 | 2012-12-05 01:46:13 +0100 | [diff] [blame] | 243 | struct library *lib = libsym->lib; |
| 244 | if (lib != NULL) { |
| Petr Machata | a94d9df | 2012-12-17 04:41:16 +0100 | [diff] [blame] | 245 | struct find_proto_data data = { libsym->name }; |
| 246 | data.ret = library_get_prototype(lib, libsym->name); |
| 247 | if (data.ret == NULL |
| 248 | && libsym->plt_type == LS_TOPLT_EXEC) |
| 249 | proc_each_library(proc, NULL, find_proto_cb, &data); |
| 250 | if (data.ret != NULL) |
| 251 | return data.ret; |
| Petr Machata | 08cdf32 | 2012-12-05 01:46:13 +0100 | [diff] [blame] | 252 | } |
| 253 | |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 254 | return build_default_prototype(); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 257 | void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 258 | output_line(struct process *proc, const char *fmt, ...) |
| Petr Machata | efc9436 | 2012-10-27 00:49:05 +0200 | [diff] [blame] | 259 | { |
| 260 | if (options.summary) |
| 261 | return; |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 262 | |
| Petr Machata | efc9436 | 2012-10-27 00:49:05 +0200 | [diff] [blame] | 263 | if (current_proc != NULL) { |
| 264 | if (current_proc->callstack[current_depth].return_addr) |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 265 | fprintf(options.output, " <unfinished ...>\n"); |
| Petr Machata | efc9436 | 2012-10-27 00:49:05 +0200 | [diff] [blame] | 266 | else |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 267 | fprintf(options.output, " <no return ...>\n"); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 268 | } |
| Petr Machata | efc9436 | 2012-10-27 00:49:05 +0200 | [diff] [blame] | 269 | current_proc = NULL; |
| 270 | if (fmt == NULL) |
| Juan Cespedes | 28f6019 | 1998-04-12 00:04:39 +0200 | [diff] [blame] | 271 | return; |
| Petr Machata | efc9436 | 2012-10-27 00:49:05 +0200 | [diff] [blame] | 272 | |
| Petr Machata | 37b73c0 | 2012-01-06 16:00:25 +0100 | [diff] [blame] | 273 | begin_of_line(proc, 0, 0); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 274 | |
| Petr Machata | efc9436 | 2012-10-27 00:49:05 +0200 | [diff] [blame] | 275 | va_list args; |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 276 | va_start(args, fmt); |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 277 | vfprintf(options.output, fmt, args); |
| 278 | fprintf(options.output, "\n"); |
| Juan Cespedes | 21c63a1 | 2001-07-07 20:56:56 +0200 | [diff] [blame] | 279 | va_end(args); |
| Petr Machata | efc9436 | 2012-10-27 00:49:05 +0200 | [diff] [blame] | 280 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 281 | current_column = 0; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 282 | } |
| 283 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 284 | static void |
| 285 | tabto(int col) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 286 | if (current_column < col) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 287 | fprintf(options.output, "%*s", col - current_column, ""); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 291 | static int |
| Petr Machata | f7c46bb | 2012-11-01 22:15:45 +0100 | [diff] [blame] | 292 | output_error(FILE *stream) |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 293 | { |
| Petr Machata | f7c46bb | 2012-11-01 22:15:45 +0100 | [diff] [blame] | 294 | return fprintf(stream, "?"); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static int |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 298 | fetch_simple_param(enum tof type, struct process *proc, |
| 299 | struct fetch_context *context, |
| Petr Machata | bc58f2a | 2012-10-30 00:25:50 +0100 | [diff] [blame] | 300 | struct value_dict *arguments, |
| 301 | struct arg_type_info *info, int own, |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 302 | struct value *valuep) |
| 303 | { |
| 304 | /* Arrays decay into pointers per C standard. We check for |
| 305 | * this here, because here we also capture arrays that come |
| 306 | * from parameter packs. */ |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 307 | if (info->type == ARGTYPE_ARRAY) { |
| 308 | struct arg_type_info *tmp = malloc(sizeof(*tmp)); |
| 309 | if (tmp != NULL) { |
| Petr Machata | bc58f2a | 2012-10-30 00:25:50 +0100 | [diff] [blame] | 310 | type_init_pointer(tmp, info, own); |
| Petr Machata | b781916 | 2012-01-09 04:26:15 +0100 | [diff] [blame] | 311 | tmp->lens = info->lens; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 312 | info = tmp; |
| 313 | own = 1; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | struct value value; |
| 318 | value_init(&value, proc, NULL, info, own); |
| 319 | if (fetch_arg_next(context, type, proc, info, &value) < 0) |
| 320 | return -1; |
| 321 | |
| 322 | if (val_dict_push_next(arguments, &value) < 0) { |
| 323 | value_destroy(&value); |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | if (valuep != NULL) |
| 328 | *valuep = value; |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static void |
| 334 | fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp) |
| 335 | { |
| 336 | if (*params_leftp == -1) |
| 337 | *params_leftp = val_dict_count(arguments); |
| 338 | } |
| 339 | |
| 340 | static int |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 341 | fetch_param_pack(enum tof type, struct process *proc, |
| 342 | struct fetch_context *context, |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 343 | struct value_dict *arguments, struct param *param, |
| 344 | ssize_t *params_leftp) |
| 345 | { |
| 346 | struct param_enum *e = param_pack_init(param, arguments); |
| 347 | if (e == NULL) |
| 348 | return -1; |
| 349 | |
| 350 | int ret = 0; |
| 351 | while (1) { |
| 352 | int insert_stop = 0; |
| 353 | struct arg_type_info *info = malloc(sizeof(*info)); |
| 354 | if (info == NULL |
| 355 | || param_pack_next(param, e, info, &insert_stop) < 0) { |
| 356 | fail: |
| 357 | free(info); |
| 358 | ret = -1; |
| 359 | break; |
| 360 | } |
| 361 | |
| 362 | if (insert_stop) |
| 363 | fetch_param_stop(arguments, params_leftp); |
| 364 | |
| Petr Machata | bc58f2a | 2012-10-30 00:25:50 +0100 | [diff] [blame] | 365 | if (info->type == ARGTYPE_VOID) { |
| 366 | type_destroy(info); |
| 367 | free(info); |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 368 | break; |
| Petr Machata | bc58f2a | 2012-10-30 00:25:50 +0100 | [diff] [blame] | 369 | } |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 370 | |
| 371 | struct value val; |
| 372 | if (fetch_simple_param(type, proc, context, arguments, |
| Petr Machata | bc58f2a | 2012-10-30 00:25:50 +0100 | [diff] [blame] | 373 | info, 1, &val) < 0) |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 374 | goto fail; |
| 375 | |
| 376 | int stop = 0; |
| 377 | switch (param_pack_stop(param, e, &val)) { |
| 378 | case PPCB_ERR: |
| 379 | goto fail; |
| 380 | case PPCB_STOP: |
| 381 | stop = 1; |
| 382 | case PPCB_CONT: |
| 383 | break; |
| 384 | } |
| 385 | |
| 386 | if (stop) |
| 387 | break; |
| 388 | } |
| 389 | |
| 390 | param_pack_done(param, e); |
| 391 | return ret; |
| 392 | } |
| 393 | |
| 394 | static int |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 395 | fetch_one_param(enum tof type, struct process *proc, |
| 396 | struct fetch_context *context, |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 397 | struct value_dict *arguments, struct param *param, |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 398 | ssize_t *params_leftp) |
| 399 | { |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 400 | switch (param->flavor) { |
| Petr Machata | e36298a | 2012-09-13 17:12:41 +0200 | [diff] [blame] | 401 | int rc; |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 402 | case PARAM_FLAVOR_TYPE: |
| 403 | return fetch_simple_param(type, proc, context, arguments, |
| Petr Machata | bc58f2a | 2012-10-30 00:25:50 +0100 | [diff] [blame] | 404 | param->u.type.type, 0, NULL); |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 405 | |
| 406 | case PARAM_FLAVOR_PACK: |
| Petr Machata | e36298a | 2012-09-13 17:12:41 +0200 | [diff] [blame] | 407 | if (fetch_param_pack_start(context, |
| 408 | param->u.pack.ppflavor) < 0) |
| 409 | return -1; |
| 410 | rc = fetch_param_pack(type, proc, context, arguments, |
| 411 | param, params_leftp); |
| 412 | fetch_param_pack_end(context); |
| 413 | return rc; |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 414 | |
| 415 | case PARAM_FLAVOR_STOP: |
| 416 | fetch_param_stop(arguments, params_leftp); |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | assert(!"Invalid param flavor!"); |
| 421 | abort(); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 422 | } |
| 423 | |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 424 | struct fetch_one_param_data |
| 425 | { |
| 426 | struct process *proc; |
| 427 | struct fetch_context *context; |
| 428 | struct value_dict *arguments; |
| 429 | ssize_t *params_leftp; |
| 430 | enum tof tof; |
| 431 | }; |
| 432 | |
| 433 | static enum callback_status |
| 434 | fetch_one_param_cb(struct prototype *proto, struct param *param, void *data) |
| 435 | { |
| 436 | struct fetch_one_param_data *cb_data = data; |
| Petr Machata | a94d9df | 2012-12-17 04:41:16 +0100 | [diff] [blame] | 437 | return CBS_STOP_IF(fetch_one_param(cb_data->tof, cb_data->proc, |
| 438 | cb_data->context, |
| 439 | cb_data->arguments, param, |
| 440 | cb_data->params_leftp) < 0); |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 441 | } |
| 442 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 443 | static int |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 444 | fetch_params(enum tof type, struct process *proc, |
| 445 | struct fetch_context *context, |
| Petr Machata | 3d083b6 | 2012-11-22 15:14:45 +0100 | [diff] [blame] | 446 | struct value_dict *arguments, struct prototype *func, |
| 447 | ssize_t *params_leftp) |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 448 | { |
| Petr Machata | ebc56a7 | 2012-12-17 04:37:57 +0100 | [diff] [blame] | 449 | struct fetch_one_param_data cb_data |
| 450 | = { proc, context, arguments, params_leftp, type }; |
| 451 | if (prototype_each_param(func, NULL, |
| 452 | &fetch_one_param_cb, &cb_data) != NULL) |
| 453 | return -1; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 454 | |
| 455 | /* Implicit stop at the end of parameter list. */ |
| 456 | fetch_param_stop(arguments, params_leftp); |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 457 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 458 | return 0; |
| 459 | } |
| 460 | |
| Petr Machata | f7c46bb | 2012-11-01 22:15:45 +0100 | [diff] [blame] | 461 | struct format_argument_data |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 462 | { |
| Petr Machata | f7c46bb | 2012-11-01 22:15:45 +0100 | [diff] [blame] | 463 | struct value *value; |
| 464 | struct value_dict *arguments; |
| 465 | }; |
| 466 | |
| 467 | static int |
| 468 | format_argument_cb(FILE *stream, void *ptr) |
| 469 | { |
| 470 | struct format_argument_data *data = ptr; |
| 471 | int o = format_argument(stream, data->value, data->arguments); |
| 472 | if (o < 0) |
| 473 | o = output_error(stream); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 474 | return o; |
| 475 | } |
| 476 | |
| 477 | static int |
| 478 | output_params(struct value_dict *arguments, size_t start, size_t end, |
| 479 | int *need_delimp) |
| 480 | { |
| 481 | size_t i; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 482 | for (i = start; i < end; ++i) { |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 483 | struct value *value = val_dict_get_num(arguments, i); |
| 484 | if (value == NULL) |
| 485 | return -1; |
| Petr Machata | f7c46bb | 2012-11-01 22:15:45 +0100 | [diff] [blame] | 486 | |
| 487 | struct format_argument_data data = { value, arguments }; |
| 488 | int o = delim_output(options.output, need_delimp, |
| 489 | format_argument_cb, &data); |
| 490 | if (o < 0) |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 491 | return -1; |
| Petr Machata | f7c46bb | 2012-11-01 22:15:45 +0100 | [diff] [blame] | 492 | current_column += o; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 493 | } |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 494 | return 0; |
| 495 | } |
| 496 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 497 | void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 498 | output_left(enum tof type, struct process *proc, |
| Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 499 | struct library_symbol *libsym) |
| 500 | { |
| Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 501 | if (options.summary) { |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 502 | return; |
| 503 | } |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 504 | if (current_proc) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 505 | fprintf(options.output, " <unfinished ...>\n"); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 506 | current_column = 0; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 507 | } |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 508 | current_proc = proc; |
| Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 509 | current_depth = proc->callstack_depth; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 510 | begin_of_line(proc, type == LT_TOF_FUNCTION, 1); |
| Petr Machata | 53bc49b | 2012-04-25 17:23:02 +0200 | [diff] [blame] | 511 | if (!options.hide_caller && libsym->lib != NULL |
| 512 | && libsym->plt_type != LS_TOPLT_NONE) |
| Petr Machata | 0965420 | 2012-12-05 17:07:21 +0100 | [diff] [blame] | 513 | /* We don't terribly mind failing this. */ |
| 514 | account_output(¤t_column, |
| 515 | fprintf(options.output, "%s->", |
| 516 | libsym->lib->soname)); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 517 | |
| Petr Machata | 08cdf32 | 2012-12-05 01:46:13 +0100 | [diff] [blame] | 518 | const char *name = libsym->name; |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 519 | #ifdef USE_DEMANGLE |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 520 | if (options.demangle) |
| Petr Machata | 08cdf32 | 2012-12-05 01:46:13 +0100 | [diff] [blame] | 521 | name = my_demangle(libsym->name); |
| Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 522 | #endif |
| Petr Machata | adec201 | 2012-11-01 22:05:04 +0100 | [diff] [blame] | 523 | if (account_output(¤t_column, |
| Petr Machata | 0965420 | 2012-12-05 17:07:21 +0100 | [diff] [blame] | 524 | fprintf(options.output, "%s", name)) < 0) |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 525 | return; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 526 | |
| Petr Machata | 0965420 | 2012-12-05 17:07:21 +0100 | [diff] [blame] | 527 | if (libsym->lib != NULL |
| 528 | && libsym->lib->type != LT_LIBTYPE_MAIN |
| 529 | && libsym->plt_type == LS_TOPLT_NONE |
| 530 | && account_output(¤t_column, |
| 531 | fprintf(options.output, "@%s", |
| 532 | libsym->lib->soname)) < 0) |
| 533 | /* We do mind failing this though. */ |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 534 | return; |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 535 | |
| Petr Machata | 0965420 | 2012-12-05 17:07:21 +0100 | [diff] [blame] | 536 | account_output(¤t_column, fprintf(options.output, "(")); |
| 537 | |
| Petr Machata | a94d9df | 2012-12-17 04:41:16 +0100 | [diff] [blame] | 538 | struct prototype *func = lookup_symbol_prototype(proc, libsym); |
| Petr Machata | 0965420 | 2012-12-05 17:07:21 +0100 | [diff] [blame] | 539 | if (func == NULL) { |
| Petr Machata | 1b02c73 | 2013-01-07 18:22:51 +0100 | [diff] [blame] | 540 | fail: |
| Petr Machata | 0965420 | 2012-12-05 17:07:21 +0100 | [diff] [blame] | 541 | account_output(¤t_column, fprintf(options.output, "???")); |
| 542 | return; |
| 543 | } |
| 544 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 545 | struct fetch_context *context = fetch_arg_init(type, proc, |
| 546 | func->return_info); |
| Petr Machata | 1b02c73 | 2013-01-07 18:22:51 +0100 | [diff] [blame] | 547 | if (context == NULL) |
| 548 | goto fail; |
| 549 | |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 550 | struct value_dict *arguments = malloc(sizeof(*arguments)); |
| Petr Machata | 1b02c73 | 2013-01-07 18:22:51 +0100 | [diff] [blame] | 551 | if (arguments == NULL) { |
| 552 | fetch_arg_done(context); |
| 553 | goto fail; |
| 554 | } |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 555 | val_dict_init(arguments); |
| 556 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 557 | ssize_t params_left = -1; |
| 558 | int need_delim = 0; |
| 559 | if (fetch_params(type, proc, context, arguments, func, ¶ms_left) < 0 |
| 560 | || output_params(arguments, 0, params_left, &need_delim) < 0) { |
| 561 | val_dict_destroy(arguments); |
| 562 | fetch_arg_done(context); |
| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 563 | arguments = NULL; |
| 564 | context = NULL; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 565 | } |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 566 | |
| 567 | struct callstack_element *stel |
| 568 | = &proc->callstack[proc->callstack_depth - 1]; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 569 | stel->fetch_context = context; |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 570 | stel->arguments = arguments; |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 571 | stel->out.params_left = params_left; |
| 572 | stel->out.need_delim = need_delim; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 573 | } |
| 574 | |
| Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 575 | static void |
| 576 | free_stringp_cb(const char **stringp, void *data) |
| 577 | { |
| 578 | free((char *)*stringp); |
| 579 | } |
| 580 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 581 | void |
| Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 582 | output_right(enum tof type, struct process *proc, struct library_symbol *libsym) |
| Petr Machata | 14184b3 | 2012-02-10 13:10:26 +0100 | [diff] [blame] | 583 | { |
| Petr Machata | a94d9df | 2012-12-17 04:41:16 +0100 | [diff] [blame] | 584 | struct prototype *func = lookup_symbol_prototype(proc, libsym); |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 585 | if (func == NULL) |
| 586 | return; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 587 | |
| Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 588 | again: |
| Juan Cespedes | da9b953 | 2009-04-07 15:33:50 +0200 | [diff] [blame] | 589 | if (options.summary) { |
| Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 590 | if (dict_opt_c == NULL) { |
| 591 | dict_opt_c = malloc(sizeof(*dict_opt_c)); |
| 592 | if (dict_opt_c == NULL) { |
| 593 | oom: |
| 594 | fprintf(stderr, |
| 595 | "Can't allocate memory for " |
| 596 | "keeping track of -c.\n"); |
| 597 | free(dict_opt_c); |
| 598 | options.summary = 0; |
| 599 | goto again; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 600 | } |
| Peter Wu | 9748550 | 2013-09-26 00:55:56 +0200 | [diff] [blame] | 601 | DICT_INIT(dict_opt_c, char *, struct opt_c_struct, |
| Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 602 | dict_hash_string, dict_eq_string, NULL); |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 603 | } |
| Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 604 | |
| Petr Machata | 98ff309 | 2013-03-08 22:11:36 +0100 | [diff] [blame] | 605 | struct opt_c_struct *st |
| Petr Machata | 08cdf32 | 2012-12-05 01:46:13 +0100 | [diff] [blame] | 606 | = DICT_FIND_REF(dict_opt_c, &libsym->name, |
| Petr Machata | 98ff309 | 2013-03-08 22:11:36 +0100 | [diff] [blame] | 607 | struct opt_c_struct); |
| Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 608 | if (st == NULL) { |
| Petr Machata | 08cdf32 | 2012-12-05 01:46:13 +0100 | [diff] [blame] | 609 | const char *na = strdup(libsym->name); |
| Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 610 | struct opt_c_struct new_st = {.count = 0, .tv = {0, 0}}; |
| 611 | if (na == NULL |
| 612 | || DICT_INSERT(dict_opt_c, &na, &new_st) < 0) { |
| 613 | free((char *)na); |
| 614 | DICT_DESTROY(dict_opt_c, const char *, |
| 615 | struct opt_c_struct, |
| 616 | free_stringp_cb, NULL, NULL); |
| 617 | goto oom; |
| 618 | } |
| Petr Machata | 08cdf32 | 2012-12-05 01:46:13 +0100 | [diff] [blame] | 619 | st = DICT_FIND_REF(dict_opt_c, &libsym->name, |
| Petr Machata | 98ff309 | 2013-03-08 22:11:36 +0100 | [diff] [blame] | 620 | struct opt_c_struct); |
| Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 621 | assert(st != NULL); |
| 622 | } |
| 623 | |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 624 | if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) { |
| 625 | st->tv.tv_usec += current_time_spent.tv_usec - 1000000; |
| 626 | st->tv.tv_sec++; |
| 627 | } else { |
| 628 | st->tv.tv_usec += current_time_spent.tv_usec; |
| 629 | } |
| 630 | st->count++; |
| 631 | st->tv.tv_sec += current_time_spent.tv_sec; |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 632 | return; |
| 633 | } |
| Petr Machata | d7e4ca8 | 2012-11-28 03:38:47 +0100 | [diff] [blame] | 634 | |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 635 | if (current_proc && (current_proc != proc || |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 636 | current_depth != proc->callstack_depth)) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 637 | fprintf(options.output, " <unfinished ...>\n"); |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 638 | current_proc = 0; |
| Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 639 | } |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 640 | if (current_proc != proc) { |
| Petr Machata | 37b73c0 | 2012-01-06 16:00:25 +0100 | [diff] [blame] | 641 | begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1); |
| Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 642 | #ifdef USE_DEMANGLE |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 643 | current_column += |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 644 | fprintf(options.output, "<... %s resumed> ", |
| Petr Machata | 08cdf32 | 2012-12-05 01:46:13 +0100 | [diff] [blame] | 645 | options.demangle ? my_demangle(libsym->name) |
| 646 | : libsym->name); |
| Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 647 | #else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 648 | current_column += |
| Petr Machata | 08cdf32 | 2012-12-05 01:46:13 +0100 | [diff] [blame] | 649 | fprintf(options.output, "<... %s resumed> ", libsym->name); |
| Juan Cespedes | 1b9cfd6 | 1999-08-30 19:34:50 +0200 | [diff] [blame] | 650 | #endif |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 651 | } |
| 652 | |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 653 | struct callstack_element *stel |
| 654 | = &proc->callstack[proc->callstack_depth - 1]; |
| 655 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 656 | struct fetch_context *context = stel->fetch_context; |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 657 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 658 | /* Fetch & enter into dictionary the retval first, so that |
| 659 | * other values can use it in expressions. */ |
| 660 | struct value retval; |
| 661 | int own_retval = 0; |
| 662 | if (context != NULL) { |
| 663 | value_init(&retval, proc, NULL, func->return_info, 0); |
| 664 | own_retval = 1; |
| 665 | if (fetch_retval(context, type, proc, func->return_info, |
| Petr Machata | a6fb1f5 | 2012-05-29 17:59:48 +0200 | [diff] [blame] | 666 | &retval) < 0) |
| 667 | value_set_type(&retval, NULL, 0); |
| 668 | else if (stel->arguments != NULL |
| 669 | && val_dict_push_named(stel->arguments, &retval, |
| 670 | "retval", 0) == 0) |
| 671 | own_retval = 0; |
| Juan Cespedes | 5e4455b | 1997-08-24 01:48:26 +0200 | [diff] [blame] | 672 | } |
| Petr Machata | 94078ec | 2012-01-05 18:07:02 +0100 | [diff] [blame] | 673 | |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 674 | if (stel->arguments != NULL) |
| 675 | output_params(stel->arguments, stel->out.params_left, |
| 676 | val_dict_count(stel->arguments), |
| 677 | &stel->out.need_delim); |
| 678 | |
| 679 | current_column += fprintf(options.output, ") "); |
| 680 | tabto(options.align - 1); |
| 681 | fprintf(options.output, "= "); |
| 682 | |
| Petr Machata | f7c46bb | 2012-11-01 22:15:45 +0100 | [diff] [blame] | 683 | if (context != NULL && retval.type != NULL) { |
| 684 | struct format_argument_data data = { &retval, stel->arguments }; |
| 685 | format_argument_cb(options.output, &data); |
| 686 | } |
| Petr Machata | f6ec08a | 2012-01-06 16:58:54 +0100 | [diff] [blame] | 687 | |
| 688 | if (own_retval) |
| 689 | value_destroy(&retval); |
| 690 | |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 691 | if (opt_T) { |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 692 | fprintf(options.output, " <%lu.%06d>", |
| Andrey Zonov | 6bb4201 | 2013-02-14 12:32:06 +0100 | [diff] [blame] | 693 | (unsigned long)current_time_spent.tv_sec, |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 694 | (int)current_time_spent.tv_usec); |
| Juan Cespedes | d65efa3 | 2003-02-03 00:22:30 +0100 | [diff] [blame] | 695 | } |
| Juan Cespedes | b65bdc5 | 2008-12-16 19:50:16 +0100 | [diff] [blame] | 696 | fprintf(options.output, "\n"); |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 697 | |
| 698 | #if defined(HAVE_LIBUNWIND) |
| Petr Machata | af1e603 | 2013-01-06 17:16:24 +0100 | [diff] [blame] | 699 | if (options.bt_depth > 0 |
| 700 | && proc->unwind_priv != NULL |
| 701 | && proc->unwind_as != NULL) { |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 702 | unw_cursor_t cursor; |
| Luca Clementi | 2927648 | 2013-10-17 19:48:38 -0700 | [diff] [blame] | 703 | arch_addr_t ip, function_offset; |
| Luca Clementi | 2927648 | 2013-10-17 19:48:38 -0700 | [diff] [blame] | 704 | struct library *lib = NULL; |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 705 | int unwind_depth = options.bt_depth; |
| 706 | char fn_name[100]; |
| Luca Clementi | 2927648 | 2013-10-17 19:48:38 -0700 | [diff] [blame] | 707 | const char *lib_name; |
| 708 | size_t distance; |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 709 | |
| Luca Clementi | 2927648 | 2013-10-17 19:48:38 -0700 | [diff] [blame] | 710 | /* Verify that we can safely cast arch_addr_t* to |
| 711 | * unw_word_t*. */ |
| 712 | (void)sizeof(char[1 - 2*(sizeof(unw_word_t) |
| 713 | != sizeof(arch_addr_t))]); |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 714 | unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv); |
| 715 | while (unwind_depth) { |
| Luca Clementi | 2927648 | 2013-10-17 19:48:38 -0700 | [diff] [blame] | 716 | unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip); |
| Luca Clementi | 2927648 | 2013-10-17 19:48:38 -0700 | [diff] [blame] | 717 | unw_get_proc_name(&cursor, fn_name, sizeof(fn_name), |
| 718 | (unw_word_t *) &function_offset); |
| 719 | |
| 720 | /* We are looking for the library with the base address |
| 721 | * closest to the current ip. */ |
| 722 | lib_name = "unmapped_area"; |
| 723 | distance = (size_t) -1; |
| 724 | lib = proc->libraries; |
| 725 | while (lib != NULL) { |
| 726 | /* N.B.: Assumes sizeof(size_t) == |
| 727 | * sizeof(arch_addr_t). |
| 728 | * Keyword: double cast. */ |
| 729 | if ((ip >= lib->base) && |
| 730 | ((size_t)(ip - lib->base) |
| 731 | < distance)) { |
| 732 | distance = ip - lib->base; |
| 733 | lib_name = lib->pathname; |
| 734 | } |
| 735 | lib = lib->next; |
| 736 | } |
| 737 | |
| 738 | fprintf(options.output, " > %s(%s+0x%p) [%p]\n", |
| 739 | lib_name, fn_name, function_offset, ip); |
| 740 | |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 741 | if (unw_step(&cursor) <= 0) |
| 742 | break; |
| 743 | unwind_depth--; |
| 744 | } |
| 745 | fprintf(options.output, "\n"); |
| 746 | } |
| 747 | #endif /* defined(HAVE_LIBUNWIND) */ |
| 748 | |
| Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 749 | current_proc = 0; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 750 | current_column = 0; |
| Juan Cespedes | c40e64a | 1997-10-26 20:34:00 +0100 | [diff] [blame] | 751 | } |
| Petr Machata | d71cf2d | 2012-01-05 18:28:31 +0100 | [diff] [blame] | 752 | |
| Petr Machata | adec201 | 2012-11-01 22:05:04 +0100 | [diff] [blame] | 753 | int |
| Petr Machata | 9c40098 | 2012-11-01 22:15:01 +0100 | [diff] [blame] | 754 | delim_output(FILE *stream, int *need_delimp, |
| 755 | int (*writer)(FILE *stream, void *data), |
| 756 | void *data) |
| 757 | { |
| 758 | int o; |
| 759 | |
| 760 | /* If we don't need a delimiter, then we don't need to go |
| 761 | * through a temporary stream. It's all the same whether |
| 762 | * WRITER emits anything or not. */ |
| 763 | if (!*need_delimp) { |
| 764 | o = writer(stream, data); |
| 765 | |
| 766 | } else { |
| Petr Machata | 2ae374a | 2012-12-08 05:08:48 +0100 | [diff] [blame] | 767 | struct memstream ms; |
| 768 | if (memstream_init(&ms) < 0) |
| 769 | return -1; |
| 770 | o = writer(ms.stream, data); |
| 771 | if (memstream_close(&ms) < 0) |
| 772 | o = -1; |
| Petr Machata | 9c40098 | 2012-11-01 22:15:01 +0100 | [diff] [blame] | 773 | if (o > 0 && ((*need_delimp |
| 774 | && account_output(&o, fprintf(stream, ", ")) < 0) |
| Petr Machata | 2ae374a | 2012-12-08 05:08:48 +0100 | [diff] [blame] | 775 | || fwrite(ms.buf, 1, ms.size, stream) != ms.size)) |
| Petr Machata | 9c40098 | 2012-11-01 22:15:01 +0100 | [diff] [blame] | 776 | o = -1; |
| 777 | |
| Petr Machata | 2ae374a | 2012-12-08 05:08:48 +0100 | [diff] [blame] | 778 | memstream_destroy(&ms); |
| Petr Machata | 9c40098 | 2012-11-01 22:15:01 +0100 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | if (o < 0) |
| 782 | return -1; |
| 783 | |
| 784 | *need_delimp = *need_delimp || o > 0; |
| 785 | return o; |
| 786 | } |
| 787 | |
| 788 | int |
| Petr Machata | adec201 | 2012-11-01 22:05:04 +0100 | [diff] [blame] | 789 | account_output(int *countp, int c) |
| 790 | { |
| 791 | if (c > 0) |
| 792 | *countp += c; |
| 793 | return c; |
| 794 | } |
| 795 | |
| Petr Machata | d71cf2d | 2012-01-05 18:28:31 +0100 | [diff] [blame] | 796 | static void |
| 797 | do_report(const char *filename, unsigned line_no, const char *severity, |
| 798 | const char *fmt, va_list args) |
| 799 | { |
| 800 | char buf[128]; |
| 801 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 802 | buf[sizeof(buf) - 1] = 0; |
| 803 | if (filename != NULL) |
| 804 | output_line(0, "%s:%d: %s: %s", |
| 805 | filename, line_no, severity, buf); |
| 806 | else |
| 807 | output_line(0, "%s: %s", severity, buf); |
| 808 | } |
| 809 | |
| 810 | void |
| Petr Machata | 89c5ca2 | 2012-10-27 00:49:38 +0200 | [diff] [blame] | 811 | report_error(const char *filename, unsigned line_no, const char *fmt, ...) |
| Petr Machata | d71cf2d | 2012-01-05 18:28:31 +0100 | [diff] [blame] | 812 | { |
| 813 | va_list args; |
| 814 | va_start(args, fmt); |
| 815 | do_report(filename, line_no, "error", fmt, args); |
| 816 | va_end(args); |
| 817 | } |
| 818 | |
| 819 | void |
| Petr Machata | 89c5ca2 | 2012-10-27 00:49:38 +0200 | [diff] [blame] | 820 | report_warning(const char *filename, unsigned line_no, const char *fmt, ...) |
| Petr Machata | d71cf2d | 2012-01-05 18:28:31 +0100 | [diff] [blame] | 821 | { |
| 822 | va_list args; |
| 823 | va_start(args, fmt); |
| 824 | do_report(filename, line_no, "warning", fmt, args); |
| 825 | va_end(args); |
| 826 | } |
| 827 | |
| 828 | void |
| Petr Machata | 89c5ca2 | 2012-10-27 00:49:38 +0200 | [diff] [blame] | 829 | report_global_error(const char *fmt, ...) |
| Petr Machata | d71cf2d | 2012-01-05 18:28:31 +0100 | [diff] [blame] | 830 | { |
| 831 | va_list args; |
| 832 | va_start(args, fmt); |
| 833 | do_report(NULL, 0, "error", fmt, args); |
| 834 | va_end(args); |
| 835 | } |