LCOV - code coverage report
Current view: top level - src - stack.c (source / functions) Hit Total Coverage
Test: elfutils-0.191 Lines: 233 343 67.9 %
Date: 2024-07-24 22:43:53 Functions: 9 10 90.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 141 244 57.8 %

           Branch data     Line data    Source code
       1                 :            : /* Unwinding of frames like gstack/pstack.
       2                 :            :    Copyright (C) 2013-2014 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 the GNU General Public License as published by
       7                 :            :    the Free Software Foundation; either version 3 of the License, or
       8                 :            :    (at your option) any later version.
       9                 :            : 
      10                 :            :    elfutils is distributed in the hope that it will be useful, but
      11                 :            :    WITHOUT ANY WARRANTY; without even the implied warranty of
      12                 :            :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13                 :            :    GNU General Public License for more details.
      14                 :            : 
      15                 :            :    You should have received a copy of the GNU General Public License
      16                 :            :    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
      17                 :            : 
      18                 :            : #include <config.h>
      19                 :            : #include <assert.h>
      20                 :            : #include <argp.h>
      21                 :            : #include <stdlib.h>
      22                 :            : #include <inttypes.h>
      23                 :            : #include <stdio.h>
      24                 :            : #include <stdio_ext.h>
      25                 :            : #include <string.h>
      26                 :            : #include <locale.h>
      27                 :            : #include <fcntl.h>
      28                 :            : #include ELFUTILS_HEADER(dwfl)
      29                 :            : 
      30                 :            : #include <dwarf.h>
      31                 :            : #include <system.h>
      32                 :            : #include <printversion.h>
      33                 :            : 
      34                 :            : /* Name and version of program.  */
      35                 :            : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
      36                 :            : 
      37                 :            : /* Bug report address.  */
      38                 :            : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
      39                 :            : 
      40                 :            : /* non-printable argp options.  */
      41                 :            : #define OPT_DEBUGINFO   0x100
      42                 :            : #define OPT_COREFILE    0x101
      43                 :            : 
      44                 :            : static bool show_activation = false;
      45                 :            : static bool show_module = false;
      46                 :            : static bool show_build_id = false;
      47                 :            : static bool show_source = false;
      48                 :            : static bool show_one_tid = false;
      49                 :            : static bool show_quiet = false;
      50                 :            : static bool show_raw = false;
      51                 :            : static bool show_modules = false;
      52                 :            : static bool show_debugname = false;
      53                 :            : static bool show_inlines = false;
      54                 :            : 
      55                 :            : static int maxframes = 256;
      56                 :            : 
      57                 :            : struct frame
      58                 :            : {
      59                 :            :   Dwarf_Addr pc;
      60                 :            :   bool isactivation;
      61                 :            : };
      62                 :            : 
      63                 :            : struct frames
      64                 :            : {
      65                 :            :   int frames;
      66                 :            :   int allocated;
      67                 :            :   struct frame *frame;
      68                 :            : };
      69                 :            : 
      70                 :            : static Dwfl *dwfl = NULL;
      71                 :            : static pid_t pid = 0;
      72                 :            : static int core_fd = -1;
      73                 :            : static Elf *core = NULL;
      74                 :            : static const char *exec = NULL;
      75                 :            : static char *debuginfo_path = NULL;
      76                 :            : static const char *sysroot = NULL;
      77                 :            : 
      78                 :            : static const Dwfl_Callbacks proc_callbacks =
      79                 :            :   {
      80                 :            :     .find_elf = dwfl_linux_proc_find_elf,
      81                 :            :     .find_debuginfo = dwfl_standard_find_debuginfo,
      82                 :            :     .debuginfo_path = &debuginfo_path,
      83                 :            :   };
      84                 :            : 
      85                 :            : static const Dwfl_Callbacks core_callbacks =
      86                 :            :   {
      87                 :            :     .find_elf = dwfl_build_id_find_elf,
      88                 :            :     .find_debuginfo = dwfl_standard_find_debuginfo,
      89                 :            :     .debuginfo_path = &debuginfo_path,
      90                 :            :   };
      91                 :            : 
      92                 :            : #ifdef USE_DEMANGLE
      93                 :            : static size_t demangle_buffer_len = 0;
      94                 :            : static char *demangle_buffer = NULL;
      95                 :            : #endif
      96                 :            : 
      97                 :            : /* Whether any frames have been shown at all.  Determines exit status.  */
      98                 :            : static bool frames_shown = false;
      99                 :            : 
     100                 :            : /* Program exit codes. All frames shown without any errors is GOOD.
     101                 :            :    Some frames shown with some non-fatal errors is an ERROR.  A fatal
     102                 :            :    error or no frames shown at all is BAD.  A command line USAGE exit
     103                 :            :    is generated by argp_error.  */
     104                 :            : #define EXIT_OK     0
     105                 :            : #define EXIT_ERROR  1
     106                 :            : #define EXIT_BAD    2
     107                 :            : #define EXIT_USAGE 64
     108                 :            : 
     109                 :            : static int
     110                 :        116 : get_addr_width (Dwfl_Module *mod)
     111                 :            : {
     112                 :            :   // Try to find the address wide if possible.
     113                 :        116 :   static int width = 0;
     114   [ +  +  +  - ]:        116 :   if (width == 0 && mod)
     115                 :            :     {
     116                 :         28 :       Dwarf_Addr bias;
     117                 :         28 :       Elf *elf = dwfl_module_getelf (mod, &bias);
     118         [ +  - ]:         28 :       if (elf)
     119                 :            :         {
     120                 :         28 :           GElf_Ehdr ehdr_mem;
     121                 :         28 :           GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
     122         [ +  - ]:         28 :           if (ehdr)
     123         [ +  - ]:         56 :             width = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16;
     124                 :            :         }
     125                 :            :     }
     126         [ -  + ]:        116 :   if (width == 0)
     127                 :          0 :     width = 16;
     128                 :            : 
     129                 :        116 :   return width;
     130                 :            : }
     131                 :            : 
     132                 :            : static int
     133                 :          0 : module_callback (Dwfl_Module *mod, void **userdata __attribute__((unused)),
     134                 :            :                  const char *name, Dwarf_Addr start,
     135                 :            :                  void *arg __attribute__((unused)))
     136                 :            : {
     137                 :            :   /* Forces resolving of main elf and debug files. */
     138                 :          0 :   Dwarf_Addr bias;
     139                 :          0 :   Elf *elf = dwfl_module_getelf (mod, &bias);
     140                 :          0 :   Dwarf *dwarf = dwfl_module_getdwarf (mod, &bias);
     141                 :            : 
     142                 :          0 :   Dwarf_Addr end;
     143                 :          0 :   const char *mainfile;
     144                 :          0 :   const char *debugfile;
     145                 :          0 :   const char *modname = dwfl_module_info (mod, NULL, NULL, &end, NULL,
     146                 :            :                                           NULL, &mainfile, &debugfile);
     147   [ #  #  #  # ]:          0 :   if (modname == NULL || strcmp (modname, name) != 0)
     148                 :            :     {
     149                 :          0 :       end = start + 1;
     150                 :          0 :       mainfile = NULL;
     151                 :          0 :       debugfile = NULL;
     152                 :            :     }
     153                 :            : 
     154                 :          0 :   int width = get_addr_width (mod);
     155                 :          0 :   printf ("0x%0*" PRIx64 "-0x%0*" PRIx64 " %s\n",
     156                 :            :           width, start, width, end, xbasename (name));
     157                 :            : 
     158                 :          0 :   const unsigned char *id;
     159                 :          0 :   GElf_Addr id_vaddr;
     160                 :          0 :   int id_len = dwfl_module_build_id (mod, &id, &id_vaddr);
     161         [ #  # ]:          0 :   if (id_len > 0)
     162                 :            :     {
     163                 :          0 :       printf ("  [");
     164                 :          0 :       do
     165                 :          0 :         printf ("%02" PRIx8, *id++);
     166         [ #  # ]:          0 :       while (--id_len > 0);
     167                 :          0 :       printf ("]\n");
     168                 :            :     }
     169                 :            : 
     170         [ #  # ]:          0 :   if (elf != NULL)
     171         [ #  # ]:          0 :     printf ("  %s\n", mainfile != NULL ? mainfile : "-");
     172         [ #  # ]:          0 :   if (dwarf != NULL)
     173         [ #  # ]:          0 :     printf ("  %s\n", debugfile != NULL ? debugfile : "-");
     174                 :            : 
     175                 :          0 :   return DWARF_CB_OK;
     176                 :            : }
     177                 :            : 
     178                 :            : static int
     179                 :         92 : frame_callback (Dwfl_Frame *state, void *arg)
     180                 :            : {
     181                 :         92 :   struct frames *frames = (struct frames *) arg;
     182                 :         92 :   int nr = frames->frames;
     183         [ +  - ]:         92 :   if (! dwfl_frame_pc (state, &frames->frame[nr].pc,
     184                 :         92 :                        &frames->frame[nr].isactivation))
     185                 :            :     return -1;
     186                 :            : 
     187                 :         92 :   frames->frames++;
     188         [ +  + ]:         92 :   if (frames->frames == maxframes)
     189                 :            :     return DWARF_CB_ABORT;
     190                 :            : 
     191         [ -  + ]:         78 :   if (frames->frames == frames->allocated)
     192                 :            :     {
     193                 :          0 :       frames->allocated *= 2;
     194                 :          0 :       frames->frame = realloc (frames->frame,
     195                 :          0 :                                sizeof (struct frame) * frames->allocated);
     196         [ #  # ]:          0 :       if (frames->frame == NULL)
     197                 :          0 :         error (EXIT_BAD, errno, "realloc frames.frame");
     198                 :            :     }
     199                 :            : 
     200                 :            :   return DWARF_CB_OK;
     201                 :            : }
     202                 :            : 
     203                 :            : static const char*
     204                 :         56 : die_name (Dwarf_Die *die)
     205                 :            : {
     206                 :         56 :   Dwarf_Attribute attr;
     207                 :         56 :   const char *name;
     208         [ +  - ]:        112 :   name = dwarf_formstring (dwarf_attr_integrate (die,
     209                 :            :                                                  DW_AT_MIPS_linkage_name,
     210                 :            :                                                  &attr)
     211                 :         56 :                            ?: dwarf_attr_integrate (die,
     212                 :            :                                                     DW_AT_linkage_name,
     213                 :            :                                                     &attr));
     214         [ +  + ]:         56 :   if (name == NULL)
     215                 :         48 :     name = dwarf_diename (die);
     216                 :            : 
     217                 :         56 :   return name;
     218                 :            : }
     219                 :            : 
     220                 :            : static void
     221                 :        116 : print_frame (int nr, Dwarf_Addr pc, bool isactivation,
     222                 :            :              Dwarf_Addr pc_adjusted, Dwfl_Module *mod,
     223                 :            :              const char *symname, Dwarf_Die *cudie,
     224                 :            :              Dwarf_Die *die)
     225                 :            : {
     226                 :        116 :   int width = get_addr_width (mod);
     227                 :        116 :   printf ("#%-2u 0x%0*" PRIx64, nr, width, (uint64_t) pc);
     228                 :            : 
     229         [ -  + ]:        116 :   if (show_activation)
     230         [ #  # ]:          0 :     printf ("%4s", ! isactivation ? "- 1" : "");
     231                 :            : 
     232         [ +  + ]:        116 :   if (symname != NULL)
     233                 :            :     {
     234                 :            : #ifdef USE_DEMANGLE
     235                 :            :       // Require GNU v3 ABI by the "_Z" prefix.
     236   [ +  +  +  +  :        114 :       if (! show_raw && symname[0] == '_' && symname[1] == 'Z')
                   +  + ]
     237                 :            :         {
     238                 :         10 :           int status = -1;
     239                 :         10 :           char *dsymname = __cxa_demangle (symname, demangle_buffer,
     240                 :            :                                            &demangle_buffer_len, &status);
     241         [ +  - ]:         10 :           if (status == 0)
     242                 :         10 :             symname = demangle_buffer = dsymname;
     243                 :            :         }
     244                 :            : #endif
     245                 :        114 :       printf (" %s", symname);
     246                 :            :     }
     247                 :            : 
     248                 :        116 :   const char* fname;
     249                 :        116 :   Dwarf_Addr start;
     250                 :        116 :   fname = dwfl_module_info(mod, NULL, &start,
     251                 :            :                            NULL, NULL, NULL, NULL, NULL);
     252         [ +  + ]:        116 :   if (show_module)
     253                 :            :     {
     254         [ +  - ]:          6 :       if (fname != NULL)
     255                 :          6 :         printf (" - %s", fname);
     256                 :            :     }
     257                 :            : 
     258         [ -  + ]:        116 :   if (show_build_id)
     259                 :            :     {
     260                 :          0 :       const unsigned char *id;
     261                 :          0 :       GElf_Addr id_vaddr;
     262                 :          0 :       int id_len = dwfl_module_build_id (mod, &id, &id_vaddr);
     263         [ #  # ]:          0 :       if (id_len > 0)
     264                 :            :         {
     265                 :          0 :           printf ("\n    [");
     266                 :          0 :           do
     267                 :          0 :             printf ("%02" PRIx8, *id++);
     268         [ #  # ]:          0 :           while (--id_len > 0);
     269                 :          0 :           printf ("]@0x%0" PRIx64 "+0x%" PRIx64,
     270                 :            :                   start, pc_adjusted - start);
     271                 :            :         }
     272                 :            :     }
     273                 :            : 
     274         [ +  + ]:        116 :   if (show_source)
     275                 :            :     {
     276                 :         36 :       int line, col;
     277                 :         36 :       const char* sname;
     278                 :         36 :       line = col = -1;
     279                 :         36 :       sname = NULL;
     280         [ +  + ]:         36 :       if (die != NULL)
     281                 :            :         {
     282                 :         16 :           Dwarf_Files *files;
     283         [ -  + ]:         16 :           if (dwarf_getsrcfiles (cudie, &files, NULL) == 0)
     284                 :            :             {
     285                 :         16 :               Dwarf_Attribute attr;
     286                 :         16 :               Dwarf_Word val;
     287         [ -  + ]:         16 :               if (dwarf_formudata (dwarf_attr (die, DW_AT_call_file, &attr),
     288                 :            :                                    &val) == 0)
     289                 :            :                 {
     290                 :         16 :                   sname = dwarf_filesrc (files, val, NULL, NULL);
     291         [ +  - ]:         16 :                   if (dwarf_formudata (dwarf_attr (die, DW_AT_call_line,
     292                 :            :                                                    &attr), &val) == 0)
     293                 :            :                     {
     294                 :         16 :                       line = val;
     295         [ -  + ]:         16 :                       if (dwarf_formudata (dwarf_attr (die, DW_AT_call_column,
     296                 :            :                                                        &attr), &val) == 0)
     297                 :          0 :                         col = val;
     298                 :            :                     }
     299                 :            :                 }
     300                 :            :             }
     301                 :            :         }
     302                 :            :       else
     303                 :            :         {
     304                 :         20 :           Dwfl_Line *lineobj = dwfl_module_getsrc(mod, pc_adjusted);
     305         [ +  - ]:         20 :           if (lineobj)
     306                 :         20 :             sname = dwfl_lineinfo (lineobj, NULL, &line, &col, NULL, NULL);
     307                 :            :         }
     308                 :            : 
     309         [ +  - ]:         36 :       if (sname != NULL)
     310                 :            :         {
     311                 :         36 :           printf ("\n    %s", sname);
     312         [ +  - ]:         36 :           if (line > 0)
     313                 :            :             {
     314                 :         36 :               printf (":%d", line);
     315         [ -  + ]:         36 :               if (col > 0)
     316                 :         36 :                 printf (":%d", col);
     317                 :            :             }
     318                 :            :         }
     319                 :            :     }
     320                 :        116 :   printf ("\n");
     321                 :        116 : }
     322                 :            : 
     323                 :            : static void
     324                 :         16 : print_inline_frames (int *nr, Dwarf_Addr pc, bool isactivation,
     325                 :            :                      Dwarf_Addr pc_adjusted, Dwfl_Module *mod,
     326                 :            :                      const char *symname, Dwarf_Die *cudie, Dwarf_Die *die)
     327                 :            : {
     328                 :         16 :   Dwarf_Die *scopes = NULL;
     329                 :         16 :   int nscopes = dwarf_getscopes_die (die, &scopes);
     330         [ +  - ]:         16 :   if (nscopes > 0)
     331                 :            :     {
     332                 :            :       /* scopes[0] == die, the lowest level, for which we already have
     333                 :            :          the name.  This is the actual source location where it
     334                 :            :          happened.  */
     335                 :         16 :       print_frame ((*nr)++, pc, isactivation, pc_adjusted, mod, symname,
     336                 :            :                    NULL, NULL);
     337                 :            : 
     338                 :            :       /* last_scope is the source location where the next frame/function
     339                 :            :          call was done. */
     340                 :         16 :       Dwarf_Die *last_scope = &scopes[0];
     341   [ +  -  -  +  :         56 :       for (int i = 1; i < nscopes && (maxframes == 0 || *nr < maxframes); i++)
                   +  + ]
     342                 :            :         {
     343                 :         48 :           Dwarf_Die *scope = &scopes[i];
     344                 :         48 :           int tag = dwarf_tag (scope);
     345                 :         48 :           if (tag != DW_TAG_inlined_subroutine
     346         [ +  + ]:         48 :               && tag != DW_TAG_entry_point
     347         [ +  + ]:         24 :               && tag != DW_TAG_subprogram)
     348                 :         16 :             continue;
     349                 :            : 
     350                 :         32 :           symname = die_name (scope);
     351                 :         32 :           print_frame ((*nr)++, pc, isactivation, pc_adjusted, mod, symname,
     352                 :            :                        cudie, last_scope);
     353                 :            : 
     354                 :            :           /* Found the "top-level" in which everything was inlined?  */
     355         [ +  + ]:         32 :           if (tag == DW_TAG_subprogram)
     356                 :            :             break;
     357                 :            : 
     358                 :            :           last_scope = scope;
     359                 :            :         }
     360                 :            :     }
     361                 :         16 :   free (scopes);
     362                 :         16 : }
     363                 :            : 
     364                 :            : static void
     365                 :         28 : print_frames (struct frames *frames, pid_t tid, int dwflerr, const char *what)
     366                 :            : {
     367         [ +  - ]:         28 :   if (frames->frames > 0)
     368                 :         28 :     frames_shown = true;
     369                 :            : 
     370                 :         28 :   printf ("TID %lld:\n", (long long) tid);
     371                 :         28 :   int frame_nr = 0;
     372   [ +  +  -  + ]:        112 :   for (int nr = 0; nr < frames->frames && (maxframes == 0
     373         [ +  + ]:        176 :                                            || frame_nr < maxframes); nr++)
     374                 :            :     {
     375                 :         84 :       Dwarf_Addr pc = frames->frame[nr].pc;
     376                 :         84 :       bool isactivation = frames->frame[nr].isactivation;
     377                 :         84 :       Dwarf_Addr pc_adjusted = pc - (isactivation ? 0 : 1);
     378                 :            : 
     379                 :            :       /* Get PC->SYMNAME.  */
     380                 :         84 :       Dwfl_Module *mod = dwfl_addrmodule (dwfl, pc_adjusted);
     381                 :         84 :       const char *symname = NULL;
     382                 :         84 :       Dwarf_Die die_mem;
     383                 :         84 :       Dwarf_Die *die = NULL;
     384                 :         84 :       Dwarf_Die *cudie = NULL;
     385   [ +  -  +  - ]:         84 :       if (mod && ! show_quiet)
     386                 :            :         {
     387         [ +  + ]:         84 :           if (show_debugname)
     388                 :            :             {
     389                 :         24 :               Dwarf_Addr bias = 0;
     390                 :         24 :               Dwarf_Die *scopes = NULL;
     391                 :         24 :               cudie = dwfl_module_addrdie (mod, pc_adjusted, &bias);
     392                 :         24 :               int nscopes = dwarf_getscopes (cudie, pc_adjusted - bias,
     393                 :            :                                              &scopes);
     394                 :            : 
     395                 :            :               /* Find the first function-like DIE with a name in scope.  */
     396         [ +  + ]:         48 :               for (int i = 0; symname == NULL && i < nscopes; i++)
     397                 :            :                 {
     398                 :         24 :                   Dwarf_Die *scope = &scopes[i];
     399                 :         24 :                   int tag = dwarf_tag (scope);
     400                 :         24 :                   if (tag == DW_TAG_subprogram
     401         [ -  + ]:         24 :                       || tag == DW_TAG_inlined_subroutine
     402         [ #  # ]:          0 :                       || tag == DW_TAG_entry_point)
     403                 :         24 :                     symname = die_name (scope);
     404                 :            : 
     405         [ +  - ]:         24 :                   if (symname != NULL)
     406                 :            :                     {
     407                 :         24 :                       die_mem = *scope;
     408                 :         24 :                       die = &die_mem;
     409                 :            :                     }
     410                 :            :                 }
     411                 :         24 :               free (scopes);
     412                 :            :             }
     413                 :            : 
     414         [ +  + ]:         84 :           if (symname == NULL)
     415                 :         60 :             symname = dwfl_module_addrname (mod, pc_adjusted);
     416                 :            :         }
     417                 :            : 
     418   [ +  +  +  - ]:         84 :       if (show_inlines && die != NULL)
     419                 :         16 :         print_inline_frames (&frame_nr, pc, isactivation, pc_adjusted, mod,
     420                 :            :                              symname, cudie, die);
     421                 :            :       else
     422                 :         68 :         print_frame (frame_nr++, pc, isactivation, pc_adjusted, mod, symname,
     423                 :            :                      NULL, NULL);
     424                 :            :     }
     425                 :            : 
     426   [ +  -  +  + ]:         28 :   if (frames->frames > 0 && frame_nr == maxframes)
     427                 :         22 :     error (0, 0, "tid %lld: shown max number of frames "
     428                 :            :            "(%d, use -n 0 for unlimited)", (long long) tid, maxframes);
     429         [ +  + ]:          6 :   else if (dwflerr != 0)
     430                 :            :     {
     431         [ +  - ]:          2 :       if (frames->frames > 0)
     432                 :            :         {
     433                 :          2 :           unsigned nr = frames->frames - 1;
     434                 :          2 :           Dwarf_Addr pc = frames->frame[nr].pc;
     435                 :          2 :           bool isactivation = frames->frame[nr].isactivation;
     436                 :          2 :           Dwarf_Addr pc_adjusted = pc - (isactivation ? 0 : 1);
     437                 :          2 :           Dwfl_Module *mod = dwfl_addrmodule (dwfl, pc_adjusted);
     438                 :          2 :           const char *mainfile = NULL;
     439                 :          2 :           const char *modname = dwfl_module_info (mod, NULL, NULL, NULL, NULL,
     440                 :            :                                                   NULL, &mainfile, NULL);
     441   [ +  -  -  + ]:          2 :           if (modname == NULL || modname[0] == '\0')
     442                 :            :             {
     443         [ #  # ]:          0 :               if (mainfile != NULL)
     444                 :          0 :                 modname = mainfile;
     445                 :            :               else
     446                 :            :                 modname = "<unknown>";
     447                 :            :             }
     448                 :          2 :           error (0, 0, "%s tid %lld at 0x%" PRIx64 " in %s: %s", what,
     449                 :            :                  (long long) tid, pc_adjusted, modname, dwfl_errmsg (dwflerr));
     450                 :            :         }
     451                 :            :       else
     452                 :          0 :         error (0, 0, "%s tid %lld: %s", what, (long long) tid,
     453                 :            :                dwfl_errmsg (dwflerr));
     454                 :            :     }
     455                 :         28 : }
     456                 :            : 
     457                 :            : static int
     458                 :         28 : thread_callback (Dwfl_Thread *thread, void *thread_arg)
     459                 :            : {
     460                 :         28 :   struct frames *frames = (struct frames *) thread_arg;
     461                 :         28 :   pid_t tid = dwfl_thread_tid (thread);
     462                 :         28 :   int err = 0;
     463                 :         28 :   frames->frames = 0;
     464      [ +  -  + ]:         28 :   switch (dwfl_thread_getframes (thread, frame_callback, thread_arg))
     465                 :            :     {
     466                 :            :     case DWARF_CB_OK:
     467                 :            :     case DWARF_CB_ABORT:
     468                 :            :       break;
     469                 :         10 :     case -1:
     470                 :         10 :       err = dwfl_errno ();
     471                 :         10 :       break;
     472                 :          0 :     default:
     473                 :          0 :       abort ();
     474                 :            :     }
     475                 :         28 :   print_frames (frames, tid, err, "dwfl_thread_getframes");
     476                 :         28 :   return DWARF_CB_OK;
     477                 :            : }
     478                 :            : 
     479                 :            : static error_t
     480                 :        248 : parse_opt (int key, char *arg __attribute__ ((unused)),
     481                 :            :            struct argp_state *state)
     482                 :            : {
     483   [ +  +  +  -  :        248 :   switch (key)
          +  +  -  +  +  
          -  -  -  +  -  
             +  -  +  +  
                      + ]
     484                 :            :     {
     485                 :            :     case 'p':
     486                 :          2 :       pid = atoi (arg);
     487         [ -  + ]:          2 :       if (pid == 0)
     488                 :          0 :         argp_error (state, N_("-p PID should be a positive process id."));
     489                 :            :       break;
     490                 :            : 
     491                 :            :     case OPT_COREFILE:
     492                 :         26 :       core_fd = open (arg, O_RDONLY);
     493         [ -  + ]:         26 :       if (core_fd < 0)
     494                 :          0 :         error (EXIT_BAD, errno, N_("Cannot open core file '%s'"), arg);
     495                 :         26 :       elf_version (EV_CURRENT);
     496                 :         26 :       core = elf_begin (core_fd, ELF_C_READ_MMAP, NULL);
     497         [ -  + ]:         26 :       if (core == NULL)
     498                 :          0 :         error (EXIT_BAD, 0, "core '%s' elf_begin: %s", arg, elf_errmsg(-1));
     499                 :            :       break;
     500                 :            : 
     501                 :         24 :     case 'e':
     502                 :         24 :       exec = arg;
     503                 :         24 :       break;
     504                 :            : 
     505                 :          0 :     case OPT_DEBUGINFO:
     506                 :          0 :       debuginfo_path = arg;
     507                 :          0 :       break;
     508                 :            : 
     509                 :          2 :     case 'm':
     510                 :          2 :       show_module = true;
     511                 :          2 :       break;
     512                 :            : 
     513                 :         10 :     case 's':
     514                 :         10 :       show_source = true;
     515                 :         10 :       break;
     516                 :            : 
     517                 :          0 :     case 'a':
     518                 :          0 :       show_activation = true;
     519                 :          0 :       break;
     520                 :            : 
     521                 :          4 :     case 'd':
     522                 :          4 :       show_debugname = true;
     523                 :          4 :       break;
     524                 :            : 
     525                 :          8 :     case 'i':
     526                 :          8 :       show_inlines = show_debugname = true;
     527                 :          8 :       break;
     528                 :            : 
     529                 :          0 :     case 'v':
     530                 :          0 :       show_activation = show_source = show_module = show_debugname = true;
     531                 :          0 :       show_inlines = true;
     532                 :          0 :       break;
     533                 :            : 
     534                 :          0 :     case 'b':
     535                 :          0 :       show_build_id = true;
     536                 :          0 :       break;
     537                 :            : 
     538                 :          0 :     case 'q':
     539                 :          0 :       show_quiet = true;
     540                 :          0 :       break;
     541                 :            : 
     542                 :          8 :     case 'r':
     543                 :          8 :       show_raw = true;
     544                 :          8 :       break;
     545                 :            : 
     546                 :          0 :     case '1':
     547                 :          0 :       show_one_tid = true;
     548                 :          0 :       break;
     549                 :            : 
     550                 :            :     case 'n':
     551                 :         22 :       maxframes = atoi (arg);
     552         [ -  + ]:         22 :       if (maxframes < 0)
     553                 :            :         {
     554                 :          0 :           argp_error (state, N_("-n MAXFRAMES should be 0 or higher."));
     555                 :          0 :           return EINVAL;
     556                 :            :         }
     557                 :            :       break;
     558                 :            : 
     559                 :          0 :     case 'l':
     560                 :          0 :       show_modules = true;
     561                 :          0 :       break;
     562                 :            : 
     563                 :          2 :     case 'S':
     564                 :          2 :       sysroot = arg;
     565                 :          2 :       break;
     566                 :            : 
     567                 :         28 :     case ARGP_KEY_END:
     568   [ +  +  -  + ]:         28 :       if (core == NULL && exec != NULL)
     569                 :          0 :         argp_error (state,
     570                 :            :                     N_("-e EXEC needs a core given by --core."));
     571                 :            : 
     572   [ +  +  -  + ]:         28 :       if (pid == 0 && show_one_tid == true)
     573                 :          0 :         argp_error (state,
     574                 :            :                     N_("-1 needs a thread id given by -p."));
     575                 :            : 
     576   [ +  +  +  -  :         28 :       if ((pid == 0 && core == NULL) || (pid != 0 && core != NULL))
             +  +  -  + ]
     577                 :          0 :         argp_error (state,
     578                 :            :                     N_("One of -p PID or --core COREFILE should be given."));
     579                 :            : 
     580         [ +  + ]:         28 :       if (pid != 0)
     581                 :            :         {
     582                 :          2 :           dwfl = dwfl_begin (&proc_callbacks);
     583         [ -  + ]:          2 :           if (dwfl == NULL)
     584                 :          0 :             error (EXIT_BAD, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
     585                 :            : 
     586                 :          2 :           int err = dwfl_linux_proc_report (dwfl, pid);
     587         [ -  + ]:          2 :           if (err < 0)
     588                 :          0 :             error (EXIT_BAD, 0, "dwfl_linux_proc_report pid %lld: %s",
     589                 :            :                    (long long) pid, dwfl_errmsg (-1));
     590         [ -  + ]:          2 :           else if (err > 0)
     591                 :          0 :             error (EXIT_BAD, err, "dwfl_linux_proc_report pid %lld",
     592                 :            :                    (long long) pid);
     593                 :            :         }
     594                 :            : 
     595         [ +  + ]:         28 :       if (core != NULL)
     596                 :            :         {
     597                 :         26 :           dwfl = dwfl_begin (&core_callbacks);
     598         [ -  + ]:         26 :           if (dwfl == NULL)
     599                 :          0 :             error (EXIT_BAD, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
     600   [ +  +  -  + ]:         26 :           if (sysroot && dwfl_set_sysroot (dwfl, sysroot) < 0)
     601                 :          0 :             error (EXIT_BAD, 0, "dwfl_set_sysroot: %m");
     602         [ -  + ]:         26 :           if (dwfl_core_file_report (dwfl, core, exec) < 0)
     603                 :          0 :             error (EXIT_BAD, 0, "dwfl_core_file_report: %s", dwfl_errmsg (-1));
     604                 :            :         }
     605                 :            : 
     606         [ -  + ]:         28 :       if (dwfl_report_end (dwfl, NULL, NULL) != 0)
     607                 :          0 :         error (EXIT_BAD, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));
     608                 :            : 
     609         [ +  + ]:         28 :       if (pid != 0)
     610                 :            :         {
     611                 :          2 :           int err = dwfl_linux_proc_attach (dwfl, pid, false);
     612         [ -  + ]:          2 :           if (err < 0)
     613                 :          0 :             error (EXIT_BAD, 0, "dwfl_linux_proc_attach pid %lld: %s",
     614                 :            :                    (long long) pid, dwfl_errmsg (-1));
     615         [ -  + ]:          2 :           else if (err > 0)
     616                 :          0 :             error (EXIT_BAD, err, "dwfl_linux_proc_attach pid %lld",
     617                 :            :                    (long long) pid);
     618                 :            :         }
     619                 :            : 
     620         [ +  + ]:         28 :       if (core != NULL)
     621                 :            :         {
     622         [ -  + ]:         26 :           if (dwfl_core_file_attach (dwfl, core) < 0)
     623                 :          0 :             error (EXIT_BAD, 0, "dwfl_core_file_attach: %s", dwfl_errmsg (-1));
     624                 :            :         }
     625                 :            : 
     626                 :            :       /* Makes sure we are properly attached.  */
     627         [ -  + ]:         28 :       if (dwfl_pid (dwfl) < 0)
     628                 :          0 :         error (EXIT_BAD, 0, "dwfl_pid: %s\n", dwfl_errmsg (-1));
     629                 :            :       break;
     630                 :            : 
     631                 :            :     default:
     632                 :            :       return ARGP_ERR_UNKNOWN;
     633                 :            :     }
     634                 :            :   return 0;
     635                 :            : }
     636                 :            : 
     637                 :            : int
     638                 :         28 : main (int argc, char **argv)
     639                 :            : {
     640                 :            :   /* We use no threads here which can interfere with handling a stream.  */
     641                 :         28 :   __fsetlocking (stdin, FSETLOCKING_BYCALLER);
     642                 :         28 :   __fsetlocking (stdout, FSETLOCKING_BYCALLER);
     643                 :         28 :   __fsetlocking (stderr, FSETLOCKING_BYCALLER);
     644                 :            : 
     645                 :            :   /* Set locale.  */
     646                 :         28 :   (void) setlocale (LC_ALL, "");
     647                 :            : 
     648                 :         28 :   const struct argp_option options[] =
     649                 :            :     {
     650                 :            :       { NULL, 0, NULL, 0, N_("Input selection options:"), 0 },
     651                 :            :       { "pid", 'p', "PID", 0,
     652                 :            :         N_("Show stack of process PID"), 0 },
     653                 :            :       { "core", OPT_COREFILE, "COREFILE", 0,
     654                 :            :         N_("Show stack found in COREFILE"), 0 },
     655                 :            :       {  "executable", 'e', "EXEC", 0, N_("(optional) EXECUTABLE that produced COREFILE"), 0 },
     656                 :            :       { "debuginfo-path", OPT_DEBUGINFO, "PATH", 0,
     657                 :            :         N_("Search path for separate debuginfo files"), 0 },
     658                 :            : 
     659                 :            :       { NULL, 0, NULL, 0, N_("Output selection options:"), 0 },
     660                 :            :       { "activation",  'a', NULL, 0,
     661                 :            :         N_("Additionally show frame activation"), 0 },
     662                 :            :       { "debugname",  'd', NULL, 0,
     663                 :            :         N_("Additionally try to lookup DWARF debuginfo name for frame address"),
     664                 :            :         0 },
     665                 :            :       { "inlines",  'i', NULL, 0,
     666                 :            :         N_("Additionally show inlined function frames using DWARF debuginfo if available (implies -d)"), 0 },
     667                 :            :       { "module",  'm', NULL, 0,
     668                 :            :         N_("Additionally show module file information"), 0 },
     669                 :            :       { "source",  's', NULL, 0,
     670                 :            :         N_("Additionally show source file information"), 0 },
     671                 :            :       { "verbose", 'v', NULL, 0,
     672                 :            :         N_("Show all additional information (activation, debugname, inlines, module and source)"), 0 },
     673                 :            :       { "quiet", 'q', NULL, 0,
     674                 :            :         N_("Do not resolve address to function symbol name"), 0 },
     675                 :            :       { "raw", 'r', NULL, 0,
     676                 :            :         N_("Show raw function symbol names, do not try to demangle names"), 0 },
     677                 :            :       { "build-id",  'b', NULL, 0,
     678                 :            :         N_("Show module build-id, load address and pc offset"), 0 },
     679                 :            :       { NULL, '1', NULL, 0,
     680                 :            :         N_("Show the backtrace of only one thread"), 0 },
     681                 :            :       { NULL, 'n', "MAXFRAMES", 0,
     682                 :            :         N_("Show at most MAXFRAMES per thread (default 256, use 0 for unlimited)"), 0 },
     683                 :            :       { "list-modules", 'l', NULL, 0,
     684                 :            :         N_("Show module memory map with build-id, elf and debug files detected"), 0 },
     685                 :            :       { "sysroot", 'S', "sysroot", 0,
     686                 :            :         N_("Set the sysroot to search for libraries referenced from the core file"), 0 },
     687                 :            :       { NULL, 0, NULL, 0, NULL, 0 }
     688                 :            :     };
     689                 :            : 
     690                 :         28 :   const struct argp argp =
     691                 :            :     {
     692                 :            :       .options = options,
     693                 :            :       .parser = parse_opt,
     694                 :            :       .doc = N_("Print a stack for each thread in a process or core file.\n\
     695                 :            : \n\
     696                 :            : Program exits with return code 0 if all frames were shown without \
     697                 :            : any errors.  If some frames were shown, but there were some non-fatal \
     698                 :            : errors, possibly causing an incomplete backtrace, the program exits \
     699                 :            : with return code 1.  If no frames could be shown, or a fatal error \
     700                 :            : occurred the program exits with return code 2.  If the program was \
     701                 :            : invoked with bad or missing arguments it will exit with return code 64.")
     702                 :            :     };
     703                 :            : 
     704                 :         28 :   argp_parse (&argp, argc, argv, 0, NULL, NULL);
     705                 :            : 
     706         [ -  + ]:         28 :   if (show_modules)
     707                 :            :     {
     708                 :          0 :       printf ("PID %lld - %s module memory map\n", (long long) dwfl_pid (dwfl),
     709         [ #  # ]:          0 :               pid != 0 ? "process" : "core");
     710         [ #  # ]:          0 :       if (dwfl_getmodules (dwfl, module_callback, NULL, 0) != 0)
     711                 :          0 :         error (EXIT_BAD, 0, "dwfl_getmodules: %s", dwfl_errmsg (-1));
     712                 :            :     }
     713                 :            : 
     714                 :         28 :   struct frames frames;
     715                 :            :   /* When maxframes is zero, then 2048 is just the initial allocation
     716                 :            :      that will be increased using realloc in framecallback ().  */
     717         [ +  - ]:         28 :   frames.allocated = maxframes == 0 ? 2048 : maxframes;
     718                 :         28 :   frames.frames = 0;
     719                 :         28 :   frames.frame = malloc (sizeof (struct frame) * frames.allocated);
     720         [ -  + ]:         28 :   if (frames.frame == NULL)
     721                 :          0 :     error (EXIT_BAD, errno, "malloc frames.frame");
     722                 :            : 
     723         [ -  + ]:         28 :   if (show_one_tid)
     724                 :            :     {
     725                 :          0 :       int err = 0;
     726      [ #  #  # ]:          0 :       switch (dwfl_getthread_frames (dwfl, pid, frame_callback, &frames))
     727                 :            :         {
     728                 :            :         case DWARF_CB_OK:
     729                 :            :         case DWARF_CB_ABORT:
     730                 :            :           break;
     731                 :          0 :         case -1:
     732                 :          0 :           err = dwfl_errno ();
     733                 :          0 :           break;
     734                 :          0 :         default:
     735                 :          0 :           abort ();
     736                 :            :         }
     737                 :          0 :       print_frames (&frames, pid, err, "dwfl_getthread_frames");
     738                 :            :     }
     739                 :            :   else
     740                 :            :     {
     741                 :         28 :       printf ("PID %lld - %s\n", (long long) dwfl_pid (dwfl),
     742         [ +  + ]:         28 :               pid != 0 ? "process" : "core");
     743      [ -  -  + ]:         28 :       switch (dwfl_getthreads (dwfl, thread_callback, &frames))
     744                 :            :         {
     745                 :            :         case DWARF_CB_OK:
     746                 :            :         case DWARF_CB_ABORT:
     747                 :            :           break;
     748                 :          0 :         case -1:
     749                 :          0 :           error (0, 0, "dwfl_getthreads: %s", dwfl_errmsg (-1));
     750                 :            :           break;
     751                 :          0 :         default:
     752                 :          0 :           abort ();
     753                 :            :         }
     754                 :            :     }
     755                 :         28 :   free (frames.frame);
     756                 :         28 :   dwfl_end (dwfl);
     757                 :            : 
     758         [ +  + ]:         28 :   if (core != NULL)
     759                 :         26 :     elf_end (core);
     760                 :            : 
     761         [ +  + ]:         28 :   if (core_fd != -1)
     762                 :         26 :     close (core_fd);
     763                 :            : 
     764                 :            : #ifdef USE_DEMANGLE
     765                 :         28 :   free (demangle_buffer);
     766                 :            : #endif
     767                 :            : 
     768         [ -  + ]:         28 :   if (! frames_shown)
     769                 :          0 :     error (EXIT_BAD, 0, N_("Couldn't show any frames."));
     770                 :            : 
     771                 :         28 :   return error_message_count != 0 ? EXIT_ERROR : EXIT_OK;
     772                 :            : }

Generated by: LCOV version 1.16