]> git.saurik.com Git - apple/system_cmds.git/blob - KDBG/TraceDataHeader.hpp
ab733dd9868a42470481cb16369c96d9ec6c12a3
[apple/system_cmds.git] / KDBG / TraceDataHeader.hpp
1 //
2 // TraceDataHeader.hpp
3 // KDBG
4 //
5 // Created by James McIlree on 10/25/12.
6 // Copyright (c) 2014 Apple. All rights reserved.
7 //
8
9 //
10 // We have to specialize this, as the K64 min alignment is 4 bytes longer,
11 // to maintain 8 byte alignment for the uint64_t _TOD_secs.
12 //
13
14 template <typename KERNEL_SIZE> class TraceDataHeaderFields {};
15
16 template <>
17 class TraceDataHeaderFields<Kernel32> {
18 public:
19 uint32_t version;
20 uint32_t thread_count;
21 uint32_t TOD_secs_top_half;
22 uint32_t TOD_secs_bottom_half;
23 uint32_t TOD_usecs;
24
25 // NOTE! The compiler has shown a tendency to place this on non 8 byte
26 // aligned addresses when stack allocating. We need to construct the
27 // uint64_t values by logical-or and shifting, treating as a pointer
28 // will fail!
29
30 TraceDataHeaderFields(uint32_t v, uint32_t tc, uint64_t s, uint32_t us) :
31 version(v),
32 thread_count(tc),
33 TOD_usecs(us)
34 {
35 TOD_secs_top_half = (uint32_t)(s >> 32);
36 TOD_secs_bottom_half = (uint32_t)(s & 0xFFFFFFFF);
37 }
38
39 uint64_t TOD_secs() {
40 return ((uint64_t)TOD_secs_top_half << 32) | (uint64_t)TOD_secs_bottom_half;
41 }
42 };
43
44 template <>
45 class TraceDataHeaderFields<Kernel64> {
46 public:
47 uint32_t version;
48 uint32_t thread_count;
49 uint64_t _TOD_secs;
50 uint32_t TOD_usecs;
51 uint32_t _force_alignment; // Need to force 8 byte alignment in 32 bit code
52
53 TraceDataHeaderFields(uint32_t v, uint32_t tc, uint64_t s, uint32_t us) :
54 version(v),
55 thread_count(tc),
56 _TOD_secs(s),
57 TOD_usecs(us),
58 _force_alignment(0)
59 {
60 }
61
62 uint64_t TOD_secs() {
63 return _TOD_secs;
64 }
65 };
66
67 template <typename KERNEL_SIZE>
68 class TraceDataHeader {
69 private:
70 TraceDataHeaderFields<KERNEL_SIZE> _fields;
71
72 public:
73 TraceDataHeader() : _fields(0, 0, 0, 0) {}
74 TraceDataHeader(uint32_t v, uint32_t tc, uint64_t s, uint32_t us) : _fields(v, tc, s, us) {}
75
76 uint32_t version() const { return _fields.version; }
77 uint32_t thread_count() const { return _fields.thread_count; }
78 uint64_t TOD_secs() const { return _fields.TOD_secs(); }
79 uint32_t TOD_usecs() const { return _fields.TOD_usecs; }
80 };
81