[Lldb-commits] Patch for Review: Fix "source list -n printf" on Linux (printf is symbol alias for __printf)

Michael Sartain mikesart at valvesoftware.com
Fri Jul 5 18:00:53 PDT 2013


"source list -n printf" wasn't working on Linux because printf is a symbol
and the code was only searching for functions.

The patch now searches for symbols if function searching fails and tries to
find a symbol that lines up exactly with a function.

The Linux test suite passes with this patch. Please let me know if it's ok
to submit.
 -Mike

http://llvm-reviews.chandlerc.com/D1107

Files:
  source/Commands/CommandObjectSource.cpp

Index: source/Commands/CommandObjectSource.cpp
===================================================================
--- source/Commands/CommandObjectSource.cpp
+++ source/Commands/CommandObjectSource.cpp
@@ -26,11 +26,13 @@
 #include "lldb/Host/FileSpec.h"
 #include "lldb/Symbol/CompileUnit.h"
 #include "lldb/Symbol/Function.h"
+#include "lldb/Symbol/Symbol.h"
 #include "lldb/Target/Process.h"
 #include "lldb/Target/TargetList.h"
 #include "lldb/Interpreter/CommandCompletions.h"
 #include "lldb/Interpreter/Options.h"

+
 using namespace lldb;
 using namespace lldb_private;

@@ -439,6 +441,39 @@
         return 0;
     }

+    size_t FindMatchingFunctions (Target *target, const ConstString &name,
Options &options, bool include_symbols, SymbolContextList& sc_list)
+    {
+        // Displaying the source for a symbol:
+        bool include_inlines = true;
+        bool append = false;
+        size_t num_matches = 0;
+
+        if (m_options.num_lines == 0)
+            m_options.num_lines = 10;
+
+        const size_t num_modules = m_options.modules.size();
+        if (num_modules > 0)
+        {
+            ModuleList matching_modules;
+            for (size_t i = 0; i < num_modules; ++i)
+            {
+                FileSpec module_file_spec(m_options.modules[i].c_str(),
false);
+                if (module_file_spec)
+                {
+                    ModuleSpec module_spec (module_file_spec);
+                    matching_modules.Clear();
+                    target->GetImages().FindModules (module_spec,
matching_modules);
+                    num_matches += matching_modules.FindFunctions (name,
eFunctionNameTypeAuto, include_symbols, include_inlines, append, sc_list);
+                }
+            }
+        }
+        else
+        {
+            num_matches = target->GetImages().FindFunctions (name,
eFunctionNameTypeAuto, include_symbols, include_inlines, append, sc_list);
+        }
+        return num_matches;
+    }
+
     bool
     DoExecute (Args& command, CommandReturnObject &result)
     {
@@ -456,38 +491,36 @@
         SymbolContextList sc_list;
         if (!m_options.symbol_name.empty())
         {
-            // Displaying the source for a symbol:
+            SymbolContext sc;
             ConstString name(m_options.symbol_name.c_str());
-            bool include_symbols = false;
-            bool include_inlines = true;
-            bool append = true;
-            size_t num_matches = 0;
-
-            if (m_options.num_lines == 0)
-                m_options.num_lines = 10;

-            const size_t num_modules = m_options.modules.size();
-            if (num_modules > 0)
-            {
-                ModuleList matching_modules;
-                for (size_t i = 0; i < num_modules; ++i)
+            // Displaying the source for a symbol. Search for function
named name.
+            size_t num_matches = FindMatchingFunctions (target, name,
m_options, false, sc_list);
+            if (!num_matches)
+            {
+                // If we didn't find any functions with that name, try
searching for symbols.
+                size_t num_sym_matches = FindMatchingFunctions (target,
name, m_options, true, sc_list);
+                if (num_sym_matches)
                 {
-                    FileSpec
module_file_spec(m_options.modules[i].c_str(), false);
-                    if (module_file_spec)
+                    // Go through all matching symbols and see if any line
up exactly with a function address.
+                    for (size_t i = 0; i < num_sym_matches; i++)
                     {
-                        ModuleSpec module_spec (module_file_spec);
-                        matching_modules.Clear();
-                        target->GetImages().FindModules (module_spec,
matching_modules);
-                        num_matches += matching_modules.FindFunctions
(name, eFunctionNameTypeAuto, include_symbols, include_inlines, append,
sc_list);
+                        sc_list.GetContextAtIndex (i, sc);
+                        if (sc.symbol)
+                        {
+                            const Address &base_address =
sc.symbol->GetAddress();
+                            Function *function =
base_address.CalculateSymbolContextFunction();
+                            if (function)
+                            {
+                                sc_list.Clear();
+                                sc_list.Append (SymbolContext(function));
+                                num_matches = 1;
+                                break;
+                            }
+                        }
                     }
                 }
             }
-            else
-            {
-                num_matches = target->GetImages().FindFunctions (name,
eFunctionNameTypeAuto, include_symbols, include_inlines, append, sc_list);
-            }
-
-            SymbolContext sc;

             if (num_matches == 0)
             {
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20130705/1551d7b3/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1107.1.patch
Type: text/x-patch
Size: 4976 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20130705/1551d7b3/attachment.bin>


More information about the lldb-commits mailing list