Home | History | Annotate | Download | only in tests
      1 /* Copyright (C) 2011 Red Hat, Inc.
      2    This file is part of elfutils.
      3    Written by Marek Polacek <mpolacek (at) redhat.com>, 2011.
      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 #ifdef HAVE_CONFIG_H
     19 # include <config.h>
     20 #endif
     21 
     22 #include ELFUTILS_HEADER(dwfl)
     23 #include <assert.h>
     24 #include <dwarf.h>
     25 #include <fcntl.h>
     26 #include <stdio.h>
     27 #include <unistd.h>
     28 
     29 
     30 int
     31 main (int argc, char *argv[])
     32 {
     33   int cnt;
     34 
     35   for (cnt = 1; cnt < argc; ++cnt)
     36     {
     37       Dwarf_Off offset = 0;
     38       size_t len;
     39 
     40       int fd = open (argv[cnt], O_RDONLY);
     41       if (fd == -1)
     42 	{
     43 	  printf ("cannot open '%s': %m\n", argv[cnt]);
     44 	  return 1;
     45 	}
     46 
     47       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
     48       if (dbg == NULL)
     49 	{
     50 	  printf ("%s not usable: %s\n", argv[cnt], dwarf_errmsg (-1));
     51 	  close (fd);
     52 	  return 1;
     53 	}
     54 
     55       /* Try to use NULL Dwarf object.  */
     56       const char *str = dwarf_getstring (NULL, offset, &len);
     57       assert (str == NULL);
     58 
     59       /* Use insane offset.  */
     60       str = dwarf_getstring (dbg, ~0UL, &len);
     61       assert (str == NULL);
     62 
     63       /* Now do some real work.  */
     64       for (int i = 0; i < 100; ++i)
     65 	{
     66 	  str = dwarf_getstring (dbg, offset, &len);
     67 	  puts (str);
     68 
     69 	  /* Advance.  */
     70 	  offset += len + 1;
     71 	}
     72 
     73       dwarf_end (dbg);
     74       close (fd);
     75     }
     76 
     77   return 0;
     78 }
     79