1/*
2 * Copyright 2007, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 * 		Hugo Santos <hugosantos@gmail.com>
7 * 		Ingo Weinhold <bonefish@cs.tu-berlin.de>
8 */
9#ifndef STRACE_CONTEXT_H
10#define STRACE_CONTEXT_H
11
12#include "Syscall.h"
13
14class Context {
15public:
16	enum {
17		STRINGS		= 1 << 0,
18		ENUMERATIONS	= 1 << 1,
19		SIMPLE_STRUCTS	= 1 << 2,
20		COMPLEX_STRUCTS	= 1 << 3,
21		POINTER_VALUES	= 1 << 4,
22		INPUT_VALUES	= 1 << 5,
23		OUTPUT_VALUES	= 1 << 6,
24		ALL		= 0xffffff9f
25	};
26
27	Context(Syscall *sc, char *data, MemoryReader &reader,
28		uint32 flags, bool decimal, uint64 returnValue = 0)
29		: fSyscall(sc), fData(data), fReader(reader),
30		  fFlags(flags), fDecimal(decimal), fReturnValue(returnValue) {}
31
32	Parameter *GetSibling(int32 index) const {
33		return fSyscall->ParameterAt(index);
34	}
35
36	Parameter *GetNextSibling(Parameter *param) const;
37
38	const void *GetValue(Parameter *param) const {
39		return fData + param->Offset();
40	}
41
42	template<typename value_t>
43	value_t ReadValue(Parameter *param) const {
44		const void *address = GetValue(param);
45		if (sizeof(align_t) > sizeof(value_t))
46			return value_t(*(align_t*)address);
47		else
48			return *(value_t*)address;
49	}
50
51	uint64 GetReturnValue() const {
52		return fReturnValue;
53	}
54
55	MemoryReader &Reader() { return fReader; }
56	bool GetContents(uint32 what) const { return fFlags & what; }
57
58	string FormatSigned(int64 value, int bytes = 8) const;
59	string FormatUnsigned(uint64 value) const;
60	string FormatFlags(uint64 value) const;
61
62	string FormatPointer(const void *address) const;
63
64private:
65	Syscall *fSyscall;
66	char *fData;
67	MemoryReader &fReader;
68	uint32 fFlags;
69	bool fDecimal;
70	uint64 fReturnValue;
71};
72
73#endif	// STRACE_CONTEXT_H
74