From 1b8814ce0b12b2d4dd59e249af66f48540a9671e Mon Sep 17 00:00:00 2001 From: York Jasper Niebuhr Date: Fri, 3 Apr 2026 21:15:37 +0200 Subject: [PATCH] Restored metadata file naming --- pinpoint/final/final.h | 4 ++ pinpoint/final/on_finish_unit.cpp | 65 +++++++++++++++++++++++++------ pinpoint/pinpoint.cpp | 7 ++++ subject/CMakeLists.txt | 1 + 4 files changed, 66 insertions(+), 11 deletions(-) diff --git a/pinpoint/final/final.h b/pinpoint/final/final.h index 98e6801..110eae3 100644 --- a/pinpoint/final/final.h +++ b/pinpoint/final/final.h @@ -6,6 +6,10 @@ void set_meta_dir(const char* dir); bool has_meta_dir(); const char* get_meta_dir(); +void set_srcroot_dir(const char* dir); +bool has_srcroot_dir(); +const char* get_srcroot_dir(); + bool init_src_file(); const char* get_src_file(); diff --git a/pinpoint/final/on_finish_unit.cpp b/pinpoint/final/on_finish_unit.cpp index b672873..d04c05b 100644 --- a/pinpoint/final/on_finish_unit.cpp +++ b/pinpoint/final/on_finish_unit.cpp @@ -14,17 +14,37 @@ static std::string src_file, cu_hash; -static bool meta_dir_known = false; -static std::string meta_dir; +static bool meta_dir_known = false, srcroot_dir_known = false; +static std::string meta_dir, srcroot_dir; bool init_src_file() { - if (!main_input_filename) + if (!main_input_filename || !has_srcroot_dir()) return false; - std::filesystem::path srcp = std::filesystem::weakly_canonical( - std::filesystem::absolute(main_input_filename) - ); - src_file = srcp.string(); + namespace fs = std::filesystem; + + // Canonicalize both paths (robust against symlinks, ../, etc.) + fs::path src_abs = fs::weakly_canonical(fs::absolute(main_input_filename)); + fs::path root_abs = fs::weakly_canonical(fs::absolute(get_srcroot_dir())); + + // Ensure src_abs is actually inside root_abs + auto src_it = src_abs.begin(); + auto root_it = root_abs.begin(); + + for (; root_it != root_abs.end(); ++root_it, ++src_it) { + if (src_it == src_abs.end() || *src_it != *root_it) { + // Not under srcroot + std::cerr << "The source file " << src_abs << " is not under source root " << root_abs << std::endl; + return false; + } + } + + // Compute relative path + fs::path rel = fs::relative(src_abs, root_abs); + + // Normalize to forward slashes (important for metadata consistency) + src_file = rel.generic_string(); + return true; } @@ -70,6 +90,27 @@ const char* get_meta_dir() { return meta_dir.c_str(); } +void set_srcroot_dir(const char* dir) { + if (!dir) { + srcroot_dir_known = false; + return; + } + + srcroot_dir = std::string{ dir }; + srcroot_dir_known = true; +} + +bool has_srcroot_dir() { + return srcroot_dir_known; +} + +const char* get_srcroot_dir() { + if (!srcroot_dir_known) + return nullptr; + + return srcroot_dir.c_str(); +} + static void emit_dpin_alias_labels() { for (const DataPin& dpin : DataPin::all()) { if (dpin.pin_symbol.empty() || dpin.symbol.empty()) @@ -82,13 +123,15 @@ static void emit_dpin_alias_labels() { static std::ofstream open_spslr_output_file() { std::filesystem::path metadir { get_meta_dir() }; - std::filesystem::path metafile { std::string{ get_cu_hash() } + SPSLR_PINFILE_EXTENSION }; + std::filesystem::path metafile { std::string{ get_src_file() } + SPSLR_PINFILE_EXTENSION }; - std::filesystem::create_directories(metadir); + std::filesystem::path op = metadir / metafile; - std::cout << "Dumping meta data to " << (metadir / metafile) << std::endl; + std::filesystem::create_directories(op.parent_path()); - std::ofstream out(metadir / metafile); + std::cout << "Dumping meta data to " << op << std::endl; + + std::ofstream out(op); if (!out) pinpoint_fatal("open_spslr_output_file failed to open spslr dump file"); diff --git a/pinpoint/pinpoint.cpp b/pinpoint/pinpoint.cpp index 1be597c..37ae424 100644 --- a/pinpoint/pinpoint.cpp +++ b/pinpoint/pinpoint.cpp @@ -19,6 +19,8 @@ int plugin_init(struct plugin_name_args* plugin_info, struct plugin_gcc_version* for (int i = 0; i < plugin_info->argc; ++i) { if (!strcmp(plugin_info->argv[i].key, "metadir")) set_meta_dir(plugin_info->argv[i].value); + else if (!strcmp(plugin_info->argv[i].key, "srcroot")) + set_srcroot_dir(plugin_info->argv[i].value); } if (!has_meta_dir()) { @@ -26,6 +28,11 @@ int plugin_init(struct plugin_name_args* plugin_info, struct plugin_gcc_version* return 1; } + if (!has_srcroot_dir()) { + std::cerr << "spslr_pinpoint -> missing source root directory argument" << std::endl; + return 1; + } + // Stage 0 -> separates relevant field offsets into function calls register_callback(plugin_info->base_name, PLUGIN_START_UNIT, on_start_unit, NULL); diff --git a/subject/CMakeLists.txt b/subject/CMakeLists.txt index 1057b91..c7a25b8 100644 --- a/subject/CMakeLists.txt +++ b/subject/CMakeLists.txt @@ -3,6 +3,7 @@ target_include_directories(subject PRIVATE .) add_dependencies(subject spslr_pinpoint spslr_finalize spslr_selfpatch) target_link_libraries(subject PRIVATE spslr_selfpatch) target_compile_options(subject PRIVATE -O1 -fplugin=$ -fdump-tree-separate_offset -fdump-tree-asm_offset + -fplugin-arg-spslr_pinpoint-srcroot=${CMAKE_CURRENT_SOURCE_DIR} -fplugin-arg-spslr_pinpoint-metadir=${CMAKE_CURRENT_BINARY_DIR}/spslr) # Apply spslr_finalizer to subject