diff options
author | Christoph Lipka <clipka@de.adit-jv.com> | 2018-12-20 14:58:19 +0100 |
---|---|---|
committer | Christoph Lipka <clipka@de.adit-jv.com> | 2018-12-21 10:23:41 +0100 |
commit | dca8ab254aa0a687c32009079d85e4d8f960b213 (patch) | |
tree | e63b6a552fd9f61873892110a56a89ef354864cd /src/core_dump_handler | |
parent | 0d0c74640c8b792db37cb9f884f89f7561ea551f (diff) | |
download | DLT-daemon-dca8ab254aa0a687c32009079d85e4d8f960b213.tar.gz |
Code beautification using uncrustify
Signed-off-by: Christoph Lipka <clipka@de.adit-jv.com>
Diffstat (limited to 'src/core_dump_handler')
-rw-r--r-- | src/core_dump_handler/cityhash_c/city_c.c | 867 | ||||
-rw-r--r-- | src/core_dump_handler/cityhash_c/city_c.h | 130 | ||||
-rw-r--r-- | src/core_dump_handler/cityhash_c/citycrc_c.h | 74 | ||||
-rw-r--r-- | src/core_dump_handler/dlt_cdh.c | 307 | ||||
-rw-r--r-- | src/core_dump_handler/dlt_cdh.h | 42 | ||||
-rw-r--r-- | src/core_dump_handler/dlt_cdh_context.c | 283 | ||||
-rw-r--r-- | src/core_dump_handler/dlt_cdh_coredump.c | 107 | ||||
-rw-r--r-- | src/core_dump_handler/dlt_cdh_cpuinfo.h | 2 | ||||
-rw-r--r-- | src/core_dump_handler/dlt_cdh_crashid.c | 108 | ||||
-rw-r--r-- | src/core_dump_handler/dlt_cdh_streamer.c | 99 | ||||
-rw-r--r-- | src/core_dump_handler/dlt_cdh_streamer.h | 18 | ||||
-rw-r--r-- | src/core_dump_handler/i686/dlt_cdh_cpuinfo.c | 10 | ||||
-rw-r--r-- | src/core_dump_handler/x86_64/dlt_cdh_cpuinfo.c | 10 |
13 files changed, 1010 insertions, 1047 deletions
diff --git a/src/core_dump_handler/cityhash_c/city_c.c b/src/core_dump_handler/cityhash_c/city_c.c index 0cf7afe..0ccbcd5 100644 --- a/src/core_dump_handler/cityhash_c/city_c.c +++ b/src/core_dump_handler/cityhash_c/city_c.c @@ -1,424 +1,458 @@ -// Copyright (c) 2011 Google, Inc. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// -// CityHash, by Geoff Pike and Jyrki Alakuijala -// -// This file provides CityHash64() and related functions. -// -// It's probably possible to create even faster hash functions by -// writing a program that systematically explores some of the space of -// possible hash functions, by using SIMD instructions, or by -// compromising on hash quality. +/* Copyright (c) 2011 Google, Inc. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining a copy */ +/* of this software and associated documentation files (the "Software"), to deal */ +/* in the Software without restriction, including without limitation the rights */ +/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */ +/* copies of the Software, and to permit persons to whom the Software is */ +/* furnished to do so, subject to the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be included in */ +/* all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */ +/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */ +/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */ +/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */ +/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */ +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN */ +/* THE SOFTWARE. */ +/* */ +/* CityHash, by Geoff Pike and Jyrki Alakuijala */ +/* */ +/* This file provides CityHash64() and related functions. */ +/* */ +/* It's probably possible to create even faster hash functions by */ +/* writing a program that systematically explores some of the space of */ +/* possible hash functions, by using SIMD instructions, or by */ +/* compromising on hash quality. */ #include "city_c.h" #include <string.h> #if defined(__sparc) || defined(__sparc__) \ - || defined(_POWER) || defined(__powerpc__) \ -|| defined(__ppc__) || defined(__hpux) || defined(__hppa) \ -|| defined(_MIPSEB) || defined(_POWER) \ -|| defined(__s390__) -# define WORDS_BIGENDIAN + || defined(_POWER) || defined(__powerpc__) \ + || defined(__ppc__) || defined(__hpux) || defined(__hppa) \ + || defined(_MIPSEB) || defined(_POWER) \ + || defined(__s390__) +# define WORDS_BIGENDIAN #elif defined(__i386__) || defined(__alpha__) \ - || defined(__ia64) || defined(__ia64__) \ -|| defined(_M_IX86) || defined(_M_IA64) \ -|| defined(_M_ALPHA) || defined(__amd64) \ -|| defined(__amd64__) || defined(_M_AMD64) \ -|| defined(__x86_64) || defined(__x86_64__) \ -|| defined(_M_X64) || defined(__bfin__) -# define WORDS_LITTLEENDIAN + || defined(__ia64) || defined(__ia64__) \ + || defined(_M_IX86) || defined(_M_IA64) \ + || defined(_M_ALPHA) || defined(__amd64) \ + || defined(__amd64__) || defined(_M_AMD64) \ + || defined(__x86_64) || defined(__x86_64__) \ + || defined(_M_X64) || defined(__bfin__) +# define WORDS_LITTLEENDIAN #endif #if !defined(WORDS_BIGENDIAN) -# define uint32_in_expected_order(x) (x) -# define uint64_in_expected_order(x) (x) +# define uint32_in_expected_order(x) (x) +# define uint64_in_expected_order(x) (x) #else -# if defined _MSC_VER -# include <stdlib.h> -# define bswap_32(x) _byteswap_ulong(x) -# define bswap_64(x) _byteswap_uint64(x) -# elif defined(__APPLE__) -// Mac OS X / Darwin features -# include <libkern/OSByteOrder.h> -# define bswap_32(x) OSSwapInt32(x) -# define bswap_64(x) OSSwapInt64(x) -# else -# include <byteswap.h> -# endif -# define uint32_in_expected_order(x) (bswap_32(x)) -# define uint64_in_expected_order(x) (bswap_64(x)) -#endif // WORDS_BIGENDIAN +# if defined _MSC_VER +# include <stdlib.h> +# define bswap_32(x) _byteswap_ulong(x) +# define bswap_64(x) _byteswap_uint64(x) +# elif defined(__APPLE__) +/* Mac OS X / Darwin features */ +# include <libkern/OSByteOrder.h> +# define bswap_32(x) OSSwapInt32(x) +# define bswap_64(x) OSSwapInt64(x) +# else +# include <byteswap.h> +# endif +# define uint32_in_expected_order(x) (bswap_32(x)) +# define uint64_in_expected_order(x) (bswap_64(x)) +#endif /* WORDS_BIGENDIAN */ #if !defined inline -# ifdef _MSC_VER -# define inline __inline -# endif +# ifdef _MSC_VER +# define inline __inline +# endif #endif #if !defined LIKELY -# if defined __GNUC__ && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96))//GCC 2.96 above -# define LIKELY(x) (__builtin_expect(!!(x), 1)) -# else -# define LIKELY(x) (x) -# endif +# if defined __GNUC__ && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96))/*GCC 2.96 above */ +# define LIKELY(x) (__builtin_expect(!!(x), 1)) +# else +# define LIKELY(x) (x) +# endif #endif -#define UNSAFE_SWAP(type, a, b) do {type tmp;tmp=(a);(a)=(b);(b)=tmp;} while (0) +#define UNSAFE_SWAP(type, a, b) do { type tmp; tmp = (a); (a) = (b); (b) = tmp; } while (0) -static inline uint128 UInt128(uint64 low, uint64 high) { - uint128 val; - val.first = low; - val.second = high; - return val; +static inline uint128 UInt128(uint64 low, uint64 high) +{ + uint128 val; + val.first = low; + val.second = high; + return val; } -static inline uint64 UNALIGNED_LOAD64(const char *p) { - uint64 result; - memcpy(&result, p, sizeof(result)); - return result; +static inline uint64 UNALIGNED_LOAD64(const char *p) +{ + uint64 result; + memcpy(&result, p, sizeof(result)); + return result; } -static inline uint32 UNALIGNED_LOAD32(const char *p) { - uint32 result; - memcpy(&result, p, sizeof(result)); - return result; +static inline uint32 UNALIGNED_LOAD32(const char *p) +{ + uint32 result; + memcpy(&result, p, sizeof(result)); + return result; } -static uint64 Hash64Pairto64(uint64 u, uint64 v) { - // Murmur-inspired hashing. - static const uint64 kMul = 0x9ddfea08eb382d69ULL; - uint64 a, b; - a = (u ^ v) * kMul; - a ^= (a >> 47); - b = (v ^ a) * kMul; - b ^= (b >> 47); - b *= kMul; - return b; +static uint64 Hash64Pairto64(uint64 u, uint64 v) +{ + /* Murmur-inspired hashing. */ + static const uint64 kMul = 0x9ddfea08eb382d69ULL; + uint64 a, b; + a = (u ^ v) * kMul; + a ^= (a >> 47); + b = (v ^ a) * kMul; + b ^= (b >> 47); + b *= kMul; + return b; } -static inline uint64 Fetch64(const char *p) { - return uint64_in_expected_order(UNALIGNED_LOAD64(p)); +static inline uint64 Fetch64(const char *p) +{ + return uint64_in_expected_order(UNALIGNED_LOAD64(p)); } -static inline uint32 Fetch32(const char *p) { - return uint32_in_expected_order(UNALIGNED_LOAD32(p)); +static inline uint32 Fetch32(const char *p) +{ + return uint32_in_expected_order(UNALIGNED_LOAD32(p)); } -// Some primes between 2^63 and 2^64 for various uses. +/* Some primes between 2^63 and 2^64 for various uses. */ static const uint64 k0 = 0xc3a5c85c97cb3127ULL; static const uint64 k1 = 0xb492b66fbe98f273ULL; static const uint64 k2 = 0x9ae16a3b2f90404fULL; static const uint64 k3 = 0xc949d7c7509e6557ULL; -// Bitwise right rotate. Normally this will compile to a single -// instruction, especially if the shift is a manifest constant. -static inline uint64 Rotate(uint64 val, int shift) { - // Avoid shifting by 64: doing so yields an undefined result. - return shift == 0 ? val : ((val >> shift) | (val << (64 - shift))); +/* Bitwise right rotate. Normally this will compile to a single */ +/* instruction, especially if the shift is a manifest constant. */ +static inline uint64 Rotate(uint64 val, int shift) +{ + /* Avoid shifting by 64: doing so yields an undefined result. */ + return shift == 0 ? val : ((val >> shift) | (val << (64 - shift))); } -// Equivalent to Rotate(), but requires the second arg to be non-zero. -// On x86-64, and probably others, it's possible for this to compile -// to a single instruction if both args are already in registers. -static inline uint64 RotateByAtLeast1(uint64 val, int shift) { - return (val >> shift) | (val << (64 - shift)); +/* Equivalent to Rotate(), but requires the second arg to be non-zero. */ +/* On x86-64, and probably others, it's possible for this to compile */ +/* to a single instruction if both args are already in registers. */ +static inline uint64 RotateByAtLeast1(uint64 val, int shift) +{ + return (val >> shift) | (val << (64 - shift)); } -static inline uint64 ShiftMix(uint64 val) { - return val ^ (val >> 47); +static inline uint64 ShiftMix(uint64 val) +{ + return val ^ (val >> 47); } -static inline uint64 HashLen16(uint64 u, uint64 v) { - //return Hash128to64(uint128(u, v)); - return Hash64Pairto64(u, v); +static inline uint64 HashLen16(uint64 u, uint64 v) +{ + /*return Hash128to64(uint128(u, v)); */ + return Hash64Pairto64(u, v); } -static uint64 HashLen0to16(const char *s, size_t len) { - if (len > 8) { - uint64 a = Fetch64(s); - uint64 b = Fetch64(s + len - 8); - return HashLen16(a, RotateByAtLeast1(b + len, len)) ^ b; - } - if (len >= 4) { - uint64 a = Fetch32(s); - return HashLen16(len + (a << 3), Fetch32(s + len - 4)); - } - if (len > 0) { - uint8 a = s[0]; - uint8 b = s[len >> 1]; - uint8 c = s[len - 1]; - uint32 y = (uint32)a + ((uint32)b << 8); - uint32 z = len + ((uint32)c << 2); - return ShiftMix(y * k2 ^ z * k3) * k2; - } - return k2; +static uint64 HashLen0to16(const char *s, size_t len) +{ + if (len > 8) { + uint64 a = Fetch64(s); + uint64 b = Fetch64(s + len - 8); + return HashLen16(a, RotateByAtLeast1(b + len, len)) ^ b; + } + + if (len >= 4) { + uint64 a = Fetch32(s); + return HashLen16(len + (a << 3), Fetch32(s + len - 4)); + } + + if (len > 0) { + uint8 a = s[0]; + uint8 b = s[len >> 1]; + uint8 c = s[len - 1]; + uint32 y = (uint32)a + ((uint32)b << 8); + uint32 z = len + ((uint32)c << 2); + return ShiftMix(y * k2 ^ z * k3) * k2; + } + + return k2; } -// This probably works well for 16-byte strings as well, but it may be overkill -// in that case. -static uint64 HashLen17to32(const char *s, size_t len) { - uint64 a = Fetch64(s) * k1; - uint64 b = Fetch64(s + 8); - uint64 c = Fetch64(s + len - 8) * k2; - uint64 d = Fetch64(s + len - 16) * k0; - return HashLen16(Rotate(a - b, 43) + Rotate(c, 30) + d, - a + Rotate(b ^ k3, 20) - c + len); +/* This probably works well for 16-byte strings as well, but it may be overkill */ +/* in that case. */ +static uint64 HashLen17to32(const char *s, size_t len) +{ + uint64 a = Fetch64(s) * k1; + uint64 b = Fetch64(s + 8); + uint64 c = Fetch64(s + len - 8) * k2; + uint64 d = Fetch64(s + len - 16) * k0; + return HashLen16(Rotate(a - b, 43) + Rotate(c, 30) + d, + a + Rotate(b ^ k3, 20) - c + len); } -// Return a 16-byte hash for 48 bytes. Quick and dirty. -// Callers do best to use "random-looking" values for a and b. -static uint128 WeakHashLen32WithSeeds6(uint64 w, uint64 x, uint64 y, uint64 z, uint64 a, uint64 b) { - uint64 c; - a += w; - b = Rotate(b + a + z, 21); - c = a; - a += x; - a += y; - b += Rotate(a, 44); - return UInt128(a + z, b + c); +/* Return a 16-byte hash for 48 bytes. Quick and dirty. */ +/* Callers do best to use "random-looking" values for a and b. */ +static uint128 WeakHashLen32WithSeeds6(uint64 w, uint64 x, uint64 y, uint64 z, uint64 a, uint64 b) +{ + uint64 c; + a += w; + b = Rotate(b + a + z, 21); + c = a; + a += x; + a += y; + b += Rotate(a, 44); + return UInt128(a + z, b + c); } -// Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty. -static uint128 WeakHashLen32WithSeeds3(const char* s, uint64 a, uint64 b) { - return WeakHashLen32WithSeeds6(Fetch64(s), - Fetch64(s + 8), - Fetch64(s + 16), - Fetch64(s + 24), - a, - b); +/* Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty. */ +static uint128 WeakHashLen32WithSeeds3(const char *s, uint64 a, uint64 b) +{ + return WeakHashLen32WithSeeds6(Fetch64(s), + Fetch64(s + 8), + Fetch64(s + 16), + Fetch64(s + 24), + a, + b); } -// Return an 8-byte hash for 33 to 64 bytes. -static uint64 HashLen33to64(const char *s, size_t len) { - uint64 z = Fetch64(s + 24); - uint64 a = Fetch64(s) + (len + Fetch64(s + len - 16)) * k0; - uint64 b = Rotate(a + z, 52); - uint64 c = Rotate(a, 37); - uint64 vf, vs, wf, ws, r; - a += Fetch64(s + 8); - c += Rotate(a, 7); - a += Fetch64(s + 16); - vf = a + z; - vs = b + Rotate(a, 31) + c; - a = Fetch64(s + 16) + Fetch64(s + len - 32); - z = Fetch64(s + len - 8); - b = Rotate(a + z, 52); - c = Rotate(a, 37); - a += Fetch64(s + len - 24); - c += Rotate(a, 7); - a += Fetch64(s + len - 16); - wf = a + z; - ws = b + Rotate(a, 31) + c; - r = ShiftMix((vf + ws) * k2 + (wf + vs) * k0); - return ShiftMix(r * k0 + vs) * k2; +/* Return an 8-byte hash for 33 to 64 bytes. */ +static uint64 HashLen33to64(const char *s, size_t len) +{ + uint64 z = Fetch64(s + 24); + uint64 a = Fetch64(s) + (len + Fetch64(s + len - 16)) * k0; + uint64 b = Rotate(a + z, 52); + uint64 c = Rotate(a, 37); + uint64 vf, vs, wf, ws, r; + a += Fetch64(s + 8); + c += Rotate(a, 7); + a += Fetch64(s + 16); + vf = a + z; + vs = b + Rotate(a, 31) + c; + a = Fetch64(s + 16) + Fetch64(s + len - 32); + z = Fetch64(s + len - 8); + b = Rotate(a + z, 52); + c = Rotate(a, 37); + a += Fetch64(s + len - 24); + c += Rotate(a, 7); + a += Fetch64(s + len - 16); + wf = a + z; + ws = b + Rotate(a, 31) + c; + r = ShiftMix((vf + ws) * k2 + (wf + vs) * k0); + return ShiftMix(r * k0 + vs) * k2; } -uint64 CityHash64(const char *s, size_t len) { - if (len <= 32) { - if (len <= 16) { - return HashLen0to16(s, len); - } else { - return HashLen17to32(s, len); +uint64 CityHash64(const char *s, size_t len) +{ + if (len <= 32) { + if (len <= 16) + return HashLen0to16(s, len); + else + return HashLen17to32(s, len); + } + else if (len <= 64) + { + return HashLen33to64(s, len); } - } else if (len <= 64) { - return HashLen33to64(s, len); - } - - do { - // For strings over 64 bytes we hash the end first, and then as we - // loop we keep 56 bytes of state: v, w, x, y, and z. - uint64 x = Fetch64(s + len - 40); - uint64 y = Fetch64(s + len - 16) + Fetch64(s + len - 56); - uint64 z = HashLen16(Fetch64(s + len - 48) + len, Fetch64(s + len - 24)); - uint128 v = WeakHashLen32WithSeeds3(s + len - 64, len, z); - uint128 w = WeakHashLen32WithSeeds3(s + len - 32, y + k1, x); - x = x * k1 + Fetch64(s); - - // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks. - len = (len - 1) & ~(size_t)63; + do { - x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1; - y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; - x ^= w.second; - y += v.first + Fetch64(s + 40); - z = Rotate(z + w.first, 33) * k1; - v = WeakHashLen32WithSeeds3(s, v.second * k1, x + w.first); - w = WeakHashLen32WithSeeds3(s + 32, z + w.second, y + Fetch64(s + 16)); - UNSAFE_SWAP(uint64, z, x); - s += 64; - len -= 64; - } while (len != 0); - return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z, - HashLen16(v.second, w.second) + x); - } while(0); + /* For strings over 64 bytes we hash the end first, and then as we */ + /* loop we keep 56 bytes of state: v, w, x, y, and z. */ + uint64 x = Fetch64(s + len - 40); + uint64 y = Fetch64(s + len - 16) + Fetch64(s + len - 56); + uint64 z = HashLen16(Fetch64(s + len - 48) + len, Fetch64(s + len - 24)); + uint128 v = WeakHashLen32WithSeeds3(s + len - 64, len, z); + uint128 w = WeakHashLen32WithSeeds3(s + len - 32, y + k1, x); + x = x * k1 + Fetch64(s); + + /* Decrease len to the nearest multiple of 64, and operate on 64-byte chunks. */ + len = (len - 1) & ~(size_t)63; + + do { + x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1; + y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; + x ^= w.second; + y += v.first + Fetch64(s + 40); + z = Rotate(z + w.first, 33) * k1; + v = WeakHashLen32WithSeeds3(s, v.second * k1, x + w.first); + w = WeakHashLen32WithSeeds3(s + 32, z + w.second, y + Fetch64(s + 16)); + UNSAFE_SWAP(uint64, z, x); + s += 64; + len -= 64; + } while (len != 0); + + return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z, + HashLen16(v.second, w.second) + x); + } while (0); } -uint64 CityHash64WithSeed(const char *s, size_t len, uint64 seed) { - return CityHash64WithSeeds(s, len, k2, seed); +uint64 CityHash64WithSeed(const char *s, size_t len, uint64 seed) +{ + return CityHash64WithSeeds(s, len, k2, seed); } -uint64 CityHash64WithSeeds(const char *s, size_t len, uint64 seed0, uint64 seed1) { - return HashLen16(CityHash64(s, len) - seed0, seed1); +uint64 CityHash64WithSeeds(const char *s, size_t len, uint64 seed0, uint64 seed1) +{ + return HashLen16(CityHash64(s, len) - seed0, seed1); } -// A subroutine for CityHash128(). Returns a decent 128-bit hash for strings -// of any length representable in signed long. Based on City and Murmur. -static uint128 CityMurmur(const char *s, size_t len, uint128 seed) { - uint64 a = Uint128Low64(seed); - uint64 b = Uint128High64(seed); - uint64 c = 0; - uint64 d = 0; - signed long l = len - 16; - if (l <= 0) { // len <= 16 - a = ShiftMix(a * k1) * k1; - c = b * k1 + HashLen0to16(s, len); - d = ShiftMix(a + (len >= 8 ? Fetch64(s) : c)); - } else { // len > 16 - c = HashLen16(Fetch64(s + len - 8) + k1, a); - d = HashLen16(b + len, c + Fetch64(s + len - 16)); - a += d; - do { - a ^= ShiftMix(Fetch64(s) * k1) * k1; - a *= k1; - b ^= a; - c ^= ShiftMix(Fetch64(s + 8) * k1) * k1; - c *= k1; - d ^= c; - s += 16; - l -= 16; - } while (l > 0); - } - a = HashLen16(a, c); - b = HashLen16(d, b); - return UInt128(a ^ b, HashLen16(b, a)); +/* A subroutine for CityHash128(). Returns a decent 128-bit hash for strings */ +/* of any length representable in signed long. Based on City and Murmur. */ +static uint128 CityMurmur(const char *s, size_t len, uint128 seed) +{ + uint64 a = Uint128Low64(seed); + uint64 b = Uint128High64(seed); + uint64 c = 0; + uint64 d = 0; + signed long l = len - 16; + + if (l <= 0) { /* len <= 16 */ + a = ShiftMix(a * k1) * k1; + c = b * k1 + HashLen0to16(s, len); + d = ShiftMix(a + (len >= 8 ? Fetch64(s) : c)); + } + else { /* len > 16 */ + c = HashLen16(Fetch64(s + len - 8) + k1, a); + d = HashLen16(b + len, c + Fetch64(s + len - 16)); + a += d; + + do { + a ^= ShiftMix(Fetch64(s) * k1) * k1; + a *= k1; + b ^= a; + c ^= ShiftMix(Fetch64(s + 8) * k1) * k1; + c *= k1; + d ^= c; + s += 16; + l -= 16; + } while (l > 0); + } + + a = HashLen16(a, c); + b = HashLen16(d, b); + return UInt128(a ^ b, HashLen16(b, a)); } -uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed) { - if (len < 128) { - return CityMurmur(s, len, seed); - } - - do { - // We expect len >= 128 to be the common case. Keep 56 bytes of state: - // v, w, x, y, and z. - uint128 v, w; - uint64 x = Uint128Low64(seed); - uint64 y = Uint128High64(seed); - uint64 z = len * k1; - size_t tail_done; - v.first = Rotate(y ^ k1, 49) * k1 + Fetch64(s); - v.second = Rotate(v.first, 42) * k1 + Fetch64(s + 8); - w.first = Rotate(y + z, 35) * k1 + x; - w.second = Rotate(x + Fetch64(s + 88), 53) * k1; - - // This is the same inner loop as CityHash64(), manually unrolled. +uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed) +{ + if (len < 128) + return CityMurmur(s, len, seed); + do { - x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1; - y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; - x ^= w.second; - y += v.first + Fetch64(s + 40); - z = Rotate(z + w.first, 33) * k1; - v = WeakHashLen32WithSeeds3(s, v.second * k1, x + w.first); - w = WeakHashLen32WithSeeds3(s + 32, z + w.second, y + Fetch64(s + 16)); - UNSAFE_SWAP(uint64, z, x); - s += 64; - x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1; - y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; - x ^= w.second; - y += v.first + Fetch64(s + 40); - z = Rotate(z + w.first, 33) * k1; - v = WeakHashLen32WithSeeds3(s, v.second * k1, x + w.first); - w = WeakHashLen32WithSeeds3(s + 32, z + w.second, y + Fetch64(s + 16)); - UNSAFE_SWAP(uint64, z, x); - s += 64; - len -= 128; - } while (LIKELY(len >= 128)); - x += Rotate(v.first + z, 49) * k0; - z += Rotate(w.first, 37) * k0; - // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s. - for (tail_done = 0; tail_done < len; ) { - tail_done += 32; - y = Rotate(x + y, 42) * k0 + v.second; - w.first += Fetch64(s + len - tail_done + 16); - x = x * k0 + w.first; - z += w.second + Fetch64(s + len - tail_done); - w.second += v.first; - v = WeakHashLen32WithSeeds3(s + len - tail_done, v.first + z, v.second); - } - // At this point our 56 bytes of state should contain more than - // enough information for a strong 128-bit hash. We use two - // different 56-byte-to-8-byte hashes to get a 16-byte final result. - x = HashLen16(x, v.first); - y = HashLen16(y + z, w.first); - return UInt128(HashLen16(x + v.second, w.second) + y, - HashLen16(x + w.second, y + v.second)); - } while(0); + /* We expect len >= 128 to be the common case. Keep 56 bytes of state: */ + /* v, w, x, y, and z. */ + uint128 v, w; + uint64 x = Uint128Low64(seed); + uint64 y = Uint128High64(seed); + uint64 z = len * k1; + size_t tail_done; + v.first = Rotate(y ^ k1, 49) * k1 + Fetch64(s); + v.second = Rotate(v.first, 42) * k1 + Fetch64(s + 8); + w.first = Rotate(y + z, 35) * k1 + x; + w.second = Rotate(x + Fetch64(s + 88), 53) * k1; + + /* This is the same inner loop as CityHash64(), manually unrolled. */ + do { + x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1; + y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; + x ^= w.second; + y += v.first + Fetch64(s + 40); + z = Rotate(z + w.first, 33) * k1; + v = WeakHashLen32WithSeeds3(s, v.second * k1, x + w.first); + w = WeakHashLen32WithSeeds3(s + 32, z + w.second, y + Fetch64(s + 16)); + UNSAFE_SWAP(uint64, z, x); + s += 64; + x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1; + y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; + x ^= w.second; + y += v.first + Fetch64(s + 40); + z = Rotate(z + w.first, 33) * k1; + v = WeakHashLen32WithSeeds3(s, v.second * k1, x + w.first); + w = WeakHashLen32WithSeeds3(s + 32, z + w.second, y + Fetch64(s + 16)); + UNSAFE_SWAP(uint64, z, x); + s += 64; + len -= 128; + } while (LIKELY(len >= 128)); + + x += Rotate(v.first + z, 49) * k0; + z += Rotate(w.first, 37) * k0; + + /* If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s. */ + for (tail_done = 0; tail_done < len;) { + tail_done += 32; + y = Rotate(x + y, 42) * k0 + v.second; + w.first += Fetch64(s + len - tail_done + 16); + x = x * k0 + w.first; + z += w.second + Fetch64(s + len - tail_done); + w.second += v.first; + v = WeakHashLen32WithSeeds3(s + len - tail_done, v.first + z, v.second); + } + + /* At this point our 56 bytes of state should contain more than */ + /* enough information for a strong 128-bit hash. We use two */ + /* different 56-byte-to-8-byte hashes to get a 16-byte final result. */ + x = HashLen16(x, v.first); + y = HashLen16(y + z, w.first); + return UInt128(HashLen16(x + v.second, w.second) + y, + HashLen16(x + w.second, y + v.second)); + } while (0); } -uint128 CityHash128(const char *s, size_t len) { - if (len >= 16) { - return CityHash128WithSeed(s + 16, - len - 16, - UInt128(Fetch64(s) ^ k3, - Fetch64(s + 8))); - } else if (len >= 8) { - return CityHash128WithSeed(NULL, - 0, - UInt128(Fetch64(s) ^ (len * k0), - Fetch64(s + len - 8) ^ k1)); - } else { - return CityHash128WithSeed(s, len, UInt128(k0, k1)); - } +uint128 CityHash128(const char *s, size_t len) +{ + if (len >= 16) + return CityHash128WithSeed(s + 16, + len - 16, + UInt128(Fetch64(s) ^ k3, + Fetch64(s + 8))); + else if (len >= 8) + return CityHash128WithSeed(NULL, + 0, + UInt128(Fetch64(s) ^ (len * k0), + Fetch64(s + len - 8) ^ k1)); + else + return CityHash128WithSeed(s, len, UInt128(k0, k1)); } #ifdef __SSE4_2__ -#include "citycrc_c.h" -#include <nmmintrin.h> - -// Requires len >= 240. -static void CityHashCrc256Long(const char *s, size_t len, uint32 seed, uint64 *result) { - uint64 a = Fetch64(s + 56) + k0; - uint64 b = Fetch64(s + 96) + k0; - uint64 c = result[0] = HashLen16(b, len); - uint64 d = result[1] = Fetch64(s + 120) * k0 + len; - uint64 e = Fetch64(s + 184) + seed; - uint64 f = seed; - uint64 g = 0; - uint64 h = 0; - uint64 i = 0; - uint64 j = 0; - uint64 t = c + d; - - // 240 bytes of input per iter. - size_t iters = len / 240; - len -= iters * 240; - do { -#define CHUNK(multiplier, z) \ +# include "citycrc_c.h" +# include <nmmintrin.h> + +/* Requires len >= 240. */ +static void CityHashCrc256Long(const char *s, size_t len, uint32 seed, uint64 *result) +{ + uint64 a = Fetch64(s + 56) + k0; + uint64 b = Fetch64(s + 96) + k0; + uint64 c = result[0] = HashLen16(b, len); + uint64 d = result[1] = Fetch64(s + 120) * k0 + len; + uint64 e = Fetch64(s + 184) + seed; + uint64 f = seed; + uint64 g = 0; + uint64 h = 0; + uint64 i = 0; + uint64 j = 0; + uint64 t = c + d; + + /* 240 bytes of input per iter. */ + size_t iters = len / 240; + len -= iters * 240; + + do { +# define CHUNK(multiplier, z) \ { \ - uint64 old_a = a; \ - a = Rotate(b, 41 ^ z) * multiplier + Fetch64(s); \ - b = Rotate(c, 27 ^ z) * multiplier + Fetch64(s + 8); \ - c = Rotate(d, 41 ^ z) * multiplier + Fetch64(s + 16); \ - d = Rotate(e, 33 ^ z) * multiplier + Fetch64(s + 24); \ - e = Rotate(t, 25 ^ z) * multiplier + Fetch64(s + 32); \ - t = old_a; \ + uint64 old_a = a; \ + a = Rotate(b, 41 ^ z) * multiplier + Fetch64(s); \ + b = Rotate(c, 27 ^ z) * multiplier + Fetch64(s + 8); \ + c = Rotate(d, 41 ^ z) * multiplier + Fetch64(s + 16); \ + d = Rotate(e, 33 ^ z) * multiplier + Fetch64(s + 24); \ + e = Rotate(t, 25 ^ z) * multiplier + Fetch64(s + 32); \ + t = old_a; \ } \ f = _mm_crc32_u64(f, a); \ g = _mm_crc32_u64(g, b); \ @@ -427,76 +461,83 @@ static void CityHashCrc256Long(const char *s, size_t len, uint32 seed, uint64 *r j = _mm_crc32_u64(j, e); \ s += 40 - CHUNK(1, 1); CHUNK(k0, 0); - CHUNK(1, 1); CHUNK(k0, 0); - CHUNK(1, 1); CHUNK(k0, 0); - } while (--iters > 0); - - while (len >= 40) { - CHUNK(k0, 0); - len -= 40; - } - if (len > 0) { - s = s + len - 40; - CHUNK(k0, 0); - } - j += i << 32; - a = HashLen16(a, j); - h += g << 32; - b += h; - c = HashLen16(c, f) + i; - d = HashLen16(d, e + result[0]); - j += e; - i += HashLen16(h, t); - e = HashLen16(a, d) + j; - f = HashLen16(b, c) + a; - g = HashLen16(j, i) + c; - result[0] = e + f + g + h; - a = ShiftMix((a + g) * k0) * k0 + b; - result[1] += a + result[0]; - a = ShiftMix(a * k0) * k0 + c; - result[2] = a + result[1]; - a = ShiftMix((a + e) * k0) * k0; - result[3] = a + result[2]; + CHUNK(1, 1); CHUNK(k0, 0); + CHUNK(1, 1); CHUNK(k0, 0); + CHUNK(1, 1); CHUNK(k0, 0); + } while (--iters > 0); + + while (len >= 40) { + CHUNK(k0, 0); + len -= 40; + } + + if (len > 0) { + s = s + len - 40; + CHUNK(k0, 0); + } + + j += i << 32; + a = HashLen16(a, j); + h += g << 32; + b += h; + c = HashLen16(c, f) + i; + d = HashLen16(d, e + result[0]); + j += e; + i += HashLen16(h, t); + e = HashLen16(a, d) + j; + f = HashLen16(b, c) + a; + g = HashLen16(j, i) + c; + result[0] = e + f + g + h; + a = ShiftMix((a + g) * k0) * k0 + b; + result[1] += a + result[0]; + a = ShiftMix(a * k0) * k0 + c; + result[2] = a + result[1]; + a = ShiftMix((a + e) * k0) * k0; + result[3] = a + result[2]; } -// Requires len < 240. -static inline void CityHashCrc256Short(const char *s, size_t len, uint64 *result) { - char buf[240]; - memcpy(buf, s, len); - memset(buf + len, 0, 240 - len); - CityHashCrc256Long(buf, 240, ~(uint32)len, result); +/* Requires len < 240. */ +static inline void CityHashCrc256Short(const char *s, size_t len, uint64 *result) +{ + char buf[240]; + memcpy(buf, s, len); + memset(buf + len, 0, 240 - len); + CityHashCrc256Long(buf, 240, ~(uint32)len, result); } -void CityHashCrc256(const char *s, size_t len, uint64 *result) { - if (LIKELY(len >= 240)) { - CityHashCrc256Long(s, len, 0, result); - } else { - CityHashCrc256Short(s, len, result); - } +void CityHashCrc256(const char *s, size_t len, uint64 *result) +{ + if (LIKELY(len >= 240)) + CityHashCrc256Long(s, len, 0, result); + else + CityHashCrc256Short(s, len, result); } -uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed) { - if (len <= 900) { - return CityHash128WithSeed(s, len, seed); - } else { - uint64 result[4], u, v; - CityHashCrc256(s, len, result); - u = Uint128High64(seed) + result[0]; - v = Uint128Low64(seed) + result[1]; - return UInt128(HashLen16(u, v + result[2]), - HashLen16(Rotate(v, 32), u * k0 + result[3])); - } +uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed) +{ + if (len <= 900) { + return CityHash128WithSeed(s, len, seed); + } + else { + uint64 result[4], u, v; + CityHashCrc256(s, len, result); + u = Uint128High64(seed) + result[0]; + v = Uint128Low64(seed) + result[1]; + return UInt128(HashLen16(u, v + result[2]), + HashLen16(Rotate(v, 32), u * k0 + result[3])); + } } -uint128 CityHashCrc128(const char *s, size_t len) { - if (len <= 900) { - return CityHash128(s, len); - } else { - uint64 result[4]; - CityHashCrc256(s, len, result); - return UInt128(result[2], result[3]); - } +uint128 CityHashCrc128(const char *s, size_t len) +{ + if (len <= 900) { + return CityHash128(s, len); + } + else { + uint64 result[4]; + CityHashCrc256(s, len, result); + return UInt128(result[2], result[3]); + } } #endif diff --git a/src/core_dump_handler/cityhash_c/city_c.h b/src/core_dump_handler/cityhash_c/city_c.h index 2fa469f..d2e1f5d 100644 --- a/src/core_dump_handler/cityhash_c/city_c.h +++ b/src/core_dump_handler/cityhash_c/city_c.h @@ -1,95 +1,95 @@ -// Copyright (c) 2011 Google, Inc. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// -// CityHash, by Geoff Pike and Jyrki Alakuijala -// -// This file provides a few functions for hashing strings. On x86-64 -// hardware in 2011, CityHash64() is faster than other high-quality -// hash functions, such as Murmur. This is largely due to higher -// instruction-level parallelism. CityHash64() and CityHash128() also perform -// well on hash-quality tests. -// -// CityHash128() is optimized for relatively long strings and returns -// a 128-bit hash. For strings more than about 2000 bytes it can be -// faster than CityHash64(). -// -// Functions in the CityHash family are not suitable for cryptography. -// -// WARNING: This code has not been tested on big-endian platforms! -// It is known to work well on little-endian platforms that have a small penalty -// for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs. -// -// By the way, for some hash functions, given strings a and b, the hash -// of a+b is easily derived from the hashes of a and b. This property -// doesn't hold for any hash functions in this file. +/* Copyright (c) 2011 Google, Inc. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining a copy */ +/* of this software and associated documentation files (the "Software"), to deal */ +/* in the Software without restriction, including without limitation the rights */ +/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */ +/* copies of the Software, and to permit persons to whom the Software is */ +/* furnished to do so, subject to the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be included in */ +/* all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */ +/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */ +/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */ +/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */ +/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */ +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN */ +/* THE SOFTWARE. */ +/* */ +/* CityHash, by Geoff Pike and Jyrki Alakuijala */ +/* */ +/* This file provides a few functions for hashing strings. On x86-64 */ +/* hardware in 2011, CityHash64() is faster than other high-quality */ +/* hash functions, such as Murmur. This is largely due to higher */ +/* instruction-level parallelism. CityHash64() and CityHash128() also perform */ +/* well on hash-quality tests. */ +/* */ +/* CityHash128() is optimized for relatively long strings and returns */ +/* a 128-bit hash. For strings more than about 2000 bytes it can be */ +/* faster than CityHash64(). */ +/* */ +/* Functions in the CityHash family are not suitable for cryptography. */ +/* */ +/* WARNING: This code has not been tested on big-endian platforms! */ +/* It is known to work well on little-endian platforms that have a small penalty */ +/* for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs. */ +/* */ +/* By the way, for some hash functions, given strings a and b, the hash */ +/* of a+b is easily derived from the hashes of a and b. This property */ +/* doesn't hold for any hash functions in this file. */ #ifndef CITY_HASH_H_ -#define CITY_HASH_H_ +# define CITY_HASH_H_ -#include <stddef.h> -#ifdef _MSC_VER +# include <stddef.h> +# ifdef _MSC_VER typedef unsigned char uint8; typedef unsigned int uint32; typedef unsigned long long uint64; -#else -# include <stdint.h> +# else +# include <stdint.h> typedef uint8_t uint8; typedef uint32_t uint32; typedef uint64_t uint64; -#endif +# endif -#ifdef __cplusplus +# ifdef __cplusplus extern "C" { -#endif +# endif -#pragma pack(1) +# pragma pack(1) typedef struct { - uint64 first, second; + uint64 first, second; } uint128; -#pragma pack() +# pragma pack() -#define Uint128Low64(x) ((x).first) -#define Uint128High64(x) ((x).second) +# define Uint128Low64(x) ((x).first) +# define Uint128High64(x) ((x).second) -// Hash function for a byte array. +/* Hash function for a byte array. */ uint64 CityHash64(const char *buf, size_t len); -// Hash function for a byte array. For convenience, a 64-bit seed is also -// hashed into the result. +/* Hash function for a byte array. For convenience, a 64-bit seed is also */ +/* hashed into the result. */ uint64 CityHash64WithSeed(const char *buf, size_t len, uint64 seed); -// Hash function for a byte array. For convenience, two seeds are also -// hashed into the result. +/* Hash function for a byte array. For convenience, two seeds are also */ +/* hashed into the result. */ uint64 CityHash64WithSeeds(const char *buf, size_t len, uint64 seed0, uint64 seed1); -// Hash function for a byte array. +/* Hash function for a byte array. */ uint128 CityHash128(const char *s, size_t len); -// Hash function for a byte array. For convenience, a 128-bit seed is also -// hashed into the result. +/* Hash function for a byte array. For convenience, a 128-bit seed is also */ +/* hashed into the result. */ uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed); -#ifdef __cplusplus +# ifdef __cplusplus } -#endif +# endif -#endif // CITY_HASH_H_ +#endif /* CITY_HASH_H_ */ diff --git a/src/core_dump_handler/cityhash_c/citycrc_c.h b/src/core_dump_handler/cityhash_c/citycrc_c.h index 5f440de..7b716b1 100644 --- a/src/core_dump_handler/cityhash_c/citycrc_c.h +++ b/src/core_dump_handler/cityhash_c/citycrc_c.h @@ -1,51 +1,51 @@ -// Copyright (c) 2011 Google, Inc. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// -// CityHash, by Geoff Pike and Jyrki Alakuijala -// -// This file declares the subset of the CityHash functions that require -// _mm_crc32_u64(). See the CityHash README for details. -// -// Functions in the CityHash family are not suitable for cryptography. +/* Copyright (c) 2011 Google, Inc. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining a copy */ +/* of this software and associated documentation files (the "Software"), to deal */ +/* in the Software without restriction, including without limitation the rights */ +/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */ +/* copies of the Software, and to permit persons to whom the Software is */ +/* furnished to do so, subject to the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be included in */ +/* all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */ +/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */ +/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */ +/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */ +/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */ +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN */ +/* THE SOFTWARE. */ +/* */ +/* CityHash, by Geoff Pike and Jyrki Alakuijala */ +/* */ +/* This file declares the subset of the CityHash functions that require */ +/* _mm_crc32_u64(). See the CityHash README for details. */ +/* */ +/* Functions in the CityHash family are not suitable for cryptography. */ #ifndef CITY_HASH_CRC_H_ -#define CITY_HASH_CRC_H_ +# define CITY_HASH_CRC_H_ -#include "city_c.h" +# include "city_c.h" -#ifdef __cplusplus +# ifdef __cplusplus extern "C" { -#endif +# endif -// Hash function for a byte array. +/* Hash function for a byte array. */ uint128 CityHashCrc128(const char *s, size_t len); -// Hash function for a byte array. For convenience, a 128-bit seed is also -// hashed into the result. +/* Hash function for a byte array. For convenience, a 128-bit seed is also */ +/* hashed into the result. */ uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed); -// Hash function for a byte array. Sets result[0] ... result[3]. +/* Hash function for a byte array. Sets result[0] ... result[3]. */ void CityHashCrc256(const char *s, size_t len, uint64 *result); -#ifdef __cplusplus +# ifdef __cplusplus } -#endif +# endif -#endif // CITY_HASH_CRC_H_ +#endif /* CITY_HASH_CRC_H_ */ diff --git a/src/core_dump_handler/dlt_cdh.c b/src/core_dump_handler/dlt_cdh.c index c573982..477a873 100644 --- a/src/core_dump_handler/dlt_cdh.c +++ b/src/core_dump_handler/dlt_cdh.c @@ -41,7 +41,7 @@ #include "dlt_cdh.h" #include <dirent.h> -// Unusual characters in a windows filename are replaced +/* Unusual characters in a windows filename are replaced */ #define UNUSUAL_CHARS ":/\\!*" #define REPLACEMENT_CHAR '_' @@ -49,19 +49,19 @@ #define COREDUMP_FILESYSTEM_MIN_SIZE_MB 40 #define COREDUMP_HANDLER_PRIORITY -19 -void core_locks(const proc_info_t* p_proc, int action); +void core_locks(const proc_info_t *p_proc, int action); /* =================================================================== - ** Method : init_proc_info(...) - ** - ** Description : initialises all members of process info structure to defined values - ** - ** Parameters : INPUT p_proc - ** OUTPUT pointer to initialised crashed process info structure - ** - ** Returns : nothing - ** ===================================================================*/ -void init_proc_info(proc_info_t* p_proc) +** Method : init_proc_info(...) +** +** Description : initialises all members of process info structure to defined values +** +** Parameters : INPUT p_proc +** OUTPUT pointer to initialised crashed process info structure +** +** Returns : nothing +** ===================================================================*/ +void init_proc_info(proc_info_t *p_proc) { memset(p_proc->name, 0, sizeof(p_proc->name)); memset(p_proc->threadname, 0, sizeof(p_proc->threadname)); @@ -87,107 +87,98 @@ void init_proc_info(proc_info_t* p_proc) } /* =================================================================== - ** Method : read_args(...) - ** - ** Description : reads command line arguments - ** - ** Parameters : INPUT argc - ** INPUT argv - ** OUTPUT pointer to crashed process info structure - ** - ** Returns : 0 if success, else -1 - ** ===================================================================*/ -cdh_status_t read_args(int argc, char** argv, proc_info_t* proc) +** Method : read_args(...) +** +** Description : reads command line arguments +** +** Parameters : INPUT argc +** INPUT argv +** OUTPUT pointer to crashed process info structure +** +** Returns : 0 if success, else -1 +** ===================================================================*/ +cdh_status_t read_args(int argc, char **argv, proc_info_t *proc) { - if (argc < 5) - { + if (argc < 5) { syslog(LOG_ERR, "Usage: cdh timestamp pid signal procname"); return CDH_NOK; } init_proc_info(proc); - if (sscanf(argv[1], "%u", &proc->timestamp) != 1) - { + if (sscanf(argv[1], "%u", &proc->timestamp) != 1) { syslog(LOG_ERR, "Unable to read timestamp argument <%s>. Closing", argv[1]); return CDH_NOK; } - if (sscanf(argv[2], "%d", &proc->pid) != 1) - { + if (sscanf(argv[2], "%d", &proc->pid) != 1) { syslog(LOG_ERR, "Unable to read pid argument <%s>. Closing", argv[2]); return CDH_NOK; } - if (sscanf(argv[3], "%d", &proc->signal) != 1) - { + if (sscanf(argv[3], "%d", &proc->signal) != 1) { syslog(LOG_ERR, "Unable to read signal argument <%s>. Closing", argv[3]); return CDH_NOK; } - // save the thread name given by the kernel + /* save the thread name given by the kernel */ strncpy(proc->threadname, argv[4], sizeof(proc->threadname) - 1); - // initialize the binary name with threadname... in case we cannot read it from /proc + /* initialize the binary name with threadname... in case we cannot read it from /proc */ strncpy(proc->name, argv[4], sizeof(proc->name) - 1); return CDH_OK; } /* =================================================================== - ** Method : remove_unusual_chars(...) - ** - ** Description : modify the input string to change UNUSUALS_CHARS to - ** REPLACEMENT_CHAR - ** Parameters : INPUT/OUTPUT string to be modified - ** - ** Returns : nothing - ** ===================================================================*/ -void remove_unusual_chars(char* p_string) +** Method : remove_unusual_chars(...) +** +** Description : modify the input string to change UNUSUALS_CHARS to +** REPLACEMENT_CHAR +** Parameters : INPUT/OUTPUT string to be modified +** +** Returns : nothing +** ===================================================================*/ +void remove_unusual_chars(char *p_string) { unsigned int l_char_index = 0; - for (l_char_index = 0; l_char_index < sizeof(UNUSUAL_CHARS) - 1; l_char_index++) - { - char* l_str_pointer = p_string; + for (l_char_index = 0; l_char_index < sizeof(UNUSUAL_CHARS) - 1; l_char_index++) { + char *l_str_pointer = p_string; - do - { + do { l_str_pointer = strchr(l_str_pointer, UNUSUAL_CHARS[l_char_index]); - if (l_str_pointer != NULL) - { + if (l_str_pointer != NULL) { *l_str_pointer = REPLACEMENT_CHAR; l_str_pointer++; } - } - while (l_str_pointer != NULL); + } while (l_str_pointer != NULL); } } /* =================================================================== - ** Method : check_disk_space(...) - ** - ** Description : check if there is sufficient disk space to write a coredump - ** Parameters : INPUT/OUTPUT string to be modified - ** - ** Returns : 0 if success, else -1 - ** ===================================================================*/ +** Method : check_disk_space(...) +** +** Description : check if there is sufficient disk space to write a coredump +** Parameters : INPUT/OUTPUT string to be modified +** +** Returns : 0 if success, else -1 +** ===================================================================*/ cdh_status_t check_disk_space() { struct statvfs stat; unsigned long free_size = 0; - if (statvfs(COREDUMP_FILESYSTEM, &stat) < 0) - { + if (statvfs(COREDUMP_FILESYSTEM, &stat) < 0) { syslog(LOG_ERR, "ERR cannot stat disk space on %s: %s", COREDUMP_FILESYSTEM, strerror(errno)); return CDH_NOK; } - // free space: size of block * number of free blocks (>>20 => MB) + /* free space: size of block * number of free blocks (>>20 => MB) */ free_size = (stat.f_bsize * stat.f_bavail) >> 20; - if (free_size < COREDUMP_FILESYSTEM_MIN_SIZE_MB) - { + + if (free_size < COREDUMP_FILESYSTEM_MIN_SIZE_MB) { syslog(LOG_WARNING, "ERR insufficient disk space for coredump: %ld MB.", free_size); return CDH_NOK; } @@ -201,20 +192,17 @@ void clean_core_tmp_dir() DIR *d = NULL; struct dirent *dir = NULL; - if ((d = opendir(CORE_TMP_DIRECTORY)) != NULL) - { + if ((d = opendir(CORE_TMP_DIRECTORY)) != NULL) { char lockfilepath[CORE_MAX_FILENAME_LENGTH]; - while ((dir = readdir(d)) != NULL) - { + while ((dir = readdir(d)) != NULL) { struct stat unused_stat; - // check if lock file exists + /* check if lock file exists */ snprintf(lockfilepath, sizeof(lockfilepath), "%s/%s", CORE_LOCK_DIRECTORY, dir->d_name); - if (stat(lockfilepath, &unused_stat) != 0) - { - // No lock file found for this coredump => from previous LC => delete + if (stat(lockfilepath, &unused_stat) != 0) { + /* No lock file found for this coredump => from previous LC => delete */ char filepath[CORE_MAX_FILENAME_LENGTH] = { 0 }; snprintf(filepath, sizeof(filepath), "%s/%s", CORE_TMP_DIRECTORY, dir->d_name); @@ -230,24 +218,23 @@ void clean_core_tmp_dir() } /* =================================================================== - ** Method : check_core_directory(...) - ** - ** Description : checks the availability of core dumps directory. - ** if not available, there is an installation issue. - ** - ** Parameters : - ** - ** Returns : 0 if success, else -1 - ** ===================================================================*/ -cdh_status_t check_and_create_directory(const char* p_dirname, int create_silently) +** Method : check_core_directory(...) +** +** Description : checks the availability of core dumps directory. +** if not available, there is an installation issue. +** +** Parameters : +** +** Returns : 0 if success, else -1 +** ===================================================================*/ +cdh_status_t check_and_create_directory(const char *p_dirname, int create_silently) { int l_need_create = 0; int l_need_delete = 0; struct stat l_stat; - if (lstat(p_dirname, &l_stat) < 0) - { + if (lstat(p_dirname, &l_stat) < 0) { l_need_create = 1; } else if (!S_ISDIR(l_stat.st_mode)) @@ -256,23 +243,20 @@ cdh_status_t check_and_create_directory(const char* p_dirname, int create_silent l_need_create = 1; } - if (l_need_delete > 0) - { + if (l_need_delete > 0) { syslog(LOG_WARNING, "WARN core directory '%s' is not a directory => removing it", p_dirname); - if (unlink(p_dirname) == -1) - { + if (unlink(p_dirname) == -1) { syslog(LOG_ERR, "ERR core directory '%s' cannot be unlinked: %s", p_dirname, strerror(errno)); return CDH_NOK; } } - if (l_need_create > 0) - { + + if (l_need_create > 0) { if (create_silently == 0) syslog(LOG_WARNING, "WARN core directory '%s' does not exist => creation", p_dirname); - if (mkdir(p_dirname, 0666) == -1) - { + if (mkdir(p_dirname, 0666) == -1) { syslog(LOG_ERR, "ERR core directory '%s' cannot be created: %s", p_dirname, strerror(errno)); return CDH_NOK; } @@ -282,15 +266,15 @@ cdh_status_t check_and_create_directory(const char* p_dirname, int create_silent } /* =================================================================== - ** Method : check_core_directory(...) - ** - ** Description : checks the availability of core dumps directory. - ** if not available, there is an installation issue. - ** - ** Parameters : - ** - ** Returns : 0 if success, else -1 - ** ===================================================================*/ +** Method : check_core_directory(...) +** +** Description : checks the availability of core dumps directory. +** if not available, there is an installation issue. +** +** Parameters : +** +** Returns : 0 if success, else -1 +** ===================================================================*/ cdh_status_t check_core_directory() { if (check_and_create_directory(CORE_DIRECTORY, 0) < 0) @@ -308,36 +292,35 @@ cdh_status_t check_core_directory() } /* =================================================================== - ** Method : move_to_core_directory(...) - ** - ** Description : move the coredump and context files - ** from temporary dir to final core directory - ** - ** Parameters : - ** - ** Returns : 0 if success, else -1 - ** ===================================================================*/ -cdh_status_t move_to_core_directory(proc_info_t* p_proc) +** Method : move_to_core_directory(...) +** +** Description : move the coredump and context files +** from temporary dir to final core directory +** +** Parameters : +** +** Returns : 0 if success, else -1 +** ===================================================================*/ +cdh_status_t move_to_core_directory(proc_info_t *p_proc) { char l_src_filename[CORE_MAX_FILENAME_LENGTH] = { 0 }; char l_dst_filename[CORE_MAX_FILENAME_LENGTH] = { 0 }; - char* patterns[] = { CORE_FILE_PATTERN, CONTEXT_FILE_PATTERN }; + char *patterns[] = { CORE_FILE_PATTERN, CONTEXT_FILE_PATTERN }; unsigned int pattern_num = 0; if (p_proc == NULL) return CDH_NOK; - for (pattern_num = 0; pattern_num < sizeof(patterns) / sizeof(char*); pattern_num++) - { - // Don't move coredump if it cannot be created - if (p_proc->can_create_coredump == 0 && pattern_num == 0) + for (pattern_num = 0; pattern_num < sizeof(patterns) / sizeof(char *); pattern_num++) { + /* Don't move coredump if it cannot be created */ + if ((p_proc->can_create_coredump == 0) && (pattern_num == 0)) continue; snprintf(l_src_filename, sizeof(l_src_filename), patterns[pattern_num], - CORE_TMP_DIRECTORY, p_proc->timestamp, p_proc->name, p_proc->pid); + CORE_TMP_DIRECTORY, p_proc->timestamp, p_proc->name, p_proc->pid); snprintf(l_dst_filename, sizeof(l_dst_filename), patterns[pattern_num], - CORE_DIRECTORY, p_proc->timestamp, p_proc->name, p_proc->pid); + CORE_DIRECTORY, p_proc->timestamp, p_proc->name, p_proc->pid); syslog(LOG_INFO, "Moving coredump from %s to %s", l_src_filename, l_dst_filename); @@ -349,18 +332,18 @@ cdh_status_t move_to_core_directory(proc_info_t* p_proc) } /* =================================================================== - ** Method : main(...) - ** - ** Description : - ** - ** Parameters : argc, argv - ** - ** Returns : - ** ===================================================================*/ -int main(int argc, char* argv[]) +** Method : main(...) +** +** Description : +** +** Parameters : argc, argv +** +** Returns : +** ===================================================================*/ +int main(int argc, char *argv[]) { proc_info_t l_proc_info; -// char l_exec_name[CORE_MAX_FILENAME_LENGTH] = {0}; +/* char l_exec_name[CORE_MAX_FILENAME_LENGTH] = {0}; */ openlog("CoredumpHandler", 0, LOG_DAEMON); @@ -371,26 +354,22 @@ int main(int argc, char* argv[]) syslog(LOG_ERR, "Failed to get executable name"); syslog(LOG_NOTICE, "Handling coredump procname:%s pid:%d timest:%d signal:%d", - l_proc_info.name, - l_proc_info.pid, - l_proc_info.timestamp, - l_proc_info.signal); + l_proc_info.name, + l_proc_info.pid, + l_proc_info.timestamp, + l_proc_info.signal); - // Increase priority of the coredump handler + /* Increase priority of the coredump handler */ if (nice(COREDUMP_HANDLER_PRIORITY) != COREDUMP_HANDLER_PRIORITY) syslog(LOG_WARNING, "Failed to change CDH priority"); if (check_disk_space() < 0) - { - //return CDH_NOK; + /*return CDH_NOK; */ l_proc_info.can_create_coredump = 0; - } if (check_core_directory() < 0) - { - //return CDH_NOK; + /*return CDH_NOK; */ l_proc_info.can_create_coredump = 0; - } remove_unusual_chars(l_proc_info.name); @@ -411,48 +390,44 @@ int main(int argc, char* argv[]) return CDH_OK; } -void core_locks(const proc_info_t* p_proc, int action) +void core_locks(const proc_info_t *p_proc, int action) { char l_lockfilepath[CORE_MAX_FILENAME_LENGTH] = { 0 }; - char* patterns[] = { CORE_FILE_PATTERN, CONTEXT_FILE_PATTERN }; + char *patterns[] = { CORE_FILE_PATTERN, CONTEXT_FILE_PATTERN }; unsigned int pattern_num = 0; int fd_lockfile = -1; if (p_proc == NULL) return; - for (pattern_num = 0; pattern_num < sizeof(patterns) / sizeof(char*); pattern_num++) - { + for (pattern_num = 0; pattern_num < sizeof(patterns) / sizeof(char *); pattern_num++) { snprintf(l_lockfilepath, sizeof(l_lockfilepath), patterns[pattern_num], - CORE_LOCK_DIRECTORY, p_proc->timestamp, p_proc->name, p_proc->pid); + CORE_LOCK_DIRECTORY, p_proc->timestamp, p_proc->name, p_proc->pid); - switch (action) + switch (action) { + case 0: { - case 0: - { - unlink(l_lockfilepath); - break; - } + unlink(l_lockfilepath); + break; + } + + case 1: + { + if ((fd_lockfile = open(l_lockfilepath, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) >= 0) { + if (write(fd_lockfile, "1", 1) < 0) + syslog(LOG_WARNING, "Failed to write lockfile %d: %s", fd_lockfile, strerror(errno)); - case 1: - { - if ((fd_lockfile = open(l_lockfilepath, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) >= 0) - { - if (write(fd_lockfile, "1", 1) < 0) - syslog(LOG_WARNING, "Failed to write lockfile %d: %s", fd_lockfile, strerror(errno)); - - close(fd_lockfile); - } - else - { - syslog(LOG_WARNING, "Failed to open lockfile %s: %s", l_lockfilepath, strerror(errno)); - } - - break; + close(fd_lockfile); } + else { + syslog(LOG_WARNING, "Failed to open lockfile %s: %s", l_lockfilepath, strerror(errno)); + } + + break; + } - default: - break; + default: + break; } } } diff --git a/src/core_dump_handler/dlt_cdh.h b/src/core_dump_handler/dlt_cdh.h index ed61560..02f637c 100644 --- a/src/core_dump_handler/dlt_cdh.h +++ b/src/core_dump_handler/dlt_cdh.h @@ -36,18 +36,18 @@ #include "dlt_cdh_streamer.h" -#define CORE_DIRECTORY "/var/core" -#define CORE_TMP_DIRECTORY "/var/core_tmp" -#define CORE_LOCK_DIRECTORY "/tmp/.core_locks" -#define CORE_MAX_FILENAME_LENGTH 255 -#define MAX_PROC_NAME_LENGTH 32 -#define CRASH_ID_LEN 8 -#define CRASHID_FILE "/tmp/.crashid" // the file where the white screen app will read the crashid - -#define CORE_FILE_PATTERN "%s/core.%d.%s.%d.gz" -#define CONTEXT_FILE_PATTERN "%s/context.%d.%s.%d.txt" - -#define ELF_Ehdr Elf32_Ehdr +#define CORE_DIRECTORY "/var/core" +#define CORE_TMP_DIRECTORY "/var/core_tmp" +#define CORE_LOCK_DIRECTORY "/tmp/.core_locks" +#define CORE_MAX_FILENAME_LENGTH 255 +#define MAX_PROC_NAME_LENGTH 32 +#define CRASH_ID_LEN 8 +#define CRASHID_FILE "/tmp/.crashid" /* the file where the white screen app will read the crashid */ + +#define CORE_FILE_PATTERN "%s/core.%d.%s.%d.gz" +#define CONTEXT_FILE_PATTERN "%s/context.%d.%s.%d.txt" + +#define ELF_Ehdr Elf32_Ehdr #define ELF_Phdr Elf32_Phdr #define ELF_Shdr Elf32_Shdr #define ELF_Nhdr Elf32_Nhdr @@ -71,10 +71,10 @@ typedef struct int can_create_coredump; file_streamer_t streamer; - // coredump content, for crash id generation + /* coredump content, for crash id generation */ ELF_Ehdr m_Ehdr; - ELF_Phdr* m_pPhdr; - char* m_Nhdr; // buffer with all NOTE pages + ELF_Phdr *m_pPhdr; + char *m_Nhdr; /* buffer with all NOTE pages */ unsigned int m_note_page_size; @@ -86,11 +86,11 @@ typedef struct } proc_info_t; -cdh_status_t get_exec_name(unsigned int p_pid_str, char* p_exec_name, int p_exec_name_maxsize); -cdh_status_t write_proc_context(const proc_info_t*); -cdh_status_t treat_coredump(proc_info_t* p_proc); -cdh_status_t treat_crash_data(proc_info_t* p_proc); -cdh_status_t move_to_core_directory(proc_info_t* p_proc); +cdh_status_t get_exec_name(unsigned int p_pid_str, char *p_exec_name, int p_exec_name_maxsize); +cdh_status_t write_proc_context(const proc_info_t *); +cdh_status_t treat_coredump(proc_info_t *p_proc); +cdh_status_t treat_crash_data(proc_info_t *p_proc); +cdh_status_t move_to_core_directory(proc_info_t *p_proc); cdh_status_t check_core_directory(); -#endif // #ifndef DLT_CDH_H +#endif /* #ifndef DLT_CDH_H */ diff --git a/src/core_dump_handler/dlt_cdh_context.c b/src/core_dump_handler/dlt_cdh_context.c index 4fb1d85..f7b8a82 100644 --- a/src/core_dump_handler/dlt_cdh_context.c +++ b/src/core_dump_handler/dlt_cdh_context.c @@ -35,21 +35,21 @@ #include "dlt_cdh.h" -// Global buffer for file reading +/* Global buffer for file reading */ char g_buffer[4096]; /* =================================================================== - ** Method : get_exec_name(...) - ** - ** Description : read executable filename - ** - ** Parameters : INPUT p_pid_str pid of the process - ** OUTPUT p_exec_name executable name - ** INPUT p_exec_name_maxsize size of p_exec_name buffer - ** - ** Returns : 0 if success, else -1 - ** ===================================================================*/ -cdh_status_t get_exec_name(unsigned int p_pid, char* p_exec_name, int p_exec_name_maxsize) +** Method : get_exec_name(...) +** +** Description : read executable filename +** +** Parameters : INPUT p_pid_str pid of the process +** OUTPUT p_exec_name executable name +** INPUT p_exec_name_maxsize size of p_exec_name buffer +** +** Returns : 0 if success, else -1 +** ===================================================================*/ +cdh_status_t get_exec_name(unsigned int p_pid, char *p_exec_name, int p_exec_name_maxsize) { char l_exe_link[CORE_MAX_FILENAME_LENGTH] = { 0 }; char *l_name_ptr = NULL; @@ -70,18 +70,18 @@ cdh_status_t get_exec_name(unsigned int p_pid, char* p_exec_name, int p_exec_nam } /* =================================================================== - ** Method : dump_file_to(...) - ** - ** Description : dump the content of file p_src_filename to the file descriptor p_fout - ** - ** Parameters : INPUT p_src_filename - ** INPUT p_fout - ** - ** Returns : 0 if success, else -1 - ** ===================================================================*/ -cdh_status_t dump_file_to(const char* p_src_filename, FILE* p_fout) +** Method : dump_file_to(...) +** +** Description : dump the content of file p_src_filename to the file descriptor p_fout +** +** Parameters : INPUT p_src_filename +** INPUT p_fout +** +** Returns : 0 if success, else -1 +** ===================================================================*/ +cdh_status_t dump_file_to(const char *p_src_filename, FILE *p_fout) { - FILE* l_fin = NULL; + FILE *l_fin = NULL; int bytes_read = 0; if (p_fout == NULL) @@ -89,32 +89,28 @@ cdh_status_t dump_file_to(const char* p_src_filename, FILE* p_fout) fprintf(p_fout, "\n==== Dumping file <%s> ====\n", p_src_filename); - if ((l_fin = fopen(p_src_filename, "rt")) == NULL) - { + if ((l_fin = fopen(p_src_filename, "rt")) == NULL) { syslog(LOG_ERR, "ERR opening info file '%s' for dumping [%s]", - p_src_filename, - strerror(errno)); + p_src_filename, + strerror(errno)); fprintf(p_fout, "**error**\n"); return CDH_NOK; } - while ((bytes_read = fread(g_buffer, 1, sizeof(g_buffer), l_fin)) != 0) - { + while ((bytes_read = fread(g_buffer, 1, sizeof(g_buffer), l_fin)) != 0) { int i = 0; - // changes all "\0" in the file to a "\n" - // (needed for example for /proc/<pid>/cmdline, to keep all arguments) + /* changes all "\0" in the file to a "\n" */ + /* (needed for example for /proc/<pid>/cmdline, to keep all arguments) */ for (i = 0; i < bytes_read; i++) - { if (g_buffer[i] == '\000') g_buffer[i] = '\n'; - } fwrite(g_buffer, 1, bytes_read, p_fout); - if (ferror(p_fout)) - { + + if (ferror(p_fout)) { syslog(LOG_ERR, "Writing in context file failed [%s]", strerror(errno)); fclose(p_fout); fclose(l_fin); @@ -124,8 +120,7 @@ cdh_status_t dump_file_to(const char* p_src_filename, FILE* p_fout) } } - if (ferror(l_fin)) - { + if (ferror(l_fin)) { syslog(LOG_ERR, "reading '%s' failed [%s]", p_src_filename, strerror(errno)); fclose(l_fin); @@ -138,42 +133,46 @@ cdh_status_t dump_file_to(const char* p_src_filename, FILE* p_fout) return CDH_OK; } -//*************************************************************************************************/ -// "ls -l" implementation for /proc/<pid>/fd (at least) -// Taken from coreutils sources, lib/filemode.c -// +/************************************************************************************************** / */ +/* "ls -l" implementation for /proc/<pid>/fd (at least) */ +/* Taken from coreutils sources, lib/filemode.c */ +/* */ /* Return a character indicating the type of file described by - file mode BITS: - '-' regular file - 'b' block special file - 'c' character special file - 'C' high performance ("contiguous data") file - 'd' directory - 'D' door - 'l' symbolic link - 'm' multiplexed file (7th edition Unix; obsolete) - 'n' network special file (HP-UX) - 'p' fifo (named pipe) - 'P' port - 's' socket - 'w' whiteout (4.4BSD) - '?' some other file type */ + * file mode BITS: + * '-' regular file + * 'b' block special file + * 'c' character special file + * 'C' high performance ("contiguous data") file + * 'd' directory + * 'D' door + * 'l' symbolic link + * 'm' multiplexed file (7th edition Unix; obsolete) + * 'n' network special file (HP-UX) + * 'p' fifo (named pipe) + * 'P' port + * 's' socket + * 'w' whiteout (4.4BSD) + * '?' some other file type */ static char ftypelet(mode_t bits) { /* These are the most common, so test for them first. */ if (S_ISREG(bits)) return '-'; + if (S_ISDIR(bits)) return 'd'; /* Other letters standardized by POSIX 1003.1-2004. */ if (S_ISBLK(bits)) return 'b'; + if (S_ISCHR(bits)) return 'c'; + if (S_ISLNK(bits)) return 'l'; + if (S_ISFIFO(bits)) return 'p'; @@ -182,18 +181,18 @@ static char ftypelet(mode_t bits) return 's'; /* Nonstandard file types. - if (S_ISCTG (bits)) - return 'C'; - if (S_ISDOOR (bits)) - return 'D'; - if (S_ISMPB (bits) || S_ISMPC (bits)) - return 'm'; - if (S_ISNWK (bits)) - return 'n'; - if (S_ISPORT (bits)) - return 'P'; - if (S_ISWHT (bits)) - return 'w'; + * if (S_ISCTG (bits)) + * return 'C'; + * if (S_ISDOOR (bits)) + * return 'D'; + * if (S_ISMPB (bits) || S_ISMPC (bits)) + * return 'm'; + * if (S_ISNWK (bits)) + * return 'n'; + * if (S_ISPORT (bits)) + * return 'P'; + * if (S_ISWHT (bits)) + * return 'w'; */ return '?'; @@ -209,49 +208,47 @@ void strmode(mode_t mode, char *str) str[2] = mode & S_IWUSR ? 'w' : '-'; str[3] = (mode & S_ISUID ? (mode & S_IXUSR ? 's' : 'S') - : - (mode & S_IXUSR ? 'x' : '-')); + : + (mode & S_IXUSR ? 'x' : '-')); str[4] = mode & S_IRGRP ? 'r' : '-'; str[5] = mode & S_IWGRP ? 'w' : '-'; str[6] = (mode & S_ISGID ? (mode & S_IXGRP ? 's' : 'S') - : - (mode & S_IXGRP ? 'x' : '-')); + : + (mode & S_IXGRP ? 'x' : '-')); str[7] = mode & S_IROTH ? 'r' : '-'; str[8] = mode & S_IWOTH ? 'w' : '-'; str[9] = (mode & S_ISVTX ? (mode & S_IXOTH ? 't' : 'T') - : - (mode & S_IXOTH ? 'x' : '-')); + : + (mode & S_IXOTH ? 'x' : '-')); str[10] = ' '; str[11] = '\0'; } /* =================================================================== - ** Method : list_dircontent_to(...) - ** - ** Description : list the filenames in p_dirname directory to the file descriptor p_fout - ** - ** Parameters : INPUT p_dirname - ** INPUT p_fout - ** - ** Returns : 0 if success, else -1 - ** ===================================================================*/ -cdh_status_t list_dircontent_to(const char* p_dirname, FILE* p_fout) +** Method : list_dircontent_to(...) +** +** Description : list the filenames in p_dirname directory to the file descriptor p_fout +** +** Parameters : INPUT p_dirname +** INPUT p_fout +** +** Returns : 0 if success, else -1 +** ===================================================================*/ +cdh_status_t list_dircontent_to(const char *p_dirname, FILE *p_fout) { - DIR* l_dd = NULL; // directory descriptor - struct dirent* l_entity = NULL; + DIR *l_dd = NULL; /* directory descriptor */ + struct dirent *l_entity = NULL; - if ((l_dd = opendir(p_dirname)) == NULL) - { + if ((l_dd = opendir(p_dirname)) == NULL) { syslog(LOG_ERR, "ERR reading info dir '%s' failed [%s]", p_dirname, strerror(errno)); return CDH_NOK; } fprintf(p_fout, "==== Listing directory <%s> ====\n", p_dirname); - while ((l_entity = readdir(l_dd)) != NULL) - { + while ((l_entity = readdir(l_dd)) != NULL) { char l_fullpath[CORE_MAX_FILENAME_LENGTH] = { 0 }; char l_linkpath[CORE_MAX_FILENAME_LENGTH] = { 0 }; char l_modebuf[12] = { 0 }; @@ -264,8 +261,7 @@ cdh_status_t list_dircontent_to(const char* p_dirname, FILE* p_fout) snprintf(l_fullpath, sizeof(l_fullpath), "%s/%s", p_dirname, l_entity->d_name); - if (lstat(l_fullpath, &l_stat) < 0) - { + if (lstat(l_fullpath, &l_stat) < 0) { syslog(LOG_ERR, "ERR lstat on '%s' failed. [%s]", l_fullpath, strerror(errno)); continue; } @@ -273,51 +269,49 @@ cdh_status_t list_dircontent_to(const char* p_dirname, FILE* p_fout) strmode(l_stat.st_mode, l_modebuf); fprintf(p_fout, "%s %ld %d %d %ld %4s", - l_modebuf, - l_stat.st_nlink, - l_stat.st_uid, - l_stat.st_gid, - l_stat.st_size, - l_entity->d_name); - - switch (l_stat.st_mode & S_IFMT) - { - case S_IFBLK: - fprintf(p_fout, " [block device]\n"); + l_modebuf, + l_stat.st_nlink, + l_stat.st_uid, + l_stat.st_gid, + l_stat.st_size, + l_entity->d_name); + + switch (l_stat.st_mode & S_IFMT) { + case S_IFBLK: + fprintf(p_fout, " [block device]\n"); break; - case S_IFCHR: - fprintf(p_fout, " [character device]\n"); + case S_IFCHR: + fprintf(p_fout, " [character device]\n"); break; - case S_IFDIR: - fprintf(p_fout, " [directory]\n"); + case S_IFDIR: + fprintf(p_fout, " [directory]\n"); break; - case S_IFIFO: - fprintf(p_fout, " [FIFO/pipe]\n"); + case S_IFIFO: + fprintf(p_fout, " [FIFO/pipe]\n"); break; - case S_IFLNK: - l_size = readlink(l_fullpath, l_linkpath, sizeof(l_linkpath)); - l_linkpath[l_size] = 0; - fprintf(p_fout, " -> %s\n", l_linkpath); + case S_IFLNK: + l_size = readlink(l_fullpath, l_linkpath, sizeof(l_linkpath)); + l_linkpath[l_size] = 0; + fprintf(p_fout, " -> %s\n", l_linkpath); break; - case S_IFREG: - fprintf(p_fout, " [regular file]\n"); + case S_IFREG: + fprintf(p_fout, " [regular file]\n"); break; - case S_IFSOCK: - fprintf(p_fout, " [socket]\n"); + case S_IFSOCK: + fprintf(p_fout, " [socket]\n"); break; - default: - fprintf(p_fout, " [unknown?]\n"); + default: + fprintf(p_fout, " [unknown?]\n"); break; } - - } // while ( (l_entity = readdir(l_dd)) != NULL ) + } /* while ( (l_entity = readdir(l_dd)) != NULL ) */ fprintf(p_fout, "===========================\n"); closedir(l_dd); @@ -325,23 +319,23 @@ cdh_status_t list_dircontent_to(const char* p_dirname, FILE* p_fout) return CDH_OK; } -//*************************************************************************************************/ -// END of "ls -l" implementation for /proc/<pid>/fd (at least) -//*************************************************************************************************/ +/************************************************************************************************** / */ +/* END of "ls -l" implementation for /proc/<pid>/fd (at least) */ +/************************************************************************************************** / */ /* =================================================================== - ** Method : write_proc_context(...) - ** - ** Description : write the context data of the crashed process - ** (context data coming mainly from /proc) - ** - ** Parameters : INPUT p_proc crashed process info - ** - ** Returns : 0 if success, else -1 - ** ===================================================================*/ -cdh_status_t write_proc_context(const proc_info_t* p_proc) +** Method : write_proc_context(...) +** +** Description : write the context data of the crashed process +** (context data coming mainly from /proc) +** +** Parameters : INPUT p_proc crashed process info +** +** Returns : 0 if success, else -1 +** ===================================================================*/ +cdh_status_t write_proc_context(const proc_info_t *p_proc) { - FILE* l_fout = NULL; + FILE *l_fout = NULL; char l_procfile[256] = { 0 }; char l_outfilename[CORE_MAX_FILENAME_LENGTH] = { 0 }; @@ -349,21 +343,20 @@ cdh_status_t write_proc_context(const proc_info_t* p_proc) return CDH_NOK; snprintf(l_outfilename, sizeof(l_outfilename), CONTEXT_FILE_PATTERN, - CORE_TMP_DIRECTORY, - p_proc->timestamp, - p_proc->name, - p_proc->pid); + CORE_TMP_DIRECTORY, + p_proc->timestamp, + p_proc->name, + p_proc->pid); - if ((l_fout = fopen(l_outfilename, "w+t")) == NULL) - { + if ((l_fout = fopen(l_outfilename, "w+t")) == NULL) { syslog(LOG_ERR, "ERR Cannot open context file '%s' [%s]", l_outfilename, strerror(errno)); return CDH_NOK; } -#define PROC_FILENAME(x) do{\ - snprintf(l_procfile, sizeof(l_procfile), "/proc/%d/"x,\ - p_proc->pid);\ -} while(0) +#define PROC_FILENAME(x) do { \ + snprintf(l_procfile, sizeof(l_procfile), "/proc/%d/"x, \ + p_proc->pid); \ +} while (0) fprintf(l_fout, "ProcName:%s\n", p_proc->name); fprintf(l_fout, "ThreadName:%s\n", p_proc->threadname); diff --git a/src/core_dump_handler/dlt_cdh_coredump.c b/src/core_dump_handler/dlt_cdh_coredump.c index 612174b..9cbc6b0 100644 --- a/src/core_dump_handler/dlt_cdh_coredump.c +++ b/src/core_dump_handler/dlt_cdh_coredump.c @@ -39,46 +39,43 @@ #include "dlt_cdh.h" -cdh_status_t read_elf_headers(proc_info_t* p_proc) +cdh_status_t read_elf_headers(proc_info_t *p_proc) { int phnum = 0; - // Read ELF header + /* Read ELF header */ stream_read(&p_proc->streamer, &p_proc->m_Ehdr, sizeof(p_proc->m_Ehdr)); - // Read until PROG position + /* Read until PROG position */ stream_move_to_offest(&p_proc->streamer, p_proc->m_Ehdr.e_phoff); - // Read and store all program headers - p_proc->m_pPhdr = (ELF_Phdr*) malloc(sizeof(ELF_Phdr) * p_proc->m_Ehdr.e_phnum); - if (p_proc->m_pPhdr == NULL) - { + /* Read and store all program headers */ + p_proc->m_pPhdr = (ELF_Phdr *)malloc(sizeof(ELF_Phdr) * p_proc->m_Ehdr.e_phnum); + + if (p_proc->m_pPhdr == NULL) { syslog(LOG_ERR, "Cannot allocate Phdr memory (%d headers)", p_proc->m_Ehdr.e_phnum); return CDH_NOK; } for (phnum = 0; phnum < p_proc->m_Ehdr.e_phnum; phnum++) - { - // Read Programm header + /* Read Programm header */ stream_read(&p_proc->streamer, &p_proc->m_pPhdr[phnum], sizeof(ELF_Phdr)); - } return CDH_OK; } -int getNotePageIndex(proc_info_t* p_proc) +int getNotePageIndex(proc_info_t *p_proc) { int i = 0; - // Search PT_NOTE section - for (i = 0; i < p_proc->m_Ehdr.e_phnum; i++) - { + /* Search PT_NOTE section */ + for (i = 0; i < p_proc->m_Ehdr.e_phnum; i++) { syslog(LOG_INFO, "==Note section prog_note:%d type:0x%X offset:0x%X size:0x%X (%dbytes)", - i, - p_proc->m_pPhdr[i].p_type, - p_proc->m_pPhdr[i].p_offset, - p_proc->m_pPhdr[i].p_filesz, - p_proc->m_pPhdr[i].p_filesz); + i, + p_proc->m_pPhdr[i].p_type, + p_proc->m_pPhdr[i].p_offset, + p_proc->m_pPhdr[i].p_filesz, + p_proc->m_pPhdr[i].p_filesz); if (p_proc->m_pPhdr[i].p_type == PT_NOTE) break; @@ -87,34 +84,30 @@ int getNotePageIndex(proc_info_t* p_proc) return i == p_proc->m_Ehdr.e_phnum ? CDH_NOK : i; } -cdh_status_t read_notes(proc_info_t* p_proc) +cdh_status_t read_notes(proc_info_t *p_proc) { int prog_note = getNotePageIndex(p_proc); -// p_proc->m_note_page_size = 0; +/* p_proc->m_note_page_size = 0; */ p_proc->m_Nhdr = NULL; - // note page not found, abort - if (prog_note < 0) - { + /* note page not found, abort */ + if (prog_note < 0) { syslog(LOG_ERR, "Cannot find note header page index"); return CDH_NOK; } - // Move to NOTE header position - if (stream_move_to_offest(&p_proc->streamer, p_proc->m_pPhdr[prog_note].p_offset) != CDH_OK) - { + /* Move to NOTE header position */ + if (stream_move_to_offest(&p_proc->streamer, p_proc->m_pPhdr[prog_note].p_offset) != CDH_OK) { syslog(LOG_ERR, "Cannot move to note header"); return CDH_NOK; } - if ((p_proc->m_Nhdr = (char*) malloc(p_proc->m_pPhdr[prog_note].p_filesz)) == NULL) - { + if ((p_proc->m_Nhdr = (char *)malloc(p_proc->m_pPhdr[prog_note].p_filesz)) == NULL) { syslog(LOG_ERR, "Cannot allocate Nhdr memory (note size %d bytes)", p_proc->m_pPhdr[prog_note].p_filesz); return CDH_NOK; } - if (stream_read(&p_proc->streamer, p_proc->m_Nhdr, p_proc->m_pPhdr[prog_note].p_filesz) != CDH_OK) - { + if (stream_read(&p_proc->streamer, p_proc->m_Nhdr, p_proc->m_pPhdr[prog_note].p_filesz) != CDH_OK) { syslog(LOG_ERR, "Cannot read note header"); return CDH_NOK; } @@ -124,76 +117,68 @@ cdh_status_t read_notes(proc_info_t* p_proc) return CDH_OK; } -cdh_status_t init_coredump(proc_info_t* p_proc) +cdh_status_t init_coredump(proc_info_t *p_proc) { if (p_proc == NULL) return CDH_NOK; - if (p_proc->can_create_coredump) - { + if (p_proc->can_create_coredump) { char l_dst_filename[CORE_MAX_FILENAME_LENGTH]; snprintf(l_dst_filename, sizeof(l_dst_filename), CORE_FILE_PATTERN, - CORE_TMP_DIRECTORY, - p_proc->timestamp, - p_proc->name, - p_proc->pid); + CORE_TMP_DIRECTORY, + p_proc->timestamp, + p_proc->name, + p_proc->pid); stream_init(&p_proc->streamer, 0, l_dst_filename); } - else - { + else { stream_init(&p_proc->streamer, 0, NULL); } return CDH_OK; } -cdh_status_t close_coredump(proc_info_t* p_proc) +cdh_status_t close_coredump(proc_info_t *p_proc) { stream_close(&p_proc->streamer); return CDH_OK; } -cdh_status_t treat_coredump(proc_info_t* p_proc) +cdh_status_t treat_coredump(proc_info_t *p_proc) { cdh_status_t ret = CDH_OK; - // open src and dest files, allocate read buffer - if (init_coredump(p_proc) != CDH_OK) - { + /* open src and dest files, allocate read buffer */ + if (init_coredump(p_proc) != CDH_OK) { syslog(LOG_ERR, "cannot init coredump system"); ret = CDH_NOK; goto finished; } - if (read_elf_headers(p_proc) == CDH_OK) - { - // TODO: No NOTES here leads to crash elsewhere!!! dlt_cdh_crashid.c: around line 76 - if (read_notes(p_proc) != CDH_OK) - { + if (read_elf_headers(p_proc) == CDH_OK) { + /* TODO: No NOTES here leads to crash elsewhere!!! dlt_cdh_crashid.c: around line 76 */ + if (read_notes(p_proc) != CDH_OK) { syslog(LOG_ERR, "cannot read NOTES"); ret = CDH_NOK; goto finished; } } - else - { - if (read_elf_headers(p_proc) != CDH_OK) - { - syslog(LOG_ERR, "cannot read ELF header"); - ret = CDH_NOK; - goto finished; - } + else if (read_elf_headers(p_proc) != CDH_OK) { + syslog(LOG_ERR, "cannot read ELF header"); + ret = CDH_NOK; + goto finished; } - finished: - // In all cases, we try to finish to read/compress the coredump until the end +finished: + + /* In all cases, we try to finish to read/compress the coredump until the end */ if (stream_finish(&p_proc->streamer) != CDH_OK) syslog(LOG_ERR, "cannot finish coredump compression"); - // In all cases, let's close the files + /* In all cases, let's close the files */ if (close_coredump(p_proc) != CDH_OK) syslog(LOG_ERR, "cannot close coredump system"); diff --git a/src/core_dump_handler/dlt_cdh_cpuinfo.h b/src/core_dump_handler/dlt_cdh_cpuinfo.h index 49fc6d0..071edac 100644 --- a/src/core_dump_handler/dlt_cdh_cpuinfo.h +++ b/src/core_dump_handler/dlt_cdh_cpuinfo.h @@ -30,6 +30,6 @@ #include "dlt_cdh.h" -void get_registers(prstatus_t* prstatus, cdh_registers_t* registers); +void get_registers(prstatus_t *prstatus, cdh_registers_t *registers); #endif /* DLT_CDH_CPUINFO_H */ diff --git a/src/core_dump_handler/dlt_cdh_crashid.c b/src/core_dump_handler/dlt_cdh_crashid.c index 905ba8b..ffd2aba 100644 --- a/src/core_dump_handler/dlt_cdh_crashid.c +++ b/src/core_dump_handler/dlt_cdh_crashid.c @@ -39,21 +39,21 @@ #include "dlt_cdh_cpuinfo.h" #ifdef HAS_CITYHASH_C -#include "city_c.h" +# include "city_c.h" #endif -//ARM32 specific -//#define REG_FRAME_POINTER 11 -//#define REG_INSTR_POINTER 12 -//#define REG_STACK_POINTER 13 -//#define REG_LINK_REGISTER 14 -//#define REG_PROC_COUNTER 15 +/*ARM32 specific */ +/*#define REG_FRAME_POINTER 11 */ +/*#define REG_INSTR_POINTER 12 */ +/*#define REG_STACK_POINTER 13 */ +/*#define REG_LINK_REGISTER 14 */ +/*#define REG_PROC_COUNTER 15 */ #ifdef HAS_CITYHASH_C -static cdh_status_t crashid_cityhash(proc_info_t* p_proc); +static cdh_status_t crashid_cityhash(proc_info_t *p_proc); #endif -cdh_status_t get_phdr_num(proc_info_t* p_proc, unsigned int p_address, int *phdr_num) +cdh_status_t get_phdr_num(proc_info_t *p_proc, unsigned int p_address, int *phdr_num) { int i = 0; @@ -61,40 +61,35 @@ cdh_status_t get_phdr_num(proc_info_t* p_proc, unsigned int p_address, int *phdr return CDH_NOK; for (i = 0; i < p_proc->m_Ehdr.e_phnum; i++) - { - if (p_proc->m_pPhdr[i].p_vaddr < p_address - && p_proc->m_pPhdr[i].p_vaddr + p_proc->m_pPhdr[i].p_memsz > p_address) - { + if ((p_proc->m_pPhdr[i].p_vaddr < p_address) + && (p_proc->m_pPhdr[i].p_vaddr + p_proc->m_pPhdr[i].p_memsz > p_address)) { *phdr_num = i; return CDH_OK; } - } *phdr_num = -1; return CDH_NOK; } -// Thanks to libunwind for the following definitions, which helps to -#define ALIGN(x,a) (((x)+(a)-1UL)&~((a)-1UL)) +/* Thanks to libunwind for the following definitions, which helps to */ +#define ALIGN(x, a) (((x) + (a) - 1UL) & ~((a) - 1UL)) #define NOTE_SIZE(_hdr) (sizeof (_hdr) + ALIGN((_hdr).n_namesz, 4) + (_hdr).n_descsz) -cdh_status_t get_crashed_registers(proc_info_t* p_proc) +cdh_status_t get_crashed_registers(proc_info_t *p_proc) { - int found = CDH_NOK; // CDH_OK, when we find the page note associated to PID of crashed process + int found = CDH_NOK; /* CDH_OK, when we find the page note associated to PID of crashed process */ unsigned int offset = 0; - // TODO: if no notes were found m_note_page_size was not set to 0 which leads to a crash in this loop because it is then used - // uninitialised here => this is an x86_64 issue - while (found != CDH_OK && offset < p_proc->m_note_page_size) - { - // Crash mentioned in TODO dlt_cdh_coredump.c line 163 - ELF_Nhdr* ptr_note = (ELF_Nhdr*) (p_proc->m_Nhdr + offset); + /* TODO: if no notes were found m_note_page_size was not set to 0 which leads to a crash in this loop because it is then used */ + /* uninitialised here => this is an x86_64 issue */ + while (found != CDH_OK && offset < p_proc->m_note_page_size) { + /* Crash mentioned in TODO dlt_cdh_coredump.c line 163 */ + ELF_Nhdr *ptr_note = (ELF_Nhdr *)(p_proc->m_Nhdr + offset); - if (ptr_note->n_type == NT_PRSTATUS) - { - // The first PRSTATUS note is the one of the crashed thread - prstatus_t* prstatus = (prstatus_t*) ((char*) ptr_note + sizeof(ELF_Nhdr) + ALIGN(ptr_note->n_namesz, 4)); + if (ptr_note->n_type == NT_PRSTATUS) { + /* The first PRSTATUS note is the one of the crashed thread */ + prstatus_t *prstatus = (prstatus_t *)((char *)ptr_note + sizeof(ELF_Nhdr) + ALIGN(ptr_note->n_namesz, 4)); p_proc->m_crashed_pid = prstatus->pr_pid; @@ -110,35 +105,35 @@ cdh_status_t get_crashed_registers(proc_info_t* p_proc) #ifdef HAS_CITYHASH_C -cdh_status_t crashid_cityhash(proc_info_t* p_proc) +cdh_status_t crashid_cityhash(proc_info_t *p_proc) { -#define CRASHID_BUF_SIZE MAX_PROC_NAME_LENGTH+sizeof(uint64_t) +# define CRASHID_BUF_SIZE MAX_PROC_NAME_LENGTH + sizeof(uint64_t) char cityhash_in[CRASHID_BUF_SIZE]; - uint64_t cityhash_result=0; + uint64_t cityhash_result = 0; memcpy(cityhash_in, p_proc->name, MAX_PROC_NAME_LENGTH); - memcpy(cityhash_in+MAX_PROC_NAME_LENGTH, &p_proc->m_crashid_phase1, sizeof(uint64_t)); + memcpy(cityhash_in + MAX_PROC_NAME_LENGTH, &p_proc->m_crashid_phase1, sizeof(uint64_t)); cityhash_result = CityHash64(cityhash_in, CRASHID_BUF_SIZE); memcpy(p_proc->m_crashid, &cityhash_result, sizeof(uint64_t)); return CDH_OK; -#undef CRASHID_BUF_SIZE +# undef CRASHID_BUF_SIZE } -#endif // HAS_CITYHASH_C +#endif /* HAS_CITYHASH_C */ -cdh_status_t create_crashid(proc_info_t* p_proc) +cdh_status_t create_crashid(proc_info_t *p_proc) { uint32_t final_lr = 0; uint32_t final_pc = 0; int pc_phnum = 0; int lr_phnum = 0; - // translate address from virtual address (process point of view) to offset in the stack memory page + /* translate address from virtual address (process point of view) to offset in the stack memory page */ #define ADDRESS_REBASE(__x, __phdr_num) (__x - p_proc->m_pPhdr[__phdr_num].p_vaddr) - // read value in the stack at position offset: +/- sizeof(), depends on stack growing upward or downward -#define READ_STACK_VALUE(__offset, __type) (*(__type*)(stack_page+__offset-sizeof(__type))) + /* read value in the stack at position offset: +/- sizeof(), depends on stack growing upward or downward */ +#define READ_STACK_VALUE(__offset, __type) (*(__type *)(stack_page + __offset - sizeof(__type))) get_phdr_num(p_proc, p_proc->m_registers.pc, &pc_phnum); final_pc = ADDRESS_REBASE(p_proc->m_registers.pc, pc_phnum); @@ -149,9 +144,9 @@ cdh_status_t create_crashid(proc_info_t* p_proc) final_lr = ADDRESS_REBASE(p_proc->m_registers.lr, lr_phnum); p_proc->m_crashid_phase1 = p_proc->signal << 24; - p_proc->m_crashid_phase1 |= (uint64_t) final_lr; + p_proc->m_crashid_phase1 |= (uint64_t)final_lr; p_proc->m_crashid_phase1 <<= 32; - p_proc->m_crashid_phase1 |= (uint64_t) final_pc; + p_proc->m_crashid_phase1 |= (uint64_t)final_pc; #ifdef HAS_CITYHASH_C crashid_cityhash(p_proc); @@ -160,44 +155,41 @@ cdh_status_t create_crashid(proc_info_t* p_proc) #endif syslog(LOG_INFO, - "Crash in \"%s\", thread=\"%s\", pid=%d, crashID=%"PRIx64", based on signal=%d, PC=0x%x, caller=0x%x", - p_proc->name, - p_proc->threadname, - p_proc->pid, - *((uint64_t*) p_proc->m_crashid), - p_proc->signal, - final_pc, final_lr - ); + "Crash in \"%s\", thread=\"%s\", pid=%d, crashID=%" PRIx64 ", based on signal=%d, PC=0x%x, caller=0x%x", + p_proc->name, + p_proc->threadname, + p_proc->pid, + *((uint64_t *)p_proc->m_crashid), + p_proc->signal, + final_pc, final_lr + ); return CDH_OK; } -int write_crashid_to_filesystem(proc_info_t* p_proc) +int write_crashid_to_filesystem(proc_info_t *p_proc) { - FILE* crashid_file = NULL; + FILE *crashid_file = NULL; - if ((crashid_file = fopen(CRASHID_FILE, "wt")) == NULL) - { + if ((crashid_file = fopen(CRASHID_FILE, "wt")) == NULL) { syslog(LOG_ERR, "(pid=%d) cannot write crashid to %s: %s", p_proc->pid, CRASHID_FILE, strerror(errno)); return CDH_NOK; } - fprintf(crashid_file, "%"PRIx64, *(uint64_t*) p_proc->m_crashid); + fprintf(crashid_file, "%" PRIx64, *(uint64_t *)p_proc->m_crashid); fclose(crashid_file); return CDH_OK; } -cdh_status_t treat_crash_data(proc_info_t* p_proc) +cdh_status_t treat_crash_data(proc_info_t *p_proc) { - if (get_crashed_registers(p_proc) != CDH_OK) - { + if (get_crashed_registers(p_proc) != CDH_OK) { syslog(LOG_ERR, "registers not found in notes"); return CDH_NOK; } - if (create_crashid(p_proc) != CDH_OK) - { + if (create_crashid(p_proc) != CDH_OK) { syslog(LOG_ERR, "crashid not generated"); return CDH_NOK; } diff --git a/src/core_dump_handler/dlt_cdh_streamer.c b/src/core_dump_handler/dlt_cdh_streamer.c index 00b6d04..24bcde2 100644 --- a/src/core_dump_handler/dlt_cdh_streamer.c +++ b/src/core_dump_handler/dlt_cdh_streamer.c @@ -32,13 +32,12 @@ #include <syslog.h> #include "dlt_cdh_streamer.h" -#define Z_CHUNK_SZ 1024*128 +#define Z_CHUNK_SZ 1024 * 128 #define Z_MODE_STR "wb1" -cdh_status_t stream_init(file_streamer_t* p_fs, const char* p_src_fname, const char* p_dst_fname) +cdh_status_t stream_init(file_streamer_t *p_fs, const char *p_src_fname, const char *p_dst_fname) { - if (p_fs == NULL) - { + if (p_fs == NULL) { syslog(LOG_ERR, "Internal pointer error in 'stream_init'"); return CDH_NOK; } @@ -46,17 +45,15 @@ cdh_status_t stream_init(file_streamer_t* p_fs, const char* p_src_fname, const c memset(p_fs, 0, sizeof(file_streamer_t)); /* Allow to not save the coredump */ - if (p_dst_fname == NULL) - { + if (p_dst_fname == NULL) { p_fs->gz_dst_file = 0; } - else - { - // Create output file + else { + /* Create output file */ p_fs->gz_dst_file = gzopen(p_dst_fname, Z_MODE_STR); - if (p_fs->gz_dst_file == Z_NULL) - { - //return CDH_NOK; + + if (p_fs->gz_dst_file == Z_NULL) { + /*return CDH_NOK; */ syslog(LOG_ERR, "Cannot open output filename <%s>. %s", p_dst_fname, strerror(errno)); p_fs->gz_dst_file = 0; @@ -66,23 +63,17 @@ cdh_status_t stream_init(file_streamer_t* p_fs, const char* p_src_fname, const c if (p_fs->gz_dst_file == Z_NULL) syslog(LOG_WARNING, "The coredump will be processed, but not written"); - // Open input file - if (p_src_fname == NULL) - { + /* Open input file */ + if (p_src_fname == NULL) { p_fs->stream = stdin; } - else - { - if ((p_fs->stream = fopen(p_src_fname, "rb")) == NULL) - { - syslog(LOG_ERR, "Cannot open filename <%s>. %s", p_src_fname, strerror(errno)); - return CDH_NOK; - } + else if ((p_fs->stream = fopen(p_src_fname, "rb")) == NULL) { + syslog(LOG_ERR, "Cannot open filename <%s>. %s", p_src_fname, strerror(errno)); + return CDH_NOK; } - // Allocate read buffer - if ((p_fs->read_buf = (unsigned char*) malloc(Z_CHUNK_SZ)) == NULL) - { + /* Allocate read buffer */ + if ((p_fs->read_buf = (unsigned char *)malloc(Z_CHUNK_SZ)) == NULL) { syslog(LOG_ERR, "Cannot allocate %d bytes for read buffer. %s", Z_CHUNK_SZ, strerror(errno)); return CDH_NOK; } @@ -90,29 +81,25 @@ cdh_status_t stream_init(file_streamer_t* p_fs, const char* p_src_fname, const c return CDH_OK; } -cdh_status_t stream_close(file_streamer_t* p_fs) +cdh_status_t stream_close(file_streamer_t *p_fs) { - if (p_fs == NULL) - { + if (p_fs == NULL) { syslog(LOG_ERR, "Internal pointer error in 'stream_close'"); return CDH_NOK; } - if (p_fs->gz_dst_file != NULL) - { + if (p_fs->gz_dst_file != NULL) { gzflush(p_fs->gz_dst_file, Z_FINISH); gzclose(p_fs->gz_dst_file); p_fs->gz_dst_file = NULL; } - if (p_fs->stream != NULL) - { + if (p_fs->stream != NULL) { fclose(p_fs->stream); p_fs->stream = NULL; } - if (p_fs->read_buf != NULL) - { + if (p_fs->read_buf != NULL) { free(p_fs->read_buf); p_fs->read_buf = NULL; } @@ -120,24 +107,21 @@ cdh_status_t stream_close(file_streamer_t* p_fs) return CDH_OK; } -cdh_status_t stream_read(file_streamer_t* p_fs, void* p_buf, unsigned int p_size) +cdh_status_t stream_read(file_streamer_t *p_fs, void *p_buf, unsigned int p_size) { unsigned int byte_read = 0; - if (p_fs == NULL) - { + if (p_fs == NULL) { syslog(LOG_ERR, "Internal pointer error in 'stream_read'"); return CDH_NOK; } - if (p_buf == NULL) - { + if (p_buf == NULL) { syslog(LOG_ERR, "Internal buffer pointer error in 'stream_read'"); return CDH_NOK; } - if ((byte_read = fread(p_buf, 1, p_size, p_fs->stream)) != p_size) - { + if ((byte_read = fread(p_buf, 1, p_size, p_fs->stream)) != p_size) { syslog(LOG_WARNING, "Cannot read %d bytes from src. %s", p_size, strerror(errno)); return CDH_NOK; } @@ -150,24 +134,22 @@ cdh_status_t stream_read(file_streamer_t* p_fs, void* p_buf, unsigned int p_size return CDH_OK; } -int stream_finish(file_streamer_t* p_fs) +int stream_finish(file_streamer_t *p_fs) { - if (p_fs == NULL || p_fs->stream == NULL) - { + if ((p_fs == NULL) || (p_fs->stream == NULL)) { syslog(LOG_ERR, "Internal pointer error in 'stream_move_ahead'"); return CDH_NOK; } - while (!feof(p_fs->stream)) - { + while (!feof(p_fs->stream)) { size_t read_bytes = fread(p_fs->read_buf, 1, Z_CHUNK_SZ, p_fs->stream); if (p_fs->gz_dst_file != NULL) gzwrite(p_fs->gz_dst_file, p_fs->read_buf, read_bytes); p_fs->offset += read_bytes; - if (ferror(p_fs->stream)) - { + + if (ferror(p_fs->stream)) { syslog(LOG_WARNING, "Error reading from the src stream: %s", strerror(errno)); return CDH_NOK; } @@ -176,12 +158,11 @@ int stream_finish(file_streamer_t* p_fs) return CDH_OK; } -int stream_move_to_offest(file_streamer_t* p_fs, unsigned int p_offset) +int stream_move_to_offest(file_streamer_t *p_fs, unsigned int p_offset) { int bytes_to_read = 0; - if (p_fs == NULL) - { + if (p_fs == NULL) { syslog(LOG_ERR, "Internal pointer error in 'stream_move_to_offest'"); return CDH_NOK; } @@ -191,23 +172,20 @@ int stream_move_to_offest(file_streamer_t* p_fs, unsigned int p_offset) return stream_move_ahead(p_fs, bytes_to_read); } -int stream_move_ahead(file_streamer_t* p_fs, unsigned int p_nbbytes) +int stream_move_ahead(file_streamer_t *p_fs, unsigned int p_nbbytes) { int bytes_to_read = p_nbbytes; - if (p_fs == NULL) - { + if (p_fs == NULL) { syslog(LOG_ERR, "Internal pointer error in 'stream_move_ahead'"); return CDH_NOK; } - while (bytes_to_read > 0) - { + while (bytes_to_read > 0) { size_t chunk_size = bytes_to_read > Z_CHUNK_SZ ? Z_CHUNK_SZ : bytes_to_read; size_t read_bytes = fread(p_fs->read_buf, 1, chunk_size, p_fs->stream); - if (read_bytes != chunk_size) - { + if (read_bytes != chunk_size) { syslog(LOG_WARNING, "Cannot move ahead by %d bytes from src. Read %lu bytes", p_nbbytes, read_bytes); return CDH_NOK; } @@ -223,10 +201,9 @@ int stream_move_ahead(file_streamer_t* p_fs, unsigned int p_nbbytes) return CDH_OK; } -unsigned int stream_get_offset(file_streamer_t* p_fs) +unsigned int stream_get_offset(file_streamer_t *p_fs) { - if (p_fs == NULL) - { + if (p_fs == NULL) { syslog(LOG_ERR, "Internal pointer error in 'stream_get_offset'"); return CDH_NOK; } diff --git a/src/core_dump_handler/dlt_cdh_streamer.h b/src/core_dump_handler/dlt_cdh_streamer.h index b73688e..3c6fe2d 100644 --- a/src/core_dump_handler/dlt_cdh_streamer.h +++ b/src/core_dump_handler/dlt_cdh_streamer.h @@ -35,19 +35,19 @@ typedef struct { - FILE* stream; + FILE *stream; unsigned int offset; gzFile gz_dst_file; - unsigned char* read_buf; + unsigned char *read_buf; } file_streamer_t; -cdh_status_t stream_init(file_streamer_t* p_fs, const char* p_src_fname, const char* p_dst_fname); -cdh_status_t stream_close(file_streamer_t* p_fs); -cdh_status_t stream_read(file_streamer_t* p_fs, void* p_buf, unsigned int p_size); -cdh_status_t stream_finish(file_streamer_t* p_fs); -cdh_status_t stream_move_to_offest(file_streamer_t* p_fs, unsigned int p_offset); -cdh_status_t stream_move_ahead(file_streamer_t* p_fs, unsigned int p_nbbytes); -unsigned int stream_get_offset(file_streamer_t* p_fs); +cdh_status_t stream_init(file_streamer_t *p_fs, const char *p_src_fname, const char *p_dst_fname); +cdh_status_t stream_close(file_streamer_t *p_fs); +cdh_status_t stream_read(file_streamer_t *p_fs, void *p_buf, unsigned int p_size); +cdh_status_t stream_finish(file_streamer_t *p_fs); +cdh_status_t stream_move_to_offest(file_streamer_t *p_fs, unsigned int p_offset); +cdh_status_t stream_move_ahead(file_streamer_t *p_fs, unsigned int p_nbbytes); +unsigned int stream_get_offset(file_streamer_t *p_fs); #endif /* #ifndef DLT_CDH_STREAMER_H */ diff --git a/src/core_dump_handler/i686/dlt_cdh_cpuinfo.c b/src/core_dump_handler/i686/dlt_cdh_cpuinfo.c index 2e547a2..aee7441 100644 --- a/src/core_dump_handler/i686/dlt_cdh_cpuinfo.c +++ b/src/core_dump_handler/i686/dlt_cdh_cpuinfo.c @@ -27,11 +27,11 @@ #include "../dlt_cdh_cpuinfo.h" -void get_registers(prstatus_t* prstatus, cdh_registers_t* registers) +void get_registers(prstatus_t *prstatus, cdh_registers_t *registers) { - struct user_regs_struct* ptr_reg = (struct user_regs_struct*) prstatus->pr_reg; + struct user_regs_struct *ptr_reg = (struct user_regs_struct *)prstatus->pr_reg; - registers->pc = ptr_reg->ecx; // [REG_PROC_COUNTER]; - registers->ip = ptr_reg->eip; // [REG_INSTR_POINTER]; - registers->lr = ptr_reg->ebp; // [REG_LINK_REGISTER]; + registers->pc = ptr_reg->ecx; /* [REG_PROC_COUNTER]; */ + registers->ip = ptr_reg->eip; /* [REG_INSTR_POINTER]; */ + registers->lr = ptr_reg->ebp; /* [REG_LINK_REGISTER]; */ } diff --git a/src/core_dump_handler/x86_64/dlt_cdh_cpuinfo.c b/src/core_dump_handler/x86_64/dlt_cdh_cpuinfo.c index 6e526ea..f18720e 100644 --- a/src/core_dump_handler/x86_64/dlt_cdh_cpuinfo.c +++ b/src/core_dump_handler/x86_64/dlt_cdh_cpuinfo.c @@ -27,11 +27,11 @@ #include "../dlt_cdh_cpuinfo.h" -void get_registers(prstatus_t* prstatus, cdh_registers_t* registers) +void get_registers(prstatus_t *prstatus, cdh_registers_t *registers) { - struct user_regs_struct* ptr_reg = (struct user_regs_struct*) prstatus->pr_reg; + struct user_regs_struct *ptr_reg = (struct user_regs_struct *)prstatus->pr_reg; - registers->pc = ptr_reg->rcx; // [REG_PROC_COUNTER]; - registers->ip = ptr_reg->rip; // [REG_INSTR_POINTER]; - registers->lr = ptr_reg->rsp; // [REG_LINK_REGISTER]; + registers->pc = ptr_reg->rcx; /* [REG_PROC_COUNTER]; */ + registers->ip = ptr_reg->rip; /* [REG_INSTR_POINTER]; */ + registers->lr = ptr_reg->rsp; /* [REG_LINK_REGISTER]; */ } |