LCOV - code coverage report
Current view: top level - libdwfl - linux-core-attach.c (source / functions) Hit Total Coverage
Test: elfutils-0.175 Lines: 170 193 88.1 %
Date: 2018-11-16 13:02:39 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Get Dwarf Frame state for target 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 <fcntl.h>
      35             : #include "system.h"
      36             : 
      37             : #include "../libdw/memory-access.h"
      38             : 
      39             : struct core_arg
      40             : {
      41             :   Elf *core;
      42             :   Elf_Data *note_data;
      43             :   size_t thread_note_offset;
      44             :   Ebl *ebl;
      45             : };
      46             : 
      47             : struct thread_arg
      48             : {
      49             :   struct core_arg *core_arg;
      50             :   size_t note_offset;
      51             : };
      52             : 
      53             : static bool
      54         626 : core_memory_read (Dwfl *dwfl, Dwarf_Addr addr, Dwarf_Word *result,
      55             :                   void *dwfl_arg)
      56             : {
      57         626 :   Dwfl_Process *process = dwfl->process;
      58         626 :   struct core_arg *core_arg = dwfl_arg;
      59         626 :   Elf *core = core_arg->core;
      60         626 :   assert (core != NULL);
      61             :   static size_t phnum;
      62         626 :   if (elf_getphdrnum (core, &phnum) < 0)
      63             :     {
      64           0 :       __libdwfl_seterrno (DWFL_E_LIBELF);
      65           0 :       return false;
      66             :     }
      67       12246 :   for (size_t cnt = 0; cnt < phnum; ++cnt)
      68             :     {
      69        6424 :       GElf_Phdr phdr_mem, *phdr = gelf_getphdr (core, cnt, &phdr_mem);
      70        6424 :       if (phdr == NULL || phdr->p_type != PT_LOAD)
      71        6436 :         continue;
      72             :       /* Bias is zero here, a core file itself has no bias.  */
      73        5798 :       GElf_Addr start = __libdwfl_segment_start (dwfl, phdr->p_vaddr);
      74        5798 :       GElf_Addr end = __libdwfl_segment_end (dwfl,
      75        5798 :                                              phdr->p_vaddr + phdr->p_memsz);
      76        5798 :       unsigned bytes = ebl_get_elfclass (process->ebl) == ELFCLASS64 ? 8 : 4;
      77        5798 :       if (addr < start || addr + bytes > end)
      78        5184 :         continue;
      79             :       Elf_Data *data;
      80         614 :       data = elf_getdata_rawchunk (core, phdr->p_offset + addr - start,
      81             :                                    bytes, ELF_T_ADDR);
      82         614 :       if (data == NULL)
      83             :         {
      84           0 :           __libdwfl_seterrno (DWFL_E_LIBELF);
      85           0 :           return false;
      86             :         }
      87         614 :       assert (data->d_size == bytes);
      88         614 :       if (bytes == 8)
      89         383 :         *result = read_8ubyte_unaligned_noncvt (data->d_buf);
      90             :       else
      91         231 :         *result = read_4ubyte_unaligned_noncvt (data->d_buf);
      92             :       return true;
      93             :     }
      94          12 :   __libdwfl_seterrno (DWFL_E_ADDR_OUTOFRANGE);
      95          12 :   return false;
      96             : }
      97             : 
      98             : static pid_t
      99          66 : core_next_thread (Dwfl *dwfl __attribute__ ((unused)), void *dwfl_arg,
     100             :                   void **thread_argp)
     101             : {
     102          66 :   struct core_arg *core_arg = dwfl_arg;
     103          66 :   Elf *core = core_arg->core;
     104             :   GElf_Nhdr nhdr;
     105             :   size_t name_offset;
     106             :   size_t desc_offset;
     107          66 :   Elf_Data *note_data = core_arg->note_data;
     108             :   size_t offset;
     109             : 
     110             :   struct thread_arg *thread_arg;
     111          66 :   if (*thread_argp == NULL)
     112             :     {
     113          26 :       core_arg->thread_note_offset = 0;
     114          26 :       thread_arg = malloc (sizeof (*thread_arg));
     115          26 :       if (thread_arg == NULL)
     116             :         {
     117           0 :           __libdwfl_seterrno (DWFL_E_NOMEM);
     118           0 :           return -1;
     119             :         }
     120          26 :       thread_arg->core_arg = core_arg;
     121          26 :       *thread_argp = thread_arg;
     122             :     }
     123             :   else
     124             :     thread_arg = (struct thread_arg *) *thread_argp;
     125             : 
     126         235 :   while (offset = core_arg->thread_note_offset, offset < note_data->d_size
     127         235 :          && (core_arg->thread_note_offset = gelf_getnote (note_data, offset,
     128             :                                                           &nhdr, &name_offset,
     129             :                                                           &desc_offset)) > 0)
     130             :     {
     131             :       /* Do not check NAME for now, help broken Linux kernels.  */
     132         418 :       const char *name = (nhdr.n_namesz == 0
     133         418 :                           ? "" : note_data->d_buf + name_offset);
     134         209 :       const char *desc = note_data->d_buf + desc_offset;
     135             :       GElf_Word regs_offset;
     136             :       size_t nregloc;
     137             :       const Ebl_Register_Location *reglocs;
     138             :       size_t nitems;
     139             :       const Ebl_Core_Item *items;
     140         209 :       if (! ebl_core_note (core_arg->ebl, &nhdr, name,
     141             :                            &regs_offset, &nregloc, &reglocs, &nitems, &items))
     142             :         {
     143             :           /* This note may be just not recognized, skip it.  */
     144         253 :           continue;
     145             :         }
     146         125 :       if (nhdr.n_type != NT_PRSTATUS)
     147          85 :         continue;
     148             :       const Ebl_Core_Item *item;
     149         280 :       for (item = items; item < items + nitems; item++)
     150         280 :         if (strcmp (item->name, "pid") == 0)
     151             :           break;
     152          40 :       if (item == items + nitems)
     153           0 :         continue;
     154          40 :       uint32_t val32 = read_4ubyte_unaligned_noncvt (desc + item->offset);
     155          80 :       val32 = (elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB
     156          48 :                 ? be32toh (val32) : le32toh (val32));
     157          40 :       pid_t tid = (int32_t) val32;
     158             :       eu_static_assert (sizeof val32 <= sizeof tid);
     159          40 :       thread_arg->note_offset = offset;
     160          40 :       return tid;
     161             :     }
     162             : 
     163          26 :   free (thread_arg);
     164          26 :   return 0;
     165             : }
     166             : 
     167             : static bool
     168          40 : core_set_initial_registers (Dwfl_Thread *thread, void *thread_arg_voidp)
     169             : {
     170          40 :   struct thread_arg *thread_arg = thread_arg_voidp;
     171          40 :   struct core_arg *core_arg = thread_arg->core_arg;
     172          40 :   Elf *core = core_arg->core;
     173          40 :   size_t offset = thread_arg->note_offset;
     174             :   GElf_Nhdr nhdr;
     175             :   size_t name_offset;
     176             :   size_t desc_offset;
     177          40 :   Elf_Data *note_data = core_arg->note_data;
     178          40 :   size_t nregs = ebl_frame_nregs (core_arg->ebl);
     179          40 :   assert (nregs > 0);
     180          40 :   assert (offset < note_data->d_size);
     181          40 :   size_t getnote_err = gelf_getnote (note_data, offset, &nhdr, &name_offset,
     182             :                                      &desc_offset);
     183             :   /* __libdwfl_attach_state_for_core already verified the note is there.  */
     184          40 :   assert (getnote_err != 0);
     185             :   /* Do not check NAME for now, help broken Linux kernels.  */
     186          80 :   const char *name = (nhdr.n_namesz == 0
     187          40 :                       ? "" : note_data->d_buf + name_offset);
     188          40 :   const char *desc = note_data->d_buf + desc_offset;
     189             :   GElf_Word regs_offset;
     190             :   size_t nregloc;
     191             :   const Ebl_Register_Location *reglocs;
     192             :   size_t nitems;
     193             :   const Ebl_Core_Item *items;
     194          40 :   int core_note_err = ebl_core_note (core_arg->ebl, &nhdr, name, &regs_offset,
     195             :                                      &nregloc, &reglocs, &nitems, &items);
     196             :   /* __libdwfl_attach_state_for_core already verified the note is there.  */
     197          40 :   assert (core_note_err != 0);
     198          40 :   assert (nhdr.n_type == NT_PRSTATUS);
     199             :   const Ebl_Core_Item *item;
     200         280 :   for (item = items; item < items + nitems; item++)
     201         280 :     if (strcmp (item->name, "pid") == 0)
     202             :       break;
     203          40 :   assert (item < items + nitems);
     204             :   pid_t tid;
     205             :   {
     206          40 :     uint32_t val32 = read_4ubyte_unaligned_noncvt (desc + item->offset);
     207          80 :     val32 = (elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB
     208          48 :              ? be32toh (val32) : le32toh (val32));
     209          40 :     tid = (int32_t) val32;
     210             :     eu_static_assert (sizeof val32 <= sizeof tid);
     211             :   }
     212             :   /* core_next_thread already found this TID there.  */
     213          40 :   assert (tid == INTUSE(dwfl_thread_tid) (thread));
     214         660 :   for (item = items; item < items + nitems; item++)
     215         630 :     if (item->pc_register)
     216             :       break;
     217          40 :   if (item < items + nitems)
     218             :     {
     219             :       Dwarf_Word pc;
     220          10 :       switch (gelf_getclass (core) == ELFCLASS32 ? 32 : 64)
     221             :       {
     222             :         case 32:;
     223           2 :           uint32_t val32 = read_4ubyte_unaligned_noncvt (desc + item->offset);
     224           4 :           val32 = (elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB
     225           4 :                    ? be32toh (val32) : le32toh (val32));
     226             :           /* Do a host width conversion.  */
     227           2 :           pc = val32;
     228           2 :           break;
     229             :         case 64:;
     230           8 :           uint64_t val64 = read_8ubyte_unaligned_noncvt (desc + item->offset);
     231           8 :           val64 = (elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB
     232          10 :                    ? be64toh (val64) : le64toh (val64));
     233             :           pc = val64;
     234             :           break;
     235             :         default:
     236             :           abort ();
     237             :       }
     238          10 :       INTUSE(dwfl_thread_state_register_pc) (thread, pc);
     239             :     }
     240          40 :   desc += regs_offset;
     241         648 :   for (size_t regloci = 0; regloci < nregloc; regloci++)
     242             :     {
     243         608 :       const Ebl_Register_Location *regloc = reglocs + regloci;
     244             :       // Iterate even regs out of NREGS range so that we can find pc_register.
     245         608 :       if (regloc->bits != 32 && regloc->bits != 64)
     246         136 :         continue;
     247         472 :       const char *reg_desc = desc + regloc->offset;
     248        1900 :       for (unsigned regno = regloc->regno;
     249        1428 :            regno < regloc->regno + (regloc->count ?: 1U);
     250         956 :            regno++)
     251             :         {
     252             :           /* PPC provides DWARF register 65 irrelevant for
     253             :              CFI which clashes with register 108 (LR) we need.
     254             :              LR (108) is provided earlier (in NT_PRSTATUS) than the # 65.
     255             :              FIXME: It depends now on their order in core notes.
     256             :              FIXME: It uses private function.  */
     257         956 :           if (regno < nregs
     258         818 :               && __libdwfl_frame_reg_get (thread->unwound, regno, NULL))
     259           0 :             continue;
     260             :           Dwarf_Word val;
     261         956 :           switch (regloc->bits)
     262             :           {
     263         240 :             case 32:;
     264         240 :               uint32_t val32 = read_4ubyte_unaligned_noncvt (reg_desc);
     265         240 :               reg_desc += sizeof val32;
     266         480 :               val32 = (elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB
     267         420 :                        ? be32toh (val32) : le32toh (val32));
     268             :               /* Do a host width conversion.  */
     269         240 :               val = val32;
     270         240 :               break;
     271         716 :             case 64:;
     272         716 :               uint64_t val64 = read_8ubyte_unaligned_noncvt (reg_desc);
     273         716 :               reg_desc += sizeof val64;
     274        1432 :               val64 = (elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB
     275         824 :                        ? be64toh (val64) : le64toh (val64));
     276             :               assert (sizeof (*thread->unwound->regs) == sizeof val64);
     277         716 :               val = val64;
     278         716 :               break;
     279           0 :             default:
     280           0 :               abort ();
     281             :           }
     282             :           /* Registers not valid for CFI are just ignored.  */
     283         956 :           if (regno < nregs)
     284         818 :             INTUSE(dwfl_thread_state_registers) (thread, regno, 1, &val);
     285         956 :           if (regloc->pc_register)
     286           4 :             INTUSE(dwfl_thread_state_register_pc) (thread, val);
     287         956 :           reg_desc += regloc->pad;
     288             :         }
     289             :     }
     290          40 :   return true;
     291             : }
     292             : 
     293             : static void
     294          36 : core_detach (Dwfl *dwfl __attribute__ ((unused)), void *dwfl_arg)
     295             : {
     296          36 :   struct core_arg *core_arg = dwfl_arg;
     297          36 :   ebl_closebackend (core_arg->ebl);
     298          36 :   free (core_arg);
     299          36 : }
     300             : 
     301             : static const Dwfl_Thread_Callbacks core_thread_callbacks =
     302             : {
     303             :   core_next_thread,
     304             :   NULL, /* get_thread */
     305             :   core_memory_read,
     306             :   core_set_initial_registers,
     307             :   core_detach,
     308             :   NULL, /* core_thread_detach */
     309             : };
     310             : 
     311             : int
     312          36 : dwfl_core_file_attach (Dwfl *dwfl, Elf *core)
     313             : {
     314          36 :   Dwfl_Error err = DWFL_E_NOERROR;
     315          36 :   Ebl *ebl = ebl_openbackend (core);
     316          36 :   if (ebl == NULL)
     317             :     {
     318             :       err = DWFL_E_LIBEBL;
     319           0 :     fail_err:
     320           0 :       if (dwfl->process == NULL && dwfl->attacherr == DWFL_E_NOERROR)
     321           0 :         dwfl->attacherr = __libdwfl_canon_error (err);
     322           0 :       __libdwfl_seterrno (err);
     323           0 :       return -1;
     324             :     }
     325          36 :   size_t nregs = ebl_frame_nregs (ebl);
     326          36 :   if (nregs == 0)
     327             :     {
     328             :       err = DWFL_E_NO_UNWIND;
     329           0 :     fail:
     330           0 :       ebl_closebackend (ebl);
     331           0 :       goto fail_err;
     332             :     }
     333          36 :   GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (core, &ehdr_mem);
     334          36 :   if (ehdr == NULL)
     335             :     {
     336             :       err = DWFL_E_LIBELF;
     337             :       goto fail;
     338             :     }
     339          36 :   if (ehdr->e_type != ET_CORE)
     340             :     {
     341             :       err = DWFL_E_NO_CORE_FILE;
     342             :       goto fail;
     343             :     }
     344             :   size_t phnum;
     345          36 :   if (elf_getphdrnum (core, &phnum) < 0)
     346             :     {
     347             :       err = DWFL_E_LIBELF;
     348             :       goto fail;
     349             :     }
     350             :   pid_t pid = -1;
     351             :   Elf_Data *note_data = NULL;
     352           0 :   for (size_t cnt = 0; cnt < phnum; ++cnt)
     353             :     {
     354          36 :       GElf_Phdr phdr_mem, *phdr = gelf_getphdr (core, cnt, &phdr_mem);
     355          36 :       if (phdr != NULL && phdr->p_type == PT_NOTE)
     356             :         {
     357          36 :           note_data = elf_getdata_rawchunk (core, phdr->p_offset,
     358          36 :                                             phdr->p_filesz, (phdr->p_align == 8
     359             :                                                              ? ELF_T_NHDR8
     360             :                                                              : ELF_T_NHDR));
     361          36 :           break;
     362             :         }
     363             :     }
     364          36 :   if (note_data == NULL)
     365             :     {
     366             :       err = DWFL_E_LIBELF;
     367             :       goto fail;
     368             :     }
     369             :   size_t offset = 0;
     370             :   GElf_Nhdr nhdr;
     371             :   size_t name_offset;
     372             :   size_t desc_offset;
     373          72 :   while (offset < note_data->d_size
     374          72 :          && (offset = gelf_getnote (note_data, offset,
     375             :                                     &nhdr, &name_offset, &desc_offset)) > 0)
     376             :     {
     377             :       /* Do not check NAME for now, help broken Linux kernels.  */
     378         144 :       const char *name = (nhdr.n_namesz == 0
     379          72 :                           ? "" : note_data->d_buf + name_offset);
     380          72 :       const char *desc = note_data->d_buf + desc_offset;
     381             :       GElf_Word regs_offset;
     382             :       size_t nregloc;
     383             :       const Ebl_Register_Location *reglocs;
     384             :       size_t nitems;
     385             :       const Ebl_Core_Item *items;
     386          72 :       if (! ebl_core_note (ebl, &nhdr, name,
     387             :                            &regs_offset, &nregloc, &reglocs, &nitems, &items))
     388             :         {
     389             :           /* This note may be just not recognized, skip it.  */
     390          36 :           continue;
     391             :         }
     392          72 :       if (nhdr.n_type != NT_PRPSINFO)
     393          36 :         continue;
     394             :       const Ebl_Core_Item *item;
     395         288 :       for (item = items; item < items + nitems; item++)
     396         288 :         if (strcmp (item->name, "pid") == 0)
     397             :           break;
     398          36 :       if (item == items + nitems)
     399           0 :         continue;
     400          36 :       uint32_t val32 = read_4ubyte_unaligned_noncvt (desc + item->offset);
     401          72 :       val32 = (elf_getident (core, NULL)[EI_DATA] == ELFDATA2MSB
     402          43 :                 ? be32toh (val32) : le32toh (val32));
     403          36 :       pid = (int32_t) val32;
     404             :       eu_static_assert (sizeof val32 <= sizeof pid);
     405          36 :       break;
     406             :     }
     407          36 :   if (pid == -1)
     408             :     {
     409             :       /* No valid NT_PRPSINFO recognized in this CORE.  */
     410             :       err = DWFL_E_BADELF;
     411             :       goto fail;
     412             :     }
     413          36 :   struct core_arg *core_arg = malloc (sizeof *core_arg);
     414          36 :   if (core_arg == NULL)
     415             :     {
     416             :       err = DWFL_E_NOMEM;
     417             :       goto fail;
     418             :     }
     419          36 :   core_arg->core = core;
     420          36 :   core_arg->note_data = note_data;
     421          36 :   core_arg->thread_note_offset = 0;
     422          36 :   core_arg->ebl = ebl;
     423          36 :   if (! INTUSE(dwfl_attach_state) (dwfl, core, pid, &core_thread_callbacks,
     424             :                                    core_arg))
     425             :     {
     426           0 :       free (core_arg);
     427           0 :       ebl_closebackend (ebl);
     428           0 :       return -1;
     429             :     }
     430             :   return pid;
     431             : }
     432             : INTDEF (dwfl_core_file_attach)

Generated by: LCOV version 1.13