[Lldb-commits] [lldb] r146422 - in /lldb/trunk: include/lldb/Target/PathMappingList.h source/Core/SourceManager.cpp source/Target/PathMappingList.cpp source/Target/Target.cpp test/source-manager/TestSourceManager.py

Johnny Chen johnny.chen at apple.com
Mon Dec 12 13:59:29 PST 2011


Author: johnny
Date: Mon Dec 12 15:59:28 2011
New Revision: 146422

URL: http://llvm.org/viewvc/llvm-project?rev=146422&view=rev
Log:
rdar://problem/10227672

There were two problems associated with this radar:
1. "settings show target.source-map" failed to show the source-map after, for example,
   "settings set target.source-map /Volumes/data/lldb/svn/trunk/test/source-manager /Volumes/data/lldb/svn/trunk/test/source-manager/hidden"
   has been executed to set the source-map.
2. "list -n main" failed to display the source of the main() function after we properly set the source-map.

The first was fixed by adding the missing functionality to TargetInstanceSettings::GetInstanceSettingsValue (Target.cpp)
and updating the support files PathMappingList.h/.cpp; the second by modifying SourceManager.cpp to fix several places
with incorrect logic.

Also added a test case test_move_and_then_display_source() to TestSourceManager.py, which moves main.c to hidden/main.c,
sets target.source-map to perform the directory mapping, and then verifies that "list -n main" can still show the main()
function.

Modified:
    lldb/trunk/include/lldb/Target/PathMappingList.h
    lldb/trunk/source/Core/SourceManager.cpp
    lldb/trunk/source/Target/PathMappingList.cpp
    lldb/trunk/source/Target/Target.cpp
    lldb/trunk/test/source-manager/TestSourceManager.py

Modified: lldb/trunk/include/lldb/Target/PathMappingList.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/PathMappingList.h?rev=146422&r1=146421&r2=146422&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/PathMappingList.h (original)
+++ lldb/trunk/include/lldb/Target/PathMappingList.h Mon Dec 12 15:59:28 2011
@@ -48,8 +48,9 @@
     void
     Clear (bool notify);
 
+    // By default, dump all pairs.
     void
-    Dump (Stream *s);
+    Dump (Stream *s, int pair_index=-1);
 
     size_t
     GetSize ();

Modified: lldb/trunk/source/Core/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/SourceManager.cpp?rev=146422&r1=146421&r2=146422&view=diff
==============================================================================
--- lldb/trunk/source/Core/SourceManager.cpp (original)
+++ lldb/trunk/source/Core/SourceManager.cpp Mon Dec 12 15:59:28 2011
@@ -86,7 +86,8 @@
 {
     FileSP file_sp;
     file_sp = m_debugger->GetSourceFileCache().FindSourceFile (file_spec);
-    if (!file_sp)
+    // If file_sp is no good or it points to a non-existent file, reset it.
+    if (!file_sp || !file_sp->GetFileSpec().Exists())
     {
         file_sp.reset (new File (file_spec, m_target));
 
@@ -336,10 +337,17 @@
                     }
                 }
             }
-            else
+            // Try remapping if m_file_spec does not correspond to an existing file.
+            if (!m_file_spec.Exists())
             {
-                if (target->GetSourcePathMap().RemapPath(file_spec.GetDirectory(), m_file_spec.GetDirectory()))
-                   m_mod_time = file_spec.GetModificationTime();
+                ConstString new_path;
+                if (target->GetSourcePathMap().RemapPath(m_file_spec.GetDirectory(), new_path))
+                {
+                    char resolved_path[PATH_MAX];
+                    ::snprintf(resolved_path, PATH_MAX, "%s/%s", new_path.AsCString(), m_file_spec.GetFilename().AsCString());
+                    m_file_spec = new FileSpec(resolved_path, true);
+                    m_mod_time = m_file_spec.GetModificationTime();
+                }
             }
         }
     }
@@ -387,13 +395,18 @@
     // source cache and only update when we determine a file has been updated.
     // For now we check each time we want to display info for the file.
     TimeValue curr_mod_time (m_file_spec.GetModificationTime());
-    if (m_mod_time != curr_mod_time)
+
+    if (curr_mod_time.IsValid() && m_mod_time != curr_mod_time)
     {
         m_mod_time = curr_mod_time;
         m_data_sp = m_file_spec.ReadFileContents ();
         m_offsets.clear();
     }
 
+    // Sanity check m_data_sp before proceeding.
+    if (!m_data_sp)
+        return 0;
+
     const uint32_t start_line = line <= context_before ? 1 : line - context_before;
     const uint32_t start_line_offset = GetLineOffset (start_line);
     if (start_line_offset != UINT32_MAX)

