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