From 468770e54ed9654a69a1d62b0ac3840a2d21833b Mon Sep 17 00:00:00 2001
From: Jon Turney <jon.turney@dronecode.org.uk>
Date: Fri, 7 Feb 2014 23:03:10 +0000
Subject: [PATCH 11/29] Default debug_file to the code_file basename

Strip any path from code_file, and use that as debug_file if one isn't recorded
in the codeview record.

The current implementation of 'ld --build-id' doesn't write a pdbfilename in the
codeview record, not least because we don't have such a thing.

The minidump processor requires both debug_file and debug_identifier (the
build-id) to locate the symbol file.

v2:
Correctly make debug-file a basename when code-file has a windows-style path
Fix by Ivan Gubarev

v3:
Fix a test which expects the previous behaviour

v4:
Fix new tests which expect the previous behaviour

Signed-off-by: Jon Turney <jon.turney@dronecode.org.uk>
---
 src/processor/minidump.cc                 | 14 ++++++++++++++
 src/processor/minidump_unittest.cc        |  4 ++--
 src/processor/testdata/minidump2.dump.out |  2 +-
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/processor/minidump.cc b/src/processor/minidump.cc
index df8b640a..4678a5ef 100644
--- a/src/processor/minidump.cc
+++ b/src/processor/minidump.cc
@@ -2077,6 +2077,20 @@ string MinidumpModule::debug_file() const {
     }
   }
 
+  // Manufacture debug-file from code-file
+  if (file.empty()) {
+    file = code_file();
+
+    BPLOG(INFO) << "Generated debug_file '" << file << "' from code_file '" << *name_ << "'";
+  }
+
+  // This may be a windows-style pathname, so find the basename considering both
+  // forward and back-slashes.
+  const size_t last_slash_idx = file.find_last_of("\\/");
+  if (std::string::npos != last_slash_idx) {
+    file.erase(0, last_slash_idx + 1);
+  }
+
   // Relatively common case
   BPLOG_IF(INFO, file.empty()) << "MinidumpModule could not determine "
                                   "debug_file for " << *name_;
diff --git a/src/processor/minidump_unittest.cc b/src/processor/minidump_unittest.cc
index 036d03f1..35091396 100644
--- a/src/processor/minidump_unittest.cc
+++ b/src/processor/minidump_unittest.cc
@@ -100,7 +100,7 @@ TEST_F(MinidumpTest, TestMinidumpFromFile) {
   const MinidumpModule *md_module = md_module_list->GetModuleAtIndex(0);
   ASSERT_TRUE(md_module != NULL);
   ASSERT_EQ("c:\\test_app.exe", md_module->code_file());
-  ASSERT_EQ("c:\\test_app.pdb", md_module->debug_file());
+  ASSERT_EQ("test_app.pdb", md_module->debug_file());
   ASSERT_EQ("45D35F6C2d000", md_module->code_identifier());
   ASSERT_EQ("5A9832E5287241C1838ED98914E9B7FF1", md_module->debug_identifier());
 }
@@ -519,7 +519,7 @@ TEST(Dump, OneModule) {
   ASSERT_EQ(0xa90206ca83eb2852ULL, md_module->base_address());
   ASSERT_EQ(0xada542bd, md_module->size());
   ASSERT_EQ("single module", md_module->code_file());
-  ASSERT_EQ("c:\\foo\\file.pdb", md_module->debug_file());
+  ASSERT_EQ("file.pdb", md_module->debug_file());
   // time_date_stamp and size_of_image concatenated
   ASSERT_EQ("B1054D2Aada542bd", md_module->code_identifier());
   ASSERT_EQ("ABCD1234F00DBEEF01020304050607081", md_module->debug_identifier());
diff --git a/src/processor/testdata/minidump2.dump.out b/src/processor/testdata/minidump2.dump.out
index 8585c89b..e8140fec 100644
--- a/src/processor/testdata/minidump2.dump.out
+++ b/src/processor/testdata/minidump2.dump.out
@@ -207,7 +207,7 @@ MDRawModule
   (cv_record).age                 = 1
   (cv_record).pdb_file_name       = "c:\test_app.pdb"
   (misc_record)                   = (null)
-  (debug_file)                    = "c:\test_app.pdb"
+  (debug_file)                    = "test_app.pdb"
   (debug_identifier)              = "5A9832E5287241C1838ED98914E9B7FF1"
   (version)                       = ""
 
-- 
2.15.0

