|
| static int | backtrace_check_address (void *pc) |
| | Checks whether a given program counter (PC) address is within a valid range.
|
| |
| static int | riscv_backtrace_find_lr_offset (char *LR) |
| | Finds the offset of the Link Register (LR) from the return address in a backtrace.
|
| |
| int | riscv_ins32_get_push_lr_framesize (uint32_t inst, int *offset) |
| | Determines the frame size for a push to the link register (LR) in a RISC-V instruction.
|
| |
| static int | riscv_ins16_get_push_lr_framesize (uint16_t inst, int *offset) |
| | Determines the frame size for a push to the link register (LR) in a RISC-V compressed instruction.
|
| |
| int | riscv_ins32_backtrace_stask_push (uint32_t inst) |
| | Checks if a RISC-V instruction corresponds to stack push instructions like addi sp, sp, imm and addiw sp, sp, imm, and calculates the effective stack push size based on the immediate value.
|
| |
| static int | riscv_ins16_backtrace_stask_push (uint32_t inst) |
| | Processes compressed RISC-V instructions related to stack pointer adjustments.
|
| |
| static int | riscv_backtrace_from_stack (long **pSP, char **pPC, char **pLR) |
| | Perform a backtrace from the current stack pointer and program counter.
|
| |
| static int | backtrace_from_stack (long **pSP, char **pPC, char **pLR) |
| | Performs a backtrace from the current stack frame.
|
| |
| static int | riscv_ins32_backtrace_return_pop (uint32_t inst) |
| | Handles the backtrace return pop for a 32-bit instruction.
|
| |
| static int | riscv_ins16_backtrace_return_pop (uint16_t inst) |
| | Handles the backtrace return pop for a 16-bit instruction.
|
| |
| static int | riscv_ins32_backtrace_stack_pop (unsigned int inst) |
| | Handles the backtrace stack pop for a 32-bit instruction.
|
| |
| static int | riscv_ins16_backtrace_stack_pop (uint16_t inst) |
| | Handles the backtrace stack pop for a 16-bit instruction.
|
| |
| static int | riscv_backtrace_from_lr (long **pSP, char **pPC, char *LR) |
| | Reconstructs a backtrace using the link register (LR) and program counter (PC).
|
| |
| int | backtrace (char *PC, long *SP, char *LR) |
| | Perform a backtrace from the given program counter (PC), stack pointer (SP), and link register (LR).
|
| |
| int | dump_stack (void) |
| | Dumps the current stack trace by fetching the program counter (PC), stack pointer (SP), and link register (LR).
|
| |
| int backtrace |
( |
char * |
PC, |
|
|
long * |
SP, |
|
|
char * |
LR |
|
) |
| |
Perform a backtrace from the given program counter (PC), stack pointer (SP), and link register (LR).
Perform a backtrace to find the call stack.
This function tries to traverse the stack to generate a backtrace, logging the current PC at each level. If the stack backtrace fails, it attempts to trace using the LR as a fallback.
- Parameters
-
| PC | Pointer to the program counter. |
| SP | Pointer to the stack pointer. |
| LR | Pointer to the link register (return address). |
- Returns
- The number of backtrace levels found, or 0 if the backtrace failed.
| static int backtrace_from_stack |
( |
long ** |
pSP, |
|
|
char ** |
pPC, |
|
|
char ** |
pLR |
|
) |
| |
|
static |
Performs a backtrace from the current stack frame.
This function is used to trace the call stack, starting from the provided program counter and stack pointer. Before performing the backtrace, it checks if the program counter is valid (i.e., points to executable code). If the program counter is invalid, the function returns an error code (-1). Otherwise, it delegates the actual backtrace work to riscv_backtrace_from_stack.
- Parameters
-
| [in,out] | pSP | Pointer to the stack pointer. The stack pointer is updated with the result of the backtrace. |
| [in,out] | pPC | Pointer to the program counter. The program counter is updated with the result of the backtrace. |
- Returns
- 0 if the backtrace is successful and the program counter is updated correctly, or -1 if an invalid program counter is provided or the backtrace fails.
Dumps the current stack trace by fetching the program counter (PC), stack pointer (SP), and link register (LR).
Dumps the current stack state and performs a backtrace.
This function retrieves the current values of the stack pointer, program counter, and link register, then uses these to generate a backtrace. If either the stack pointer or program counter is invalid, it returns 0.
- Returns
- The backtrace level, or 0 if SP or PC is invalid.
| static int riscv_backtrace_from_stack |
( |
long ** |
pSP, |
|
|
char ** |
pPC, |
|
|
char ** |
pLR |
|
) |
| |
|
static |
Perform a backtrace from the current stack pointer and program counter.
This function walks the stack to retrieve the calling function's information by analyzing the return address (LR) and corresponding program counter (PC) values. It starts by looking for instructions that push the link register (LR) to the stack, then works backwards through the stack frames. The function handles both 16-bit and 32-bit instructions in RISC-V architecture.
The function attempts to find the appropriate return address and adjusts the stack pointer (SP) accordingly. It then calculates the program counter (PC) from the link register (LR) by finding the corresponding offset. The function is responsible for determining whether the address information is valid and updates the SP and PC pointers.
The function returns 1 if a valid backtrace is successfully performed, or 0 if the link register's offset is zero. If an error is encountered at any point, the function prints an error message and returns -1.
- Parameters
-
| pSP | Pointer to the stack pointer (SP) to be updated. |
| pPC | Pointer to the program counter (PC) to be updated. |
- Returns
- int Returns:
1 if a valid backtrace is successfully performed.
0 if the link register's offset is zero.
-1 in case of failure (e.g., invalid addresses or instructions).
- Note
- The backtrace relies on the presence of valid instruction addresses and will fail if the stack pointer or program counter point to invalid regions. The function also uses
insn_length() to determine instruction lengths and adjusts the stack and program counter accordingly.
| static int riscv_ins16_backtrace_stask_push |
( |
uint32_t |
inst | ) |
|
|
static |
Processes compressed RISC-V instructions related to stack pointer adjustments.
This function checks if a 16-bit compressed RISC-V instruction corresponds to one of the following stack pointer modification operations:
c.addi16sp (compressed addi16sp instruction, used for modifying the stack pointer with a 9-bit signed immediate)
c.addi sp (compressed addi instruction for modifying sp with a 6-bit immediate)
c.addiw sp (compressed addiw instruction for modifying sp with a 6-bit immediate)
If the instruction matches one of these formats, it extracts the immediate value and calculates the corresponding adjustment to the stack pointer, returning the size of the adjustment in terms of long units. The function returns -1 if the instruction doesn't correspond to any recognized format or if the immediate value does not result in a valid stack adjustment.
- Parameters
-
| inst | The 16-bit compressed RISC-V instruction to be analyzed. |
- Returns
- int Returns the stack adjustment size (in terms of
long units) if the instruction is valid, otherwise returns -1.
- Note
- The function assumes that the instruction is valid and checks for stack pointer adjustments only.
- For
c.addi16sp, the immediate is a 9-bit signed value that modifies the stack pointer (sp).
- For
c.addi sp, sp, imm and c.addiw sp, imm, the immediate is a 6-bit signed value used to modify sp.
- The function divides the immediate value by
sizeof(long) to return the number of long units adjusted.
| int riscv_ins32_backtrace_stask_push |
( |
uint32_t |
inst | ) |
|
Checks if a RISC-V instruction corresponds to stack push instructions like addi sp, sp, imm and addiw sp, sp, imm, and calculates the effective stack push size based on the immediate value.
This function examines a 32-bit RISC-V instruction and checks if it corresponds to one of two stack adjustment operations:
addi sp, sp, imm (add immediate to the stack pointer)
addiw sp, sp, imm (add immediate to the stack pointer, with word extension).
If the instruction matches one of these operations, it calculates the immediate value and adjusts the stack pointer accordingly. The size of the stack push is calculated based on the immediate value, and the function returns this value divided by the size of a long type.
If the instruction does not correspond to one of the stack push operations, the function returns -1.
- Parameters
-
| inst | The 32-bit RISC-V instruction to check. |
- Returns
- int Returns the stack push size in terms of the number of
long units if the instruction corresponds to a valid stack push operation, otherwise returns -1.
- Note
- The instruction must modify the stack pointer (
sp) to be considered valid. The immediate value is expected to be a signed 12-bit value and may represent a positive or negative offset to the stack pointer.