Modified: lldb/trunk/source/Target/PathMappingList.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/PathMappingList.cpp?rev=146422&r1=146421&r2=146422&view=diff
==============================================================================
--- lldb/trunk/source/Target/PathMappingList.cpp (original)
+++ lldb/trunk/source/Target/PathMappingList.cpp Mon Dec 12 15:59:28 2011
@@ -101,16 +101,25 @@
     return true;
 }
 
+// For clients which do not need the pair index dumped, pass a pair_index >= 0
+// to only dump the indicated pair.
 void
-PathMappingList::Dump (Stream *s)
+PathMappingList::Dump (Stream *s, int pair_index)
 {
     unsigned int numPairs = m_pairs.size();
-    unsigned int index;
 
-    for (index = 0; index < numPairs; ++index)
+    if (pair_index < 0)
     {
-        s->Printf("[%d] \"%s\" -> \"%s\"\n",
-                  index, m_pairs[index].first.GetCString(), m_pairs[index].second.GetCString());
+        unsigned int index;
+        for (index = 0; index < numPairs; ++index)
+            s->Printf("[%d] \"%s\" -> \"%s\"\n",
+                      index, m_pairs[index].first.GetCString(), m_pairs[index].second.GetCString());
+    }
+    else
+    {
+        if (pair_index < numPairs)
+            s->Printf("%s -> %s",
+                      m_pairs[pair_index].first.GetCString(), m_pairs[pair_index].second.GetCString());
     }
 }
 

Modified: lldb/trunk/source/Target/Target.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Target.cpp?rev=146422&r1=146421&r2=146422&view=diff
==============================================================================
--- lldb/trunk/source/Target/Target.cpp (original)
+++ lldb/trunk/source/Target/Target.cpp Mon Dec 12 15:59:28 2011
@@ -2486,6 +2486,15 @@
     }
     else if (var_name == GetSettingNameForSourcePathMap ())
     {
+        if (m_source_map.GetSize())
+        {
+            size_t i;
+            for (i = 0; i < m_source_map.GetSize(); ++i) {
+                StreamString sstr;
+                m_source_map.Dump(&sstr, i);
+                value.AppendString(sstr.GetData());
+            }
+        }
     }
     else if (var_name == GetSettingNameForMaxChildren())
     {

Modified: lldb/trunk/test/source-manager/TestSourceManager.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/source-manager/TestSourceManager.py?rev=146422&r1=146421&r2=146422&view=diff
==============================================================================
--- lldb/trunk/test/source-manager/TestSourceManager.py (original)
+++ lldb/trunk/test/source-manager/TestSourceManager.py Mon Dec 12 15:59:28 2011
@@ -22,6 +22,7 @@
         TestBase.setUp(self)
         # Find the line number to break inside main().
         self.line = line_number('main.c', '// Set break point at this line.')
+        lldb.skip_build_and_cleanup = False
 
     @python_api_test
     def test_display_source_python(self):
@@ -29,6 +30,11 @@
         self.buildDefault()
         self.display_source_python()
 
+    def test_move_and_then_display_source(self):
+        """Test that target.source-map settings work by moving main.c to hidden/main.c."""
+        self.buildDefault()
+        self.move_and_then_display_source()
+
     def test_modify_source_file_while_debugging(self):
         """Modify a source file while debugging the executable."""
         self.buildDefault()
@@ -70,6 +76,33 @@
                     exe=False,
             patterns = ['=> %d.*Hello world' % self.line])        
 
+    def move_and_then_display_source(self):
+        """Test that target.source-map settings work by moving main.c to hidden/main.c."""
+        exe = os.path.join(os.getcwd(), "a.out")
+        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
+
+        # Move main.c to hidden/main.c.
+        main_c = "main.c"
+        main_c_hidden = os.path.join("hidden", main_c)
+        os.rename(main_c, main_c_hidden)
+
+        if self.TraceOn():
+            system(["ls"])
+            system(["ls", "hidden"])
+
+        # Restore main.c after the test.
+        self.addTearDownHook(lambda: os.rename(main_c_hidden, main_c))
+
+        # Set target.source-map settings.
+        self.runCmd("settings set target.source-map %s %s" % (os.getcwd(), os.path.join(os.getcwd(), "hidden")))
+        # And verify that the settings work.
+        self.expect("settings show target.source-map",
+            substrs = [os.getcwd(), os.path.join(os.getcwd(), "hidden")])
+
+        # Display main() and verify that the source mapping has been kicked in.
+        self.expect("list -n main", SOURCE_DISPLAYED_CORRECTLY,
+            substrs = ['Hello world'])
+
     def modify_source_file_while_debugging(self):
         """Modify a source file while debugging the executable."""
         exe = os.path.join(os.getcwd(), "a.out")





More information about the lldb-commits mailing list