LCOV - code coverage report
Current view: top level - libdwfl - dwfl_frame.c (source / functions) Hit Total Coverage
Test: elfutils-0.183 Lines: 127 203 62.6 %
Date: 2021-02-07 19:08:58 Functions: 10 14 71.4 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 57 120 47.5 %

           Branch data     Line data    Source code
       1                 :            : /* Get Dwarf Frame state for target PID or core file.
       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 either
       7                 :            : 
       8                 :            :      * the GNU Lesser General Public License as published by the Free
       9                 :            :        Software Foundation; either version 3 of the License, or (at
      10                 :            :        your option) any later version
      11                 :            : 
      12                 :            :    or
      13                 :            : 
      14                 :            :      * the GNU General Public License as published by the Free
      15                 :            :        Software Foundation; either version 2 of the License, or (at
      16                 :            :        your option) any later version
      17                 :            : 
      18                 :            :    or both in parallel, as here.
      19                 :            : 
      20                 :            :    elfutils is distributed in the hope that it will be useful, but
      21                 :            :    WITHOUT ANY WARRANTY; without even the implied warranty of
      22                 :            :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      23                 :            :    General Public License for more details.
      24                 :            : 
      25                 :            :    You should have received copies of the GNU General Public License and
      26                 :            :    the GNU Lesser General Public License along with this program.  If
      27                 :            :    not, see <http://www.gnu.org/licenses/>.  */
      28                 :            : 
      29                 :            : #ifdef HAVE_CONFIG_H
      30                 :            : # include <config.h>
      31                 :            : #endif
      32                 :            : 
      33                 :            : #include "libdwflP.h"
      34                 :            : #include <unistd.h>
      35                 :            : 
      36                 :            : /* Set STATE->pc_set from STATE->regs according to the backend.  Return true on
      37                 :            :    success, false on error.  */
      38                 :            : static bool
      39                 :         47 : state_fetch_pc (Dwfl_Frame *state)
      40                 :            : {
      41   [ -  +  -  + ]:         47 :   switch (state->pc_state)
      42                 :            :     {
      43                 :            :     case DWFL_FRAME_STATE_PC_SET:
      44                 :            :       return true;
      45                 :          0 :     case DWFL_FRAME_STATE_PC_UNDEFINED:
      46                 :          0 :       abort ();
      47                 :         33 :     case DWFL_FRAME_STATE_ERROR:
      48                 :          0 :       {
      49                 :         33 :         Ebl *ebl = state->thread->process->ebl;
      50                 :         33 :         Dwarf_CIE abi_info;
      51         [ -  + ]:         33 :         if (ebl_abi_cfi (ebl, &abi_info) != 0)
      52                 :            :           {
      53                 :          0 :             __libdwfl_seterrno (DWFL_E_LIBEBL);
      54                 :          0 :             return false;
      55                 :            :           }
      56                 :         33 :         unsigned ra = abi_info.return_address_register;
      57                 :            :         /* dwarf_frame_state_reg_is_set is not applied here.  */
      58         [ -  + ]:         33 :         if (ra >= ebl_frame_nregs (ebl))
      59                 :            :           {
      60                 :          0 :             __libdwfl_seterrno (DWFL_E_LIBEBL_BAD);
      61                 :          0 :             return false;
      62                 :            :           }
      63                 :         33 :         state->pc = state->regs[ra] + ebl_ra_offset (ebl);
      64                 :         33 :         state->pc_state = DWFL_FRAME_STATE_PC_SET;
      65                 :            :       }
      66                 :         33 :       return true;
      67                 :            :     }
      68                 :          0 :   abort ();
      69                 :            : }
      70                 :            : 
      71                 :            : /* Do not call it on your own, to be used by thread_* functions only.  */
      72                 :            : 
      73                 :            : static void
      74                 :            : free_states (Dwfl_Frame *state)
      75                 :            : {
      76   [ #  #  #  # ]:         82 :   while (state)
         [ +  + ][ -  + ]
                 [ +  + ]
      77                 :            :     {
      78                 :         36 :       Dwfl_Frame *next = state->unwound;
      79                 :         36 :       free(state);
      80                 :         36 :       state = next;
      81                 :            :     }
      82                 :            : }
      83                 :            : 
      84                 :            : static Dwfl_Frame *
      85                 :         47 : state_alloc (Dwfl_Thread *thread)
      86                 :            : {
      87         [ -  + ]:         47 :   assert (thread->unwound == NULL);
      88                 :         47 :   Ebl *ebl = thread->process->ebl;
      89                 :         47 :   size_t nregs = ebl_frame_nregs (ebl);
      90         [ +  - ]:         47 :   if (nregs == 0)
      91                 :            :     return NULL;
      92         [ -  + ]:         47 :   assert (nregs < sizeof (((Dwfl_Frame *) NULL)->regs_set) * 8);
      93                 :         47 :   Dwfl_Frame *state = malloc (sizeof (*state) + sizeof (*state->regs) * nregs);
      94         [ +  - ]:         47 :   if (state == NULL)
      95                 :            :     return NULL;
      96                 :         47 :   state->thread = thread;
      97                 :         47 :   state->signal_frame = false;
      98                 :         47 :   state->initial_frame = true;
      99                 :         47 :   state->pc_state = DWFL_FRAME_STATE_ERROR;
     100                 :         47 :   memset (state->regs_set, 0, sizeof (state->regs_set));
     101                 :         47 :   thread->unwound = state;
     102                 :         47 :   state->unwound = NULL;
     103                 :         47 :   return state;
     104                 :            : }
     105                 :            : 
     106                 :            : void
     107                 :            : internal_function
     108                 :         48 : __libdwfl_process_free (Dwfl_Process *process)
     109                 :            : {
     110                 :         48 :   Dwfl *dwfl = process->dwfl;
     111         [ +  + ]:         48 :   if (process->callbacks->detach != NULL)
     112                 :         47 :     process->callbacks->detach (dwfl, process->callbacks_arg);
     113         [ -  + ]:         48 :   assert (dwfl->process == process);
     114                 :         48 :   dwfl->process = NULL;
     115         [ +  + ]:         48 :   if (process->ebl_close)
     116                 :         47 :     ebl_closebackend (process->ebl);
     117                 :         48 :   free (process);
     118                 :         48 :   dwfl->attacherr = DWFL_E_NOERROR;
     119                 :         48 : }
     120                 :            : 
     121                 :            : /* Allocate new Dwfl_Process for DWFL.  */
     122                 :            : static void
     123                 :            : process_alloc (Dwfl *dwfl)
     124                 :            : {
     125                 :         49 :   Dwfl_Process *process = malloc (sizeof (*process));
     126                 :         49 :   if (process == NULL)
     127                 :            :     return;
     128                 :         49 :   process->dwfl = dwfl;
     129                 :         49 :   dwfl->process = process;
     130                 :            : }
     131                 :            : 
     132                 :            : bool
     133                 :         49 : dwfl_attach_state (Dwfl *dwfl, Elf *elf, pid_t pid,
     134                 :            :                    const Dwfl_Thread_Callbacks *thread_callbacks, void *arg)
     135                 :            : {
     136         [ -  + ]:         49 :   if (dwfl->process != NULL)
     137                 :            :     {
     138                 :          0 :       __libdwfl_seterrno (DWFL_E_ATTACH_STATE_CONFLICT);
     139                 :          0 :       return false;
     140                 :            :     }
     141                 :            : 
     142                 :            :   /* Reset any previous error, we are just going to try again.  */
     143                 :         49 :   dwfl->attacherr = DWFL_E_NOERROR;
     144                 :            :   /* thread_callbacks is declared NN */
     145         [ +  - ]:         49 :   if (thread_callbacks->next_thread == NULL
     146         [ -  + ]:         49 :       || thread_callbacks->set_initial_registers == NULL)
     147                 :            :     {
     148                 :          0 :       dwfl->attacherr = DWFL_E_INVALID_ARGUMENT;
     149                 :          0 :     fail:
     150                 :          0 :       dwfl->attacherr = __libdwfl_canon_error (dwfl->attacherr);
     151                 :          0 :       __libdwfl_seterrno (dwfl->attacherr);
     152                 :          0 :       return false;
     153                 :            :     }
     154                 :            : 
     155                 :         49 :   Ebl *ebl;
     156                 :         49 :   bool ebl_close;
     157         [ +  + ]:         49 :   if (elf != NULL)
     158                 :            :     {
     159                 :         48 :       ebl = ebl_openbackend (elf);
     160                 :         48 :       ebl_close = true;
     161                 :            :     }
     162                 :            :   else
     163                 :            :     {
     164                 :          1 :       ebl = NULL;
     165         [ +  - ]:          1 :       for (Dwfl_Module *mod = dwfl->modulelist; mod != NULL; mod = mod->next)
     166                 :            :         {
     167                 :            :           /* Reading of the vDSO or (deleted) modules may fail as
     168                 :            :              /proc/PID/mem is unreadable without PTRACE_ATTACH and
     169                 :            :              we may not be PTRACE_ATTACH-ed now.  MOD would not be
     170                 :            :              re-read later to unwind it when we are already
     171                 :            :              PTRACE_ATTACH-ed to PID.  This happens when this function
     172                 :            :              is called from dwfl_linux_proc_attach with elf == NULL.
     173                 :            :              __libdwfl_module_getebl will call __libdwfl_getelf which
     174                 :            :              will call the find_elf callback.  */
     175         [ +  - ]:          1 :           if (strncmp (mod->name, "[vdso: ", 7) == 0
     176 [ +  - ][ +  - ]:          2 :               || strcmp (strrchr (mod->name, ' ') ?: "",
     177                 :            :                          " (deleted)") == 0)
     178                 :            :             continue;
     179                 :          1 :           Dwfl_Error error = __libdwfl_module_getebl (mod);
     180         [ +  - ]:          1 :           if (error != DWFL_E_NOERROR)
     181                 :            :             continue;
     182                 :          1 :           ebl = mod->ebl;
     183                 :          1 :           break;
     184                 :            :         }
     185                 :            :       ebl_close = false;
     186                 :            :     }
     187         [ -  + ]:         49 :   if (ebl == NULL)
     188                 :            :     {
     189                 :            :       /* Not identified EBL from any of the modules.  */
     190                 :          0 :       dwfl->attacherr = DWFL_E_PROCESS_NO_ARCH;
     191                 :          0 :       goto fail;
     192                 :            :     }
     193         [ +  - ]:         49 :   process_alloc (dwfl);
     194                 :         49 :   Dwfl_Process *process = dwfl->process;
     195         [ -  + ]:         49 :   if (process == NULL)
     196                 :            :     {
     197         [ #  # ]:          0 :       if (ebl_close)
     198                 :          0 :         ebl_closebackend (ebl);
     199                 :          0 :       dwfl->attacherr = DWFL_E_NOMEM;
     200                 :          0 :       goto fail;
     201                 :            :     }
     202                 :         49 :   process->ebl = ebl;
     203                 :         49 :   process->ebl_close = ebl_close;
     204                 :         49 :   process->pid = pid;
     205                 :         49 :   process->callbacks = thread_callbacks;
     206                 :         49 :   process->callbacks_arg = arg;
     207                 :         49 :   return true;
     208                 :            : }
     209                 :            : INTDEF(dwfl_attach_state)
     210                 :            : 
     211                 :            : pid_t
     212                 :         62 : dwfl_pid (Dwfl *dwfl)
     213                 :            : {
     214         [ -  + ]:         62 :   if (dwfl->attacherr != DWFL_E_NOERROR)
     215                 :            :     {
     216                 :          0 :       __libdwfl_seterrno (dwfl->attacherr);
     217                 :          0 :       return -1;
     218                 :            :     }
     219                 :            : 
     220         [ -  + ]:         62 :   if (dwfl->process == NULL)
     221                 :            :     {
     222                 :          0 :       __libdwfl_seterrno (DWFL_E_NO_ATTACH_STATE);
     223                 :          0 :       return -1;
     224                 :            :     }
     225                 :         62 :   return dwfl->process->pid;
     226                 :            : }
     227                 :            : INTDEF(dwfl_pid)
     228                 :            : 
     229                 :            : Dwfl *
     230                 :        198 : dwfl_thread_dwfl (Dwfl_Thread *thread)
     231                 :            : {
     232                 :        198 :   return thread->process->dwfl;
     233                 :            : }
     234                 :            : INTDEF(dwfl_thread_dwfl)
     235                 :            : 
     236                 :            : pid_t
     237                 :        287 : dwfl_thread_tid (Dwfl_Thread *thread)
     238                 :            : {
     239                 :        287 :   return thread->tid;
     240                 :            : }
     241                 :            : INTDEF(dwfl_thread_tid)
     242                 :            : 
     243                 :            : Dwfl_Thread *
     244                 :        197 : dwfl_frame_thread (Dwfl_Frame *state)
     245                 :            : {
     246                 :        197 :   return state->thread;
     247                 :            : }
     248                 :            : INTDEF(dwfl_frame_thread)
     249                 :            : 
     250                 :            : int
     251                 :         32 : dwfl_getthreads (Dwfl *dwfl, int (*callback) (Dwfl_Thread *thread, void *arg),
     252                 :            :                  void *arg)
     253                 :            : {
     254         [ -  + ]:         32 :   if (dwfl->attacherr != DWFL_E_NOERROR)
     255                 :            :     {
     256                 :          0 :       __libdwfl_seterrno (dwfl->attacherr);
     257                 :          0 :       return -1;
     258                 :            :     }
     259                 :            : 
     260                 :         32 :   Dwfl_Process *process = dwfl->process;
     261         [ -  + ]:         32 :   if (process == NULL)
     262                 :            :     {
     263                 :          0 :       __libdwfl_seterrno (DWFL_E_NO_ATTACH_STATE);
     264                 :          0 :       return -1;
     265                 :            :     }
     266                 :            : 
     267                 :         32 :   Dwfl_Thread thread;
     268                 :         32 :   thread.process = process;
     269                 :         32 :   thread.unwound = NULL;
     270                 :         32 :   thread.callbacks_arg = NULL;
     271                 :         81 :   for (;;)
     272                 :            :     {
     273                 :         81 :       thread.tid = process->callbacks->next_thread (dwfl,
     274                 :            :                                                     process->callbacks_arg,
     275                 :            :                                                     &thread.callbacks_arg);
     276         [ +  - ]:         81 :       if (thread.tid < 0)
     277                 :            :         return -1;
     278         [ +  + ]:         81 :       if (thread.tid == 0)
     279                 :            :         {
     280                 :         31 :           __libdwfl_seterrno (DWFL_E_NOERROR);
     281                 :         31 :           return 0;
     282                 :            :         }
     283                 :         50 :       int err = callback (&thread, arg);
     284         [ -  + ]:         49 :       if (err != DWARF_CB_OK)
     285                 :          0 :         return err;
     286         [ +  - ]:         49 :       assert (thread.unwound == NULL);
     287                 :            :     }
     288                 :            :   /* NOTREACHED */
     289                 :            : }
     290                 :            : INTDEF(dwfl_getthreads)
     291                 :            : 
     292                 :            : struct one_arg
     293                 :            : {
     294                 :            :   pid_t tid;
     295                 :            :   bool seen;
     296                 :            :   int (*callback) (Dwfl_Thread *thread, void *arg);
     297                 :            :   void *arg;
     298                 :            :   int ret;
     299                 :            : };
     300                 :            : 
     301                 :            : static int
     302                 :          0 : get_one_thread_cb (Dwfl_Thread *thread, void *arg)
     303                 :            : {
     304                 :          0 :   struct one_arg *oa = (struct one_arg *) arg;
     305   [ #  #  #  # ]:          0 :   if (! oa->seen && INTUSE(dwfl_thread_tid) (thread) == oa->tid)
     306                 :            :     {
     307                 :          0 :       oa->seen = true;
     308                 :          0 :       oa->ret = oa->callback (thread, oa->arg);
     309                 :          0 :       return DWARF_CB_ABORT;
     310                 :            :     }
     311                 :            : 
     312                 :            :   return DWARF_CB_OK;
     313                 :            : }
     314                 :            : 
     315                 :            : /* Note not currently exported, will be when there are more Dwfl_Thread
     316                 :            :    properties to query.  Use dwfl_getthread_frames for now directly.  */
     317                 :            : static int
     318                 :          0 : getthread (Dwfl *dwfl, pid_t tid,
     319                 :            :            int (*callback) (Dwfl_Thread *thread, void *arg),
     320                 :            :            void *arg)
     321                 :            : {
     322         [ #  # ]:          0 :   if (dwfl->attacherr != DWFL_E_NOERROR)
     323                 :            :     {
     324                 :          0 :       __libdwfl_seterrno (dwfl->attacherr);
     325                 :          0 :       return -1;
     326                 :            :     }
     327                 :            : 
     328                 :          0 :   Dwfl_Process *process = dwfl->process;
     329         [ #  # ]:          0 :   if (process == NULL)
     330                 :            :     {
     331                 :          0 :       __libdwfl_seterrno (DWFL_E_NO_ATTACH_STATE);
     332                 :          0 :       return -1;
     333                 :            :     }
     334                 :            : 
     335         [ #  # ]:          0 :   if (process->callbacks->get_thread != NULL)
     336                 :            :     {
     337                 :          0 :       Dwfl_Thread thread;
     338                 :          0 :       thread.process = process;
     339                 :          0 :       thread.unwound = NULL;
     340                 :          0 :       thread.callbacks_arg = NULL;
     341                 :            : 
     342         [ #  # ]:          0 :       if (process->callbacks->get_thread (dwfl, tid, process->callbacks_arg,
     343                 :            :                                           &thread.callbacks_arg))
     344                 :            :         {
     345                 :          0 :           thread.tid = tid;
     346                 :          0 :           return callback (&thread, arg);
     347                 :            :         }
     348                 :            : 
     349                 :            :       return -1;
     350                 :            :     }
     351                 :            : 
     352                 :          0 :    struct one_arg oa = { .tid = tid, .callback = callback,
     353                 :            :                          .arg = arg, .seen = false };
     354                 :          0 :    int err = INTUSE(dwfl_getthreads) (dwfl, get_one_thread_cb, &oa);
     355                 :            : 
     356   [ #  #  #  # ]:          0 :    if (err == DWARF_CB_ABORT && oa.seen)
     357                 :          0 :      return oa.ret;
     358                 :            : 
     359   [ #  #  #  # ]:          0 :    if (err == DWARF_CB_OK && ! oa.seen)
     360                 :            :      {
     361                 :          0 :         errno = ESRCH;
     362                 :          0 :         __libdwfl_seterrno (DWFL_E_ERRNO);
     363                 :          0 :         return -1;
     364                 :            :      }
     365                 :            : 
     366                 :            :    return err;
     367                 :            : }
     368                 :            : 
     369                 :            : struct one_thread
     370                 :            : {
     371                 :            :   int (*callback) (Dwfl_Frame *frame, void *arg);
     372                 :            :   void *arg;
     373                 :            : };
     374                 :            : 
     375                 :            : static int
     376                 :          0 : get_one_thread_frames_cb (Dwfl_Thread *thread, void *arg)
     377                 :            : {
     378                 :          0 :   struct one_thread *ot = (struct one_thread *) arg;
     379                 :          0 :   return INTUSE(dwfl_thread_getframes) (thread, ot->callback, ot->arg);
     380                 :            : }
     381                 :            : 
     382                 :            : int
     383                 :          0 : dwfl_getthread_frames (Dwfl *dwfl, pid_t tid,
     384                 :            :                        int (*callback) (Dwfl_Frame *frame, void *arg),
     385                 :            :                        void *arg)
     386                 :            : {
     387                 :          0 :   struct one_thread ot = { .callback = callback, .arg = arg };
     388                 :          0 :   return getthread (dwfl, tid, get_one_thread_frames_cb, &ot);
     389                 :            : }
     390                 :            : INTDEF(dwfl_getthread_frames)
     391                 :            : 
     392                 :            : int
     393                 :         47 : dwfl_thread_getframes (Dwfl_Thread *thread,
     394                 :            :                        int (*callback) (Dwfl_Frame *state, void *arg),
     395                 :            :                        void *arg)
     396                 :            : {
     397                 :         47 :   Ebl *ebl = thread->process->ebl;
     398         [ -  + ]:         47 :   if (ebl_frame_nregs (ebl) == 0)
     399                 :            :     {
     400                 :          0 :       __libdwfl_seterrno (DWFL_E_NO_UNWIND);
     401                 :          0 :       return -1;
     402                 :            :     }
     403         [ -  + ]:         47 :   if (state_alloc (thread) == NULL)
     404                 :            :     {
     405                 :          0 :       __libdwfl_seterrno (DWFL_E_NOMEM);
     406                 :          0 :       return -1;
     407                 :            :     }
     408                 :         47 :   Dwfl_Process *process = thread->process;
     409         [ -  + ]:         47 :   if (! process->callbacks->set_initial_registers (thread,
     410                 :            :                                                    thread->callbacks_arg))
     411                 :            :     {
     412                 :          0 :       free_states (thread->unwound);
     413                 :          0 :       thread->unwound = NULL;
     414                 :          0 :       return -1;
     415                 :            :     }
     416                 :         47 :   Dwfl_Frame *state = thread->unwound;
     417                 :         47 :   thread->unwound = NULL;
     418         [ -  + ]:         47 :   if (! state_fetch_pc (state))
     419                 :            :     {
     420         [ #  # ]:          0 :       if (process->callbacks->thread_detach)
     421                 :          0 :         process->callbacks->thread_detach (thread, thread->callbacks_arg);
     422                 :            :       free_states (state);
     423                 :            :       return -1;
     424                 :            :     }
     425                 :        232 :   do
     426                 :            :     {
     427                 :        232 :       int err = callback (state, arg);
     428         [ +  + ]:        231 :       if (err != DWARF_CB_OK)
     429                 :            :         {
     430         [ -  + ]:          7 :           if (process->callbacks->thread_detach)
     431                 :          0 :             process->callbacks->thread_detach (thread, thread->callbacks_arg);
     432                 :            :           free_states (state);
     433                 :            :           return err;
     434                 :            :         }
     435                 :        224 :       __libdwfl_frame_unwind (state);
     436                 :        224 :       Dwfl_Frame *next = state->unwound;
     437                 :            :       /* The old frame is no longer needed.  */
     438                 :        224 :       free (state);
     439                 :        224 :       state = next;
     440                 :            :     }
     441 [ +  + ][ +  + ]:        224 :   while (state && state->pc_state == DWFL_FRAME_STATE_PC_SET);
     442                 :            : 
     443                 :         39 :   Dwfl_Error err = dwfl_errno ();
     444         [ +  + ]:         39 :   if (process->callbacks->thread_detach)
     445                 :          5 :     process->callbacks->thread_detach (thread, thread->callbacks_arg);
     446 [ +  + ][ -  + ]:         39 :   if (state == NULL || state->pc_state == DWFL_FRAME_STATE_ERROR)
     447                 :            :     {
     448                 :         17 :       free_states (state);
     449                 :         17 :       __libdwfl_seterrno (err);
     450                 :         17 :       return -1;
     451                 :            :     }
     452         [ -  + ]:         22 :   assert (state->pc_state == DWFL_FRAME_STATE_PC_UNDEFINED);
     453                 :            :   free_states (state);
     454                 :            :   return 0;
     455                 :            : }
     456                 :            : INTDEF(dwfl_thread_getframes)

Generated by: LCOV version 1.13