xref: /haiku/src/kits/debug/arch/riscv64/arch_debug_support.cpp (revision 1a76488fc88584bf66b9751d7fb9b6527ac20d87)
1 /*
2  * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net.
3  * Copyright 2012, Alex Smith, alex@alex-smith.me.uk.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include <debug_support.h>
9 
10 #include "arch_debug_support.h"
11 
12 
13 struct stack_frame {
14 	struct stack_frame	*previous;
15 	void				*return_address;
16 };
17 
18 
19 status_t
20 arch_debug_get_instruction_pointer(debug_context *context, thread_id thread,
21 	void **ip, void **stackFrameAddress)
22 {
23 	// get the CPU state
24 	debug_cpu_state cpuState;
25 	status_t error = debug_get_cpu_state(context, thread, NULL, &cpuState);
26 	if (error != B_OK)
27 		return error;
28 
29 	*ip = (void*)cpuState.pc;
30 	*stackFrameAddress = (void*)cpuState.x[7];
31 
32 	return B_OK;
33 }
34 
35 
36 status_t
37 arch_debug_get_stack_frame(debug_context *context, void *stackFrameAddress,
38 	debug_stack_frame_info *stackFrameInfo)
39 {
40 	stack_frame stackFrame;
41 	ssize_t bytesRead = debug_read_memory(context,
42 		(uint8*)stackFrameAddress - sizeof(stackFrame),
43 		&stackFrame, sizeof(stackFrame));
44 
45 	if (bytesRead < B_OK)
46 		return bytesRead;
47 	if (bytesRead != sizeof(stackFrame))
48 		return B_ERROR;
49 
50 	stackFrameInfo->frame = stackFrameAddress;
51 	stackFrameInfo->parent_frame = stackFrame.previous;
52 	stackFrameInfo->return_address = stackFrame.return_address;
53 
54 	return B_OK;
55 }
56