[Lldb-commits] [lldb] r194035 - Update ABISysV_x86_64.cpp to use more efficient register finding calls.

Greg Clayton gclayton at apple.com
Mon Nov 4 17:24:05 PST 2013


Author: gclayton
Date: Mon Nov  4 19:24:05 2013
New Revision: 194035

URL: http://llvm.org/viewvc/llvm-project?rev=194035&view=rev
Log:
Update ABISysV_x86_64.cpp to use more efficient register finding calls. 

Instead of looking up registers by name, we use the generic ID when we can.

Also added code that creates an extra frame when running expressions by pushing the current PC and FP and then hooking up the FP backchain. This code is "#if 0" out for now until we can pair it with unwinder fixes.


Modified:
    lldb/trunk/include/lldb/Target/RegisterContext.h
    lldb/trunk/source/Plugins/ABI/SysV-x86_64/ABISysV_x86_64.cpp
    lldb/trunk/source/Target/RegisterContext.cpp

Modified: lldb/trunk/include/lldb/Target/RegisterContext.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/RegisterContext.h?rev=194035&r1=194034&r2=194035&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/RegisterContext.h (original)
+++ lldb/trunk/include/lldb/Target/RegisterContext.h Mon Nov  4 19:24:05 2013
@@ -123,6 +123,9 @@ public:
     const RegisterInfo *
     GetRegisterInfoByName (const char *reg_name, uint32_t start_idx = 0);
 
+    const RegisterInfo *
+    GetRegisterInfo (uint32_t reg_kind, uint32_t reg_num);
+
     uint64_t
     GetPC (uint64_t fail_value = LLDB_INVALID_ADDRESS);
 

