Fixed printing of signed numbers. If a long long is passed to snprintf()

the correct modifier ("ll") has to be used. On x86 it doesn't make a
difference, if that's the last argument, but with other ABIs it might.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20425 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold 2007-03-26 02:41:12 +00:00
parent 95ebbeb153
commit 9a8d7c00c9
3 changed files with 40 additions and 24 deletions

View File

@ -9,19 +9,41 @@
#include "Context.h"
#include <stdio.h>
#include <string.h>
string
Context::FormatSigned(int64 value, const char *type) const
Context::FormatSigned(int64 value, int bytes) const
{
char modifier[16], tmp[32];
char tmp[32];
if (fDecimal)
snprintf(modifier, sizeof(modifier), "%%%si", type);
else
snprintf(modifier, sizeof(modifier), "0x%%%sx", type);
// decimal
snprintf(tmp, sizeof(tmp), modifier, value);
return tmp;
if (fDecimal) {
snprintf(tmp, sizeof(tmp), "%lld", value);
return tmp;
}
// hex
snprintf(tmp, sizeof(tmp), "0x%llx", value);
// Negative numbers are expanded when being converted to int64. Hence
// we skip all but the last 2 * bytes hex digits to retain the original
// type's width.
int len = strlen(tmp);
int offset = len - min_c(len, bytes * 2);
// use the existing "0x" prefix or prepend it again
if (offset <= 2) {
offset = 0;
} else {
tmp[--offset] = 'x';
tmp[--offset] = '0';
}
return tmp + offset;
}
string

View File

@ -37,7 +37,7 @@ public:
MemoryReader &Reader() { return fReader; }
bool GetContents(uint32 what) const { return fFlags & what; }
string FormatSigned(int64 value, const char *modifier = "ll") const;
string FormatSigned(int64 value, int bytes = 8) const;
string FormatUnsigned(uint64 value) const;
string FormatFlags(uint64 value) const;

View File

@ -441,22 +441,16 @@ format_pointer(Context &context, msghdr *h)
template<typename Type>
class SignedIntegerTypeHandler : public TypeHandler {
public:
SignedIntegerTypeHandler(const char *modifier)
: fModifier(modifier) {}
string GetParameterValue(Context &context, Parameter *,
const void *address)
{
return context.FormatSigned(get_value<Type>(address), fModifier);
return context.FormatSigned(get_value<Type>(address), sizeof(Type));
}
string GetReturnValue(Context &context, uint64 value)
{
return context.FormatSigned(value, fModifier);
return context.FormatSigned(value, sizeof(Type));
}
private:
const char *fModifier;
};
template<typename Type>
@ -494,12 +488,12 @@ class SpecializedPointerTypeHandler : public TypeHandler {
return new TypeHandlerImpl<type>(); \
}
#define SIGNED_INTEGER_TYPE(type, modifier) \
#define SIGNED_INTEGER_TYPE(type) \
template<> \
TypeHandler * \
TypeHandlerFactory<type>::Create() \
{ \
return new SignedIntegerTypeHandler<type>(modifier); \
return new SignedIntegerTypeHandler<type>(); \
}
#define UNSIGNED_INTEGER_TYPE(type) \
@ -516,11 +510,11 @@ class SpecializedPointerTypeHandler : public TypeHandler {
return new SpecializedPointerTypeHandler<type>(); \
}
SIGNED_INTEGER_TYPE(char, "hh");
SIGNED_INTEGER_TYPE(short, "h");
SIGNED_INTEGER_TYPE(int, "");
SIGNED_INTEGER_TYPE(long, "l");
SIGNED_INTEGER_TYPE(long long, "ll");
SIGNED_INTEGER_TYPE(char);
SIGNED_INTEGER_TYPE(short);
SIGNED_INTEGER_TYPE(int);
SIGNED_INTEGER_TYPE(long);
SIGNED_INTEGER_TYPE(long long);
UNSIGNED_INTEGER_TYPE(unsigned char);
UNSIGNED_INTEGER_TYPE(unsigned short);