LCOV - code coverage report
Current view: top level - libdwfl - argp-std.c (source / functions) Hit Total Coverage
Test: elfutils-0.183 Lines: 88 150 58.7 %
Date: 2021-02-07 19:08:58 Functions: 2 3 66.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 39 87 44.8 %

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

Generated by: LCOV version 1.13