LCOV - code coverage report
Current view: top level - src - stack.c (source / functions) Coverage Total Hit
Test: elfutils-0.193 Lines: 68.3 % 350 239
Test Date: 2025-08-30 14:31:09 Functions: 90.0 % 10 9
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 58.4 % 245 143

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

Generated by: LCOV version 2.0-1