summaryrefslogtreecommitdiff
path: root/rtems-tld.cpp
blob: 8765dcadc987a1ca8d559b38c870860ec39d1ada (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*
 * Copyright (c) 2014, 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 Trace Linker manages creating a tracable RTEMS executable.
 *
 */

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

#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <locale>
#include <sstream>

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

#include <getopt.h>

#include <rld.h>
#include <rld-cc.h>
#include <rld-config.h>
#include <rld-process.h>

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

namespace rld
{
  /**
   * RTEMS Trace Linker.
   */
  namespace trace
  {
    /**
     * A container of arguments.
     */
    typedef std::vector < std::string > function_args;

    /**
     * The return value.
     */
    typedef std::string function_return;

    /**
     * A function's signature.
     */
    struct function_sig
    {
      std::string     name; /**< The function's name. */
      function_args   args; /**< The function's list of arguments. */
      function_return ret;  /**< The fuctions return value. */

      /**
       * The default constructor.
       */
      function_sig ();

      /**
       * Construct the signature loading it from the configuration.
       */
      function_sig (const rld::config::record& record);

      /**
       * Copy constructor.
       */
      function_sig (const function_sig& orig);

      /**
       * Return the function's declaration.
       */
      const std::string decl () const;
    };

    /**
     * A container of function signatures.
     */
    typedef std::map < std::string, function_sig > function_sigs;

    /**
     * Wrappers hold the data used when wrapping the code. It knows how to wrap
     * a specific trace function. Wrapping a function requires specific defines
     * and header files.
     */
    struct wrapper
    {
      std::string   name;            /**< The name of this wrapper. */
      rld::strings  headers;         /**< Include statements. */
      rld::strings  defines;         /**< Define statements. */
      std::string   map_sym_prefix;  /**< Mapping symbol prefix. */
      std::string   arg_trace;       /**< Code template to trace an argument. */
      std::string   ret_trace;       /**< Code template to trace the return value. */
      std::string   code;            /**< Code block inserted before the trace code. */
      function_sigs sigs;            /**< The functions this wrapper wraps. */

      /**
       * Load the wrapper.
       */
      wrapper (const std::string&   name,
               rld::config::config& config);

      /**
       * Parse the generator.
       */
      void parse_generator (rld::config::config& config,
                            const rld::config::section& section);

      /**
       * Recursive parser for strings.
       */
      void parse (rld::config::config&        config,
                  const rld::config::section& section,
                  const std::string&          sec_name,
                  const std::string&          rec_name,
                  rld::strings&               items,
                  int                         depth = 0);

      /**
       * Dump the wrapper.
       */
      void dump (std::ostream& out) const;
    };

    /**
     * A container of wrappers. The order is the order we wrap.
     */
    typedef std::vector < wrapper > wrappers;

    /**
     * Tracer.
     */
    class tracer
    {
    public:
      tracer ();

      /**
       * Load the user's configuration.
       */
      void load (rld::config::config& config,
                 const std::string&   section);

      /**
       * Dump the wrapper.
       */
      void dump (std::ostream& out) const;

    private:

      std::string  name;            /**< The name of the trace. */
      std::string  bsp;             /**< The BSP we are linking to. */
      rld::strings trace;           /**< The functions to trace. */
      wrappers     wrappers;        /**< Wrappers wrap trace functions. */
    };

    /**
     * Trace Linker.
     */
    class linker
    {
    public:
      linker ();

      /**
       * Load the user's configuration.
       */
      void load_config (const std::string& path,
                        const std::string& trace);

      /**
       * Generate the C file.
       */
      void generate_c ();

      /**
       * Dump the linker.
       */
      void dump (std::ostream& out) const;

    private:

      rld::config::config config;   /**< User configuration. */
      tracer              tracer;   /**< The tracer */
    };

    function_sig::function_sig ()
    {
    }

    function_sig::function_sig (const rld::config::record& record)
    {
      /*
       * There can only be one function signature in the configuration.
       */
      if (!record.single ())
        throw rld::error ("duplicate", "function signature: " + record.name);

      name = record.name;

      /*
       * Function signatures are defined as the return value then the arguments
       * delimited by a comma and white space. No checking is made of the
       * return value or arguments.
       */
      rld::strings si;
      rld::config::parse_items (record, si);

      if (si.size () == 0)
        throw rld::error ("no return value", "function signature: " + record.name);
      if (si.size () == 1)
          throw rld::error ("no arguments", "function signature: " + record.name);

      ret = si[0];
      args.resize (si.size () - 1);
      std::copy (si.begin ()  + 1, si.end (), args.begin ());
    }

    function_sig::function_sig (const function_sig& orig)
      : name (orig.name),
        args (orig.args),
        ret (orig.ret)
    {
    }

    const std::string
    function_sig::decl () const
    {
      std::string ds = ret + ' ' + name + '(';
      int         arg = 0;
      for (function_args::const_iterator ai = args.begin ();
           ai != args.end ();
           ++ai)
        {
          if (ai != args.begin ())
            ds += ", ";
          ds += (*ai) + " a" + rld::to_string (++arg);
        }
      ds += ')';

      return ds;
    }

    wrapper::wrapper (const std::string&   name,
                      rld::config::config& config)
      : name (name)
    {
      /*
       * A wrapper section optionally contain one or more records of:
       *
       * # trace      A list of functions to trace.
       * # generator  The name of the generator section. Defaults if not present.
       * # headers    A list of sections containing headers or header records.
       * # header     A list of include string that are single or double quoted.
       * # defines    A list of sections containing defines or define record.
       * # defines    A list of define string that are single or double quoted.
       * # signatures A list of section names of function signatures.
       *
       * @note The quoting and list spliting is a little weak because a delimiter
       *       in a quote should not be seen as a delimiter.
       */
      const rld::config::section& section = config.get_section (name);

      parse (config, section, "headers", "header", headers);
      parse (config, section, "defines", "define", defines);

      parse_generator (config, section);

      rld::strings sig_list;

      rld::config::parse_items (section, "signatures", sig_list);

      for (rld::strings::const_iterator sli = sig_list.begin ();
           sli != sig_list.end ();
           ++sli)
      {
        const rld::config::section& sig_sec = config.get_section (*sli);
        for (rld::config::records::const_iterator ri = sig_sec.recs.begin ();
             ri != sig_sec.recs.end ();
             ++ri)
        {
          function_sig func (*ri);
          sigs[func.name] = func;
        }
      }
    }

    void
    wrapper::parse_generator (rld::config::config& config,
                              const rld::config::section& section)
    {
      const rld::config::record* rec = 0;
      try
      {
        const rld::config::record& record = section.get_record ("generator");
        rec = &record;
      }
      catch (rld::error re)
      {
        /*
         * No error, continue.
         */
      }

      std::string gen_section;

      if (rec)
      {
        if (!rec->single ())
          throw rld::error ("duplicate", "generator: " + section.name + "/generator");
        gen_section = rec->items[0].text;
      }
      else
      {
        gen_section = config.get_section ("default-generator").get_record_item ("generator");
      }

      const rld::config::section& sec = config.get_section (gen_section);

      map_sym_prefix = sec.get_record_item ("map-sym-prefix");
      arg_trace = rld::dequote (sec.get_record_item ("arg-trace"));
      ret_trace = rld::dequote (sec.get_record_item ("ret-trace"));
      code = rld::dequote (sec.get_record_item ("code"));
    }

    void
    wrapper::parse (rld::config::config&        config,
                    const rld::config::section& section,
                    const std::string&          sec_name,
                    const std::string&          rec_name,
                    rld::strings&               items,
                    int                         depth)
    {
      if (depth > 32)
        throw rld::error ("too deep", "parsing: " + sec_name + '/' + rec_name);

      rld::config::parse_items (section, rec_name, items);

      rld::strings sl;

      rld::config::parse_items (section, sec_name, sl);

      for (rld::strings::const_iterator sli = sl.begin ();
           sli != sl.end ();
           ++sli)
      {
        const rld::config::section& sec = config.get_section (*sli);
        parse (config, sec, sec_name, rec_name, items, depth + 1);
      }
    }

    void
    wrapper::dump (std::ostream& out) const
    {
      out << "  Wrapper: " << name << std::endl
          << "   Headers: " << headers.size () << std::endl;
      for (rld::strings::const_iterator hi = headers.begin ();
           hi != headers.end ();
           ++hi)
      {
        out << "    " << (*hi) << std::endl;
      }
      out << "   Defines: " << defines.size () << std::endl;
      for (rld::strings::const_iterator di = defines.begin ();
           di != defines.end ();
           ++di)
      {
        out << "    " << (*di) << std::endl;
      }
      out << "   Mapping Symbol Prefix: " << map_sym_prefix << std::endl
          << "   Arg Trace Code: " << arg_trace << std::endl
          << "   Return Trace Code: " << ret_trace << std::endl
          << "   Code: | "
          << rld::find_replace (code, "\n", "\n         | ") << std::endl
          << "   Function Signatures: " << sigs.size () << std::endl;
      for (function_sigs::const_iterator si = sigs.begin ();
           si != sigs.end ();
           ++si)
      {
        const function_sig& sig = (*si).second;
        out << "    " << sig.name << ": " << sig.decl () << ';' << std::endl;
      }
    }

    tracer::tracer ()
    {
    }

    void
    tracer::load (rld::config::config& config,
                  const std::string&   section)
    {
      /*
       * The configuration must contain a "trace" section. This is the top level
       * configuration and must the following fields:
       *
       *  # name    The name of trace being linked.
       *  # trace   The list of sections containing functions to trace.
       *  # wrapper The list of sections containing wrapping details.
       *
       * The following record are optional:
       *
       *  # bdp     The BSP the executable is for. Can be supplied on the command
       *            line.
       *  # include Include the INI file.
       *
       * The following will throw an error is the section or records are not
       * found.
       */
      rld::strings ss;

      const rld::config::section& tsec = config.get_section (section);
      const rld::config::record&  nrec = tsec.get_record ("name");
      const rld::config::record&  brec = tsec.get_record ("bsp");
      const rld::config::record&  trec = tsec.get_record ("trace");
      const rld::config::record&  wrec = tsec.get_record ("wrapper");

      if (!nrec.single ())
        throw rld::error ("duplicate", "trace names");
      name = nrec.items[0].text;

      if (!brec.single ())
        throw rld::error ("duplicate", "trace bsp");
      bsp = brec.items[0].text;

      /*
       * Include any files.
       */
      config.includes (tsec);

      /*
       * Load the wrappers.
       */
      rld::strings wi;
      rld::config::parse_items (wrec, wi);
      for (rld::strings::const_iterator wsi = wi.begin ();
           wsi != wi.end ();
           ++wsi)
      {
        wrappers.push_back (wrapper (*wsi, config));
      }

      /*
       * Load the trace functions.
       */
      rld::strings ti;
      rld::config::parse_items (trec, ti);
      for (rld::strings::const_iterator tsi = ti.begin ();
           tsi != ti.end ();
           ++tsi)
      {
        rld::config::parse_items (config, *tsi, "trace", trace, true);
      }

    }

    void
    tracer::dump (std::ostream& out) const
    {
      out << " Tracer: " << name << std::endl
          << "  BSP: " << bsp << std::endl;
      for (wrappers::const_iterator wi = wrappers.begin ();
           wi != wrappers.end ();
           ++wi)
      {
        (*wi).dump (out);
      }
    }

    linker::linker ()
    {
    }

    void
    linker::load_config (const std::string& path,
                         const std::string& trace)
    {
      config.clear ();
      config.load (path);
      tracer.load (config, trace);
    }

    void
    linker::dump (std::ostream& out) const
    {
      const rld::config::paths& cpaths = config.get_paths ();
      out << "RTEMS Trace Linker" << std::endl
          << " Configuration Files: " << cpaths.size () << std::endl;
      for (rld::config::paths::const_iterator pi = cpaths.begin ();
           pi != cpaths.end ();
           ++pi)
      {
        out << "  " << (*pi) << std::endl;
      }

      tracer.dump (out);
    }
  }
}

/**
 * RTEMS Trace Linker options. This needs to be rewritten to be like cc where
 * only a single '-' and long options is present. Anything after '--' is passed
 * to RTEMS's real linker.
 */
static struct option rld_opts[] = {
  { "help",        no_argument,            NULL,           'h' },
  { "version",     no_argument,            NULL,           'V' },
  { "verbose",     no_argument,            NULL,           'v' },
  { "warn",        no_argument,            NULL,           'w' },
  { "exec-prefix", required_argument,      NULL,           'E' },
  { "march",       required_argument,      NULL,           'a' },
  { "mcpu",        required_argument,      NULL,           'c' },
  { "config",      required_argument,      NULL,           'C' },
  { NULL,          0,                      NULL,            0 }
};

void
usage (int exit_code)
{
  std::cout << "rtems-trace-ld [options] objects" << std::endl
            << "Options and arguments:" << std::endl
            << " -h        : help (also --help)" << std::endl
            << " -V        : print linker 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
            << " -w        : generate warnings (also --warn)" << std::endl
            << " -E prefix : the RTEMS tool prefix (also --exec-prefix)" << std::endl
            << " -a march  : machine architecture (also --march)" << std::endl
            << " -c cpu    : machine architecture's CPU (also --mcpu)" << std::endl
            << " -C ini    : user configuration INI file (also --config)" << std::endl;
  ::exit (exit_code);
}

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

  rld::process::temporaries.clean_up ();

  /*
   * 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
}

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

  setup_signals ();

  try
  {
    rld::trace::linker linker;
    std::string        ld_cmd;
    std::string        configuration;
    std::string        trace = "tracer";
    bool               exec_prefix_set = false;
#if HAVE_WARNINGS
    bool               warnings = false;
#endif

    while (true)
    {
      int opt = ::getopt_long (argc, argv, "hvwVE:a:c:C:", rld_opts, NULL);
      if (opt < 0)
        break;

      switch (opt)
      {
        case 'V':
          std::cout << "rtems-trace-ld (RTEMS Trace Linker) " << rld::version ()
                    << std::endl;
          ::exit (0);
          break;

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

        case 'w':
#if HAVE_WARNINGS
          warnings = true;
#endif
          break;

        case 'E':
          exec_prefix_set = true;
          rld::cc::exec_prefix = optarg;
          break;

        case 'a':
          rld::cc::march = optarg;
          break;

        case 'c':
          rld::cc::mcpu = optarg;
          break;

        case 'C':
          configuration = optarg;
          break;

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

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

    argc -= optind;
    argv += optind;

    if (rld::verbose ())
      std::cout << "RTEMS Trace Linker " << rld::version () << std::endl;

    /*
     * Load the remaining command line arguments into the linker command line.
     */
    while (argc--)
    {
      if (ld_cmd.length () != 0)
        ld_cmd += " ";
      ld_cmd += *argv++;
    }

    /*
     * If there are no object files there is nothing to link.
     */
    if (ld_cmd.empty ())
      throw rld::error ("no trace linker options", "options");

    /*
     * Perform a trace link.
     */
    try
    {
      linker.load_config (configuration, trace);
      linker.dump (std::cout);
    }
    catch (...)
    {
      throw;
    }

  }
  catch (rld::error re)
  {
    std::cerr << "error: "
              << re.where << ": " << re.what
              << std::endl;
    ec = 10;
  }
  catch (std::exception e)
  {
    int   status;
    char* realname;
    realname = abi::__cxa_demangle (e.what(), 0, 0, &status);
    std::cerr << "error: exception: " << realname << " [";
    ::free (realname);
    const std::type_info &ti = typeid (e);
    realname = abi::__cxa_demangle (ti.name(), 0, 0, &status);
    std::cerr << realname << "] " << e.what () << std::endl;
    ::free (realname);
    ec = 11;
  }
  catch (...)
  {
    /*
     * Helps to know if this happens.
     */
    std::cout << "error: unhandled exception" << std::endl;
    ec = 12;
  }

  return ec;
}