diff options
Diffstat (limited to 'translate.cxx')
-rw-r--r-- | translate.cxx | 301 |
1 files changed, 211 insertions, 90 deletions
diff --git a/translate.cxx b/translate.cxx index 557c2f12..0e10f0fc 100644 --- a/translate.cxx +++ b/translate.cxx @@ -14,6 +14,7 @@ #include "session.h" #include "tapsets.h" #include "util.h" +#include "dwarf_wrappers.h" #include <cstdlib> #include <iostream> @@ -21,6 +22,7 @@ #include <sstream> #include <string> #include <cassert> +#include <cstring> extern "C" { #include <elfutils/libdwfl.h> @@ -4323,51 +4325,175 @@ c_unparser::visit_hist_op (hist_op*) } -static map< Dwarf_Addr, string> addrmap; -#include <string.h> - -static int -kernel_filter (const char *module, const char *file __attribute__((unused))) +struct unwindsym_dump_context { - return !strcmp(module,"kernel"); -} + systemtap_session& session; + ostream& output; + unsigned stp_module_index; +}; + static int -get_symbols (Dwfl_Module *m, - void **userdata __attribute__ ((unused)), - const char *name __attribute__ ((unused)), - Dwarf_Addr base __attribute__ ((unused)), - void *arg __attribute__ ((unused))) +dump_unwindsyms (Dwfl_Module *m, + void **userdata __attribute__ ((unused)), + const char *name, + Dwarf_Addr base, + void *arg) { + unwindsym_dump_context* c = (unwindsym_dump_context*) arg; + assert (c); + unsigned real_stpmodules_index = c->stp_module_index; + + string modname = name; + + // skip modules/files we're not actually interested in + if (c->session.unwindsym_modules.find(modname) == c->session.unwindsym_modules.end()) + return DWARF_CB_OK; + + c->stp_module_index ++; + + if (c->session.verbose > 1) + clog << "dump_unwindsyms " << name + << " index=" << real_stpmodules_index + << " base=0x" << hex << base << dec << endl; + + // We want to extract several bits of information: + // - parts of the program-header that map the file's physical offsets to the text section + // - section table: just a list of section (relocation) base addresses + // - symbol table of the text section, with all addresses relativized to .text base + // - the contents of .debug_frame section, for unwinding purposes + // In the future, we'll also care about data symbols. + + c->output << "struct _stp_symbol _stp_module_" << real_stpmodules_index<< "_sections[] = {" << endl; + if (modname != "kernel") + c->output << " { 0, \".text\" }, " << endl; // XXX + else + c->output << " { 0, \"_stext\" }, " << endl; // XXX + c->output << "};" << endl; + int syments = dwfl_module_getsymtab(m); assert(syments); + + // Look up the relocation basis for symbols + int n = dwfl_module_relocations (m); + dwfl_assert ("dwfl_module_relocations", n >= 0); + + // XXX: unfortunate duplication with tapsets.cxx:emit_address() + + typedef map<Dwarf_Addr,const char*> addrmap_t; // NB: plain map, sorted by address + addrmap_t addrmap; // NB: plain map, sorted by address + + Dwarf_Addr extra_offset = 0; + for (int i = 1; i < syments; ++i) { GElf_Sym sym; const char *name = dwfl_module_getsym(m, i, &sym, NULL); - if (name) { - if (GELF_ST_TYPE (sym.st_info) == STT_FUNC || - strcmp(name, "_etext") == 0 || - strcmp(name, "_stext") == 0 || - strcmp(name, "modules_op") == 0) - addrmap[sym.st_value] = name; - } + if (name) + { + // NB: Yey, we found the kernel's _stext value. + // Sess.sym_stext may be unset (0) at this point, since + // there may have been no kernel probes set. We could + // use tapsets.cxx:lookup_symbol_address(), but then + // we're already iterating over the same data here... + if (modname == "kernel" && !strcmp(name, "_stext")) + { + extra_offset = sym.st_value; + if (c->session.verbose > 2) + clog << "Found kernel _stext 0x" << hex << extra_offset << dec << endl; + } + + if (GELF_ST_TYPE (sym.st_info) == STT_FUNC) + { + Dwarf_Addr sym_addr = sym.st_value; + + int i = dwfl_module_relocate_address (m, &sym_addr); + dwfl_assert ("dwfl_module_relocate_address", i >= 0); + const char *secname = dwfl_module_relocation_info (m, i, NULL); + + if (n == 0 || (n==1 && secname == NULL)) + { + if (c->session.verbose > 2) + clog << "Skipped absolute symbol " << name << endl; + continue; + } + + if (n == 1 && modname == "kernel" && secname[0] == '\0') + { + // This is a symbol within a relocatable kernel image. + secname = "_stext"; // not actually used + // NB: don't subtract session.sym_stext, which could be inconveniently NULL. + } + else if (strcmp (secname, ".text")) /* XXX: only care about .text-related relocations for now. */ + { + if (c->session.verbose > 2) + clog << "Skipped symbol " << name << ", due to non-.text relocation section " << secname << endl; + continue; + } + else + { + // sym_addr has already been relocate relative to .text + } + + addrmap[sym_addr] = name; + } + } } + + // We write out a *sorted* symbol table, so the runtime doesn't have to sort them later. + c->output << "struct _stp_symbol _stp_module_" << real_stpmodules_index<< "_symbols[] = {" << endl; + for (addrmap_t::iterator it = addrmap.begin(); it != addrmap.end(); it++) + { + if (it->first < extra_offset) + continue; // skip symbols that occur before our chosen base address + + c->output << " { 0x" << hex << it->first-extra_offset << dec + << ", " << lex_cast_qstring (it->second) << " }," << endl; + } + c->output << "};" << endl; + + c->output << "struct _stp_module _stp_module_" << real_stpmodules_index << " = {" << endl; + c->output << ".name = " << lex_cast_qstring (modname) << ", " << endl; + + c->output << ".sections = _stp_module_" << real_stpmodules_index << "_sections" << ", " << endl; + c->output << ".num_sections = sizeof(_stp_module_" << real_stpmodules_index << "_sections)/" + << "sizeof(struct _stp_symbol), " << endl; + + c->output << ".symbols = _stp_module_" << real_stpmodules_index << "_symbols" << ", " << endl; + c->output << ".num_symbols = sizeof(_stp_module_" << real_stpmodules_index << "_symbols)/" + << "sizeof(struct _stp_symbol), " << endl; + + c->output << "};" << endl << endl; + return DWARF_CB_OK; } -int -emit_symbol_data_from_debuginfo(systemtap_session& s, ofstream& kallsyms_out) + +// Emit symbol table & unwind data, plus any calls needed to register +// them with the runtime. + +void +emit_symbol_data (systemtap_session& s) { + string symfile = "stap-symbols.h"; + + s.op->newline() << "#include " << lex_cast_qstring (symfile); + + ofstream kallsyms_out ((s.tmpdir + "/" + symfile).c_str()); + + unwindsym_dump_context ctx = { s, kallsyms_out, 0 }; + + // XXX: copied from tapsets.cxx, sadly static char debuginfo_path_arr[] = "-:.debug:/usr/lib/debug:build"; static char *debuginfo_env_arr = getenv("SYSTEMTAP_DEBUGINFO_PATH"); static char *debuginfo_path = (debuginfo_env_arr ? - debuginfo_env_arr : debuginfo_path_arr); + debuginfo_env_arr : debuginfo_path_arr); static const char *debug_path = (debuginfo_env_arr ? - debuginfo_env_arr : s.kernel_release.c_str()); - + debuginfo_env_arr : s.kernel_release.c_str()); + + // ---- step 1: process any kernel modules listed static const Dwfl_Callbacks kernel_callbacks = { dwfl_linux_kernel_find_elf, @@ -4376,75 +4502,70 @@ emit_symbol_data_from_debuginfo(systemtap_session& s, ofstream& kallsyms_out) & debuginfo_path }; - Dwfl *dwfl = dwfl_begin (&kernel_callbacks); + Dwfl *dwfl = dwfl_begin (&kernel_callbacks); + if (!dwfl) + throw semantic_error ("cannot open dwfl"); + dwfl_report_begin (dwfl); + int rc = dwfl_linux_kernel_report_offline (dwfl, debug_path, NULL /* XXX: filtering callback */); + dwfl_report_end (dwfl, NULL, NULL); + dwfl_assert ("dwfl_linux_kernel_report_offline", rc); + ptrdiff_t off = 0; + do + { + if (pending_interrupts) return; + off = dwfl_getmodules (dwfl, &dump_unwindsyms, (void *) &ctx, 0); + } + while (off > 0); + dwfl_assert("dwfl_getmodules", off == 0); + dwfl_end(dwfl); + + + // ---- step 2: process any user modules (files) listed + static const Dwfl_Callbacks user_callbacks = + { + NULL, /* dwfl_linux_kernel_find_elf, */ + dwfl_standard_find_debuginfo, + dwfl_offline_section_address, + & debuginfo_path + }; + + for (std::set<std::string>::iterator it = s.unwindsym_modules.begin(); + it != s.unwindsym_modules.end(); + it++) + { + string modname = *it; + assert (modname.length() != 0); + if (modname[0] != '/') continue; // user-space files must be full paths + Dwfl *dwfl = dwfl_begin (&user_callbacks); if (!dwfl) - throw semantic_error ("cannot open dwfl"); + throw semantic_error ("cannot create dwfl for " + modname); + dwfl_report_begin (dwfl); - - int rc = dwfl_linux_kernel_report_offline (dwfl, - debug_path, - kernel_filter); + Dwfl_Module* mod = dwfl_report_offline (dwfl, modname.c_str(), modname.c_str(), -1); dwfl_report_end (dwfl, NULL, NULL); - if (rc < 0) - return rc; - - dwfl_getmodules (dwfl, &get_symbols, NULL, 0); + dwfl_assert ("dwfl_report_offline", mod); + ptrdiff_t off = 0; + do + { + if (pending_interrupts) return; + off = dwfl_getmodules (dwfl, &dump_unwindsyms, (void *) &ctx, 0); + } + while (off > 0); + dwfl_assert("dwfl_getmodules", off == 0); dwfl_end(dwfl); + } - int i = 0; - map< Dwarf_Addr, string>::iterator pos; - kallsyms_out << "struct _stp_symbol _stp_kernel_symbols [] = {"; - for (pos = addrmap.begin(); pos != addrmap.end(); pos++) { - kallsyms_out << " { 0x" << hex << pos->first << ", " << "\"" << pos->second << "\" },\n"; - i++; - } - kallsyms_out << "};\n"; - kallsyms_out << "unsigned _stp_num_kernel_symbols = " << dec << i << ";\n"; - return i == 0; -} + // Print out a definition of the runtime's _stp_modules[] globals. -int -emit_symbol_data (systemtap_session& s) -{ - unsigned i=0; - char kallsyms_outbuf [4096]; - ofstream kallsyms_out ((s.tmpdir + "/stap-symbols.h").c_str()); - kallsyms_out.rdbuf()->pubsetbuf (kallsyms_outbuf, - sizeof(kallsyms_outbuf)); - s.op->newline() << "\n\n#include \"stap-symbols.h\""; - - // FIXME for non-debuginfo use. - if (true) { - return emit_symbol_data_from_debuginfo(s, kallsyms_out); - } else { - // For symbol-table only operation, we don't have debuginfo, - // so parse /proc/kallsyms. - - ifstream kallsyms("/proc/kallsyms"); - string lastaddr; - - kallsyms_out << "struct _stp_symbol _stp_kernel_symbols [] = {"; - while (! kallsyms.eof()) - { - string addr, type, sym; - kallsyms >> addr >> type >> sym >> ws; - - if (kallsyms.peek() == '[') - break; - - // NB: kallsyms includes some duplicate addresses - if ((type == "t" || type == "T" || type == "A" || sym == "modules_op") && lastaddr != addr) - { - kallsyms_out << " { 0x" << addr << ", " << "\"" << sym << "\" },\n"; - lastaddr = addr; - i ++; - } - } - kallsyms_out << "};\n"; - kallsyms_out << "unsigned _stp_num_kernel_symbols = " << i << ";\n"; - } - return (i == 0); + kallsyms_out << endl; + kallsyms_out << "struct _stp_module *_stp_modules [] = {" << endl; + for (unsigned i=0; i<ctx.stp_module_index; i++) + { + kallsyms_out << "& _stp_module_" << i << "," << endl; + } + kallsyms_out << "};" << endl; + kallsyms_out << "int _stp_num_modules = " << ctx.stp_module_index << ";" << endl; } @@ -4610,16 +4731,16 @@ translate_pass (systemtap_session& s) s.up->emit_global_param (s.globals[i]); } - s.op->newline() << "MODULE_DESCRIPTION(\"systemtap probe\");"; - s.op->newline() << "MODULE_LICENSE(\"GPL\");"; // XXX + emit_symbol_data (s); + + s.op->newline() << "MODULE_DESCRIPTION(\"systemtap-generated probe\");"; + s.op->newline() << "MODULE_LICENSE(\"GPL\");"; } catch (const semantic_error& e) { s.print_error (e); } - rc |= emit_symbol_data (s); - s.op->line() << "\n"; delete s.op; |