Modified: lldb/trunk/source/Plugins/ABI/SysV-x86_64/ABISysV_x86_64.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ABI/SysV-x86_64/ABISysV_x86_64.cpp?rev=194035&r1=194034&r2=194035&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ABI/SysV-x86_64/ABISysV_x86_64.cpp (original)
+++ lldb/trunk/source/Plugins/ABI/SysV-x86_64/ABISysV_x86_64.cpp Mon Nov  4 19:24:05 2013
@@ -354,7 +354,7 @@ ABISysV_x86_64::PrepareTrivialCall (Thre
     const RegisterInfo *reg_info = NULL;
     if (arg1_ptr)
     {
-        reg_info = reg_ctx->GetRegisterInfoByName("rdi", 0);
+        reg_info = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1);
         if (log)
             log->Printf("About to write arg1 (0x%" PRIx64 ") into %s", (uint64_t)*arg1_ptr, reg_info->name);
 
@@ -363,7 +363,7 @@ ABISysV_x86_64::PrepareTrivialCall (Thre
 
         if (arg2_ptr)
         {
-            reg_info = reg_ctx->GetRegisterInfoByName("rsi", 0);
+            reg_info = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2);
             if (log)
                 log->Printf("About to write arg2 (0x%" PRIx64 ") into %s", (uint64_t)*arg2_ptr, reg_info->name);
             if (!reg_ctx->WriteRegisterFromUnsigned (reg_info, *arg2_ptr))
@@ -371,7 +371,7 @@ ABISysV_x86_64::PrepareTrivialCall (Thre
 
             if (arg3_ptr)
             {
-                reg_info = reg_ctx->GetRegisterInfoByName("rdx", 0);
+                reg_info = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG3);
                 if (log)
                     log->Printf("About to write arg3 (0x%" PRIx64 ") into %s", (uint64_t)*arg3_ptr, reg_info->name);
                 if (!reg_ctx->WriteRegisterFromUnsigned (reg_info, *arg3_ptr))
@@ -379,7 +379,7 @@ ABISysV_x86_64::PrepareTrivialCall (Thre
 
                 if (arg4_ptr)
                 {
-                    reg_info = reg_ctx->GetRegisterInfoByName("rcx", 0);
+                    reg_info = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG4);
                     if (log)
                         log->Printf("About to write arg4 (0x%" PRIx64 ") into %s", (uint64_t)*arg4_ptr, reg_info->name);
                     if (!reg_ctx->WriteRegisterFromUnsigned (reg_info, *arg4_ptr))
@@ -387,7 +387,7 @@ ABISysV_x86_64::PrepareTrivialCall (Thre
 
                     if (arg5_ptr)
                     {
-                        reg_info = reg_ctx->GetRegisterInfoByName("r8", 0);
+                        reg_info = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG5);
                         if (log)
                             log->Printf("About to write arg5 (0x%" PRIx64 ") into %s", (uint64_t)*arg5_ptr, reg_info->name);
                         if (!reg_ctx->WriteRegisterFromUnsigned (reg_info, *arg5_ptr))
@@ -395,7 +395,7 @@ ABISysV_x86_64::PrepareTrivialCall (Thre
 
                         if (arg6_ptr)
                         {
-                            reg_info = reg_ctx->GetRegisterInfoByName("r9", 0);
+                            reg_info = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG6);
                             if (log)
                                 log->Printf("About to write arg6 (0x%" PRIx64 ") into %s", (uint64_t)*arg6_ptr, reg_info->name);
                             if (!reg_ctx->WriteRegisterFromUnsigned (reg_info, *arg6_ptr))
@@ -415,32 +415,75 @@ ABISysV_x86_64::PrepareTrivialCall (Thre
 
     sp &= ~(0xfull); // 16-byte alignment
 
-    // The return address is pushed onto the stack (yes after the alignment...)
     sp -= 8;
 
+    Error error;
+    const RegisterInfo *pc_reg_info = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
+    const RegisterInfo *sp_reg_info = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
+    ProcessSP process_sp (thread.GetProcess());
+
     RegisterValue reg_value;
-    reg_value.SetUInt64 (return_addr);
 
+#if 0
+    // This code adds an extra frame so that we don't lose the function that we came from
+    // by pushing the PC and the FP and then writing the current FP to point to the FP value
+    // we just pushed. It is disabled for now until the stack backtracing code can be debugged.
+
+    // Save current PC
+    const RegisterInfo *fp_reg_info = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP);
+    if (reg_ctx->ReadRegister(pc_reg_info, reg_value))
+    {
+        if (log)
+            log->Printf("Pushing the current PC onto the stack: 0x%" PRIx64 ": 0x%" PRIx64, (uint64_t)sp, reg_value.GetAsUInt64());
+        
+        if (!process_sp->WritePointerToMemory(sp, reg_value.GetAsUInt64(), error))
+            return false;
+
+        sp -= 8;
+        
+        // Save current FP
+        if (reg_ctx->ReadRegister(fp_reg_info, reg_value))
+        {
+            if (log)
+                log->Printf("Pushing the current FP onto the stack: 0x%" PRIx64 ": 0x%" PRIx64, (uint64_t)sp, reg_value.GetAsUInt64());
+            
+            if (!process_sp->WritePointerToMemory(sp, reg_value.GetAsUInt64(), error))
+                return false;
+        }
+        // Setup FP backchain
+        reg_value.SetUInt64 (sp);
+        
+        if (log)
+            log->Printf("Writing FP:  0x%" PRIx64 " (for FP backchain)", reg_value.GetAsUInt64());
+
+        if (!reg_ctx->WriteRegister(fp_reg_info, reg_value))
+        {
+            return false;
+        }
+        
+        sp -= 8;
+    }
+#endif 
+    
     if (log)
-        log->Printf("Pushing the return address onto the stack: new SP 0x%" PRIx64 ", return address 0x%" PRIx64, (uint64_t)sp, (uint64_t)return_addr);
+        log->Printf("Pushing the return address onto the stack: 0x%" PRIx64 ": 0x%" PRIx64, (uint64_t)sp, (uint64_t)return_addr);
 
-    const RegisterInfo *pc_reg_info = reg_ctx->GetRegisterInfoByName("rip");
-    Error error (reg_ctx->WriteRegisterValueToMemory(pc_reg_info, sp, pc_reg_info->byte_size, reg_value));
-    if (error.Fail())
+    // Save return address onto the stack
+    if (!process_sp->WritePointerToMemory(sp, return_addr, error))
         return false;
 
     // %rsp is set to the actual stack value.
 
     if (log)
-        log->Printf("Writing SP (0x%" PRIx64 ") down", (uint64_t)sp);
+        log->Printf("Writing SP: 0x%" PRIx64, (uint64_t)sp);
     
-    if (!reg_ctx->WriteRegisterFromUnsigned (reg_ctx->GetRegisterInfoByName("rsp"), sp))
+    if (!reg_ctx->WriteRegisterFromUnsigned (sp_reg_info, sp))
         return false;
 
     // %rip is set to the address of the called function.
     
     if (log)
-        log->Printf("Writing new IP (0x%" PRIx64 ") down", (uint64_t)func_addr);
+        log->Printf("Writing IP: 0x%" PRIx64, (uint64_t)func_addr);
 
     if (!reg_ctx->WriteRegisterFromUnsigned (pc_reg_info, func_addr))
         return false;
@@ -506,12 +549,12 @@ ABISysV_x86_64::GetArgumentValues (Threa
     
     uint32_t argument_register_ids[6];
     
-    argument_register_ids[0] = reg_ctx->GetRegisterInfoByName("rdi", 0)->kinds[eRegisterKindLLDB];
-    argument_register_ids[1] = reg_ctx->GetRegisterInfoByName("rsi", 0)->kinds[eRegisterKindLLDB];
-    argument_register_ids[2] = reg_ctx->GetRegisterInfoByName("rdx", 0)->kinds[eRegisterKindLLDB];
-    argument_register_ids[3] = reg_ctx->GetRegisterInfoByName("rcx", 0)->kinds[eRegisterKindLLDB];
-    argument_register_ids[4] = reg_ctx->GetRegisterInfoByName("r8", 0)->kinds[eRegisterKindLLDB];
-    argument_register_ids[5] = reg_ctx->GetRegisterInfoByName("r9", 0)->kinds[eRegisterKindLLDB];
+    argument_register_ids[0] = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1)->kinds[eRegisterKindLLDB];
+    argument_register_ids[1] = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2)->kinds[eRegisterKindLLDB];
+    argument_register_ids[2] = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG3)->kinds[eRegisterKindLLDB];
+    argument_register_ids[3] = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG4)->kinds[eRegisterKindLLDB];
+    argument_register_ids[4] = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG5)->kinds[eRegisterKindLLDB];
+    argument_register_ids[5] = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG6)->kinds[eRegisterKindLLDB];
     
     unsigned int current_argument_register = 0;
     

Modified: lldb/trunk/source/Target/RegisterContext.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/RegisterContext.cpp?rev=194035&r1=194034&r2=194035&view=diff
==============================================================================
--- lldb/trunk/source/Target/RegisterContext.cpp (original)
+++ lldb/trunk/source/Target/RegisterContext.cpp Mon Nov  4 19:24:05 2013
@@ -81,6 +81,15 @@ RegisterContext::GetRegisterInfoByName (
     return NULL;
 }
 
+const RegisterInfo *
+RegisterContext::GetRegisterInfo (uint32_t kind, uint32_t num)
+{
+    const uint32_t reg_num = ConvertRegisterKindToRegisterNumber(kind, num);
+    if (reg_num == LLDB_INVALID_REGNUM)
+        return NULL;
+    return GetRegisterInfoAtIndex (reg_num);
+}
+
 const char *
 RegisterContext::GetRegisterName (uint32_t reg)
 {





More information about the lldb-commits mailing list