LCOV - code coverage report
Current view: top level - libdwfl - argp-std.c (source / functions) Hit Total Coverage
Test: lcov.out Lines: 79 135 58.5 %
Date: 2017-01-05 09:15:16 Functions: 2 3 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Standard argp argument parsers for tools using libdwfl.
       2             :    Copyright (C) 2005-2010, 2012, 2015 Red Hat, Inc.
       3             :    This file is part of elfutils.
       4             : 
       5             :    This file is free software; you can redistribute it and/or modify
       6             :    it under the terms of either
       7             : 
       8             :      * the GNU Lesser General Public License as published by the Free
       9             :        Software Foundation; either version 3 of the License, or (at
      10             :        your option) any later version
      11             : 
      12             :    or
      13             : 
      14             :      * the GNU General Public License as published by the Free
      15             :        Software Foundation; either version 2 of the License, or (at
      16             :        your option) any later version
      17             : 
      18             :    or both in parallel, as here.
      19             : 
      20             :    elfutils is distributed in the hope that it will be useful, but
      21             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      22             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      23             :    General Public License for more details.
      24             : 
      25             :    You should have received copies of the GNU General Public License and
      26             :    the GNU Lesser General Public License along with this program.  If
      27             :    not, see <http://www.gnu.org/licenses/>.  */
      28             : 
      29             : #include "libdwflP.h"
      30             : #include <argp.h>
      31             : #include <stdlib.h>
      32             : #include <assert.h>
      33             : #include <libintl.h>
      34             : #include <fcntl.h>
      35             : #include <unistd.h>
      36             : 
      37             : /* gettext helper macros.  */
      38             : #define _(Str) dgettext ("elfutils", Str)
      39             : 
      40             : 
      41             : #define OPT_DEBUGINFO   0x100
      42             : #define OPT_COREFILE    0x101
      43             : 
      44             : static const struct argp_option options[] =
      45             : {
      46             :   { NULL, 0, NULL, 0, N_("Input selection options:"), 0 },
      47             :   { "executable", 'e', "FILE", 0, N_("Find addresses in FILE"), 0 },
      48             :   { "core", OPT_COREFILE, "COREFILE", 0,
      49             :     N_("Find addresses from signatures found in COREFILE"), 0 },
      50             :   { "pid", 'p', "PID", 0,
      51             :     N_("Find addresses in files mapped into process PID"), 0 },
      52             :   { "linux-process-map", 'M', "FILE", 0,
      53             :     N_("Find addresses in files mapped as read from FILE"
      54             :        " in Linux /proc/PID/maps format"), 0 },
      55             :   { "kernel", 'k', NULL, 0, N_("Find addresses in the running kernel"), 0 },
      56             :   { "offline-kernel", 'K', "RELEASE", OPTION_ARG_OPTIONAL,
      57             :     N_("Kernel with all modules"), 0 },
      58             :   { "debuginfo-path", OPT_DEBUGINFO, "PATH", 0,
      59             :     N_("Search path for separate debuginfo files"), 0 },
      60             :   { NULL, 0, NULL, 0, NULL, 0 }
      61             : };
      62             : 
      63             : static char *debuginfo_path;
      64             : 
      65             : static const Dwfl_Callbacks offline_callbacks =
      66             :   {
      67             :     .find_debuginfo = INTUSE(dwfl_standard_find_debuginfo),
      68             :     .debuginfo_path = &debuginfo_path,
      69             : 
      70             :     .section_address = INTUSE(dwfl_offline_section_address),
      71             : 
      72             :     /* We use this table for core files too.  */
      73             :     .find_elf = INTUSE(dwfl_build_id_find_elf),
      74             :   };
      75             : 
      76             : static const Dwfl_Callbacks proc_callbacks =
      77             :   {
      78             :     .find_debuginfo = INTUSE(dwfl_standard_find_debuginfo),
      79             :     .debuginfo_path = &debuginfo_path,
      80             : 
      81             :     .find_elf = INTUSE(dwfl_linux_proc_find_elf),
      82             :   };
      83             : 
      84             : static const Dwfl_Callbacks kernel_callbacks =
      85             :   {
      86             :     .find_debuginfo = INTUSE(dwfl_standard_find_debuginfo),
      87             :     .debuginfo_path = &debuginfo_path,
      88             : 
      89             :     .find_elf = INTUSE(dwfl_linux_kernel_find_elf),
      90             :     .section_address = INTUSE(dwfl_linux_kernel_module_section_address),
      91             :   };
      92             : 
      93             : /* Structure held at state->HOOK.  */
      94             : struct parse_opt
      95             : {
      96             :   Dwfl *dwfl;
      97             :   /* The -e|--executable parameter.  */
      98             :   const char *e;
      99             :   /* The --core parameter.  */
     100             :   const char *core;
     101             : };
     102             : 
     103             : static error_t
     104        1055 : parse_opt (int key, char *arg, struct argp_state *state)
     105             : {
     106           0 :   inline void failure (Dwfl *dwfl, int errnum, const char *msg)
     107             :     {
     108           0 :       if (dwfl != NULL)
     109           0 :         dwfl_end (dwfl);
     110           0 :       if (errnum == -1)
     111           0 :         argp_failure (state, EXIT_FAILURE, 0, "%s: %s",
     112             :                       msg, INTUSE(dwfl_errmsg) (-1));
     113             :       else
     114           0 :         argp_failure (state, EXIT_FAILURE, errnum, "%s", msg);
     115           0 :     }
     116             :   inline error_t fail (Dwfl *dwfl, int errnum, const char *msg)
     117             :     {
     118           0 :       failure (dwfl, errnum, msg);
     119           0 :       return errnum == -1 ? EIO : errnum;
     120             :     }
     121             : 
     122        1055 :   switch (key)
     123             :     {
     124             :     case ARGP_KEY_INIT:
     125             :       {
     126         183 :         assert (state->hook == NULL);
     127         183 :         struct parse_opt *opt = calloc (1, sizeof (*opt));
     128         183 :         if (opt == NULL)
     129           0 :           failure (NULL, DWFL_E_ERRNO, "calloc");
     130         183 :         state->hook = opt;
     131             :       }
     132         183 :       break;
     133             : 
     134             :     case OPT_DEBUGINFO:
     135           1 :       debuginfo_path = arg;
     136           1 :       break;
     137             : 
     138             :     case 'e':
     139             :       {
     140         151 :         struct parse_opt *opt = state->hook;
     141         151 :         Dwfl *dwfl = opt->dwfl;
     142         151 :         if (dwfl == NULL)
     143             :           {
     144         150 :             dwfl = INTUSE(dwfl_begin) (&offline_callbacks);
     145         150 :             if (dwfl == NULL)
     146           0 :               return fail (dwfl, -1, arg);
     147         150 :             opt->dwfl = dwfl;
     148             : 
     149             :             /* Start at zero so if there is just one -e foo.so,
     150             :                the DSO is shown without address bias.  */
     151         150 :             dwfl->offline_next_address = 0;
     152             :           }
     153         151 :         if (dwfl->callbacks != &offline_callbacks)
     154             :           {
     155             :           toomany:
     156           0 :             argp_error (state, "%s",
     157             :                         _("only one of -e, -p, -k, -K, or --core allowed"));
     158           0 :             return EINVAL;
     159             :           }
     160         151 :         opt->e = arg;
     161             :       }
     162         151 :       break;
     163             : 
     164             :     case 'p':
     165             :       {
     166           4 :         struct parse_opt *opt = state->hook;
     167           4 :         if (opt->dwfl == NULL)
     168             :           {
     169           4 :             Dwfl *dwfl = INTUSE(dwfl_begin) (&proc_callbacks);
     170           4 :             int result = INTUSE(dwfl_linux_proc_report) (dwfl, atoi (arg));
     171           4 :             if (result != 0)
     172             :               return fail (dwfl, result, arg);
     173             : 
     174             :             /* Non-fatal to not be able to attach to process, ignore error.  */
     175           4 :             INTUSE(dwfl_linux_proc_attach) (dwfl, atoi (arg), false);
     176             : 
     177           4 :             opt->dwfl = dwfl;
     178             :           }
     179             :         else
     180             :           goto toomany;
     181             :       }
     182           4 :       break;
     183             : 
     184             :     case 'M':
     185             :       {
     186           6 :         struct parse_opt *opt = state->hook;
     187           6 :         if (opt->dwfl == NULL)
     188             :           {
     189           6 :             FILE *f = fopen (arg, "r");
     190           6 :             if (f == NULL)
     191             :               {
     192           0 :                 int code = errno;
     193           0 :                 argp_failure (state, EXIT_FAILURE, code,
     194             :                               "cannot open '%s'", arg);
     195           0 :                 return code;
     196             :               }
     197           6 :             Dwfl *dwfl = INTUSE(dwfl_begin) (&proc_callbacks);
     198           6 :             int result = INTUSE(dwfl_linux_proc_maps_report) (dwfl, f);
     199           6 :             fclose (f);
     200           6 :             if (result != 0)
     201             :               return fail (dwfl, result, arg);
     202           6 :             opt->dwfl = dwfl;
     203             :           }
     204             :         else
     205             :           goto toomany;
     206             :       }
     207           6 :       break;
     208             : 
     209             :     case OPT_COREFILE:
     210             :       {
     211          18 :         struct parse_opt *opt = state->hook;
     212          18 :         Dwfl *dwfl = opt->dwfl;
     213          18 :         if (dwfl == NULL)
     214           5 :           opt->dwfl = dwfl = INTUSE(dwfl_begin) (&offline_callbacks);
     215             :         /* Permit -e and --core together.  */
     216          13 :         else if (dwfl->callbacks != &offline_callbacks)
     217             :           goto toomany;
     218          18 :         opt->core = arg;
     219             :       }
     220          18 :       break;
     221             : 
     222             :     case 'k':
     223             :       {
     224           0 :         struct parse_opt *opt = state->hook;
     225           0 :         if (opt->dwfl == NULL)
     226             :           {
     227           0 :             Dwfl *dwfl = INTUSE(dwfl_begin) (&kernel_callbacks);
     228           0 :             int result = INTUSE(dwfl_linux_kernel_report_kernel) (dwfl);
     229           0 :             if (result != 0)
     230           0 :               return fail (dwfl, result, _("cannot load kernel symbols"));
     231           0 :             result = INTUSE(dwfl_linux_kernel_report_modules) (dwfl);
     232           0 :             if (result != 0)
     233             :               /* Non-fatal to have no modules since we do have the kernel.  */
     234           0 :               failure (dwfl, result, _("cannot find kernel modules"));
     235           0 :             opt->dwfl = dwfl;
     236             :           }
     237             :         else
     238             :           goto toomany;
     239             :       }
     240           0 :       break;
     241             : 
     242             :     case 'K':
     243             :       {
     244           0 :         struct parse_opt *opt = state->hook;
     245           0 :         if (opt->dwfl == NULL)
     246             :           {
     247           0 :             Dwfl *dwfl = INTUSE(dwfl_begin) (&offline_callbacks);
     248           0 :             int result = INTUSE(dwfl_linux_kernel_report_offline) (dwfl, arg,
     249             :                                                                    NULL);
     250           0 :             if (result != 0)
     251           0 :               return fail (dwfl, result, _("cannot find kernel or modules"));
     252           0 :             opt->dwfl = dwfl;
     253             :           }
     254             :         else
     255             :           goto toomany;
     256             :       }
     257           0 :       break;
     258             : 
     259             :     case ARGP_KEY_SUCCESS:
     260             :       {
     261         165 :         struct parse_opt *opt = state->hook;
     262         165 :         Dwfl *dwfl = opt->dwfl;
     263             : 
     264         165 :         if (dwfl == NULL)
     265             :           {
     266             :             /* Default if no -e, -p, or -k, is "-e a.out".  */
     267           0 :             arg = "a.out";
     268           0 :             dwfl = INTUSE(dwfl_begin) (&offline_callbacks);
     269           0 :             if (INTUSE(dwfl_report_offline) (dwfl, "", arg, -1) == NULL)
     270           0 :               return fail (dwfl, -1, arg);
     271           0 :             opt->dwfl = dwfl;
     272             :           }
     273             : 
     274         165 :         if (opt->core)
     275             :           {
     276          18 :             int fd = open (opt->core, O_RDONLY);
     277          18 :             if (fd < 0)
     278             :               {
     279           0 :                 int code = errno;
     280           0 :                 argp_failure (state, EXIT_FAILURE, code,
     281             :                               "cannot open '%s'", opt->core);
     282           0 :                 return code;
     283             :               }
     284             : 
     285             :             Elf *core;
     286          18 :             Dwfl_Error error = __libdw_open_file (&fd, &core, true, false);
     287          18 :             if (error != DWFL_E_NOERROR)
     288             :               {
     289           0 :                 argp_failure (state, EXIT_FAILURE, 0,
     290           0 :                               _("cannot read ELF core file: %s"),
     291             :                               INTUSE(dwfl_errmsg) (error));
     292           0 :                 return error == DWFL_E_ERRNO ? errno : EIO;
     293             :               }
     294             : 
     295          18 :             int result = INTUSE(dwfl_core_file_report) (dwfl, core, opt->e);
     296          18 :             if (result < 0)
     297             :               {
     298           0 :                 elf_end (core);
     299           0 :                 close (fd);
     300           0 :                 return fail (dwfl, result, opt->core);
     301             :               }
     302             : 
     303             :             /* Non-fatal to not be able to attach to core, ignore error.  */
     304          18 :             INTUSE(dwfl_core_file_attach) (dwfl, core);
     305             : 
     306             :             /* Store core Elf and fd in Dwfl to expose with dwfl_end.  */
     307          18 :             if (dwfl->user_core == NULL)
     308             :               {
     309           4 :                 dwfl->user_core = calloc (1, sizeof (struct Dwfl_User_Core));
     310           4 :                 if (dwfl->user_core == NULL)
     311             :                   {
     312           0 :                     argp_failure (state, EXIT_FAILURE, 0,
     313           0 :                                   _("Not enough memory"));
     314           0 :                     return ENOMEM;
     315             :                   }
     316             :               }
     317          18 :             dwfl->user_core->core = core;
     318          18 :             dwfl->user_core->fd = fd;
     319             : 
     320          18 :             if (result == 0)
     321             :               {
     322           0 :                 argp_failure (state, EXIT_FAILURE, 0,
     323           0 :                               _("No modules recognized in core file"));
     324           0 :                 return ENOENT;
     325             :               }
     326             :           }
     327         147 :         else if (opt->e)
     328             :           {
     329         137 :             if (INTUSE(dwfl_report_offline) (dwfl, "", opt->e, -1) == NULL)
     330           0 :               return fail (dwfl, -1, opt->e);
     331             :           }
     332             : 
     333             :         /* One of the three flavors has done dwfl_begin and some reporting
     334             :            if we got here.  Tie up the Dwfl and return it to the caller of
     335             :            argp_parse.  */
     336             : 
     337         165 :         int result = INTUSE(dwfl_report_end) (dwfl, NULL, NULL);
     338         165 :         assert (result == 0);
     339             : 
     340             :         /* Update the input all along, so a parent parser can see it.
     341             :            As we free OPT the update below will be no longer active.  */
     342         165 :         *(Dwfl **) state->input = dwfl;
     343         165 :         free (opt);
     344         165 :         state->hook = NULL;
     345             :       }
     346         165 :       break;
     347             : 
     348             :     case ARGP_KEY_ERROR:
     349             :       {
     350          16 :         struct parse_opt *opt = state->hook;
     351          16 :         dwfl_end (opt->dwfl);
     352          16 :         free (opt);
     353          16 :         state->hook = NULL;
     354             :       }
     355          16 :       break;
     356             : 
     357             :     default:
     358             :       return ARGP_ERR_UNKNOWN;
     359             :     }
     360             : 
     361             :   /* Update the input all along, so a parent parser can see it.  */
     362         544 :   struct parse_opt *opt = state->hook;
     363         544 :   if (opt)
     364         363 :     *(Dwfl **) state->input = opt->dwfl;
     365             : 
     366             :   return 0;
     367             : }
     368             : 
     369             : static const struct argp libdwfl_argp =
     370             :   { .options = options, .parser = parse_opt };
     371             : 
     372             : const struct argp *
     373         183 : dwfl_standard_argp (void)
     374             : {
     375         183 :   return &libdwfl_argp;
     376             : }

Generated by: LCOV version 1.12