xref: /haiku/src/kits/debug/arch/x86_64/arch_debug_support.cpp (revision 68ea01249e1e2088933cb12f9c28d4e5c5d1c9ef)
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.rip;
30 	*stackFrameAddress = (void*)cpuState.rbp;
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, stackFrameAddress,
42 		&stackFrame, sizeof(stackFrame));
43 	if (bytesRead < B_OK)
44 		return bytesRead;
45 	if (bytesRead != sizeof(stackFrame))
46 		return B_ERROR;
47 
48 	stackFrameInfo->frame = stackFrameAddress;
49 	stackFrameInfo->parent_frame = stackFrame.previous;
50 	stackFrameInfo->return_address = stackFrame.return_address;
51 	return B_OK;
52 }
53