[Lldb-commits] [lldb] r171945 - in /lldb/trunk: include/lldb/Core/SourceManager.h source/Commands/CommandObjectSource.cpp source/Core/SourceManager.cpp

Jim Ingham jingham at apple.com
Tue Jan 8 19:27:34 PST 2013


Author: jingham
Date: Tue Jan  8 21:27:33 2013
New Revision: 171945

URL: http://llvm.org/viewvc/llvm-project?rev=171945&view=rev
Log:
Add a "--reverse" or "-r" option to the "list" with no options command.  This will list backwards from the
last source point listed.
Also fix the setting of the default file & line to the file containing main, when you do a plain "list".

<rdar://problem/12685226>

Modified:
    lldb/trunk/include/lldb/Core/SourceManager.h
    lldb/trunk/source/Commands/CommandObjectSource.cpp
    lldb/trunk/source/Core/SourceManager.cpp

Modified: lldb/trunk/include/lldb/Core/SourceManager.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/SourceManager.h?rev=171945&r1=171944&r2=171945&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/SourceManager.h (original)
+++ lldb/trunk/include/lldb/Core/SourceManager.h Tue Jan  8 21:27:33 2013
@@ -119,13 +119,6 @@
     }
 
     size_t
-    DisplaySourceLines (const FileSpec &file,
-                        uint32_t line,
-                        uint32_t context_before,
-                        uint32_t context_after,
-                        Stream *s);
-
-    size_t
     DisplaySourceLinesWithLineNumbers (const FileSpec &file,
                                        uint32_t line,
                                        uint32_t context_before,
@@ -145,7 +138,8 @@
 
     size_t
     DisplayMoreWithLineNumbers (Stream *s,
-                                const SymbolContextList *bp_locs = NULL);
+                                const SymbolContextList *bp_locs = NULL,
+                                bool reverse = false);
 
     bool
     SetDefaultFileAndLine (const FileSpec &file_spec, uint32_t line);
@@ -179,6 +173,7 @@
     uint32_t m_last_file_context_before;
     uint32_t m_last_file_context_after;
     bool     m_default_set;
+    bool     m_first_reverse;
     Target *m_target;
     Debugger *m_debugger;
     

Modified: lldb/trunk/source/Commands/CommandObjectSource.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectSource.cpp?rev=171945&r1=171944&r2=171945&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectSource.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectSource.cpp Tue Jan  8 21:27:33 2013
@@ -201,6 +201,9 @@
             case 'b':
                 show_bp_locs = true;
                 break;
+            case 'r':
+                reverse = true;
+                break;
            default:
                 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
                 break;
@@ -219,6 +222,7 @@
             start_line = 0;
             num_lines = 10;
             show_bp_locs = false;
+            reverse = false;
             modules.clear();
         }
 
@@ -238,6 +242,7 @@
         uint32_t num_lines;
         STLStringArray modules;        
         bool show_bp_locs;
+        bool reverse;
     };
  
 public:   
@@ -248,18 +253,6 @@
                              NULL),
         m_options (interpreter)
     {
-        CommandArgumentEntry arg;
-        CommandArgumentData file_arg;
-        
-        // Define the first (and only) variant of this arg.
-        file_arg.arg_type = eArgTypeFilename;
-        file_arg.arg_repetition = eArgRepeatOptional;
-        
-        // There is only one variant this argument could be; put it into the argument entry.
-        arg.push_back (file_arg);
-        
-        // Push the data for the first argument into the m_arguments vector.
-        m_arguments.push_back (arg);
     }
 
     ~CommandObjectSourceList ()
@@ -276,7 +269,29 @@
     virtual const char *
     GetRepeatCommand (Args &current_command_args, uint32_t index)
     {
-        return m_cmd_name.c_str();
+        // This is kind of gross, but the command hasn't been parsed yet so we can't look at the option
+        // values for this invocation...  I have to scan the arguments directly.
+        size_t num_args = current_command_args.GetArgumentCount();
+        bool is_reverse = false;
+        for (size_t i = 0 ; i < num_args; i++)
+        {
+            const char *arg = current_command_args.GetArgumentAtIndex(i);
+            if (arg && (strcmp(arg, "-r") == 0 || strcmp(arg, "--reverse") == 0))
+            {
+                is_reverse = true;
+            }
+        }
+        if (is_reverse)
+        {
+            if (m_reverse_name.empty())
+            {
+                m_reverse_name = m_cmd_name;
+                m_reverse_name.append (" -r");
+            }
+            return m_reverse_name.c_str();
+        }
+        else
+            return m_cmd_name.c_str();
     }
 
 protected:
@@ -570,7 +585,8 @@
             if (m_options.start_line == 0)
             {
                 if (target->GetSourceManager().DisplayMoreWithLineNumbers (&result.GetOutputStream(),
-                                                                                               GetBreakpointLocations ()))
+                                                                           GetBreakpointLocations (),
+                                                                           m_options.reverse))
                 {
                     result.SetStatus (eReturnStatusSuccessFinishResult);
                 }
