Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Standard argp argument parsers for tools using libdwfl. |
Roland McGrath | d11f9cb | 2008-03-26 20:51:59 +0000 | [diff] [blame] | 2 | Copyright (C) 2005, 2007, 2008 Red Hat, Inc. |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 3 | This file is part of Red Hat elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 5 | Red Hat elfutils is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by the |
| 7 | Free Software Foundation; version 2 of the License. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 8 | |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 9 | Red Hat elfutils is distributed in the hope that it will be useful, but |
| 10 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License along |
| 15 | with Red Hat elfutils; if not, write to the Free Software Foundation, |
Ulrich Drepper | 1e9ef50 | 2006-04-04 22:29:06 +0000 | [diff] [blame] | 16 | Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 17 | |
| 18 | In addition, as a special exception, Red Hat, Inc. gives You the |
| 19 | additional right to link the code of Red Hat elfutils with code licensed |
| 20 | under any Open Source Initiative certified open source license |
| 21 | (http://www.opensource.org/licenses/index.php) which requires the |
| 22 | distribution of source code with any binary distribution and to |
| 23 | distribute linked combinations of the two. Non-GPL Code permitted under |
| 24 | this exception must only link to the code of Red Hat elfutils through |
| 25 | those well defined interfaces identified in the file named EXCEPTION |
| 26 | found in the source code files (the "Approved Interfaces"). The files |
| 27 | of Non-GPL Code may instantiate templates or use macros or inline |
| 28 | functions from the Approved Interfaces without causing the resulting |
| 29 | work to be covered by the GNU General Public License. Only Red Hat, |
| 30 | Inc. may make changes or additions to the list of Approved Interfaces. |
| 31 | Red Hat's grant of this exception is conditioned upon your not adding |
| 32 | any new exceptions. If you wish to add a new Approved Interface or |
| 33 | exception, please contact Red Hat. You must obey the GNU General Public |
| 34 | License in all respects for all of the Red Hat elfutils code and other |
| 35 | code used in conjunction with Red Hat elfutils except the Non-GPL Code |
| 36 | covered by this exception. If you modify this file, you may extend this |
| 37 | exception to your version of the file, but you are not obligated to do |
| 38 | so. If you do not wish to provide this exception without modification, |
| 39 | you must delete this exception statement from your version and license |
| 40 | this file solely under the GPL without exception. |
| 41 | |
| 42 | Red Hat elfutils is an included package of the Open Invention Network. |
| 43 | An included package of the Open Invention Network is a package for which |
| 44 | Open Invention Network licensees cross-license their patents. No patent |
| 45 | license is granted, either expressly or impliedly, by designation as an |
| 46 | included package. Should you wish to participate in the Open Invention |
| 47 | Network licensing program, please visit www.openinventionnetwork.com |
| 48 | <http://www.openinventionnetwork.com>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 49 | |
| 50 | #include "libdwflP.h" |
| 51 | #include <argp.h> |
| 52 | #include <stdlib.h> |
| 53 | #include <assert.h> |
| 54 | #include <libintl.h> |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame^] | 55 | #include <fcntl.h> |
| 56 | #include <unistd.h> |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 57 | |
| 58 | /* gettext helper macros. */ |
| 59 | #define _(Str) dgettext ("elfutils", Str) |
| 60 | |
| 61 | |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame^] | 62 | #define OPT_DEBUGINFO 0x100 |
| 63 | #define OPT_COREFILE 0x101 |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 64 | |
| 65 | static const struct argp_option options[] = |
| 66 | { |
Roland McGrath | 4be1524 | 2007-04-25 03:09:33 +0000 | [diff] [blame] | 67 | { NULL, 0, NULL, 0, N_("Input selection options:"), 0 }, |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 68 | { "executable", 'e', "FILE", 0, N_("Find addresses in FILE"), 0 }, |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame^] | 69 | { "core", OPT_COREFILE, "COREFILE", 0, |
| 70 | N_("Find addresses from signatures found in COREFILE"), 0 }, |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 71 | { "pid", 'p', "PID", 0, |
| 72 | N_("Find addresses in files mapped into process PID"), 0 }, |
Roland McGrath | 1656bc0 | 2005-12-23 01:49:50 +0000 | [diff] [blame] | 73 | { "linux-process-map", 'M', "FILE", 0, |
| 74 | N_("Find addresses in files mapped as read from FILE" |
| 75 | " in Linux /proc/PID/maps format"), 0 }, |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 76 | { "kernel", 'k', NULL, 0, N_("Find addresses in the running kernel"), 0 }, |
Roland McGrath | d17fac7 | 2005-08-23 08:20:21 +0000 | [diff] [blame] | 77 | { "offline-kernel", 'K', "RELEASE", OPTION_ARG_OPTIONAL, |
| 78 | N_("Kernel with all modules"), 0 }, |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 79 | { "debuginfo-path", OPT_DEBUGINFO, "PATH", 0, |
| 80 | N_("Search path for separate debuginfo files"), 0 }, |
| 81 | { NULL, 0, NULL, 0, NULL, 0 } |
| 82 | }; |
| 83 | |
| 84 | static char *debuginfo_path; |
| 85 | |
Roland McGrath | d17fac7 | 2005-08-23 08:20:21 +0000 | [diff] [blame] | 86 | static const Dwfl_Callbacks offline_callbacks = |
| 87 | { |
| 88 | .find_debuginfo = INTUSE(dwfl_standard_find_debuginfo), |
| 89 | .debuginfo_path = &debuginfo_path, |
| 90 | |
| 91 | .section_address = INTUSE(dwfl_offline_section_address), |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame^] | 92 | |
| 93 | /* We use this table for core files too. */ |
| 94 | .find_elf = INTUSE(dwfl_build_id_find_elf), |
Roland McGrath | d17fac7 | 2005-08-23 08:20:21 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 97 | static const Dwfl_Callbacks proc_callbacks = |
| 98 | { |
| 99 | .find_debuginfo = INTUSE(dwfl_standard_find_debuginfo), |
| 100 | .debuginfo_path = &debuginfo_path, |
| 101 | |
| 102 | .find_elf = INTUSE(dwfl_linux_proc_find_elf), |
| 103 | }; |
| 104 | |
| 105 | static const Dwfl_Callbacks kernel_callbacks = |
| 106 | { |
| 107 | .find_debuginfo = INTUSE(dwfl_standard_find_debuginfo), |
| 108 | .debuginfo_path = &debuginfo_path, |
| 109 | |
| 110 | .find_elf = INTUSE(dwfl_linux_kernel_find_elf), |
| 111 | .section_address = INTUSE(dwfl_linux_kernel_module_section_address), |
| 112 | }; |
| 113 | |
| 114 | static error_t |
| 115 | parse_opt (int key, char *arg, struct argp_state *state) |
| 116 | { |
Roland McGrath | 1656bc0 | 2005-12-23 01:49:50 +0000 | [diff] [blame] | 117 | inline void failure (Dwfl *dwfl, int errnum, const char *msg) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 118 | { |
Roland McGrath | d11f9cb | 2008-03-26 20:51:59 +0000 | [diff] [blame] | 119 | if (dwfl != NULL) |
| 120 | dwfl_end (dwfl); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 121 | if (errnum == -1) |
| 122 | argp_failure (state, EXIT_FAILURE, 0, "%s: %s", |
| 123 | msg, INTUSE(dwfl_errmsg) (-1)); |
| 124 | else |
| 125 | argp_failure (state, EXIT_FAILURE, errnum, "%s", msg); |
| 126 | } |
Roland McGrath | 1656bc0 | 2005-12-23 01:49:50 +0000 | [diff] [blame] | 127 | inline error_t fail (Dwfl *dwfl, int errnum, const char *msg) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 128 | { |
Roland McGrath | 1656bc0 | 2005-12-23 01:49:50 +0000 | [diff] [blame] | 129 | failure (dwfl, errnum, msg); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 130 | return errnum == -1 ? EIO : errnum; |
| 131 | } |
| 132 | |
| 133 | switch (key) |
| 134 | { |
| 135 | case OPT_DEBUGINFO: |
| 136 | debuginfo_path = arg; |
| 137 | break; |
| 138 | |
| 139 | case 'e': |
Roland McGrath | d17fac7 | 2005-08-23 08:20:21 +0000 | [diff] [blame] | 140 | { |
| 141 | Dwfl *dwfl = state->hook; |
| 142 | if (dwfl == NULL) |
| 143 | { |
| 144 | dwfl = INTUSE(dwfl_begin) (&offline_callbacks); |
| 145 | if (dwfl == NULL) |
Roland McGrath | 1656bc0 | 2005-12-23 01:49:50 +0000 | [diff] [blame] | 146 | return fail (dwfl, -1, arg); |
Roland McGrath | d17fac7 | 2005-08-23 08:20:21 +0000 | [diff] [blame] | 147 | state->hook = dwfl; |
Ulrich Drepper | aa915fd | 2007-02-05 07:25:33 +0000 | [diff] [blame] | 148 | |
| 149 | /* Start at zero so if there is just one -e foo.so, |
| 150 | the DSO is shown without address bias. */ |
| 151 | dwfl->offline_next_address = 0; |
Roland McGrath | d17fac7 | 2005-08-23 08:20:21 +0000 | [diff] [blame] | 152 | } |
| 153 | if (dwfl->callbacks == &offline_callbacks) |
| 154 | { |
| 155 | if (INTUSE(dwfl_report_offline) (dwfl, "", arg, -1) == NULL) |
Roland McGrath | 1656bc0 | 2005-12-23 01:49:50 +0000 | [diff] [blame] | 156 | return fail (dwfl, -1, arg); |
Roland McGrath | d17fac7 | 2005-08-23 08:20:21 +0000 | [diff] [blame] | 157 | state->hook = dwfl; |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | toomany: |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame^] | 162 | argp_error (state, "%s", |
| 163 | _("only one of -e, -p, -k, -K, or --core allowed")); |
Roland McGrath | d17fac7 | 2005-08-23 08:20:21 +0000 | [diff] [blame] | 164 | return EINVAL; |
| 165 | } |
| 166 | } |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 167 | break; |
| 168 | |
| 169 | case 'p': |
| 170 | if (state->hook == NULL) |
| 171 | { |
| 172 | Dwfl *dwfl = INTUSE(dwfl_begin) (&proc_callbacks); |
| 173 | int result = INTUSE(dwfl_linux_proc_report) (dwfl, atoi (arg)); |
| 174 | if (result != 0) |
Roland McGrath | 1656bc0 | 2005-12-23 01:49:50 +0000 | [diff] [blame] | 175 | return fail (dwfl, result, arg); |
| 176 | state->hook = dwfl; |
| 177 | } |
| 178 | else |
| 179 | goto toomany; |
| 180 | break; |
| 181 | |
| 182 | case 'M': |
| 183 | if (state->hook == NULL) |
| 184 | { |
| 185 | FILE *f = fopen (arg, "r"); |
| 186 | if (f == NULL) |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame^] | 187 | nofile: |
Roland McGrath | 1656bc0 | 2005-12-23 01:49:50 +0000 | [diff] [blame] | 188 | { |
| 189 | int code = errno; |
| 190 | argp_failure (state, EXIT_FAILURE, code, |
| 191 | "cannot open '%s'", arg); |
| 192 | return code; |
| 193 | } |
| 194 | Dwfl *dwfl = INTUSE(dwfl_begin) (&proc_callbacks); |
| 195 | int result = INTUSE(dwfl_linux_proc_maps_report) (dwfl, f); |
| 196 | fclose (f); |
| 197 | if (result != 0) |
| 198 | return fail (dwfl, result, arg); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 199 | state->hook = dwfl; |
| 200 | } |
| 201 | else |
| 202 | goto toomany; |
| 203 | break; |
| 204 | |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame^] | 205 | case OPT_COREFILE: |
| 206 | { |
| 207 | Dwfl *dwfl = state->hook; |
| 208 | if (dwfl == NULL) |
| 209 | state->hook = dwfl = INTUSE(dwfl_begin) (&offline_callbacks); |
| 210 | /* Permit -e and --core together. */ |
| 211 | else if (dwfl->callbacks != &offline_callbacks) |
| 212 | goto toomany; |
| 213 | |
| 214 | int fd = open64 (arg, O_RDONLY); |
| 215 | if (fd < 0) |
| 216 | goto nofile; |
| 217 | |
| 218 | Elf *core = elf_begin (fd, ELF_C_READ_MMAP_PRIVATE, NULL); |
| 219 | if (core == NULL) |
| 220 | { |
| 221 | close (fd); |
| 222 | argp_failure (state, EXIT_FAILURE, 0, |
| 223 | _("cannot read ELF core file: %s"), |
| 224 | elf_errmsg (-1)); |
| 225 | return EIO; |
| 226 | } |
| 227 | |
| 228 | GElf_Ehdr ehdr; |
| 229 | int result = INTUSE(dwfl_core_file_report) (dwfl, core, |
| 230 | gelf_getehdr (core, &ehdr)); |
| 231 | if (result < 0) |
| 232 | { |
| 233 | elf_end (core); |
| 234 | close (fd); |
| 235 | return fail (dwfl, result, arg); |
| 236 | } |
| 237 | |
| 238 | /* From now we leak FD and CORE. */ |
| 239 | |
| 240 | if (result == 0) |
| 241 | { |
| 242 | argp_failure (state, EXIT_FAILURE, 0, |
| 243 | _("No modules recognized in core file")); |
| 244 | return ENOENT; |
| 245 | } |
| 246 | } |
| 247 | break; |
| 248 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 249 | case 'k': |
| 250 | if (state->hook == NULL) |
| 251 | { |
| 252 | Dwfl *dwfl = INTUSE(dwfl_begin) (&kernel_callbacks); |
| 253 | int result = INTUSE(dwfl_linux_kernel_report_kernel) (dwfl); |
| 254 | if (result != 0) |
Roland McGrath | 1656bc0 | 2005-12-23 01:49:50 +0000 | [diff] [blame] | 255 | return fail (dwfl, result, _("cannot load kernel symbols")); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 256 | result = INTUSE(dwfl_linux_kernel_report_modules) (dwfl); |
| 257 | if (result != 0) |
| 258 | /* Non-fatal to have no modules since we do have the kernel. */ |
Roland McGrath | 1656bc0 | 2005-12-23 01:49:50 +0000 | [diff] [blame] | 259 | failure (dwfl, result, _("cannot find kernel modules")); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 260 | state->hook = dwfl; |
| 261 | } |
| 262 | else |
| 263 | goto toomany; |
| 264 | break; |
| 265 | |
Roland McGrath | d17fac7 | 2005-08-23 08:20:21 +0000 | [diff] [blame] | 266 | case 'K': |
| 267 | if (state->hook == NULL) |
| 268 | { |
| 269 | Dwfl *dwfl = INTUSE(dwfl_begin) (&offline_callbacks); |
| 270 | int result = INTUSE(dwfl_linux_kernel_report_offline) (dwfl, arg, |
| 271 | NULL); |
| 272 | if (result != 0) |
Roland McGrath | 1656bc0 | 2005-12-23 01:49:50 +0000 | [diff] [blame] | 273 | return fail (dwfl, result, _("cannot find kernel or modules")); |
Roland McGrath | d17fac7 | 2005-08-23 08:20:21 +0000 | [diff] [blame] | 274 | state->hook = dwfl; |
| 275 | } |
| 276 | else |
| 277 | goto toomany; |
| 278 | break; |
| 279 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 280 | case ARGP_KEY_SUCCESS: |
| 281 | { |
| 282 | Dwfl *dwfl = state->hook; |
| 283 | |
| 284 | if (dwfl == NULL) |
| 285 | { |
| 286 | /* Default if no -e, -p, or -k, is "-e a.out". */ |
| 287 | arg = "a.out"; |
Roland McGrath | d17fac7 | 2005-08-23 08:20:21 +0000 | [diff] [blame] | 288 | dwfl = INTUSE(dwfl_begin) (&offline_callbacks); |
| 289 | if (INTUSE(dwfl_report_offline) (dwfl, "", arg, -1) == NULL) |
Roland McGrath | 1656bc0 | 2005-12-23 01:49:50 +0000 | [diff] [blame] | 290 | return fail (dwfl, -1, arg); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 291 | state->hook = dwfl; |
| 292 | } |
| 293 | |
| 294 | /* One of the three flavors has done dwfl_begin and some reporting |
| 295 | if we got here. Tie up the Dwfl and return it to the caller of |
| 296 | argp_parse. */ |
| 297 | |
| 298 | int result = INTUSE(dwfl_report_end) (dwfl, NULL, NULL); |
| 299 | assert (result == 0); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 300 | } |
| 301 | break; |
| 302 | |
Roland McGrath | 4be1524 | 2007-04-25 03:09:33 +0000 | [diff] [blame] | 303 | case ARGP_KEY_ERROR: |
| 304 | dwfl_end (state->hook); |
| 305 | state->hook = NULL; |
| 306 | break; |
| 307 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 308 | default: |
| 309 | return ARGP_ERR_UNKNOWN; |
| 310 | } |
Roland McGrath | 4be1524 | 2007-04-25 03:09:33 +0000 | [diff] [blame] | 311 | |
| 312 | /* Update the input all along, so a parent parser can see it. */ |
| 313 | *(Dwfl **) state->input = state->hook; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static const struct argp libdwfl_argp = |
| 318 | { .options = options, .parser = parse_opt }; |
| 319 | |
| 320 | const struct argp * |
| 321 | dwfl_standard_argp (void) |
| 322 | { |
| 323 | return &libdwfl_argp; |
| 324 | } |
Roland McGrath | 80e43a7 | 2005-08-24 21:12:15 +0000 | [diff] [blame] | 325 | |
| 326 | #ifdef _MUDFLAP |
| 327 | /* In the absence of a mudflap wrapper for argp_parse, or a libc compiled |
| 328 | with -fmudflap, we'll see spurious errors for using the struct argp_state |
| 329 | on argp_parse's stack. */ |
| 330 | |
| 331 | void __attribute__ ((constructor)) |
| 332 | __libdwfl_argp_mudflap_options (void) |
| 333 | { |
| 334 | __mf_set_options ("-heur-stack-bound"); |
| 335 | } |
| 336 | #endif |