1/*
2 * Copyright 2012, Rene Gollent, rene@gollent.com.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include "CliStackFrameCommand.h"
8
9#include <stdio.h>
10
11#include <AutoLocker.h>
12
13#include "CliContext.h"
14#include "FunctionInstance.h"
15#include "StackFrame.h"
16#include "StackTrace.h"
17#include "Team.h"
18
19
20CliStackFrameCommand::CliStackFrameCommand()
21	:
22	CliCommand("set current stack frame",
23		"%s [ <frame number> ]\n"
24		"Sets the current stack frame to <frame number>, if supplied. "
25		"Otherwise\n prints the current frame.")
26{
27}
28
29
30void
31CliStackFrameCommand::Execute(int argc, const char* const* argv,
32	CliContext& context)
33{
34	if (argc > 2) {
35		PrintUsage(argv[0]);
36		return;
37	}
38
39	StackTrace* stackTrace = context.GetStackTrace();
40	if (argc == 1) {
41		int32 currentFrameIndex = context.CurrentStackFrameIndex();
42		if (currentFrameIndex < 0)
43			printf("No current frame.\n");
44		else {
45			StackFrame* frame = stackTrace->FrameAt(currentFrameIndex);
46			printf("Current frame: %" B_PRId32 ": %s\n", currentFrameIndex,
47				frame->Function()->PrettyName().String());
48		}
49		return;
50	}
51	// parse the argument
52	char* endPointer;
53	int32 frameNumber = strtol(argv[1], &endPointer, 0);
54	if (*endPointer != '\0' || frameNumber < 0) {
55		printf("Error: Invalid parameter \"%s\"\n", argv[1]);
56		return;
57	}
58
59	if (stackTrace == NULL || frameNumber >= stackTrace->CountFrames()) {
60		printf("Error: Index %" B_PRId32 " out of range\n", frameNumber);
61		return;
62	} else
63		context.SetCurrentStackFrameIndex(frameNumber);
64}
65