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.


git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@146422 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Core/SourceManager.cpp b/source/Core/SourceManager.cpp
index 3e4a78a..de24b1e 100644
--- a/source/Core/SourceManager.cpp
+++ b/source/Core/SourceManager.cpp
@@ -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)
diff --git a/source/Target/PathMappingList.cpp b/source/Target/PathMappingList.cpp
index 5067422..628fcc2 100644
--- a/source/Target/PathMappingList.cpp
+++ b/source/Target/PathMappingList.cpp
@@ -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());
     }
 }
 
diff --git a/source/Target/Target.cpp b/source/Target/Target.cpp
index a887866..902ffc7 100644
--- a/source/Target/Target.cpp
+++ b/source/Target/Target.cpp
@@ -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())
     {