diff --git a/source/bin/rocprof-sys-instrument/details.cpp b/source/bin/rocprof-sys-instrument/details.cpp index 698e8adf14..5da3bd4ba1 100644 --- a/source/bin/rocprof-sys-instrument/details.cpp +++ b/source/bin/rocprof-sys-instrument/details.cpp @@ -488,6 +488,77 @@ find_function(image_t* app_image, const std::string& _name, const strset_t& _ext return _func; } +//======================================================================================// +// +// Find undefined function symbols (external references) in the binary +// +symtab_symbol_t* +find_undefined_function_symbol(image_t* app_image, const std::string& _name) +{ + if(_name.empty()) return nullptr; + + // Get all objects from the image + BPatch_Vector app_objects; + app_image->getObjects(app_objects); + + if(app_objects.empty()) + { + verbprintf(3, "No objects found in image for symbol search\n"); + return nullptr; + } + // Search helper lambda for code reuse + auto _find_symbol = [](SymTab::Symtab* symtab, + const std::string& target_name) -> symtab_symbol_t* { + if(!symtab) return nullptr; + + std::vector all_symbols; + if(!symtab->getAllSymbols(all_symbols)) return nullptr; + + for(auto* symbol : all_symbols) + { + if(!symbol || symbol->getType() != SymTab::Symbol::ST_FUNCTION || + symbol->getRegion()) + continue; + + // Try all possible symbol name representations + std::string symbol_name = symbol->getPrettyName(); + if(symbol_name.empty()) symbol_name = symbol->getMangledName(); + if(symbol_name.empty()) symbol_name = symbol->getTypedName(); + + // Check for exact match and undefined function criteria + if(symbol_name == target_name) return symbol; + } + return nullptr; + }; + + // Search through each object + for(auto* app_object : app_objects) + { + if(!app_object) continue; + + std::string binary_path = app_object->name(); + // Open Symtab directly for comprehensive symbol access + SymTab::Symtab* symtab = nullptr; + if(!SymTab::Symtab::openFile(symtab, binary_path)) + { + verbprintf(3, "Failed to open Symtab for: %s\n", binary_path.c_str()); + continue; + } + + // Search for the primary symbol name + auto* result = _find_symbol(symtab, _name); + if(result) + { + verbprintf(1, "Found undefined function symbol: '%s' in %s\n", _name.c_str(), + binary_path.c_str()); + return result; + } + } + + verbprintf(1, "Undefined function symbol: '%s' ... not found\n", _name.c_str()); + return nullptr; +} + //======================================================================================// // // Get the realpath to this exe diff --git a/source/bin/rocprof-sys-instrument/fwd.hpp b/source/bin/rocprof-sys-instrument/fwd.hpp index 900cd8f17e..ca9a6e7b48 100644 --- a/source/bin/rocprof-sys-instrument/fwd.hpp +++ b/source/bin/rocprof-sys-instrument/fwd.hpp @@ -360,6 +360,9 @@ insert_instr(address_space_t* mutatee, Tp traceFunc, procedure_loc_t traceLoc, procedure_t* find_function(image_t* appImage, const string_t& functionName, const strset_t& = {}); +symtab_symbol_t* +find_undefined_function_symbol(image_t* app_image, const std::string& _name); + void error_func_real(error_level_t level, int num, const char* const* params); diff --git a/source/bin/rocprof-sys-instrument/rocprof-sys-instrument.cpp b/source/bin/rocprof-sys-instrument/rocprof-sys-instrument.cpp index 3c0f9877fd..852473eca7 100644 --- a/source/bin/rocprof-sys-instrument/rocprof-sys-instrument.cpp +++ b/source/bin/rocprof-sys-instrument/rocprof-sys-instrument.cpp @@ -1721,6 +1721,14 @@ main(int argc, char** argv) use_mpi = true; break; } + else if(find_undefined_function_symbol(app_image, itr) != nullptr) + { + verbprintf(0, + "Found undefined symbol '%s' in '%s'. Enabling MPI support...\n", + itr, _cmdv[0]); + use_mpi = true; + break; + } } #endif