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
10#include "Context.h"
11
12#include <stdio.h>
13#include <string.h>
14
15
16Parameter *
17Context::GetNextSibling(Parameter *param) const
18{
19	for (int32 i = 0; i + 1 < fSyscall->CountParameters(); i++) {
20		if (fSyscall->ParameterAt(i) == param)
21			return fSyscall->ParameterAt(i + 1);
22	}
23	return NULL;
24}
25
26
27string
28Context::FormatSigned(int64 value, int bytes) const
29{
30	char tmp[32];
31
32	// decimal
33
34	if (fDecimal) {
35		snprintf(tmp, sizeof(tmp), "%" B_PRId64, value);
36		return tmp;
37	}
38
39	// hex
40
41	snprintf(tmp, sizeof(tmp), "0x%" B_PRIx64, value);
42
43	// Negative numbers are expanded when being converted to int64. Hence
44	// we skip all but the last 2 * bytes hex digits to retain the original
45	// type's width.
46	int len = strlen(tmp);
47	int offset = len - min_c(len, bytes * 2);
48
49	// use the existing "0x" prefix or prepend it again
50	if (offset <= 2) {
51		offset = 0;
52	} else {
53		tmp[--offset] = 'x';
54		tmp[--offset] = '0';
55	}
56
57	return tmp + offset;
58}
59
60string
61Context::FormatUnsigned(uint64 value) const
62{
63	char tmp[32];
64	snprintf(tmp, sizeof(tmp), fDecimal ? "%" B_PRIu64 : "0x%" B_PRIx64, value);
65	return tmp;
66}
67
68string
69Context::FormatFlags(uint64 value) const
70{
71	char tmp[32];
72	snprintf(tmp, sizeof(tmp), "0x%" B_PRIx64, value);
73	return tmp;
74}
75
76string
77Context::FormatPointer(const void *address) const
78{
79	char buffer[32];
80	sprintf(buffer, "%p", address);
81	return buffer;
82}
83