xref: /haiku/src/bin/debug/strace/Context.cpp (revision a2cb4665deaa0231aa190aaec6795fc59af8686b)
18df6a8dbSIngo Weinhold /*
28df6a8dbSIngo Weinhold  * Copyright 2007, Haiku Inc. All rights reserved.
38df6a8dbSIngo Weinhold  * Distributed under the terms of the MIT License.
48df6a8dbSIngo Weinhold  *
58df6a8dbSIngo Weinhold  * Authors:
68df6a8dbSIngo Weinhold  * 		Hugo Santos <hugosantos@gmail.com>
78df6a8dbSIngo Weinhold  * 		Ingo Weinhold <bonefish@cs.tu-berlin.de>
88df6a8dbSIngo Weinhold  */
98df6a8dbSIngo Weinhold 
108df6a8dbSIngo Weinhold #include "Context.h"
118df6a8dbSIngo Weinhold 
128df6a8dbSIngo Weinhold #include <stdio.h>
138df6a8dbSIngo Weinhold #include <string.h>
148df6a8dbSIngo Weinhold 
15*a2cb4665STrung Nguyen 
16*a2cb4665STrung Nguyen Parameter *
GetNextSibling(Parameter * param) const17*a2cb4665STrung Nguyen Context::GetNextSibling(Parameter *param) const
18*a2cb4665STrung Nguyen {
19*a2cb4665STrung Nguyen 	for (int32 i = 0; i + 1 < fSyscall->CountParameters(); i++) {
20*a2cb4665STrung Nguyen 		if (fSyscall->ParameterAt(i) == param)
21*a2cb4665STrung Nguyen 			return fSyscall->ParameterAt(i + 1);
22*a2cb4665STrung Nguyen 	}
23*a2cb4665STrung Nguyen 	return NULL;
24*a2cb4665STrung Nguyen }
25*a2cb4665STrung Nguyen 
26*a2cb4665STrung Nguyen 
278df6a8dbSIngo Weinhold string
FormatSigned(int64 value,int bytes) const288df6a8dbSIngo Weinhold Context::FormatSigned(int64 value, int bytes) const
298df6a8dbSIngo Weinhold {
308df6a8dbSIngo Weinhold 	char tmp[32];
318df6a8dbSIngo Weinhold 
328df6a8dbSIngo Weinhold 	// decimal
338df6a8dbSIngo Weinhold 
348df6a8dbSIngo Weinhold 	if (fDecimal) {
35a3802ca9SAlex Smith 		snprintf(tmp, sizeof(tmp), "%" B_PRId64, value);
368df6a8dbSIngo Weinhold 		return tmp;
378df6a8dbSIngo Weinhold 	}
388df6a8dbSIngo Weinhold 
398df6a8dbSIngo Weinhold 	// hex
408df6a8dbSIngo Weinhold 
41a3802ca9SAlex Smith 	snprintf(tmp, sizeof(tmp), "0x%" B_PRIx64, value);
428df6a8dbSIngo Weinhold 
438df6a8dbSIngo Weinhold 	// Negative numbers are expanded when being converted to int64. Hence
448df6a8dbSIngo Weinhold 	// we skip all but the last 2 * bytes hex digits to retain the original
458df6a8dbSIngo Weinhold 	// type's width.
468df6a8dbSIngo Weinhold 	int len = strlen(tmp);
478df6a8dbSIngo Weinhold 	int offset = len - min_c(len, bytes * 2);
488df6a8dbSIngo Weinhold 
498df6a8dbSIngo Weinhold 	// use the existing "0x" prefix or prepend it again
508df6a8dbSIngo Weinhold 	if (offset <= 2) {
518df6a8dbSIngo Weinhold 		offset = 0;
528df6a8dbSIngo Weinhold 	} else {
538df6a8dbSIngo Weinhold 		tmp[--offset] = 'x';
548df6a8dbSIngo Weinhold 		tmp[--offset] = '0';
558df6a8dbSIngo Weinhold 	}
568df6a8dbSIngo Weinhold 
578df6a8dbSIngo Weinhold 	return tmp + offset;
588df6a8dbSIngo Weinhold }
598df6a8dbSIngo Weinhold 
608df6a8dbSIngo Weinhold string
FormatUnsigned(uint64 value) const618df6a8dbSIngo Weinhold Context::FormatUnsigned(uint64 value) const
628df6a8dbSIngo Weinhold {
638df6a8dbSIngo Weinhold 	char tmp[32];
64a3802ca9SAlex Smith 	snprintf(tmp, sizeof(tmp), fDecimal ? "%" B_PRIu64 : "0x%" B_PRIx64, value);
658df6a8dbSIngo Weinhold 	return tmp;
668df6a8dbSIngo Weinhold }
678df6a8dbSIngo Weinhold 
688df6a8dbSIngo Weinhold string
FormatFlags(uint64 value) const698df6a8dbSIngo Weinhold Context::FormatFlags(uint64 value) const
708df6a8dbSIngo Weinhold {
718df6a8dbSIngo Weinhold 	char tmp[32];
72a3802ca9SAlex Smith 	snprintf(tmp, sizeof(tmp), "0x%" B_PRIx64, value);
738df6a8dbSIngo Weinhold 	return tmp;
748df6a8dbSIngo Weinhold }
758df6a8dbSIngo Weinhold 
768df6a8dbSIngo Weinhold string
FormatPointer(const void * address) const778df6a8dbSIngo Weinhold Context::FormatPointer(const void *address) const
788df6a8dbSIngo Weinhold {
798df6a8dbSIngo Weinhold 	char buffer[32];
808df6a8dbSIngo Weinhold 	sprintf(buffer, "%p", address);
818df6a8dbSIngo Weinhold 	return buffer;
828df6a8dbSIngo Weinhold }
83