@@ -724,6 +740,7 @@
     }
     CommandOptions m_options;
     FileLineResolver m_breakpoint_locations;
+    std::string    m_reverse_name;
 
 };
 
@@ -738,6 +755,7 @@
 { LLDB_OPT_SET_1  , false, "line",   'l', required_argument, NULL, 0, eArgTypeLineNum,    "The line number at which to start the display source."},
 { LLDB_OPT_SET_2  , false, "name",   'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeSymbol,    "The name of a function whose source to display."},
 { LLDB_OPT_SET_3  , false, "address",'a', required_argument, NULL, 0, eArgTypeAddress, "Lookup the address and display the source information for the corresponding file and line."},
+{ LLDB_OPT_SET_4, false, "reverse", 'r', no_argument, NULL, 0, eArgTypeNone, "Reverse the listing to look backwards from the last displayed block of source."},
 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
 };
 

Modified: lldb/trunk/source/Core/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/SourceManager.cpp?rev=171945&r1=171944&r2=171945&view=diff
==============================================================================
--- lldb/trunk/source/Core/SourceManager.cpp (original)
+++ lldb/trunk/source/Core/SourceManager.cpp Tue Jan  8 21:27:33 2013
@@ -42,6 +42,7 @@
     m_last_file_context_before (0),
     m_last_file_context_after (10),
     m_default_set(false),
+    m_first_reverse(true),
     m_target (&target),
     m_debugger(NULL)
 {
@@ -54,6 +55,7 @@
     m_last_file_context_before (0),
     m_last_file_context_after (10),
     m_default_set(false),
+    m_first_reverse (true),
     m_target (NULL),
     m_debugger (&debugger)
 {
@@ -66,26 +68,6 @@
 {
 }
 
-size_t
-SourceManager::DisplaySourceLines
-(
-    const FileSpec &file_spec,
-    uint32_t line,
-    uint32_t context_before,
-    uint32_t context_after,
-    Stream *s
-)
-{
-    m_last_file_sp = GetFile (file_spec);
-    m_last_file_line = line + context_after + 1;
-    m_last_file_context_before = context_before;
-    m_last_file_context_after = context_after;
-    if (m_last_file_sp.get())
-        return m_last_file_sp->DisplaySourceLines (line, context_before, context_after, s);
-
-    return 0;
-}
-
 SourceManager::FileSP
 SourceManager::GetFile (const FileSpec &file_spec)
 {
@@ -112,6 +94,7 @@
     const SymbolContextList *bp_locs
 )
 {
+    m_first_reverse = true;
     size_t return_value = 0;
     if (line == 0)
     {
@@ -199,13 +182,67 @@
 }
 
 size_t
-SourceManager::DisplayMoreWithLineNumbers (Stream *s, const SymbolContextList *bp_locs)
+SourceManager::DisplayMoreWithLineNumbers (Stream *s, const SymbolContextList *bp_locs, bool reverse)
 {
+    // If we get called before anybody has set a default file and line, then try to figure it out here.
+    if (!m_default_set)
+    {
+        FileSpec tmp_spec;
+        uint32_t tmp_line;
+        GetDefaultFileAndLine(tmp_spec, tmp_line);
+    }
+    
     if (m_last_file_sp)
     {
         if (m_last_file_line == UINT32_MAX)
             return 0;
-        return DisplaySourceLinesWithLineNumbersUsingLastFile (0, m_last_file_context_before, m_last_file_context_after, "", s, bp_locs);
+        
+        if (reverse && m_last_file_line == 1)
+            return 0;
+        
+        uint32_t line;
+        uint32_t new_last_file_line = 0;
+        
+        if (m_last_file_line != 0
+            && m_last_file_line != UINT32_MAX)
+        {
+            if (reverse)
+            {
+                // If this is the first time we've done a reverse, then back up one more time so we end
+                // up showing the chunk before the last one we've shown:
+                if (m_first_reverse)
+                {
+                    if (m_last_file_line > m_last_file_context_after)
+                        m_last_file_line -= m_last_file_context_after + 1;
+                }
+                
+                if (m_last_file_line > m_last_file_context_after)
+                {
+                    line = m_last_file_line - m_last_file_context_after;
+                    if (line > m_last_file_context_before)
+                        new_last_file_line = line - m_last_file_context_before - 1;
+                    else
+                        new_last_file_line = 1;
+                }
+                else
+                {
+                    line = 1;
+                    new_last_file_line = 1;
+                }
+            }
+            else
+                line = m_last_file_line + m_last_file_context_before;
+        }
+        else
+            line = 1;
+        
+        size_t num_chars_shown = DisplaySourceLinesWithLineNumbersUsingLastFile (line, m_last_file_context_before, m_last_file_context_after, "", s, bp_locs);
+        if (new_last_file_line != 0)
+            m_last_file_line = new_last_file_line;
+        if (reverse)
+            m_first_reverse = false;
+        
+        return num_chars_shown;
     }
     return 0;
 }





More information about the lldb-commits mailing list