diff --git a/src/bin/strace/Context.cpp b/src/bin/strace/Context.cpp index 301990dba2..0c81b1b1d7 100644 --- a/src/bin/strace/Context.cpp +++ b/src/bin/strace/Context.cpp @@ -9,19 +9,41 @@ #include "Context.h" +#include +#include + 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 diff --git a/src/bin/strace/Context.h b/src/bin/strace/Context.h index ba766a9698..c61a02fa8d 100644 --- a/src/bin/strace/Context.h +++ b/src/bin/strace/Context.h @@ -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; diff --git a/src/bin/strace/TypeHandler.cpp b/src/bin/strace/TypeHandler.cpp index ebe887453a..5121be9c32 100644 --- a/src/bin/strace/TypeHandler.cpp +++ b/src/bin/strace/TypeHandler.cpp @@ -441,22 +441,16 @@ format_pointer(Context &context, msghdr *h) template class SignedIntegerTypeHandler : public TypeHandler { public: - SignedIntegerTypeHandler(const char *modifier) - : fModifier(modifier) {} - string GetParameterValue(Context &context, Parameter *, const void *address) { - return context.FormatSigned(get_value(address), fModifier); + return context.FormatSigned(get_value(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 @@ -494,12 +488,12 @@ class SpecializedPointerTypeHandler : public TypeHandler { return new TypeHandlerImpl(); \ } -#define SIGNED_INTEGER_TYPE(type, modifier) \ +#define SIGNED_INTEGER_TYPE(type) \ template<> \ TypeHandler * \ TypeHandlerFactory::Create() \ { \ - return new SignedIntegerTypeHandler(modifier); \ + return new SignedIntegerTypeHandler(); \ } #define UNSIGNED_INTEGER_TYPE(type) \ @@ -516,11 +510,11 @@ class SpecializedPointerTypeHandler : public TypeHandler { return new SpecializedPointerTypeHandler(); \ } -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);