[Lldb-commits] [lldb] [llvm] [lldb][RISCV] function calls support in lldb expressions (PR #99336)

via lldb-commits lldb-commits at lists.llvm.org
Thu Jul 25 14:13:50 PDT 2024


================
@@ -0,0 +1,58 @@
+//===--- DirectToIndirectFCR.h - RISC-V specific pass ---------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#pragma once
+
+#include "lldb/lldb-types.h"
+
+#include "llvm/IR/Instructions.h"
+#include "llvm/Pass.h"
+
+namespace lldb_private {
+
+class ExecutionContext;
+
+// During the lldb expression execution lldb wraps a user expression, jittes
+// fabricated code and then puts it into the stack memory. Thus, if user tried
----------------
dlav-sc wrote:

Well, lldb generates its own code that wraps expression, something like:

```
#line 1 "<lldb wrapper prefix>"                                                                                                                                                                                                                                                    
#ifndef offsetof                                                                                                                                                                                                                                                                   
#define offsetof(t, d) __builtin_offsetof(t, d)                                                                                                                                                                                                                                    
#endif                                                                                                                                                                                                                                                                             
#ifndef NULL                                                                                                                                                                                                                                                                       
#define NULL (__null)                                                                                                                                                                                                                                                              
#endif                                                                                                                                                                                                                                                                             
#ifndef Nil                                                                                                                                                                                                                                                                        
#define Nil (__null)                                                                                                                                                                                                                                                               
#endif                                                                                                                                                                                                                                                                             
#ifndef nil                                                                                                                                                                                                                                                                        
#define nil (__null)                                                                                                                                                                                                                                                               
#endif                                                                                                                                                                                                                                                                             
#ifndef YES                                                                                                                                                                                                                                                                        
#define YES ((BOOL)1)                                                                                                                                                                                                                                                              
#endif                                                                                                                                                                                                                                                                             
#ifndef NO                                                                                                                                                                                                                                                                         
#define NO ((BOOL)0)                                                                                                                                                                                                                                                               
#endif                                                                                                                                                                                                                                                                             
typedef __INT8_TYPE__ int8_t;                                                                                                                                                                                                                                                      
typedef __UINT8_TYPE__ uint8_t;                                                                                                                                                                                                                                                    
typedef __INT16_TYPE__ int16_t;                                                                                                                                                                                                                                                    
typedef __UINT16_TYPE__ uint16_t;                                                                                                                                                                                                                                                  
typedef __INT32_TYPE__ int32_t;                                                                                                                                                                                                                                                    
typedef __UINT32_TYPE__ uint32_t;                                                                                                                                                                                                                                                  
typedef __INT64_TYPE__ int64_t;                                                                                                                                                                                                                                                    
typedef __UINT64_TYPE__ uint64_t;                                                                                                                                                                                                                                                  
typedef __INTPTR_TYPE__ intptr_t;                                                                                                                                                                                                                                                  
typedef __UINTPTR_TYPE__ uintptr_t;                                                                                                                                                                                                                                                
typedef __SIZE_TYPE__ size_t;                                                                                                                                                                                                                                                      
typedef __PTRDIFF_TYPE__ ptrdiff_t;                                                                                                                                                                                                                                                
typedef unsigned short unichar;                                                                                                                                                                                                                                                    
extern "C"                                                                                                                                                                                                                                                                         
{                                                                                                                                                                                                                                                                                  
    int printf(const char * __restrict, ...);                                                                                                                                                                                                                                      
}                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                   
typedef signed char BOOL;                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                   
void                                                                                                                                                                                                                                                                               
$__lldb_expr(void *$__lldb_arg)                                                                                                                                                                                                                                                    
{                                                                                                                                                                                                                                                                                  
    ;                                                                                                                                                                                                                                                                              
#line 1 "<user expression 0>"                                                                                                                                                                                                                                                      
foo()                                                                                                                                                                                                                                                                              
;                                                                                                                                                                                                                                                                                  
#line 1 "<lldb wrapper suffix>"                                                                                                                                                                                                                                                    
}
```

So lldb have to puts this code somewhere, resolves relocations and executes. I have no idea where lldb can place the code except of the stack.

I've checked that on x86 lldb does the same:

```                                                                                                                                                                        
lldb             IRMemoryMap::Malloc (79, 0x10, 0x6, eAllocationPolicyProcessOnly) -> 0x7ffff7fc7040                                                                                                                                                                               
lldb             IRMemoryMap::Malloc (1, 0x1, 0x2, eAllocationPolicyProcessOnly) -> 0x7ffff7fc8450                                                                                                                                                                                 
lldb             IRMemoryMap::Malloc (102, 0x1, 0x2, eAllocationPolicyProcessOnly) -> 0x7ffff7fc8460                                                                                                                                                                               
lldb             IRMemoryMap::Malloc (7, 0x4, 0x6, eAllocationPolicyProcessOnly) -> 0x7ffff7fc7090                                                                                                                                                                                 
lldb             IRMemoryMap::Malloc (31, 0x8, 0x2, eAllocationPolicyProcessOnly) -> 0x7ffff7fc84d0                                                                                                                                                                                
lldb             IRMemoryMap::Malloc (111, 0x1, 0x2, eAllocationPolicyProcessOnly) -> 0x7ffff7fc84f0                                                                                                                                                                               
lldb             IRMemoryMap::Malloc (1, 0x1, 0x2, eAllocationPolicyProcessOnly) -> 0x7ffff7fc8560                                                                                                                                                                                 
lldb             IRMemoryMap::Malloc (103, 0x8, 0x2, eAllocationPolicyProcessOnly) -> 0x7ffff7fc8570                                                                                                                                                                               
```
```                                                                                                                                                                                                                                             
lldb             Function disassembly:
0x7ffff7fc7040: 55                             other       pushq  %rbp
0x7ffff7fc7041: 48 89 e5                       other       movq   %rsp, %rbp
0x7ffff7fc7044: 53                             other       pushq  %rbx
0x7ffff7fc7045: 50                             other       pushq  %rax
0x7ffff7fc7046: 48 89 fb                       other       movq   %rdi, %rbx
0x7ffff7fc7049: 48 8d 7d f0                    other       leaq   -0x10(%rbp), %rdi
0x7ffff7fc704d: 48 b9 00 70 fc f7 ff 7f 00 00  other       movabsq $0x7ffff7fc7000, %rcx ; imm = 0x7FFFF7FC7000 
0x7ffff7fc7057: b0 00                          other       movb   $0x0, %al
0x7ffff7fc7059: ff d1                          call        callq  *%rcx
0x7ffff7fc705b: 48 89 5d f0                    other       movq   %rbx, -0x10(%rbp)
0x7ffff7fc705f: 48 b8 29 51 55 55 55 55 00 00  other       movabsq $0x555555555129, %rax ; imm = 0x555555555129 
0x7ffff7fc7069: ff d0                          call        callq  *%rax
0x7ffff7fc706b: 48 83 c4 08                    other       addq   $0x8, %rsp
0x7ffff7fc706f: 5b                             other       popq   %rbx
0x7ffff7fc7070: 5d                             other       popq   %rbp
0x7ffff7fc7071: c3                             return      retq   

```

https://github.com/llvm/llvm-project/pull/99336


More information about the lldb-commits mailing list