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