Home | History | Annotate | Download | only in libdwfl
      1 /* Standard libdwfl callbacks for debugging a live Linux process.
      2    Copyright (C) 2005-2010 Red Hat, Inc.
      3    This file is part of Red Hat elfutils.
      4 
      5    Red Hat elfutils is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by the
      7    Free Software Foundation; version 2 of the License.
      8 
      9    Red Hat elfutils is distributed in the hope that it will be useful, but
     10    WITHOUT ANY WARRANTY; without even the implied warranty of
     11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12    General Public License for more details.
     13 
     14    You should have received a copy of the GNU General Public License along
     15    with Red Hat elfutils; if not, write to the Free Software Foundation,
     16    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     17 
     18    In addition, as a special exception, Red Hat, Inc. gives You the
     19    additional right to link the code of Red Hat elfutils with code licensed
     20    under any Open Source Initiative certified open source license
     21    (http://www.opensource.org/licenses/index.php) which requires the
     22    distribution of source code with any binary distribution and to
     23    distribute linked combinations of the two.  Non-GPL Code permitted under
     24    this exception must only link to the code of Red Hat elfutils through
     25    those well defined interfaces identified in the file named EXCEPTION
     26    found in the source code files (the "Approved Interfaces").  The files
     27    of Non-GPL Code may instantiate templates or use macros or inline
     28    functions from the Approved Interfaces without causing the resulting
     29    work to be covered by the GNU General Public License.  Only Red Hat,
     30    Inc. may make changes or additions to the list of Approved Interfaces.
     31    Red Hat's grant of this exception is conditioned upon your not adding
     32    any new exceptions.  If you wish to add a new Approved Interface or
     33    exception, please contact Red Hat.  You must obey the GNU General Public
     34    License in all respects for all of the Red Hat elfutils code and other
     35    code used in conjunction with Red Hat elfutils except the Non-GPL Code
     36    covered by this exception.  If you modify this file, you may extend this
     37    exception to your version of the file, but you are not obligated to do
     38    so.  If you do not wish to provide this exception without modification,
     39    you must delete this exception statement from your version and license
     40    this file solely under the GPL without exception.
     41 
     42    Red Hat elfutils is an included package of the Open Invention Network.
     43    An included package of the Open Invention Network is a package for which
     44    Open Invention Network licensees cross-license their patents.  No patent
     45    license is granted, either expressly or impliedly, by designation as an
     46    included package.  Should you wish to participate in the Open Invention
     47    Network licensing program, please visit www.openinventionnetwork.com
     48    <http://www.openinventionnetwork.com>.  */
     49 
     50 #include "libdwflP.h"
     51 #include <inttypes.h>
     52 #include <sys/types.h>
     53 #include <errno.h>
     54 #include <stdio.h>
     55 #include <stdio_ext.h>
     56 #include <stdbool.h>
     57 #include <string.h>
     58 #include <stdlib.h>
     59 #include <fcntl.h>
     60 #include <unistd.h>
     61 #include <assert.h>
     62 #include <endian.h>
     63 
     64 
     65 #define PROCMAPSFMT	"/proc/%d/maps"
     66 #define PROCMEMFMT	"/proc/%d/mem"
     67 #define PROCAUXVFMT	"/proc/%d/auxv"
     68 
     69 
     70 /* Search /proc/PID/auxv for the AT_SYSINFO_EHDR tag.  */
     71 
     72 static int
     73 grovel_auxv (pid_t pid, Dwfl *dwfl, GElf_Addr *sysinfo_ehdr)
     74 {
     75   char *fname;
     76   if (asprintf (&fname, PROCAUXVFMT, pid) < 0)
     77     return ENOMEM;
     78 
     79   int fd = open64 (fname, O_RDONLY);
     80   free (fname);
     81   if (fd < 0)
     82     return errno == ENOENT ? 0 : errno;
     83 
     84   ssize_t nread;
     85   do
     86     {
     87       union
     88       {
     89 	char buffer[sizeof (long int) * 2 * 64];
     90 	Elf64_auxv_t a64[sizeof (long int) * 2 * 64 / sizeof (Elf64_auxv_t)];
     91 	Elf32_auxv_t a32[sizeof (long int) * 2 * 32 / sizeof (Elf32_auxv_t)];
     92       } d;
     93       nread = read (fd, &d, sizeof d);
     94       if (nread > 0)
     95 	{
     96 	  switch (sizeof (long int))
     97 	    {
     98 	    case 4:
     99 	      for (size_t i = 0; (char *) &d.a32[i] < &d.buffer[nread]; ++i)
    100 		if (d.a32[i].a_type == AT_SYSINFO_EHDR)
    101 		  {
    102 		    *sysinfo_ehdr = d.a32[i].a_un.a_val;
    103 		    if (dwfl->segment_align > 1)
    104 		      {
    105 			nread = 0;
    106 			break;
    107 		      }
    108 		  }
    109 		else if (d.a32[i].a_type == AT_PAGESZ
    110 			 && dwfl->segment_align <= 1)
    111 		  dwfl->segment_align = d.a32[i].a_un.a_val;
    112 	      break;
    113 	    case 8:
    114 	      for (size_t i = 0; (char *) &d.a64[i] < &d.buffer[nread]; ++i)
    115 		if (d.a64[i].a_type == AT_SYSINFO_EHDR)
    116 		  {
    117 		    *sysinfo_ehdr = d.a64[i].a_un.a_val;
    118 		    if (dwfl->segment_align > 1)
    119 		      {
    120 			nread = 0;
    121 			break;
    122 		      }
    123 		  }
    124 		else if (d.a64[i].a_type == AT_PAGESZ
    125 			 && dwfl->segment_align <= 1)
    126 		  dwfl->segment_align = d.a64[i].a_un.a_val;
    127 	      break;
    128 	    default:
    129 	      abort ();
    130 	      break;
    131 	    }
    132 	}
    133     }
    134   while (nread > 0);
    135 
    136   close (fd);
    137 
    138   return nread < 0 ? errno : 0;
    139 }
    140 
    141 static int
    142 proc_maps_report (Dwfl *dwfl, FILE *f, GElf_Addr sysinfo_ehdr, pid_t pid)
    143 {
    144   unsigned int last_dmajor = -1, last_dminor = -1;
    145   uint64_t last_ino = -1;
    146   char *last_file = NULL;
    147   Dwarf_Addr low = 0, high = 0;
    148 
    149   inline bool report (void)
    150     {
    151       if (last_file != NULL)
    152 	{
    153 	  Dwfl_Module *mod = INTUSE(dwfl_report_module) (dwfl, last_file,
    154 							 low, high);
    155 	  free (last_file);
    156 	  last_file = NULL;
    157 	  if (unlikely (mod == NULL))
    158 	    return true;
    159 	}
    160       return false;
    161     }
    162 
    163   char *line = NULL;
    164   size_t linesz;
    165   ssize_t len;
    166   while ((len = getline (&line, &linesz, f)) > 0)
    167     {
    168       if (line[len - 1] == '\n')
    169 	line[len - 1] = '\0';
    170 
    171       Dwarf_Addr start, end, offset;
    172       unsigned int dmajor, dminor;
    173       uint64_t ino;
    174       int nread = -1;
    175       if (sscanf (line, "%" PRIx64 "-%" PRIx64 " %*s %" PRIx64
    176 		  " %x:%x %" PRIi64 " %n",
    177 		  &start, &end, &offset, &dmajor, &dminor, &ino, &nread) < 6
    178 	  || nread <= 0)
    179 	{
    180 	  free (line);
    181 	  return ENOEXEC;
    182 	}
    183 
    184       /* If this is the special mapping AT_SYSINFO_EHDR pointed us at,
    185 	 report the last one and then this special one.  */
    186       if (start == sysinfo_ehdr && start != 0)
    187 	{
    188 	  if (report ())
    189 	    {
    190 	    bad_report:
    191 	      free (line);
    192 	      fclose (f);
    193 	      return -1;
    194 	    }
    195 
    196 	  low = start;
    197 	  high = end;
    198 	  if (asprintf (&last_file, "[vdso: %d]", (int) pid) < 0
    199 	      || report ())
    200 	    goto bad_report;
    201 	}
    202 
    203       char *file = line + nread + strspn (line + nread, " \t");
    204       if (file[0] == '\0' || (ino == 0 && dmajor == 0 && dminor == 0))
    205 	/* This line doesn't indicate a file mapping.  */
    206 	continue;
    207 
    208       if (last_file != NULL
    209 	  && ino == last_ino && dmajor == last_dmajor && dminor == last_dminor)
    210 	{
    211 	  /* This is another portion of the same file's mapping.  */
    212 	  assert (!strcmp (last_file, file));
    213 	  high = end;
    214 	}
    215       else
    216 	{
    217 	  /* This is a different file mapping.  Report the last one.  */
    218 	  if (report ())
    219 	    goto bad_report;
    220 	  low = start;
    221 	  high = end;
    222 	  last_file = strdup (file);
    223 	  last_ino = ino;
    224 	  last_dmajor = dmajor;
    225 	  last_dminor = dminor;
    226 	}
    227     }
    228   free (line);
    229 
    230   int result = ferror_unlocked (f) ? errno : feof_unlocked (f) ? 0 : ENOEXEC;
    231 
    232   /* Report the final one.  */
    233   bool lose = report ();
    234 
    235   return result != 0 ? result : lose ? -1 : 0;
    236 }
    237 
    238 int
    239 dwfl_linux_proc_maps_report (Dwfl *dwfl, FILE *f)
    240 {
    241   return proc_maps_report (dwfl, f, 0, 0);
    242 }
    243 INTDEF (dwfl_linux_proc_maps_report)
    244 
    245 int
    246 dwfl_linux_proc_report (Dwfl *dwfl, pid_t pid)
    247 {
    248   if (dwfl == NULL)
    249     return -1;
    250 
    251   /* We'll notice the AT_SYSINFO_EHDR address specially when we hit it.  */
    252   GElf_Addr sysinfo_ehdr = 0;
    253   int result = grovel_auxv (pid, dwfl, &sysinfo_ehdr);
    254   if (result != 0)
    255     return result;
    256 
    257   char *fname;
    258   if (asprintf (&fname, PROCMAPSFMT, pid) < 0)
    259     return ENOMEM;
    260 
    261   FILE *f = fopen (fname, "r");
    262   free (fname);
    263   if (f == NULL)
    264     return errno;
    265 
    266   (void) __fsetlocking (f, FSETLOCKING_BYCALLER);
    267 
    268   result = proc_maps_report (dwfl, f, sysinfo_ehdr, pid);
    269 
    270   fclose (f);
    271 
    272   return result;
    273 }
    274 INTDEF (dwfl_linux_proc_report)
    275 
    276 static ssize_t
    277 read_proc_memory (void *arg, void *data, GElf_Addr address,
    278 		  size_t minread, size_t maxread)
    279 {
    280   const int fd = *(const int *) arg;
    281   ssize_t nread = pread64 (fd, data, maxread, (off64_t) address);
    282   /* Some kernels don't actually let us do this read, ignore those errors.  */
    283   if (nread < 0 && (errno == EINVAL || errno == EPERM))
    284     return 0;
    285   if (nread > 0 && (size_t) nread < minread)
    286     nread = 0;
    287   return nread;
    288 }
    289 
    290 extern Elf *elf_from_remote_memory (GElf_Addr ehdr_vma,
    291 				    GElf_Addr *loadbasep,
    292 				    ssize_t (*read_memory) (void *arg,
    293 							    void *data,
    294 							    GElf_Addr address,
    295 							    size_t minread,
    296 							    size_t maxread),
    297 				    void *arg);
    298 
    299 
    300 /* Dwfl_Callbacks.find_elf */
    301 
    302 int
    303 dwfl_linux_proc_find_elf (Dwfl_Module *mod __attribute__ ((unused)),
    304 			  void **userdata __attribute__ ((unused)),
    305 			  const char *module_name, Dwarf_Addr base,
    306 			  char **file_name, Elf **elfp)
    307 {
    308   if (module_name[0] == '/')
    309     {
    310       int fd = open64 (module_name, O_RDONLY);
    311       if (fd >= 0)
    312 	{
    313 	  *file_name = strdup (module_name);
    314 	  if (*file_name == NULL)
    315 	    {
    316 	      close (fd);
    317 	      return ENOMEM;
    318 	    }
    319 	}
    320       return fd;
    321     }
    322 
    323   int pid;
    324   if (sscanf (module_name, "[vdso: %d]", &pid) == 1)
    325     {
    326       /* Special case for in-memory ELF image.  */
    327 
    328       char *fname;
    329       if (asprintf (&fname, PROCMEMFMT, pid) < 0)
    330 	return -1;
    331 
    332       int fd = open64 (fname, O_RDONLY);
    333       free (fname);
    334       if (fd < 0)
    335 	return -1;
    336 
    337       *elfp = elf_from_remote_memory (base, NULL, &read_proc_memory, &fd);
    338 
    339       close (fd);
    340 
    341       *file_name = NULL;
    342       return -1;
    343     }
    344 
    345   abort ();
    346   return -1;
    347 }
    348 INTDEF (dwfl_linux_proc_find_elf)
    349