[ea29902] | 1 | /* |
---|
[29ad92c5] | 2 | * Copyright (c) 2014-2015, Chris Johns <chrisj@rtems.org> |
---|
[ea29902] | 3 | * |
---|
| 4 | * Permission to use, copy, modify, and/or distribute this software for any |
---|
| 5 | * purpose with or without fee is hereby granted, provided that the above |
---|
| 6 | * copyright notice and this permission notice appear in all copies. |
---|
| 7 | * |
---|
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
| 15 | */ |
---|
| 16 | /** |
---|
| 17 | * @file |
---|
| 18 | * |
---|
| 19 | * @ingroup rtems_rld |
---|
| 20 | * |
---|
| 21 | * @brief RTEMS Trace Linker manages creating a tracable RTEMS executable. |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #if HAVE_CONFIG_H |
---|
| 26 | #include "config.h" |
---|
| 27 | #endif |
---|
| 28 | |
---|
| 29 | #include <algorithm> |
---|
| 30 | #include <cctype> |
---|
| 31 | #include <functional> |
---|
| 32 | #include <iostream> |
---|
[29ad92c5] | 33 | #include <iomanip> |
---|
[ea29902] | 34 | #include <locale> |
---|
| 35 | #include <sstream> |
---|
| 36 | |
---|
| 37 | #include <cxxabi.h> |
---|
| 38 | #include <signal.h> |
---|
| 39 | #include <stdlib.h> |
---|
| 40 | #include <string.h> |
---|
| 41 | #include <unistd.h> |
---|
| 42 | |
---|
| 43 | #include <getopt.h> |
---|
| 44 | |
---|
| 45 | #include <rld.h> |
---|
| 46 | #include <rld-cc.h> |
---|
| 47 | #include <rld-config.h> |
---|
| 48 | #include <rld-process.h> |
---|
[31bf375] | 49 | #include <rld-rtems.h> |
---|
[ea29902] | 50 | |
---|
| 51 | #ifndef HAVE_KILL |
---|
| 52 | #define kill(p,s) raise(s) |
---|
| 53 | #endif |
---|
| 54 | |
---|
| 55 | namespace rld |
---|
| 56 | { |
---|
| 57 | /** |
---|
[b6d7f5f] | 58 | * RTEMS Trace Linker. |
---|
[ea29902] | 59 | */ |
---|
[b6d7f5f] | 60 | namespace trace |
---|
[ea29902] | 61 | { |
---|
[5025439] | 62 | /** |
---|
| 63 | * Dump on error user option. |
---|
| 64 | */ |
---|
| 65 | bool dump_on_error; |
---|
| 66 | |
---|
[b6d7f5f] | 67 | /** |
---|
| 68 | * A container of arguments. |
---|
| 69 | */ |
---|
| 70 | typedef std::vector < std::string > function_args; |
---|
[ea29902] | 71 | |
---|
[b6d7f5f] | 72 | /** |
---|
| 73 | * The return value. |
---|
| 74 | */ |
---|
| 75 | typedef std::string function_return; |
---|
[ea29902] | 76 | |
---|
[29ad92c5] | 77 | /** |
---|
| 78 | * An option is a name and value pair. We consider options as global. |
---|
| 79 | */ |
---|
| 80 | struct option |
---|
| 81 | { |
---|
| 82 | std::string name; /**< The name of this option. */ |
---|
| 83 | std::string value; /**< The option's value.. */ |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * Load the option. |
---|
| 87 | */ |
---|
| 88 | option (const std::string& name, const std::string& value) |
---|
| 89 | : name (name), |
---|
| 90 | value (value) { |
---|
| 91 | } |
---|
| 92 | }; |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | * A container of options. |
---|
| 96 | */ |
---|
| 97 | typedef std::vector < option > options; |
---|
| 98 | |
---|
[b6d7f5f] | 99 | /** |
---|
| 100 | * A function's signature. |
---|
| 101 | */ |
---|
[097f1fd] | 102 | struct signature |
---|
[b6d7f5f] | 103 | { |
---|
| 104 | std::string name; /**< The function's name. */ |
---|
| 105 | function_args args; /**< The function's list of arguments. */ |
---|
| 106 | function_return ret; /**< The fuctions return value. */ |
---|
[ea29902] | 107 | |
---|
[b6d7f5f] | 108 | /** |
---|
| 109 | * The default constructor. |
---|
| 110 | */ |
---|
[097f1fd] | 111 | signature (); |
---|
[ea29902] | 112 | |
---|
[b6d7f5f] | 113 | /** |
---|
| 114 | * Construct the signature loading it from the configuration. |
---|
| 115 | */ |
---|
[097f1fd] | 116 | signature (const rld::config::record& record); |
---|
[ea29902] | 117 | |
---|
[5025439] | 118 | /** |
---|
| 119 | * Has the signature got a return value ? |
---|
| 120 | */ |
---|
| 121 | bool has_ret () const; |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | * Has the signature got any arguments ? |
---|
| 125 | */ |
---|
| 126 | bool has_args () const; |
---|
| 127 | |
---|
[b6d7f5f] | 128 | /** |
---|
| 129 | * Return the function's declaration. |
---|
| 130 | */ |
---|
[6506aa1] | 131 | const std::string decl (const std::string& prefix = "") const; |
---|
[b6d7f5f] | 132 | }; |
---|
[ea29902] | 133 | |
---|
[b6d7f5f] | 134 | /** |
---|
[097f1fd] | 135 | * A container of signatures. |
---|
[b6d7f5f] | 136 | */ |
---|
[097f1fd] | 137 | typedef std::map < std::string, signature > signatures; |
---|
[ea29902] | 138 | |
---|
| 139 | /** |
---|
[097f1fd] | 140 | * A function is list of function signatures headers and defines that allow |
---|
| 141 | * a function to be wrapped. |
---|
[ea29902] | 142 | */ |
---|
[097f1fd] | 143 | struct function |
---|
[b6d7f5f] | 144 | { |
---|
[097f1fd] | 145 | std::string name; /**< The name of this wrapper. */ |
---|
| 146 | rld::strings headers; /**< Include statements. */ |
---|
| 147 | rld::strings defines; /**< Define statements. */ |
---|
| 148 | signatures signatures_; /**< Signatures in this function. */ |
---|
[b6d7f5f] | 149 | |
---|
| 150 | /** |
---|
[097f1fd] | 151 | * Load the function. |
---|
[b6d7f5f] | 152 | */ |
---|
[097f1fd] | 153 | function (rld::config::config& config, |
---|
| 154 | const std::string& name); |
---|
[b6d7f5f] | 155 | |
---|
[058d502] | 156 | /** |
---|
[097f1fd] | 157 | * Dump the function. |
---|
[058d502] | 158 | */ |
---|
[097f1fd] | 159 | void dump (std::ostream& out) const; |
---|
| 160 | }; |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * A container of functions. |
---|
| 164 | */ |
---|
| 165 | typedef std::vector < function > functions; |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * A generator and that contains the functions used to trace arguments and |
---|
| 169 | * return values. It also provides the implementation of those functions. |
---|
| 170 | */ |
---|
| 171 | struct generator |
---|
| 172 | { |
---|
| 173 | std::string name; /**< The name of this wrapper. */ |
---|
[1703041] | 174 | std::string lock_local; /**< Code template to declare a local lock variable. */ |
---|
| 175 | std::string lock_acquire; /**< The lock acquire if provided. */ |
---|
| 176 | std::string lock_release; /**< The lock release if provided. */ |
---|
| 177 | std::string buffer_local; /**< Code template to declare a local buffer variable. */ |
---|
[097f1fd] | 178 | rld::strings headers; /**< Include statements. */ |
---|
| 179 | rld::strings defines; /**< Define statements. */ |
---|
[6fb1409] | 180 | std::string entry_trace; /**< Code template to trace the function entry. */ |
---|
[b988768] | 181 | std::string entry_alloc; /**< Code template to perform a buffer allocation. */ |
---|
[097f1fd] | 182 | std::string arg_trace; /**< Code template to trace an argument. */ |
---|
[6fb1409] | 183 | std::string exit_trace; /**< Code template to trace the function exit. */ |
---|
[b988768] | 184 | std::string exit_alloc; /**< Code template to perform a buffer allocation. */ |
---|
[097f1fd] | 185 | std::string ret_trace; /**< Code template to trace the return value. */ |
---|
| 186 | rld::strings code; /**< Code block inserted before the trace code. */ |
---|
[058d502] | 187 | |
---|
[0a21024] | 188 | /** |
---|
[097f1fd] | 189 | * Default constructor. |
---|
[0a21024] | 190 | */ |
---|
[097f1fd] | 191 | generator (); |
---|
[0a21024] | 192 | |
---|
[b6d7f5f] | 193 | /** |
---|
[097f1fd] | 194 | * Load the generator. |
---|
| 195 | */ |
---|
| 196 | generator (rld::config::config& config, |
---|
| 197 | const std::string& name); |
---|
| 198 | |
---|
| 199 | /** |
---|
| 200 | * Dump the generator. |
---|
[b6d7f5f] | 201 | */ |
---|
| 202 | void dump (std::ostream& out) const; |
---|
| 203 | }; |
---|
[ea29902] | 204 | |
---|
[b6d7f5f] | 205 | /** |
---|
| 206 | * Tracer. |
---|
| 207 | */ |
---|
| 208 | class tracer |
---|
| 209 | { |
---|
| 210 | public: |
---|
| 211 | tracer (); |
---|
[ea29902] | 212 | |
---|
[b6d7f5f] | 213 | /** |
---|
| 214 | * Load the user's configuration. |
---|
| 215 | */ |
---|
| 216 | void load (rld::config::config& config, |
---|
| 217 | const std::string& section); |
---|
[ea29902] | 218 | |
---|
[5025439] | 219 | /** |
---|
| 220 | * Process any script based options. |
---|
| 221 | */ |
---|
[29ad92c5] | 222 | void load_options (rld::config::config& config, |
---|
| 223 | const rld::config::section& section); |
---|
| 224 | |
---|
| 225 | /** |
---|
| 226 | * The defines for the trace. |
---|
| 227 | */ |
---|
| 228 | void load_defines (rld::config::config& config, |
---|
| 229 | const rld::config::section& section); |
---|
[5025439] | 230 | |
---|
[097f1fd] | 231 | /** |
---|
[29ad92c5] | 232 | * The functions for the trace. |
---|
[097f1fd] | 233 | */ |
---|
| 234 | void load_functions (rld::config::config& config, |
---|
| 235 | const rld::config::section& section); |
---|
| 236 | |
---|
| 237 | /** |
---|
[29ad92c5] | 238 | * The enables for the tracer. |
---|
| 239 | */ |
---|
| 240 | void load_enables (rld::config::config& config, |
---|
| 241 | const rld::config::section& section); |
---|
| 242 | |
---|
| 243 | /** |
---|
| 244 | * The triggers for the tracer. |
---|
| 245 | */ |
---|
| 246 | void load_triggers (rld::config::config& config, |
---|
| 247 | const rld::config::section& section); |
---|
| 248 | |
---|
| 249 | /** |
---|
| 250 | * The traces for the tracer. |
---|
[097f1fd] | 251 | */ |
---|
| 252 | void load_traces (rld::config::config& config, |
---|
| 253 | const rld::config::section& section); |
---|
| 254 | |
---|
[29ad92c5] | 255 | /** |
---|
| 256 | * Get option from the options section. |
---|
| 257 | */ |
---|
| 258 | const std::string get_option (const std::string& name) const; |
---|
| 259 | |
---|
[4fd758e] | 260 | /** |
---|
| 261 | * Generate the wrapper object file. |
---|
| 262 | */ |
---|
[c4c8218] | 263 | void generate (rld::process::tempfile& c); |
---|
[4fd758e] | 264 | |
---|
[29ad92c5] | 265 | /** |
---|
| 266 | * Generate the trace names as a string table. |
---|
| 267 | */ |
---|
| 268 | void generate_names (rld::process::tempfile& c); |
---|
| 269 | |
---|
| 270 | /** |
---|
| 271 | * Generate the enabled trace bitmap. |
---|
| 272 | */ |
---|
| 273 | void generate_enables (rld::process::tempfile& c); |
---|
| 274 | |
---|
| 275 | /** |
---|
| 276 | * Generate the triggered trace bitmap. |
---|
| 277 | */ |
---|
| 278 | void generate_triggers (rld::process::tempfile& c); |
---|
| 279 | |
---|
[4fd758e] | 280 | /** |
---|
| 281 | * Generate the trace functions. |
---|
| 282 | */ |
---|
| 283 | void generate_traces (rld::process::tempfile& c); |
---|
| 284 | |
---|
[29ad92c5] | 285 | /** |
---|
| 286 | * Generate a bitmap. |
---|
| 287 | */ |
---|
| 288 | void generate_bitmap (rld::process::tempfile& c, |
---|
| 289 | const rld::strings& names, |
---|
| 290 | const std::string& label, |
---|
| 291 | const bool global_set); |
---|
| 292 | |
---|
[6506aa1] | 293 | /** |
---|
| 294 | * Get the traces. |
---|
| 295 | */ |
---|
| 296 | const rld::strings& get_traces () const; |
---|
| 297 | |
---|
[b6d7f5f] | 298 | /** |
---|
| 299 | * Dump the wrapper. |
---|
| 300 | */ |
---|
| 301 | void dump (std::ostream& out) const; |
---|
[ea29902] | 302 | |
---|
[b6d7f5f] | 303 | private: |
---|
[ea29902] | 304 | |
---|
[5025439] | 305 | std::string name; /**< The name of the trace. */ |
---|
[29ad92c5] | 306 | rld::strings defines; /**< Define statements. */ |
---|
| 307 | rld::strings enables; /**< The default enabled functions. */ |
---|
| 308 | rld::strings triggers; /**< The default trigger functions. */ |
---|
[5025439] | 309 | rld::strings traces; /**< The functions to trace. */ |
---|
[29ad92c5] | 310 | options options_; /**< The options. */ |
---|
[5025439] | 311 | functions functions_; /**< The functions that can be traced. */ |
---|
| 312 | generator generator_; /**< The tracer's generator. */ |
---|
[b6d7f5f] | 313 | }; |
---|
| 314 | |
---|
| 315 | /** |
---|
| 316 | * Trace Linker. |
---|
[ea29902] | 317 | */ |
---|
[b6d7f5f] | 318 | class linker |
---|
| 319 | { |
---|
| 320 | public: |
---|
| 321 | linker (); |
---|
[ea29902] | 322 | |
---|
[b6d7f5f] | 323 | /** |
---|
| 324 | * Load the user's configuration. |
---|
| 325 | */ |
---|
[29ad92c5] | 326 | void load_config (const std::string& name, |
---|
| 327 | const std::string& trace, |
---|
| 328 | const std::string& path); |
---|
[ea29902] | 329 | |
---|
[b6d7f5f] | 330 | /** |
---|
| 331 | * Generate the C file. |
---|
| 332 | */ |
---|
[c4c8218] | 333 | void generate_wrapper (rld::process::tempfile& c); |
---|
[b6d7f5f] | 334 | |
---|
[8807135] | 335 | /** |
---|
| 336 | * Compile the C file. |
---|
| 337 | */ |
---|
[c4c8218] | 338 | void compile_wrapper (rld::process::tempfile& c, |
---|
| 339 | rld::process::tempfile& o); |
---|
[8807135] | 340 | |
---|
[b28e8b3] | 341 | /** |
---|
| 342 | * Link the application. |
---|
| 343 | */ |
---|
| 344 | void link (rld::process::tempfile& o, |
---|
| 345 | const std::string& ld_cmds); |
---|
| 346 | |
---|
[b6d7f5f] | 347 | /** |
---|
| 348 | * Dump the linker. |
---|
| 349 | */ |
---|
| 350 | void dump (std::ostream& out) const; |
---|
| 351 | |
---|
| 352 | private: |
---|
| 353 | |
---|
[c4c8218] | 354 | rld::config::config config; /**< User configuration. */ |
---|
| 355 | tracer tracer_; /**< The tracer */ |
---|
| 356 | rld::process::tempfile c; /**< The C wrapper file */ |
---|
| 357 | rld::process::tempfile o; /**< The wrapper object file */ |
---|
[b6d7f5f] | 358 | }; |
---|
| 359 | |
---|
[097f1fd] | 360 | /** |
---|
| 361 | * Recursive parser for strings. |
---|
| 362 | */ |
---|
| 363 | void |
---|
| 364 | parse (rld::config::config& config, |
---|
| 365 | const rld::config::section& section, |
---|
| 366 | const std::string& sec_name, |
---|
| 367 | const std::string& rec_name, |
---|
| 368 | rld::strings& items, |
---|
| 369 | bool split = true, |
---|
| 370 | int depth = 0) |
---|
| 371 | { |
---|
| 372 | if (depth > 32) |
---|
| 373 | throw rld::error ("too deep", "parsing: " + sec_name + '/' + rec_name); |
---|
| 374 | |
---|
| 375 | rld::config::parse_items (section, rec_name, items, false, false, split); |
---|
| 376 | |
---|
| 377 | rld::strings sl; |
---|
| 378 | |
---|
| 379 | rld::config::parse_items (section, sec_name, sl); |
---|
| 380 | |
---|
| 381 | for (rld::strings::iterator sli = sl.begin (); |
---|
| 382 | sli != sl.end (); |
---|
| 383 | ++sli) |
---|
| 384 | { |
---|
| 385 | const rld::config::section& sec = config.get_section (*sli); |
---|
| 386 | parse (config, sec, sec_name, rec_name, items, split, depth + 1); |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | /* |
---|
| 390 | * Make the items unique. |
---|
| 391 | */ |
---|
| 392 | rld::strings::iterator ii; |
---|
| 393 | ii = std::unique (items.begin (), items.end ()); |
---|
| 394 | items.resize (std::distance (items.begin (), ii)); |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | signature::signature () |
---|
[ea29902] | 398 | { |
---|
[b6d7f5f] | 399 | } |
---|
[ea29902] | 400 | |
---|
[097f1fd] | 401 | signature::signature (const rld::config::record& record) |
---|
[b6d7f5f] | 402 | { |
---|
| 403 | /* |
---|
| 404 | * There can only be one function signature in the configuration. |
---|
| 405 | */ |
---|
| 406 | if (!record.single ()) |
---|
[097f1fd] | 407 | throw rld::error ("duplicate", "signature: " + record.name); |
---|
[b6d7f5f] | 408 | |
---|
| 409 | name = record.name; |
---|
| 410 | |
---|
| 411 | /* |
---|
[097f1fd] | 412 | * Signatures are defined as the return value then the arguments |
---|
[b6d7f5f] | 413 | * delimited by a comma and white space. No checking is made of the |
---|
| 414 | * return value or arguments. |
---|
| 415 | */ |
---|
| 416 | rld::strings si; |
---|
| 417 | rld::config::parse_items (record, si); |
---|
| 418 | |
---|
| 419 | if (si.size () == 0) |
---|
[5025439] | 420 | throw rld::error ("no return value", "signature: " + name); |
---|
[b6d7f5f] | 421 | if (si.size () == 1) |
---|
[5025439] | 422 | throw rld::error ("no arguments", "signature: " + name); |
---|
[b6d7f5f] | 423 | |
---|
| 424 | ret = si[0]; |
---|
| 425 | args.resize (si.size () - 1); |
---|
| 426 | std::copy (si.begin () + 1, si.end (), args.begin ()); |
---|
| 427 | } |
---|
| 428 | |
---|
[5025439] | 429 | bool |
---|
| 430 | signature::has_ret () const |
---|
| 431 | { |
---|
| 432 | /* |
---|
| 433 | * @todo Need to define as part of the function signature if ret |
---|
| 434 | * processing is required. |
---|
| 435 | */ |
---|
| 436 | return ret != "void"; |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | bool |
---|
| 440 | signature::has_args () const |
---|
| 441 | { |
---|
| 442 | if (args.empty ()) |
---|
| 443 | return false; |
---|
| 444 | return ((args.size() == 1) && (args[0] == "void")) ? false : true; |
---|
| 445 | } |
---|
| 446 | |
---|
[b6d7f5f] | 447 | const std::string |
---|
[6506aa1] | 448 | signature::decl (const std::string& prefix) const |
---|
[b6d7f5f] | 449 | { |
---|
[6506aa1] | 450 | std::string ds = ret + ' ' + prefix + name + '('; |
---|
[5025439] | 451 | if (has_args ()) |
---|
| 452 | { |
---|
| 453 | int arg = 0; |
---|
| 454 | for (function_args::const_iterator ai = args.begin (); |
---|
| 455 | ai != args.end (); |
---|
| 456 | ++ai) |
---|
[b6d7f5f] | 457 | { |
---|
| 458 | if (ai != args.begin ()) |
---|
| 459 | ds += ", "; |
---|
| 460 | ds += (*ai) + " a" + rld::to_string (++arg); |
---|
| 461 | } |
---|
[5025439] | 462 | } |
---|
| 463 | else |
---|
| 464 | { |
---|
| 465 | ds += "void"; |
---|
| 466 | } |
---|
[b6d7f5f] | 467 | ds += ')'; |
---|
| 468 | return ds; |
---|
| 469 | } |
---|
[ea29902] | 470 | |
---|
[097f1fd] | 471 | function::function (rld::config::config& config, |
---|
| 472 | const std::string& name) |
---|
| 473 | : name (name) |
---|
[b6d7f5f] | 474 | { |
---|
[ea29902] | 475 | /* |
---|
[097f1fd] | 476 | * A function section optionally contain one or more records of: |
---|
[b6d7f5f] | 477 | * |
---|
[097f1fd] | 478 | * # headers A list of sections containing headers or header records. |
---|
| 479 | * # header A list of include string that are single or double quoted. |
---|
| 480 | * # defines A list of sections containing defines or define record. |
---|
| 481 | * # defines A list of define string that are single or double quoted. |
---|
| 482 | * # signatures A list of section names of signatures. |
---|
| 483 | * # includes A list of files to include. |
---|
[ea29902] | 484 | * |
---|
[b6d7f5f] | 485 | * @note The quoting and list spliting is a little weak because a delimiter |
---|
| 486 | * in a quote should not be seen as a delimiter. |
---|
[ea29902] | 487 | */ |
---|
[b6d7f5f] | 488 | const rld::config::section& section = config.get_section (name); |
---|
[ea29902] | 489 | |
---|
[097f1fd] | 490 | config.includes (section); |
---|
| 491 | |
---|
[0a21024] | 492 | parse (config, section, "headers", "header", headers); |
---|
| 493 | parse (config, section, "defines", "define", defines); |
---|
| 494 | |
---|
[b6d7f5f] | 495 | rld::strings sig_list; |
---|
[097f1fd] | 496 | section.get_record_items ("signatures", sig_list); |
---|
[b6d7f5f] | 497 | |
---|
| 498 | for (rld::strings::const_iterator sli = sig_list.begin (); |
---|
| 499 | sli != sig_list.end (); |
---|
| 500 | ++sli) |
---|
[ea29902] | 501 | { |
---|
[5025439] | 502 | rld::strings sigs; |
---|
| 503 | rld::split(sigs, *sli, ','); |
---|
| 504 | |
---|
| 505 | for (rld::strings::const_iterator ssi = sigs.begin (); |
---|
| 506 | ssi != sigs.end (); |
---|
| 507 | ++ssi) |
---|
[b6d7f5f] | 508 | { |
---|
[5025439] | 509 | const rld::config::section& sig_sec = config.get_section (*ssi); |
---|
| 510 | for (rld::config::records::const_iterator si = sig_sec.recs.begin (); |
---|
| 511 | si != sig_sec.recs.end (); |
---|
| 512 | ++si) |
---|
| 513 | { |
---|
| 514 | signature sig (*si); |
---|
| 515 | signatures_[sig.name] = sig; |
---|
| 516 | } |
---|
[b6d7f5f] | 517 | } |
---|
[ea29902] | 518 | } |
---|
| 519 | } |
---|
[b6d7f5f] | 520 | |
---|
[058d502] | 521 | void |
---|
[097f1fd] | 522 | function::dump (std::ostream& out) const |
---|
[058d502] | 523 | { |
---|
[097f1fd] | 524 | out << " Function: " << name << std::endl |
---|
| 525 | << " Headers: " << headers.size () << std::endl; |
---|
| 526 | for (rld::strings::const_iterator hi = headers.begin (); |
---|
| 527 | hi != headers.end (); |
---|
| 528 | ++hi) |
---|
[058d502] | 529 | { |
---|
[097f1fd] | 530 | out << " " << (*hi) << std::endl; |
---|
[058d502] | 531 | } |
---|
[097f1fd] | 532 | out << " Defines: " << defines.size () << std::endl; |
---|
| 533 | for (rld::strings::const_iterator di = defines.begin (); |
---|
| 534 | di != defines.end (); |
---|
| 535 | ++di) |
---|
[058d502] | 536 | { |
---|
[097f1fd] | 537 | out << " " << (*di) << std::endl; |
---|
[058d502] | 538 | } |
---|
[097f1fd] | 539 | out << " Signatures: " << signatures_.size () << std::endl; |
---|
| 540 | for (signatures::const_iterator si = signatures_.begin (); |
---|
| 541 | si != signatures_.end (); |
---|
| 542 | ++si) |
---|
[058d502] | 543 | { |
---|
[097f1fd] | 544 | const signature& sig = (*si).second; |
---|
| 545 | out << " " << sig.name << ": " << sig.decl () << ';' << std::endl; |
---|
[058d502] | 546 | } |
---|
| 547 | } |
---|
| 548 | |
---|
[097f1fd] | 549 | generator::generator () |
---|
[0a21024] | 550 | { |
---|
[097f1fd] | 551 | } |
---|
[0a21024] | 552 | |
---|
[097f1fd] | 553 | generator::generator (rld::config::config& config, |
---|
| 554 | const std::string& name) |
---|
| 555 | : name (name) |
---|
| 556 | { |
---|
| 557 | /* |
---|
| 558 | * A generator section optionally contain one or more records of: |
---|
| 559 | * |
---|
[1703041] | 560 | * # headers A list of sections containing headers or header records. |
---|
| 561 | * # header A list of include string that are single or double quoted. |
---|
| 562 | * # defines A list of sections containing defines or define record. |
---|
| 563 | * # define A list of define string that are single or double quoted. |
---|
| 564 | * # entry-trace The wrapper call made on a function's entry. Returns `bool |
---|
| 565 | * where `true` is the function is being traced. This call is made |
---|
| 566 | * without the lock being held if a lock is defined. |
---|
| 567 | * # arg-trace The wrapper call made for each argment to the trace function if |
---|
| 568 | * the function is being traced. This call is made without the |
---|
| 569 | * lock being held if a lock is defined. |
---|
| 570 | * # exit-trace The wrapper call made after a function's exit. Returns `bool |
---|
| 571 | * where `true` is the function is being traced. This call is made |
---|
| 572 | * without the lock being held if a lock is defined. |
---|
| 573 | * # ret-trace The wrapper call made to log the return value if the funciton |
---|
| 574 | * is being traced. This call is made without the lock being held |
---|
| 575 | * if a lock is defined. |
---|
| 576 | * # lock-local The wrapper code to declare a local lock variable. |
---|
| 577 | * # lock-acquire The wrapper code to acquire the lock. |
---|
| 578 | * # lock-release The wrapper code to release the lock. |
---|
| 579 | * # buffer-local The wrapper code to declare a buffer index local variable. |
---|
| 580 | * # buffer-alloc The wrapper call made with a lock held if defined to allocate |
---|
| 581 | * buffer space to hold the trace data. A suitable 32bit buffer |
---|
| 582 | * index is returned. If there is no space an invalid index is |
---|
| 583 | * returned. The generator must handle any overhead space needed. |
---|
| 584 | * the generator needs to make sure the space is available before |
---|
| 585 | * making the alloc all. |
---|
| 586 | * # code-blocks A list of code blcok section names. |
---|
| 587 | * # code A code block in `<<CODE ... CODE` (without the single quote). |
---|
| 588 | * # includes A list of files to include. |
---|
| 589 | * |
---|
| 590 | * The following macros can be used in specific wrapper calls. The lists of |
---|
| 591 | * where you can use them is listed before. The macros are: |
---|
| 592 | * |
---|
| 593 | * # @FUNC_NAME@ The trace function name as a quote C string. |
---|
| 594 | * # @FUNC_INDEX@ The trace function index as a held in the sorted list |
---|
| 595 | * of trace functions by teh trace linker. It can be |
---|
| 596 | * used to index the `names`, `enables` and `triggers` |
---|
| 597 | * data. |
---|
| 598 | * # @FUNC_LABEL@ The trace function name as a C label that can be |
---|
| 599 | * referenced. You can take the address of the label. |
---|
| 600 | * # @FUNC_DATA_SIZE@ The size of the daya in bytes. |
---|
| 601 | * # @ARG_NUM@ The argument number to the trace function. |
---|
| 602 | * # @ARG_TYPE@ The type of the argument as a C string. |
---|
| 603 | * # @ARG_SIZE@ The size of the type of the argument in bytes. |
---|
| 604 | * # @ARG_LABEL@ The argument as a C label that can be referenced. |
---|
| 605 | * # @RET_TYPE@ The type of the return value as a C string. |
---|
| 606 | * # @RET_SIZE@ The size of the type of the return value in bytes. |
---|
| 607 | * # @RET_LABEL@ The retrun value as a C label that can be referenced. |
---|
| 608 | * |
---|
| 609 | * The `buffer-alloc`, `entry-trace` and `exit-trace` can be transformed using |
---|
| 610 | * the following macros: |
---|
| 611 | * |
---|
| 612 | * # @FUNC_NAME@ |
---|
| 613 | * # @FUNC_INDEX@ |
---|
| 614 | * # @FUNC_LABEL@ |
---|
| 615 | * # @FUNC_DATA_SZIE@ |
---|
| 616 | * |
---|
| 617 | * The `arg-trace` can be transformed using the following macros: |
---|
| 618 | * |
---|
| 619 | * # @ARG_NUM@ |
---|
| 620 | * # @ARG_TYPE@ |
---|
| 621 | * # @ARG_SIZE@ |
---|
| 622 | * # @ARG_LABEL@ |
---|
| 623 | * |
---|
| 624 | * The `ret-trace` can be transformed using the following macros: |
---|
| 625 | * |
---|
| 626 | * # @RET_TYPE@ |
---|
| 627 | * # @RET_SIZE@ |
---|
| 628 | * # @RET_LABEL@ |
---|
[097f1fd] | 629 | * |
---|
| 630 | * @note The quoting and list spliting is a little weak because a delimiter |
---|
| 631 | * in a quote should not be seen as a delimiter. |
---|
| 632 | */ |
---|
| 633 | const rld::config::section& section = config.get_section (name); |
---|
[0a21024] | 634 | |
---|
[097f1fd] | 635 | config.includes (section); |
---|
[0a21024] | 636 | |
---|
[097f1fd] | 637 | parse (config, section, "headers", "header", headers); |
---|
| 638 | parse (config, section, "defines", "define", defines); |
---|
| 639 | parse (config, section, "code-blocks", "code", code, false); |
---|
[0a21024] | 640 | |
---|
[1703041] | 641 | if (section.has_record ("lock-local")) |
---|
| 642 | lock_local = rld::dequote (section.get_record_item ("lock-local")); |
---|
| 643 | if (section.has_record ("lock-acquire")) |
---|
| 644 | lock_acquire = rld::dequote (section.get_record_item ("lock-acquire")); |
---|
| 645 | if (section.has_record ("lock-release")) |
---|
| 646 | lock_release = rld::dequote (section.get_record_item ("lock-release")); |
---|
| 647 | if (section.has_record ("buffer-local")) |
---|
| 648 | buffer_local = rld::dequote (section.get_record_item ("buffer-local")); |
---|
[6fb1409] | 649 | if (section.has_record ("entry-trace")) |
---|
| 650 | entry_trace = rld::dequote (section.get_record_item ("entry-trace")); |
---|
[b988768] | 651 | if (section.has_record ("entry-alloc")) |
---|
| 652 | entry_alloc = rld::dequote (section.get_record_item ("entry-alloc")); |
---|
[6fb1409] | 653 | if (section.has_record ("arg-trace")) |
---|
| 654 | arg_trace = rld::dequote (section.get_record_item ("arg-trace")); |
---|
| 655 | if (section.has_record ("exit-trace")) |
---|
| 656 | exit_trace = rld::dequote (section.get_record_item ("exit-trace")); |
---|
[b988768] | 657 | if (section.has_record ("exit-alloc")) |
---|
| 658 | exit_alloc = rld::dequote (section.get_record_item ("exit-alloc")); |
---|
[6fb1409] | 659 | if (section.has_record ("ret-trace")) |
---|
| 660 | ret_trace = rld::dequote (section.get_record_item ("ret-trace")); |
---|
[0a21024] | 661 | } |
---|
| 662 | |
---|
[b6d7f5f] | 663 | void |
---|
[097f1fd] | 664 | generator::dump (std::ostream& out) const |
---|
[b6d7f5f] | 665 | { |
---|
[097f1fd] | 666 | out << " Generator: " << name << std::endl |
---|
[b6d7f5f] | 667 | << " Headers: " << headers.size () << std::endl; |
---|
| 668 | for (rld::strings::const_iterator hi = headers.begin (); |
---|
| 669 | hi != headers.end (); |
---|
| 670 | ++hi) |
---|
| 671 | { |
---|
| 672 | out << " " << (*hi) << std::endl; |
---|
| 673 | } |
---|
| 674 | out << " Defines: " << defines.size () << std::endl; |
---|
| 675 | for (rld::strings::const_iterator di = defines.begin (); |
---|
| 676 | di != defines.end (); |
---|
| 677 | ++di) |
---|
| 678 | { |
---|
| 679 | out << " " << (*di) << std::endl; |
---|
| 680 | } |
---|
[2126ea7] | 681 | out << " Arg Trace Code: " << arg_trace << std::endl |
---|
[058d502] | 682 | << " Return Trace Code: " << ret_trace << std::endl |
---|
[097f1fd] | 683 | << " Code blocks: " << std::endl; |
---|
| 684 | for (rld::strings::const_iterator ci = code.begin (); |
---|
| 685 | ci != code.end (); |
---|
| 686 | ++ci) |
---|
[b6d7f5f] | 687 | { |
---|
[097f1fd] | 688 | out << " > " |
---|
| 689 | << rld::find_replace (*ci, "\n", "\n | ") << std::endl; |
---|
[b6d7f5f] | 690 | } |
---|
| 691 | } |
---|
| 692 | |
---|
| 693 | tracer::tracer () |
---|
| 694 | { |
---|
| 695 | } |
---|
| 696 | |
---|
| 697 | void |
---|
| 698 | tracer::load (rld::config::config& config, |
---|
[097f1fd] | 699 | const std::string& tname) |
---|
[ea29902] | 700 | { |
---|
| 701 | /* |
---|
[097f1fd] | 702 | * The configuration must contain a "section" section. This is the top level |
---|
| 703 | * configuration and may contain: |
---|
[b6d7f5f] | 704 | * |
---|
[097f1fd] | 705 | * # name The name of trace being linked. |
---|
[29ad92c5] | 706 | * # options A list of option sections. |
---|
| 707 | * # defines A list of sections containing defines or define record. |
---|
| 708 | * # define A list of define string that are single or double quoted. |
---|
| 709 | * # enables The list of sections containing enabled functions to trace. |
---|
| 710 | * # triggers The list of sections containing enabled functions to trigger trace on. |
---|
[097f1fd] | 711 | * # traces The list of sections containing function lists to trace. |
---|
| 712 | * # functions The list of sections containing function details. |
---|
| 713 | * # include The list of files to include. |
---|
[b6d7f5f] | 714 | * |
---|
[097f1fd] | 715 | * The following records are required: |
---|
[b6d7f5f] | 716 | * |
---|
[097f1fd] | 717 | * # name |
---|
[5025439] | 718 | * # traces |
---|
[097f1fd] | 719 | * # functions |
---|
[ea29902] | 720 | */ |
---|
[097f1fd] | 721 | const rld::config::section& section = config.get_section (tname); |
---|
[ea29902] | 722 | |
---|
[097f1fd] | 723 | name = section.get_record_item ("name"); |
---|
[29ad92c5] | 724 | load_options (config, section); |
---|
[5025439] | 725 | config.includes (section); |
---|
[29ad92c5] | 726 | load_defines (config, section); |
---|
[097f1fd] | 727 | load_functions (config, section); |
---|
[29ad92c5] | 728 | load_enables (config, section); |
---|
| 729 | load_triggers (config, section); |
---|
[097f1fd] | 730 | load_traces (config, section); |
---|
| 731 | } |
---|
[ea29902] | 732 | |
---|
[5025439] | 733 | void |
---|
[29ad92c5] | 734 | tracer::load_options (rld::config::config& config, |
---|
| 735 | const rld::config::section& section) |
---|
[5025439] | 736 | { |
---|
[29ad92c5] | 737 | rld::strings opts; |
---|
| 738 | rld::config::parse_items (section, "options", opts, false, true, true); |
---|
| 739 | |
---|
| 740 | if (rld::verbose ()) |
---|
| 741 | std::cout << "options: " << section.name << ": " << opts.size () << std::endl; |
---|
| 742 | |
---|
| 743 | options_.clear (); |
---|
[5025439] | 744 | |
---|
[29ad92c5] | 745 | for (rld::strings::const_iterator osi = opts.begin (); |
---|
| 746 | osi != opts.end (); |
---|
| 747 | ++osi) |
---|
[5025439] | 748 | { |
---|
[29ad92c5] | 749 | const rld::config::section& osec = config.get_section (*osi); |
---|
| 750 | |
---|
| 751 | if (rld::verbose ()) |
---|
| 752 | std::cout << " options: " << osec.name |
---|
| 753 | << ": recs:" << osec.recs.size () << std::endl; |
---|
| 754 | |
---|
| 755 | for (rld::config::records::const_iterator ori = osec.recs.begin (); |
---|
| 756 | ori != osec.recs.end (); |
---|
| 757 | ++ori) |
---|
[5025439] | 758 | { |
---|
[29ad92c5] | 759 | const rld::config::record& opt = *ori; |
---|
| 760 | |
---|
| 761 | if (!opt.single ()) |
---|
| 762 | throw rld::error ("mode than one option specified", "option: " + opt.name); |
---|
| 763 | |
---|
| 764 | options_.push_back (option (opt.name, opt[0])); |
---|
| 765 | |
---|
| 766 | if (opt.name == "dump-on-error") |
---|
| 767 | { |
---|
[5025439] | 768 | dump_on_error = true; |
---|
[29ad92c5] | 769 | } |
---|
| 770 | else if (opt.name == "verbose") |
---|
[5025439] | 771 | { |
---|
[29ad92c5] | 772 | int level = ::strtoul(opt[0].c_str (), 0, 0); |
---|
| 773 | if (level == 0) |
---|
| 774 | level = 1; |
---|
| 775 | for (int l = 0; l < level; ++l) |
---|
| 776 | rld::verbose_inc (); |
---|
[5025439] | 777 | } |
---|
[29ad92c5] | 778 | else if (opt.name == "prefix") |
---|
[5025439] | 779 | { |
---|
[29ad92c5] | 780 | rld::cc::set_exec_prefix (opt[0]); |
---|
[5025439] | 781 | } |
---|
[29ad92c5] | 782 | else if (opt.name == "cc") |
---|
[5025439] | 783 | { |
---|
[29ad92c5] | 784 | rld::cc::set_cc (opt[0]); |
---|
[5025439] | 785 | } |
---|
[29ad92c5] | 786 | else if (opt.name == "ld") |
---|
[5025439] | 787 | { |
---|
[29ad92c5] | 788 | rld::cc::set_ld (opt[0]); |
---|
| 789 | } |
---|
| 790 | else if (opt.name == "cflags") |
---|
| 791 | { |
---|
| 792 | rld::cc::append_flags (opt[0], rld::cc::ft_cflags); |
---|
| 793 | } |
---|
| 794 | else if (opt.name == "rtems-path") |
---|
| 795 | { |
---|
| 796 | rld::rtems::set_path(opt[0]); |
---|
| 797 | } |
---|
| 798 | else if (opt.name == "rtems-bsp") |
---|
| 799 | { |
---|
| 800 | rld::rtems::set_arch_bsp(opt[0]); |
---|
[5025439] | 801 | } |
---|
| 802 | } |
---|
| 803 | } |
---|
| 804 | } |
---|
| 805 | |
---|
[29ad92c5] | 806 | void |
---|
| 807 | tracer::load_defines (rld::config::config& config, |
---|
| 808 | const rld::config::section& section) |
---|
| 809 | { |
---|
| 810 | parse (config, section, "defines", "define", defines); |
---|
| 811 | } |
---|
| 812 | |
---|
[097f1fd] | 813 | void |
---|
| 814 | tracer::load_functions (rld::config::config& config, |
---|
| 815 | const rld::config::section& section) |
---|
| 816 | { |
---|
| 817 | rld::strings fl; |
---|
| 818 | rld::config::parse_items (section, "functions", fl, true); |
---|
| 819 | for (rld::strings::const_iterator fli = fl.begin (); |
---|
| 820 | fli != fl.end (); |
---|
| 821 | ++fli) |
---|
[b6d7f5f] | 822 | { |
---|
[097f1fd] | 823 | functions_.push_back (function (config, *fli)); |
---|
[b6d7f5f] | 824 | } |
---|
[097f1fd] | 825 | } |
---|
[ea29902] | 826 | |
---|
[29ad92c5] | 827 | void |
---|
| 828 | tracer::load_enables (rld::config::config& config, |
---|
| 829 | const rld::config::section& section) |
---|
| 830 | { |
---|
| 831 | parse (config, section, "enables", "enable", enables); |
---|
| 832 | } |
---|
| 833 | |
---|
| 834 | void |
---|
| 835 | tracer::load_triggers (rld::config::config& config, |
---|
| 836 | const rld::config::section& section) |
---|
| 837 | { |
---|
| 838 | parse (config, section, "triggers", "trigger", triggers); |
---|
| 839 | } |
---|
| 840 | |
---|
[097f1fd] | 841 | void |
---|
| 842 | tracer::load_traces (rld::config::config& config, |
---|
| 843 | const rld::config::section& section) |
---|
| 844 | { |
---|
| 845 | parse (config, section, "traces", "trace", traces); |
---|
| 846 | |
---|
| 847 | rld::strings gens; |
---|
| 848 | std::string gen; |
---|
| 849 | |
---|
| 850 | parse (config, section, "traces", "generator", gens); |
---|
| 851 | |
---|
| 852 | if (gens.size () > 1) |
---|
[5025439] | 853 | { |
---|
| 854 | if (dump_on_error) |
---|
| 855 | dump (std::cout); |
---|
[097f1fd] | 856 | throw rld::error ("duplicate generators", "tracer: " + section.name); |
---|
[5025439] | 857 | } |
---|
[097f1fd] | 858 | |
---|
| 859 | if (gens.size () == 0) |
---|
| 860 | { |
---|
[5025439] | 861 | const rld::config::section& dg_section = config.get_section ("default-generator"); |
---|
| 862 | gen = dg_section.get_record_item ("generator"); |
---|
| 863 | config.includes (dg_section); |
---|
[097f1fd] | 864 | } |
---|
| 865 | else |
---|
[ea29902] | 866 | { |
---|
[097f1fd] | 867 | gen = gens[0]; |
---|
[4fd758e] | 868 | } |
---|
| 869 | |
---|
[29ad92c5] | 870 | sort (traces.begin (), traces.end ()); |
---|
| 871 | |
---|
[097f1fd] | 872 | generator_ = generator (config, gen); |
---|
[4fd758e] | 873 | } |
---|
| 874 | |
---|
[29ad92c5] | 875 | const std::string |
---|
| 876 | tracer::get_option (const std::string& name) const |
---|
| 877 | { |
---|
| 878 | std::string value; |
---|
| 879 | for (options::const_iterator oi = options_.begin (); |
---|
| 880 | oi != options_.end (); |
---|
| 881 | ++oi) |
---|
| 882 | { |
---|
| 883 | const option& opt = *oi; |
---|
| 884 | if (opt.name == name) |
---|
| 885 | { |
---|
| 886 | value = opt.value; |
---|
| 887 | break; |
---|
| 888 | } |
---|
| 889 | } |
---|
| 890 | return value; |
---|
| 891 | } |
---|
| 892 | |
---|
[c4c8218] | 893 | void |
---|
| 894 | tracer::generate (rld::process::tempfile& c) |
---|
[4fd758e] | 895 | { |
---|
| 896 | c.open (true); |
---|
| 897 | |
---|
| 898 | if (rld::verbose ()) |
---|
| 899 | std::cout << "wrapper C file: " << c.name () << std::endl; |
---|
| 900 | |
---|
| 901 | try |
---|
| 902 | { |
---|
| 903 | c.write_line ("/*"); |
---|
| 904 | c.write_line (" * RTEMS Trace Linker Wrapper"); |
---|
| 905 | c.write_line (" * Automatically generated."); |
---|
| 906 | c.write_line (" */"); |
---|
| 907 | |
---|
[29ad92c5] | 908 | c.write_line (""); |
---|
| 909 | c.write_line ("/*"); |
---|
| 910 | c.write_line (" * Tracer: " + name); |
---|
| 911 | c.write_line (" */"); |
---|
| 912 | c.write_lines (defines); |
---|
| 913 | |
---|
[4fd758e] | 914 | c.write_line (""); |
---|
| 915 | c.write_line ("/*"); |
---|
[097f1fd] | 916 | c.write_line (" * Generator: " + generator_.name); |
---|
[4fd758e] | 917 | c.write_line (" */"); |
---|
[097f1fd] | 918 | c.write_lines (generator_.defines); |
---|
| 919 | c.write_lines (generator_.headers); |
---|
| 920 | c.write_line (""); |
---|
[29ad92c5] | 921 | generate_names (c); |
---|
| 922 | generate_enables (c); |
---|
| 923 | generate_triggers (c); |
---|
| 924 | c.write_line (""); |
---|
[097f1fd] | 925 | c.write_lines (generator_.code); |
---|
[4fd758e] | 926 | |
---|
| 927 | generate_traces (c); |
---|
| 928 | } |
---|
| 929 | catch (...) |
---|
| 930 | { |
---|
| 931 | c.close (); |
---|
[5025439] | 932 | if (dump_on_error) |
---|
| 933 | dump (std::cout); |
---|
[4fd758e] | 934 | throw; |
---|
[ea29902] | 935 | } |
---|
| 936 | |
---|
[4fd758e] | 937 | c.close (); |
---|
[5afb2b8] | 938 | |
---|
| 939 | if (rld::verbose (RLD_VERBOSE_DETAILS)) |
---|
| 940 | { |
---|
| 941 | std::cout << "Generated C file:" << std::endl; |
---|
| 942 | c.output (" ", std::cout, true); |
---|
| 943 | } |
---|
[4fd758e] | 944 | } |
---|
| 945 | |
---|
[29ad92c5] | 946 | void |
---|
| 947 | tracer::generate_names (rld::process::tempfile& c) |
---|
| 948 | { |
---|
| 949 | const std::string opt = get_option ("gen-names"); |
---|
| 950 | |
---|
| 951 | if (opt == "disable") |
---|
| 952 | return; |
---|
| 953 | |
---|
| 954 | c.write_line (""); |
---|
| 955 | c.write_line ("/*"); |
---|
| 956 | c.write_line (" * Names."); |
---|
| 957 | c.write_line (" */"); |
---|
| 958 | |
---|
| 959 | std::stringstream sss; |
---|
[b988768] | 960 | sss << "uint32_t __rtld_trace_names_size = " << traces.size() << ";" << std::endl |
---|
| 961 | << "const char const* __rtld_trace_names[" << traces.size() << "] = " << std::endl |
---|
[29ad92c5] | 962 | << "{"; |
---|
| 963 | c.write_line (sss.str ()); |
---|
| 964 | |
---|
| 965 | int count = 0; |
---|
| 966 | |
---|
| 967 | for (rld::strings::const_iterator ti = traces.begin (); |
---|
| 968 | ti != traces.end (); |
---|
| 969 | ++ti) |
---|
| 970 | { |
---|
| 971 | const std::string& trace = *ti; |
---|
| 972 | sss.str (std::string ()); |
---|
| 973 | sss << " /* " << std::setw (3) << count << " */ \"" << trace << "\","; |
---|
| 974 | c.write_line (sss.str ()); |
---|
| 975 | ++count; |
---|
| 976 | } |
---|
| 977 | |
---|
| 978 | c.write_line ("};"); |
---|
| 979 | } |
---|
| 980 | |
---|
| 981 | void |
---|
| 982 | tracer::generate_enables (rld::process::tempfile& c) |
---|
| 983 | { |
---|
| 984 | const std::string opt = get_option ("gen-enables"); |
---|
| 985 | bool global_state = false; |
---|
| 986 | |
---|
| 987 | if (opt == "disable") |
---|
| 988 | return; |
---|
| 989 | |
---|
| 990 | if (opt == "global-on") |
---|
| 991 | global_state = true; |
---|
| 992 | |
---|
| 993 | c.write_line (""); |
---|
| 994 | c.write_line ("/*"); |
---|
| 995 | c.write_line (" * Enables."); |
---|
| 996 | c.write_line (" */"); |
---|
| 997 | |
---|
| 998 | generate_bitmap (c, enables, "enables", global_state); |
---|
| 999 | } |
---|
| 1000 | |
---|
| 1001 | void |
---|
| 1002 | tracer::generate_triggers (rld::process::tempfile& c) |
---|
| 1003 | { |
---|
| 1004 | const std::string opt = get_option ("gen-triggers"); |
---|
| 1005 | bool global_state = false; |
---|
| 1006 | |
---|
| 1007 | if (opt == "disable") |
---|
| 1008 | return; |
---|
| 1009 | |
---|
| 1010 | if (opt == "global-on") |
---|
| 1011 | global_state = true; |
---|
| 1012 | |
---|
| 1013 | c.write_line (""); |
---|
| 1014 | c.write_line ("/*"); |
---|
| 1015 | c.write_line (" * Triggers."); |
---|
| 1016 | c.write_line (" */"); |
---|
| 1017 | |
---|
| 1018 | generate_bitmap (c, triggers, "triggers", global_state); |
---|
| 1019 | |
---|
| 1020 | c.write_line (""); |
---|
| 1021 | } |
---|
| 1022 | |
---|
[4fd758e] | 1023 | void |
---|
| 1024 | tracer::generate_traces (rld::process::tempfile& c) |
---|
| 1025 | { |
---|
[097f1fd] | 1026 | for (functions::const_iterator fi = functions_.begin (); |
---|
| 1027 | fi != functions_.end (); |
---|
| 1028 | ++fi) |
---|
| 1029 | { |
---|
| 1030 | const function& funcs = *fi; |
---|
| 1031 | |
---|
| 1032 | for (rld::strings::const_iterator ti = traces.begin (); |
---|
| 1033 | ti != traces.end (); |
---|
| 1034 | ++ti) |
---|
| 1035 | { |
---|
| 1036 | const std::string& trace = *ti; |
---|
| 1037 | signatures::const_iterator si = funcs.signatures_.find (trace); |
---|
| 1038 | |
---|
| 1039 | if (si != funcs.signatures_.end ()) |
---|
| 1040 | { |
---|
| 1041 | c.write_line (""); |
---|
| 1042 | c.write_line ("/*"); |
---|
| 1043 | c.write_line (" * Function: " + funcs.name); |
---|
| 1044 | c.write_line (" */"); |
---|
| 1045 | c.write_lines (funcs.defines); |
---|
| 1046 | c.write_lines (funcs.headers); |
---|
| 1047 | break; |
---|
| 1048 | } |
---|
| 1049 | } |
---|
| 1050 | } |
---|
| 1051 | |
---|
| 1052 | c.write_line (""); |
---|
| 1053 | c.write_line ("/*"); |
---|
| 1054 | c.write_line (" * Wrappers."); |
---|
| 1055 | c.write_line (" */"); |
---|
| 1056 | |
---|
[29ad92c5] | 1057 | size_t count = 0; |
---|
| 1058 | |
---|
[4fd758e] | 1059 | for (rld::strings::const_iterator ti = traces.begin (); |
---|
| 1060 | ti != traces.end (); |
---|
| 1061 | ++ti) |
---|
| 1062 | { |
---|
[097f1fd] | 1063 | const std::string& trace = *ti; |
---|
[4fd758e] | 1064 | bool found = false; |
---|
| 1065 | |
---|
[097f1fd] | 1066 | for (functions::const_iterator fi = functions_.begin (); |
---|
| 1067 | fi != functions_.end (); |
---|
| 1068 | ++fi) |
---|
[4fd758e] | 1069 | { |
---|
[097f1fd] | 1070 | const function& funcs = *fi; |
---|
| 1071 | signatures::const_iterator si = funcs.signatures_.find (trace); |
---|
[4fd758e] | 1072 | |
---|
[097f1fd] | 1073 | if (si != funcs.signatures_.end ()) |
---|
[4fd758e] | 1074 | { |
---|
| 1075 | found = true; |
---|
| 1076 | |
---|
[097f1fd] | 1077 | const signature& sig = (*si).second; |
---|
[4fd758e] | 1078 | |
---|
[1703041] | 1079 | std::stringstream lss; |
---|
| 1080 | lss << count; |
---|
| 1081 | |
---|
[4fd758e] | 1082 | c.write_line(""); |
---|
[1703041] | 1083 | |
---|
| 1084 | if (sig.has_args () || (sig.has_ret () && !generator_.ret_trace.empty ())) |
---|
| 1085 | { |
---|
[b988768] | 1086 | std::string ds; |
---|
| 1087 | std::string des; |
---|
| 1088 | std::string drs; |
---|
| 1089 | bool ds_added = false; |
---|
| 1090 | bool des_added = false; |
---|
| 1091 | bool drs_added = false; |
---|
| 1092 | ds = "#define FUNC_DATA_SIZE_" + sig.name + " ("; |
---|
| 1093 | des = "#define FUNC_DATA_ENTRY_SIZE_" + sig.name + " ("; |
---|
| 1094 | drs = "#define FUNC_DATA_RET_SIZE_" + sig.name + " ("; |
---|
[1703041] | 1095 | if (sig.has_args ()) |
---|
| 1096 | { |
---|
| 1097 | for (size_t a = 0; a < sig.args.size (); ++a) |
---|
| 1098 | { |
---|
[b988768] | 1099 | if (ds_added) |
---|
| 1100 | ds += " + "; |
---|
[1703041] | 1101 | else |
---|
[b988768] | 1102 | ds_added = true; |
---|
| 1103 | if (des_added) |
---|
| 1104 | des += " + "; |
---|
| 1105 | else |
---|
| 1106 | des_added = true; |
---|
| 1107 | ds += "sizeof(" + sig.args[a] + ')'; |
---|
| 1108 | des += "sizeof(" + sig.args[a] + ')'; |
---|
[1703041] | 1109 | } |
---|
| 1110 | } |
---|
| 1111 | if (sig.has_ret () && !generator_.ret_trace.empty ()) |
---|
| 1112 | { |
---|
[b988768] | 1113 | if (ds_added) |
---|
| 1114 | ds += " + "; |
---|
| 1115 | else |
---|
| 1116 | ds_added = true; |
---|
| 1117 | if (drs_added) |
---|
| 1118 | drs += " + "; |
---|
[1703041] | 1119 | else |
---|
[b988768] | 1120 | drs_added = true; |
---|
| 1121 | ds += "sizeof(" + sig.ret + ')'; |
---|
| 1122 | drs += "sizeof(" + sig.ret + ')'; |
---|
[1703041] | 1123 | } |
---|
[b988768] | 1124 | if (!ds_added) |
---|
| 1125 | ds += '0'; |
---|
| 1126 | ds += ')'; |
---|
| 1127 | if (!des_added) |
---|
| 1128 | des += '0'; |
---|
| 1129 | des += ')'; |
---|
| 1130 | if (!drs_added) |
---|
| 1131 | drs += '0'; |
---|
| 1132 | drs += ')'; |
---|
| 1133 | c.write_line(ds); |
---|
| 1134 | c.write_line(des); |
---|
| 1135 | c.write_line(drs); |
---|
[1703041] | 1136 | } |
---|
| 1137 | |
---|
[5afb2b8] | 1138 | c.write_line(sig.decl () + ";"); |
---|
| 1139 | c.write_line(sig.decl ("__real_") + ";"); |
---|
[6506aa1] | 1140 | c.write_line(sig.decl ("__wrap_")); |
---|
[4fd758e] | 1141 | c.write_line("{"); |
---|
| 1142 | |
---|
[1703041] | 1143 | if (!generator_.lock_local.empty ()) |
---|
| 1144 | c.write_line(generator_.lock_local); |
---|
| 1145 | |
---|
| 1146 | if (!generator_.buffer_local.empty ()) |
---|
| 1147 | c.write_line(generator_.buffer_local); |
---|
| 1148 | |
---|
[5025439] | 1149 | if (sig.has_ret ()) |
---|
[097f1fd] | 1150 | c.write_line(" " + sig.ret + " ret;"); |
---|
[2126ea7] | 1151 | |
---|
[b988768] | 1152 | std::string l; |
---|
| 1153 | |
---|
[1703041] | 1154 | if (!generator_.lock_acquire.empty ()) |
---|
| 1155 | c.write_line(generator_.lock_acquire); |
---|
[29ad92c5] | 1156 | |
---|
[b988768] | 1157 | if (!generator_.entry_alloc.empty ()) |
---|
[1703041] | 1158 | { |
---|
[b988768] | 1159 | l = " " + generator_.entry_alloc; |
---|
[1703041] | 1160 | l = rld::find_replace (l, "@FUNC_NAME@", '"' + sig.name + '"'); |
---|
| 1161 | l = rld::find_replace (l, "@FUNC_INDEX@", lss.str ()); |
---|
| 1162 | l = rld::find_replace (l, "@FUNC_LABEL@", sig.name); |
---|
| 1163 | l = rld::find_replace (l, "@FUNC_DATA_SIZE@", "FUNC_DATA_SIZE_" + sig.name); |
---|
[b988768] | 1164 | l = rld::find_replace (l, "@FUNC_DATA_ENTRY_SIZE@", "FUNC_DATA_ENTRY_SIZE_" + sig.name); |
---|
| 1165 | l = rld::find_replace (l, "@FUNC_DATA_RET_SIZE@", "FUNC_DATA_RET_SIZE_" + sig.name); |
---|
[1703041] | 1166 | c.write_line(l); |
---|
| 1167 | } |
---|
| 1168 | |
---|
| 1169 | if (!generator_.lock_release.empty ()) |
---|
| 1170 | c.write_line(generator_.lock_release); |
---|
[2126ea7] | 1171 | |
---|
[6fb1409] | 1172 | if (!generator_.entry_trace.empty ()) |
---|
[2126ea7] | 1173 | { |
---|
[1703041] | 1174 | l = " " + generator_.entry_trace; |
---|
[6fb1409] | 1175 | l = rld::find_replace (l, "@FUNC_NAME@", '"' + sig.name + '"'); |
---|
[29ad92c5] | 1176 | l = rld::find_replace (l, "@FUNC_INDEX@", lss.str ()); |
---|
[6fb1409] | 1177 | l = rld::find_replace (l, "@FUNC_LABEL@", sig.name); |
---|
[1703041] | 1178 | l = rld::find_replace (l, "@FUNC_DATA_SIZE@", "FUNC_DATA_SIZE_" + sig.name); |
---|
[b988768] | 1179 | l = rld::find_replace (l, "@FUNC_DATA_ENTRY_SIZE@", "FUNC_DATA_ENTRY_SIZE_" + sig.name); |
---|
| 1180 | l = rld::find_replace (l, "@FUNC_DATA_RET_SIZE@", "FUNC_DATA_RET_SIZE_" + sig.name); |
---|
[2126ea7] | 1181 | c.write_line(l); |
---|
[4fd758e] | 1182 | } |
---|
| 1183 | |
---|
[5025439] | 1184 | if (sig.has_args ()) |
---|
[6fb1409] | 1185 | { |
---|
| 1186 | for (size_t a = 0; a < sig.args.size (); ++a) |
---|
| 1187 | { |
---|
| 1188 | std::string n = rld::to_string ((int) (a + 1)); |
---|
[1703041] | 1189 | l = " " + generator_.arg_trace; |
---|
[6fb1409] | 1190 | l = rld::find_replace (l, "@ARG_NUM@", n); |
---|
[5afb2b8] | 1191 | l = rld::find_replace (l, "@ARG_TYPE@", '"' + sig.args[a] + '"'); |
---|
| 1192 | l = rld::find_replace (l, "@ARG_SIZE@", "sizeof(" + sig.args[a] + ')'); |
---|
[6fb1409] | 1193 | l = rld::find_replace (l, "@ARG_LABEL@", "a" + n); |
---|
| 1194 | c.write_line(l); |
---|
| 1195 | } |
---|
| 1196 | } |
---|
| 1197 | |
---|
[2126ea7] | 1198 | l.clear (); |
---|
| 1199 | |
---|
[5025439] | 1200 | if (sig.has_ret ()) |
---|
[2126ea7] | 1201 | l = " ret ="; |
---|
| 1202 | |
---|
[6506aa1] | 1203 | l += " __real_" + sig.name + '('; |
---|
[5025439] | 1204 | if (sig.has_args ()) |
---|
[4fd758e] | 1205 | { |
---|
[5025439] | 1206 | for (size_t a = 0; a < sig.args.size (); ++a) |
---|
| 1207 | { |
---|
| 1208 | if (a) |
---|
| 1209 | l += ", "; |
---|
| 1210 | l += "a" + rld::to_string ((int) (a + 1)); |
---|
| 1211 | } |
---|
[4fd758e] | 1212 | } |
---|
| 1213 | l += ");"; |
---|
| 1214 | c.write_line(l); |
---|
| 1215 | |
---|
[b988768] | 1216 | if (!generator_.lock_acquire.empty ()) |
---|
| 1217 | c.write_line(generator_.lock_acquire); |
---|
| 1218 | |
---|
| 1219 | if (!generator_.exit_alloc.empty ()) |
---|
| 1220 | { |
---|
| 1221 | l = " " + generator_.exit_alloc; |
---|
| 1222 | l = rld::find_replace (l, "@FUNC_NAME@", '"' + sig.name + '"'); |
---|
| 1223 | l = rld::find_replace (l, "@FUNC_INDEX@", lss.str ()); |
---|
| 1224 | l = rld::find_replace (l, "@FUNC_LABEL@", sig.name); |
---|
| 1225 | l = rld::find_replace (l, "@FUNC_DATA_SIZE@", "FUNC_DATA_SIZE_" + sig.name); |
---|
| 1226 | l = rld::find_replace (l, "@FUNC_DATA_ENTRY_SIZE@", "FUNC_DATA_ENTRY_SIZE_" + sig.name); |
---|
| 1227 | l = rld::find_replace (l, "@FUNC_DATA_RET_SIZE@", "FUNC_DATA_RET_SIZE_" + sig.name); |
---|
| 1228 | c.write_line(l); |
---|
| 1229 | } |
---|
| 1230 | |
---|
| 1231 | if (!generator_.lock_release.empty ()) |
---|
| 1232 | c.write_line(generator_.lock_release); |
---|
| 1233 | |
---|
[6fb1409] | 1234 | if (!generator_.exit_trace.empty ()) |
---|
| 1235 | { |
---|
[1703041] | 1236 | l = " " + generator_.exit_trace; |
---|
[6fb1409] | 1237 | l = rld::find_replace (l, "@FUNC_NAME@", '"' + sig.name + '"'); |
---|
[29ad92c5] | 1238 | l = rld::find_replace (l, "@FUNC_INDEX@", lss.str ()); |
---|
[6fb1409] | 1239 | l = rld::find_replace (l, "@FUNC_LABEL@", sig.name); |
---|
[b988768] | 1240 | l = rld::find_replace (l, "@FUNC_DATA_ENTRY_SIZE@", "FUNC_DATA_ENTRY_SIZE_" + sig.name); |
---|
| 1241 | l = rld::find_replace (l, "@FUNC_DATA_RET_SIZE@", "FUNC_DATA_RET_SIZE_" + sig.name); |
---|
[6fb1409] | 1242 | c.write_line(l); |
---|
| 1243 | } |
---|
| 1244 | |
---|
[5025439] | 1245 | if (sig.has_ret () && !generator_.ret_trace.empty ()) |
---|
[4fd758e] | 1246 | { |
---|
[1703041] | 1247 | std::string l = " " + generator_.ret_trace; |
---|
[2126ea7] | 1248 | l = rld::find_replace (l, "@RET_TYPE@", '"' + sig.ret + '"'); |
---|
| 1249 | l = rld::find_replace (l, "@RET_SIZE@", "sizeof(" + sig.ret + ')'); |
---|
| 1250 | l = rld::find_replace (l, "@RET_LABEL@", "ret"); |
---|
| 1251 | c.write_line(l); |
---|
[4fd758e] | 1252 | c.write_line(" return ret;"); |
---|
| 1253 | } |
---|
| 1254 | |
---|
| 1255 | c.write_line("}"); |
---|
| 1256 | } |
---|
| 1257 | } |
---|
| 1258 | |
---|
| 1259 | if (!found) |
---|
[097f1fd] | 1260 | throw rld::error ("not found", "trace function: " + trace); |
---|
[29ad92c5] | 1261 | |
---|
| 1262 | ++count; |
---|
| 1263 | } |
---|
| 1264 | } |
---|
| 1265 | |
---|
| 1266 | void |
---|
| 1267 | tracer::generate_bitmap (rld::process::tempfile& c, |
---|
| 1268 | const rld::strings& names, |
---|
| 1269 | const std::string& label, |
---|
| 1270 | const bool global_set) |
---|
| 1271 | { |
---|
| 1272 | uint32_t bitmap_size = ((traces.size () - 1) / (4 * 8)) + 1; |
---|
| 1273 | |
---|
| 1274 | std::stringstream ss; |
---|
| 1275 | |
---|
[b988768] | 1276 | ss << "uint32_t __rtld_trace_" << label << "_size = " << traces.size() << ";" << std::endl |
---|
| 1277 | << "const uint32_t __rtld_trace_" << label << "[" << bitmap_size << "] = " << std::endl |
---|
[29ad92c5] | 1278 | << "{" << std::endl; |
---|
| 1279 | |
---|
| 1280 | size_t count = 0; |
---|
| 1281 | size_t bit = 0; |
---|
| 1282 | uint32_t bitmask = 0; |
---|
| 1283 | |
---|
| 1284 | for (rld::strings::const_iterator ti = traces.begin (); |
---|
| 1285 | ti != traces.end (); |
---|
| 1286 | ++ti) |
---|
| 1287 | { |
---|
| 1288 | const std::string& trace = *ti; |
---|
| 1289 | bool set = global_set; |
---|
| 1290 | if (!global_set) |
---|
| 1291 | { |
---|
| 1292 | for (rld::strings::const_iterator ni = names.begin (); |
---|
| 1293 | ni != names.end (); |
---|
| 1294 | ++ni) |
---|
| 1295 | { |
---|
| 1296 | const std::string& name = *ni; |
---|
| 1297 | if (trace == name) |
---|
| 1298 | set = true; |
---|
| 1299 | } |
---|
| 1300 | } |
---|
| 1301 | if (set) |
---|
| 1302 | bitmask |= 1 << bit; |
---|
| 1303 | ++bit; |
---|
| 1304 | ++count; |
---|
| 1305 | if ((bit >= 32) || (count >= traces.size ())) |
---|
| 1306 | { |
---|
| 1307 | ss << " 0x" << std::hex << std::setfill ('0') << std::setw (8) << bitmask << ','; |
---|
| 1308 | if ((count % 4) == 0) |
---|
| 1309 | ss << std::endl; |
---|
| 1310 | bit = 0; |
---|
| 1311 | bitmask = 0; |
---|
| 1312 | } |
---|
[4fd758e] | 1313 | } |
---|
[29ad92c5] | 1314 | |
---|
| 1315 | c.write_line (ss.str ()); |
---|
| 1316 | |
---|
| 1317 | c.write_line ("};"); |
---|
[b6d7f5f] | 1318 | } |
---|
[ea29902] | 1319 | |
---|
[6506aa1] | 1320 | const rld::strings& |
---|
| 1321 | tracer::get_traces () const |
---|
| 1322 | { |
---|
| 1323 | return traces; |
---|
| 1324 | } |
---|
| 1325 | |
---|
[b6d7f5f] | 1326 | void |
---|
| 1327 | tracer::dump (std::ostream& out) const |
---|
| 1328 | { |
---|
| 1329 | out << " Tracer: " << name << std::endl |
---|
[097f1fd] | 1330 | << " Traces: " << traces.size () << std::endl; |
---|
| 1331 | for (rld::strings::const_iterator ti = traces.begin (); |
---|
| 1332 | ti != traces.end (); |
---|
| 1333 | ++ti) |
---|
[b6d7f5f] | 1334 | { |
---|
[097f1fd] | 1335 | out << " " << (*ti) << std::endl; |
---|
[b6d7f5f] | 1336 | } |
---|
[097f1fd] | 1337 | out << " Functions: " << functions_.size () << std::endl; |
---|
| 1338 | for (functions::const_iterator fi = functions_.begin (); |
---|
| 1339 | fi != functions_.end (); |
---|
| 1340 | ++fi) |
---|
[4fd758e] | 1341 | { |
---|
[097f1fd] | 1342 | (*fi).dump (out); |
---|
[4fd758e] | 1343 | } |
---|
[097f1fd] | 1344 | out << " Generator: " << std::endl; |
---|
| 1345 | generator_.dump (out); |
---|
[b6d7f5f] | 1346 | } |
---|
[ea29902] | 1347 | |
---|
[b6d7f5f] | 1348 | linker::linker () |
---|
| 1349 | { |
---|
[ea29902] | 1350 | } |
---|
| 1351 | |
---|
[b6d7f5f] | 1352 | void |
---|
[29ad92c5] | 1353 | linker::load_config (const std::string& name, |
---|
| 1354 | const std::string& trace, |
---|
| 1355 | const std::string& path) |
---|
[ea29902] | 1356 | { |
---|
[6506aa1] | 1357 | std::string sp = get_prefix (); |
---|
| 1358 | |
---|
| 1359 | rld::path::path_join (sp, "share", sp); |
---|
| 1360 | rld::path::path_join (sp, "rtems", sp); |
---|
| 1361 | rld::path::path_join (sp, "trace-linker", sp); |
---|
| 1362 | |
---|
[29ad92c5] | 1363 | if (!path.empty ()) |
---|
| 1364 | sp = path + ':' + sp; |
---|
| 1365 | |
---|
[149a8a6] | 1366 | if (rld::verbose ()) |
---|
[6506aa1] | 1367 | std::cout << "search path: " << sp << std::endl; |
---|
| 1368 | |
---|
| 1369 | config.set_search_path (sp); |
---|
[b6d7f5f] | 1370 | config.clear (); |
---|
[29ad92c5] | 1371 | config.load (name); |
---|
[097f1fd] | 1372 | tracer_.load (config, trace); |
---|
[ea29902] | 1373 | } |
---|
| 1374 | |
---|
[4fd758e] | 1375 | void |
---|
[c4c8218] | 1376 | linker::generate_wrapper (rld::process::tempfile& c) |
---|
[4fd758e] | 1377 | { |
---|
[c4c8218] | 1378 | tracer_.generate (c); |
---|
[8807135] | 1379 | } |
---|
| 1380 | |
---|
| 1381 | void |
---|
[c4c8218] | 1382 | linker::compile_wrapper (rld::process::tempfile& c, |
---|
| 1383 | rld::process::tempfile& o) |
---|
[8807135] | 1384 | { |
---|
| 1385 | rld::process::arg_container args; |
---|
| 1386 | |
---|
| 1387 | if (rld::verbose ()) |
---|
| 1388 | std::cout << "wrapper O file: " << o.name () << std::endl; |
---|
| 1389 | |
---|
| 1390 | rld::cc::make_cc_command (args); |
---|
| 1391 | rld::cc::append_flags (rld::cc::ft_cflags, args); |
---|
| 1392 | |
---|
[32cd4fc] | 1393 | args.push_back ("-O2"); |
---|
| 1394 | args.push_back ("-g"); |
---|
[8807135] | 1395 | args.push_back ("-c"); |
---|
[32cd4fc] | 1396 | args.push_back ("-o"); |
---|
[8807135] | 1397 | args.push_back (o.name ()); |
---|
[c4c8218] | 1398 | args.push_back (c.name ()); |
---|
[8807135] | 1399 | |
---|
| 1400 | rld::process::tempfile out; |
---|
| 1401 | rld::process::tempfile err; |
---|
| 1402 | rld::process::status status; |
---|
| 1403 | |
---|
| 1404 | status = rld::process::execute (rld::cc::get_cc (), |
---|
| 1405 | args, |
---|
| 1406 | out.name (), |
---|
| 1407 | err.name ()); |
---|
| 1408 | |
---|
| 1409 | if ((status.type != rld::process::status::normal) || |
---|
| 1410 | (status.code != 0)) |
---|
| 1411 | { |
---|
| 1412 | err.output (rld::cc::get_cc (), std::cout); |
---|
[5025439] | 1413 | if (dump_on_error) |
---|
| 1414 | dump (std::cout); |
---|
[8807135] | 1415 | throw rld::error ("Compiler error", "compiling wrapper"); |
---|
| 1416 | } |
---|
[4fd758e] | 1417 | } |
---|
| 1418 | |
---|
[b28e8b3] | 1419 | void |
---|
| 1420 | linker::link (rld::process::tempfile& o, |
---|
[6506aa1] | 1421 | const std::string& ld_cmd) |
---|
[b28e8b3] | 1422 | { |
---|
[6506aa1] | 1423 | rld::process::arg_container args; |
---|
| 1424 | |
---|
| 1425 | if (rld::verbose ()) |
---|
| 1426 | std::cout << "linking: " << o.name () << std::endl; |
---|
| 1427 | |
---|
[2126ea7] | 1428 | std::string wrap = " -Wl,--wrap="; |
---|
[6506aa1] | 1429 | |
---|
| 1430 | rld::cc::make_ld_command (args); |
---|
| 1431 | |
---|
| 1432 | rld::process::args_append (args, |
---|
| 1433 | wrap + rld::join (tracer_.get_traces (), wrap)); |
---|
[2126ea7] | 1434 | args.push_back (o.name ()); |
---|
| 1435 | rld::process::args_append (args, ld_cmd); |
---|
[6506aa1] | 1436 | |
---|
| 1437 | rld::process::tempfile out; |
---|
| 1438 | rld::process::tempfile err; |
---|
| 1439 | rld::process::status status; |
---|
| 1440 | |
---|
| 1441 | status = rld::process::execute (rld::cc::get_ld (), |
---|
| 1442 | args, |
---|
| 1443 | out.name (), |
---|
| 1444 | err.name ()); |
---|
[b28e8b3] | 1445 | |
---|
[6506aa1] | 1446 | if ((status.type != rld::process::status::normal) || |
---|
| 1447 | (status.code != 0)) |
---|
| 1448 | { |
---|
[2126ea7] | 1449 | err.output (rld::cc::get_ld (), std::cout); |
---|
[6506aa1] | 1450 | throw rld::error ("Linker error", "linking"); |
---|
| 1451 | } |
---|
[2126ea7] | 1452 | err.output (rld::cc::get_ld (), std::cout); |
---|
[b28e8b3] | 1453 | } |
---|
| 1454 | |
---|
[b6d7f5f] | 1455 | void |
---|
| 1456 | linker::dump (std::ostream& out) const |
---|
[ea29902] | 1457 | { |
---|
[b6d7f5f] | 1458 | const rld::config::paths& cpaths = config.get_paths (); |
---|
[4fd758e] | 1459 | out << " Configuration Files: " << cpaths.size () << std::endl; |
---|
[b6d7f5f] | 1460 | for (rld::config::paths::const_iterator pi = cpaths.begin (); |
---|
| 1461 | pi != cpaths.end (); |
---|
| 1462 | ++pi) |
---|
[ea29902] | 1463 | { |
---|
[b6d7f5f] | 1464 | out << " " << (*pi) << std::endl; |
---|
[ea29902] | 1465 | } |
---|
[b6d7f5f] | 1466 | |
---|
[097f1fd] | 1467 | tracer_.dump (out); |
---|
[ea29902] | 1468 | } |
---|
| 1469 | } |
---|
| 1470 | } |
---|
| 1471 | |
---|
| 1472 | /** |
---|
| 1473 | * RTEMS Trace Linker options. This needs to be rewritten to be like cc where |
---|
| 1474 | * only a single '-' and long options is present. Anything after '--' is passed |
---|
| 1475 | * to RTEMS's real linker. |
---|
| 1476 | */ |
---|
| 1477 | static struct option rld_opts[] = { |
---|
| 1478 | { "help", no_argument, NULL, 'h' }, |
---|
| 1479 | { "version", no_argument, NULL, 'V' }, |
---|
| 1480 | { "verbose", no_argument, NULL, 'v' }, |
---|
| 1481 | { "warn", no_argument, NULL, 'w' }, |
---|
[4fd758e] | 1482 | { "keep", no_argument, NULL, 'k' }, |
---|
[6506aa1] | 1483 | { "compiler", required_argument, NULL, 'c' }, |
---|
[b28e8b3] | 1484 | { "linker", required_argument, NULL, 'l' }, |
---|
[ea29902] | 1485 | { "exec-prefix", required_argument, NULL, 'E' }, |
---|
[6506aa1] | 1486 | { "cflags", required_argument, NULL, 'f' }, |
---|
[31bf375] | 1487 | { "rtems", required_argument, NULL, 'r' }, |
---|
| 1488 | { "rtems-bsp", required_argument, NULL, 'B' }, |
---|
[ea29902] | 1489 | { "config", required_argument, NULL, 'C' }, |
---|
[29ad92c5] | 1490 | { "path", required_argument, NULL, 'P' }, |
---|
[32cd4fc] | 1491 | { "wrapper", required_argument, NULL, 'W' }, |
---|
[ea29902] | 1492 | { NULL, 0, NULL, 0 } |
---|
| 1493 | }; |
---|
| 1494 | |
---|
| 1495 | void |
---|
| 1496 | usage (int exit_code) |
---|
| 1497 | { |
---|
| 1498 | std::cout << "rtems-trace-ld [options] objects" << std::endl |
---|
| 1499 | << "Options and arguments:" << std::endl |
---|
[6506aa1] | 1500 | << " -h : help (also --help)" << std::endl |
---|
| 1501 | << " -V : print linker version number and exit (also --version)" << std::endl |
---|
| 1502 | << " -v : verbose (trace import parts), can supply multiple times" << std::endl |
---|
| 1503 | << " to increase verbosity (also --verbose)" << std::endl |
---|
| 1504 | << " -w : generate warnings (also --warn)" << std::endl |
---|
| 1505 | << " -k : keep temporary files (also --keep)" << std::endl |
---|
| 1506 | << " -c compiler : target compiler is not standard (also --compiler)" << std::endl |
---|
| 1507 | << " -l linker : target linker is not standard (also --linker)" << std::endl |
---|
| 1508 | << " -E prefix : the RTEMS tool prefix (also --exec-prefix)" << std::endl |
---|
| 1509 | << " -f cflags : C compiler flags (also --cflags)" << std::endl |
---|
| 1510 | << " -r path : RTEMS path (also --rtems)" << std::endl |
---|
| 1511 | << " -B bsp : RTEMS arch/bsp (also --rtems-bsp)" << std::endl |
---|
[5025439] | 1512 | << " -W wrapper : wrapper file name without ext (also --wrapper)" << std::endl |
---|
[29ad92c5] | 1513 | << " -C ini : user configuration INI file (also --config)" << std::endl |
---|
| 1514 | << " -P path : user configuration file search path (also --path)" << std::endl; |
---|
[ea29902] | 1515 | ::exit (exit_code); |
---|
| 1516 | } |
---|
| 1517 | |
---|
| 1518 | static void |
---|
| 1519 | fatal_signal (int signum) |
---|
| 1520 | { |
---|
| 1521 | signal (signum, SIG_DFL); |
---|
| 1522 | |
---|
[4fd758e] | 1523 | rld::process::temporaries_clean_up (); |
---|
[ea29902] | 1524 | |
---|
| 1525 | /* |
---|
| 1526 | * Get the same signal again, this time not handled, so its normal effect |
---|
| 1527 | * occurs. |
---|
| 1528 | */ |
---|
| 1529 | kill (getpid (), signum); |
---|
| 1530 | } |
---|
| 1531 | |
---|
| 1532 | static void |
---|
| 1533 | setup_signals (void) |
---|
| 1534 | { |
---|
| 1535 | if (signal (SIGINT, SIG_IGN) != SIG_IGN) |
---|
| 1536 | signal (SIGINT, fatal_signal); |
---|
| 1537 | #ifdef SIGHUP |
---|
| 1538 | if (signal (SIGHUP, SIG_IGN) != SIG_IGN) |
---|
| 1539 | signal (SIGHUP, fatal_signal); |
---|
| 1540 | #endif |
---|
| 1541 | if (signal (SIGTERM, SIG_IGN) != SIG_IGN) |
---|
| 1542 | signal (SIGTERM, fatal_signal); |
---|
| 1543 | #ifdef SIGPIPE |
---|
| 1544 | if (signal (SIGPIPE, SIG_IGN) != SIG_IGN) |
---|
| 1545 | signal (SIGPIPE, fatal_signal); |
---|
| 1546 | #endif |
---|
| 1547 | #ifdef SIGCHLD |
---|
| 1548 | signal (SIGCHLD, SIG_DFL); |
---|
| 1549 | #endif |
---|
| 1550 | } |
---|
| 1551 | |
---|
| 1552 | int |
---|
| 1553 | main (int argc, char* argv[]) |
---|
| 1554 | { |
---|
| 1555 | int ec = 0; |
---|
| 1556 | |
---|
| 1557 | setup_signals (); |
---|
| 1558 | |
---|
| 1559 | try |
---|
| 1560 | { |
---|
[b6d7f5f] | 1561 | rld::trace::linker linker; |
---|
[6506aa1] | 1562 | std::string cc; |
---|
[b28e8b3] | 1563 | std::string ld; |
---|
[b6d7f5f] | 1564 | std::string ld_cmd; |
---|
| 1565 | std::string configuration; |
---|
[29ad92c5] | 1566 | std::string path; |
---|
[b6d7f5f] | 1567 | std::string trace = "tracer"; |
---|
[32cd4fc] | 1568 | std::string wrapper; |
---|
[b28e8b3] | 1569 | std::string rtems_path; |
---|
| 1570 | std::string rtems_arch_bsp; |
---|
[ea29902] | 1571 | |
---|
[b7ad4a2] | 1572 | rld::set_cmdline (argc, argv); |
---|
| 1573 | |
---|
[ea29902] | 1574 | while (true) |
---|
| 1575 | { |
---|
[29ad92c5] | 1576 | int opt = ::getopt_long (argc, argv, "hvwkVc:l:E:f:C:P:r:B:W:", rld_opts, NULL); |
---|
[ea29902] | 1577 | if (opt < 0) |
---|
| 1578 | break; |
---|
| 1579 | |
---|
| 1580 | switch (opt) |
---|
| 1581 | { |
---|
| 1582 | case 'V': |
---|
| 1583 | std::cout << "rtems-trace-ld (RTEMS Trace Linker) " << rld::version () |
---|
| 1584 | << std::endl; |
---|
| 1585 | ::exit (0); |
---|
| 1586 | break; |
---|
| 1587 | |
---|
| 1588 | case 'v': |
---|
| 1589 | rld::verbose_inc (); |
---|
| 1590 | break; |
---|
| 1591 | |
---|
| 1592 | case 'w': |
---|
| 1593 | #if HAVE_WARNINGS |
---|
| 1594 | warnings = true; |
---|
| 1595 | #endif |
---|
| 1596 | break; |
---|
| 1597 | |
---|
[4fd758e] | 1598 | case 'k': |
---|
| 1599 | rld::process::set_keep_temporary_files (); |
---|
| 1600 | break; |
---|
| 1601 | |
---|
[6506aa1] | 1602 | case 'c': |
---|
| 1603 | cc = optarg; |
---|
| 1604 | break; |
---|
| 1605 | |
---|
[b28e8b3] | 1606 | case 'l': |
---|
| 1607 | ld = optarg; |
---|
| 1608 | break; |
---|
| 1609 | |
---|
[ea29902] | 1610 | case 'E': |
---|
[8807135] | 1611 | rld::cc::set_exec_prefix (optarg); |
---|
[ea29902] | 1612 | break; |
---|
| 1613 | |
---|
[6506aa1] | 1614 | case 'f': |
---|
[8807135] | 1615 | rld::cc::append_flags (optarg, rld::cc::ft_cflags); |
---|
[ea29902] | 1616 | break; |
---|
| 1617 | |
---|
[31bf375] | 1618 | case 'r': |
---|
[b28e8b3] | 1619 | rtems_path = optarg; |
---|
[31bf375] | 1620 | break; |
---|
| 1621 | |
---|
| 1622 | case 'B': |
---|
[b28e8b3] | 1623 | rtems_arch_bsp = optarg; |
---|
[31bf375] | 1624 | break; |
---|
| 1625 | |
---|
[ea29902] | 1626 | case 'C': |
---|
| 1627 | configuration = optarg; |
---|
| 1628 | break; |
---|
| 1629 | |
---|
[29ad92c5] | 1630 | case 'P': |
---|
| 1631 | if (!path.empty ()) |
---|
| 1632 | path += ":"; |
---|
| 1633 | path += optarg; |
---|
| 1634 | break; |
---|
| 1635 | |
---|
[32cd4fc] | 1636 | case 'W': |
---|
| 1637 | wrapper = optarg; |
---|
| 1638 | break; |
---|
| 1639 | |
---|
[ea29902] | 1640 | case '?': |
---|
| 1641 | usage (3); |
---|
| 1642 | break; |
---|
| 1643 | |
---|
| 1644 | case 'h': |
---|
| 1645 | usage (0); |
---|
| 1646 | break; |
---|
| 1647 | } |
---|
| 1648 | } |
---|
| 1649 | |
---|
[6506aa1] | 1650 | /* |
---|
| 1651 | * Set the program name. |
---|
| 1652 | */ |
---|
| 1653 | rld::set_progname (argv[0]); |
---|
| 1654 | |
---|
[ea29902] | 1655 | argc -= optind; |
---|
| 1656 | argv += optind; |
---|
| 1657 | |
---|
| 1658 | if (rld::verbose ()) |
---|
[b7ad4a2] | 1659 | { |
---|
[ea29902] | 1660 | std::cout << "RTEMS Trace Linker " << rld::version () << std::endl; |
---|
[b7ad4a2] | 1661 | std::cout << " " << rld::get_cmdline () << std::endl; |
---|
| 1662 | } |
---|
[ea29902] | 1663 | |
---|
[31bf375] | 1664 | /* |
---|
| 1665 | * Load the arch/bsp value if provided. |
---|
| 1666 | */ |
---|
[b28e8b3] | 1667 | if (!rtems_arch_bsp.empty ()) |
---|
| 1668 | { |
---|
[b7ad4a2] | 1669 | const std::string& prefix = rld::get_prefix (); |
---|
| 1670 | if (rtems_path.empty () && prefix.empty ()) |
---|
[5afb2b8] | 1671 | throw rld::error ("No RTEMS path provided with arch/bsp", "options"); |
---|
[b7ad4a2] | 1672 | if (!rtems_path.empty ()) |
---|
| 1673 | rld::rtems::set_path (rtems_path); |
---|
| 1674 | else |
---|
| 1675 | rld::rtems::set_path (prefix); |
---|
[b28e8b3] | 1676 | rld::rtems::set_arch_bsp (rtems_arch_bsp); |
---|
| 1677 | } |
---|
[31bf375] | 1678 | |
---|
[6506aa1] | 1679 | /** |
---|
| 1680 | * Set the compiler and/or linker if provided. |
---|
| 1681 | */ |
---|
| 1682 | if (!cc.empty ()) |
---|
| 1683 | rld::cc::set_cc (cc); |
---|
| 1684 | if (!ld.empty ()) |
---|
| 1685 | rld::cc::set_ld (ld); |
---|
| 1686 | |
---|
[ea29902] | 1687 | /* |
---|
| 1688 | * Load the remaining command line arguments into the linker command line. |
---|
| 1689 | */ |
---|
| 1690 | while (argc--) |
---|
[6506aa1] | 1691 | { |
---|
| 1692 | /* |
---|
| 1693 | * Create this value because 'ld_cmd += ' ' + *argv++' fails on clang. |
---|
| 1694 | */ |
---|
| 1695 | std::string av = *argv++; |
---|
| 1696 | ld_cmd += ' ' + av; |
---|
| 1697 | } |
---|
[b28e8b3] | 1698 | ld_cmd = rld::trim (ld_cmd); |
---|
[ea29902] | 1699 | |
---|
| 1700 | /* |
---|
| 1701 | * If there are no object files there is nothing to link. |
---|
| 1702 | */ |
---|
| 1703 | if (ld_cmd.empty ()) |
---|
| 1704 | throw rld::error ("no trace linker options", "options"); |
---|
| 1705 | |
---|
| 1706 | /* |
---|
| 1707 | * Perform a trace link. |
---|
| 1708 | */ |
---|
| 1709 | try |
---|
| 1710 | { |
---|
[29ad92c5] | 1711 | linker.load_config (configuration, trace, path); |
---|
[32cd4fc] | 1712 | |
---|
[c4c8218] | 1713 | rld::process::tempfile c (".c"); |
---|
| 1714 | rld::process::tempfile o (".o"); |
---|
| 1715 | |
---|
[32cd4fc] | 1716 | if (!wrapper.empty ()) |
---|
| 1717 | { |
---|
| 1718 | c.override (wrapper); |
---|
| 1719 | c.keep (); |
---|
| 1720 | o.override (wrapper); |
---|
| 1721 | o.keep (); |
---|
| 1722 | } |
---|
| 1723 | |
---|
[c4c8218] | 1724 | linker.generate_wrapper (c); |
---|
| 1725 | linker.compile_wrapper (c, o); |
---|
[b28e8b3] | 1726 | linker.link (o, ld_cmd); |
---|
[4fd758e] | 1727 | |
---|
| 1728 | if (rld::verbose ()) |
---|
| 1729 | linker.dump (std::cout); |
---|
[ea29902] | 1730 | } |
---|
| 1731 | catch (...) |
---|
| 1732 | { |
---|
| 1733 | throw; |
---|
| 1734 | } |
---|
| 1735 | |
---|
| 1736 | } |
---|
| 1737 | catch (rld::error re) |
---|
| 1738 | { |
---|
| 1739 | std::cerr << "error: " |
---|
| 1740 | << re.where << ": " << re.what |
---|
| 1741 | << std::endl; |
---|
| 1742 | ec = 10; |
---|
| 1743 | } |
---|
| 1744 | catch (std::exception e) |
---|
| 1745 | { |
---|
| 1746 | int status; |
---|
| 1747 | char* realname; |
---|
| 1748 | realname = abi::__cxa_demangle (e.what(), 0, 0, &status); |
---|
| 1749 | std::cerr << "error: exception: " << realname << " ["; |
---|
| 1750 | ::free (realname); |
---|
| 1751 | const std::type_info &ti = typeid (e); |
---|
| 1752 | realname = abi::__cxa_demangle (ti.name(), 0, 0, &status); |
---|
| 1753 | std::cerr << realname << "] " << e.what () << std::endl; |
---|
| 1754 | ::free (realname); |
---|
| 1755 | ec = 11; |
---|
| 1756 | } |
---|
| 1757 | catch (...) |
---|
| 1758 | { |
---|
| 1759 | /* |
---|
| 1760 | * Helps to know if this happens. |
---|
| 1761 | */ |
---|
| 1762 | std::cout << "error: unhandled exception" << std::endl; |
---|
| 1763 | ec = 12; |
---|
| 1764 | } |
---|
| 1765 | |
---|
| 1766 | return ec; |
---|
| 1767 | } |
---|