summaryrefslogtreecommitdiff
path: root/linkers/rtems-addr2line.cpp
blob: 309fa688969d2b4fed11e4dc01f53be29cf18241 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 * Copyright (c) 2018, Chris Johns <chrisj@rtems.org>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
/**
 * @file
 *
 * @ingroup rtems_rld
 *
 * @brief RTEMS Address to Line is a version of the classic addr2line
 *        utility to test the DWARF info support in the RTEMS Toolkit.
 *
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <iomanip>
#include <iostream>

#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <getopt.h>

#include <rld.h>
#include <rld-dwarf.h>
#include <rld-files.h>
#include <rld-rtems.h>

#ifndef HAVE_KILL
#define kill(p,s) raise(s)
#endif

/**
 * RTEMS Symbols options.
 */
static struct option rld_opts[] = {
  { "help",         no_argument,            NULL,           'h' },
  { "version",      no_argument,            NULL,           'V' },
  { "verbose",      no_argument,            NULL,           'v' },
  { "executable",   required_argument,      NULL,           'e' },
  { "functions" ,   no_argument,            NULL,           'f' },
  { "addresses",    no_argument,            NULL,           'a' },
  { "pretty-print", no_argument,            NULL,           'p' },
  { "basenames",    no_argument,            NULL,           's' },
  { NULL,           0,                      NULL,            0 }
};

void
usage (int exit_code)
{
  std::cout << "rtems-addr2line [options] addresses" << std::endl
            << "Options and arguments:" << std::endl
            << " -h        : help (also --help)" << std::endl
            << " -V        : print version number and exit (also --version)" << std::endl
            << " -v        : verbose (trace import parts), can supply multiple times" << std::endl
            << "             to increase verbosity (also --verbose)" << std::endl
            << " -e        : executable (also --executable)" << std::endl
            << " -f        : show function names (also --functions)" << std::endl
            << " -a        : show addresses (also --addresses)" << std::endl
            << " -p        : human readable format (also --pretty-print)" << std::endl
            << " -s        : Strip directory paths (also --basenames)" << std::endl;
  ::exit (exit_code);
}

static void
fatal_signal (int signum)
{
  signal (signum, SIG_DFL);

  /*
   * Get the same signal again, this time not handled, so its normal effect
   * occurs.
   */
  kill (getpid (), signum);
}

static void
setup_signals (void)
{
  if (signal (SIGINT, SIG_IGN) != SIG_IGN)
    signal (SIGINT, fatal_signal);
#ifdef SIGHUP
  if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
    signal (SIGHUP, fatal_signal);
#endif
  if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
    signal (SIGTERM, fatal_signal);
#ifdef SIGPIPE
  if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
    signal (SIGPIPE, fatal_signal);
#endif
#ifdef SIGCHLD
  signal (SIGCHLD, SIG_DFL);
#endif
}

void
unhandled_exception (void)
{
  std::cerr << "error: exception handling error, please report" << std::endl;
  exit (13);
}

int
main (int argc, char* argv[])
{
  int ec = 0;

  setup_signals ();

  std::set_terminate(unhandled_exception);

  try
  {
    std::string exe_name = "a.out";
    bool        show_functions = false;
    bool        show_addresses = false;
    bool        pretty_print = false;
    bool        show_basenames = false;

    rld::set_cmdline (argc, argv);

    while (true)
    {
      int opt = ::getopt_long (argc, argv, "hvVe:faps", rld_opts, NULL);
      if (opt < 0)
        break;

      switch (opt)
      {
        case 'V':
          std::cout << "rtems-addr2line (RTEMS Address To Line) " << rld::version ()
                    << ", RTEMS revision " << rld::rtems::version ()
                    << std::endl;
          ::exit (0);
          break;

        case 'v':
          rld::verbose_inc ();
          break;

        case 'e':
          exe_name = optarg;
          break;

        case 'f':
          show_functions = true;
          break;

        case 'a':
          show_addresses = true;
          break;

        case 'p':
          pretty_print = true;
          break;

        case 's':
          show_basenames = true;
          break;

        case '?':
          usage (3);
          break;

        case 'h':
          usage (0);
          break;
      }
    }

    /*
     * Set the program name.
     */
    rld::set_progname (argv[0]);

    argc -= optind;
    argv += optind;

    if (rld::verbose ())
      std::cout << "RTEMS Address To Line " << rld::version () << std::endl;

    /*
     * If there are no object files there is nothing to link.
     */
    if (argc == 0)
      throw rld::error ("no addresses provided", "options");

    if (rld::verbose ())
      std::cout << "exe: " << exe_name << std::endl;

    /*
     * Load the executable's debug info.
     */
    rld::files::object exe (exe_name);
    rld::dwarf::file   debug;

    try
    {
      /*
       * Load the executable's ELF file debug info.
       */
      exe.open ();
      exe.begin ();
      debug.begin (exe.elf ());
      debug.load_debug ();
      debug.load_types ();
      debug.load_functions ();

      for (int arg = 0; arg < argc; ++arg)
      {
        rld::dwarf::dwarf_address location;

        if (rld::verbose ())
          std::cout << "address: " << argv[arg] << std::endl;

        /*
         * Use the C routine as C++ does not have a way to automatically handle
         * different bases on the input.
         */
        location = ::strtoul (argv[arg], 0, 0);

        std::string path;
        int         line;

        debug.get_source (location, path, line);

        if (show_addresses)
        {
          std::cout << std::hex << std::setfill ('0')
                    << "0x" << location
                    << std::dec << std::setfill (' ');

          if (pretty_print)
            std::cout << ": ";
          else
            std::cout << std::endl;
        }

        if (show_functions)
        {
          std::string function;
          debug.get_function (location, function);
          std::cout << function << " at ";
        }

        if (show_basenames)
          std::cout << rld::path::basename (path);
        else
          std::cout << path;

        std::cout << ':' << line << std::endl;
      }

      debug.end ();
      exe.end ();
      exe.close ();
    }
    catch (...)
    {
      debug.end ();
      exe.end ();
      exe.close ();
      throw;
    }
  }
  catch (rld::error re)
  {
    std::cerr << "error: "
              << re.where << ": " << re.what
              << std::endl;
    ec = 10;
  }
  catch (std::exception& e)
  {
    rld::output_std_exception (e, std::cerr);
    ec = 11;
  }
  catch (...)
  {
    /*
     * Helps to know if this happens.
     */
    std::cout << "error: unhandled exception" << std::endl;
    ec = 12;
  }

  return ec;
}