]>
Commit | Line | Data |
---|---|---|
1 | // | |
2 | // Kernel.cpp | |
3 | // KDBG | |
4 | // | |
5 | // Created by James McIlree on 10/24/12. | |
6 | // Copyright (c) 2014 Apple. All rights reserved. | |
7 | // | |
8 | ||
9 | #include <CPPUtil/CPPUtil.h> | |
10 | ||
11 | using namespace util; | |
12 | ||
13 | #include "KDebug.h" | |
14 | ||
15 | KDState KDBG::state() | |
16 | { | |
17 | static_assert(sizeof(KDState) == sizeof(kbufinfo_t), "Types must be the same size"); | |
18 | ||
19 | KDState state; | |
20 | int mib[3]; | |
21 | size_t len = sizeof(state); | |
22 | ||
23 | mib[0] = CTL_KERN; | |
24 | mib[1] = KERN_KDEBUG; | |
25 | mib[2] = KERN_KDGETBUF; | |
26 | ||
27 | if (sysctl(mib, 3, &state, &len, 0, 0) < 0) { | |
28 | DEBUG_ONLY(log_msg(ASL_LEVEL_ERR, "trace facility failure, KERN_KDGETBUF: %s\n", strerror(errno))); | |
29 | THROW("trace facility failure, KERN_KDGETBUF: %s\n" << strerror(errno)); | |
30 | } | |
31 | ||
32 | return state; | |
33 | } | |
34 | ||
35 | bool KDBG::reset() | |
36 | { | |
37 | int mib[3]; | |
38 | ||
39 | mib[0] = CTL_KERN; | |
40 | mib[1] = KERN_KDEBUG; | |
41 | mib[2] = KERN_KDREMOVE; | |
42 | if (sysctl(mib, 3, NULL, NULL, NULL, 0) < 0) { | |
43 | DEBUG_ONLY(log_msg(ASL_LEVEL_WARNING, "trace facility failure, KERN_KDREMOVE: %s\n", strerror(errno))); | |
44 | return false; | |
45 | } | |
46 | ||
47 | return true; | |
48 | } | |
49 | ||
50 | bool KDBG::set_buffer_capacity(uint32_t capacity) | |
51 | { | |
52 | int mib[4]; | |
53 | ||
54 | mib[0] = CTL_KERN; | |
55 | mib[1] = KERN_KDEBUG; | |
56 | mib[2] = KERN_KDSETBUF; | |
57 | mib[3] = (int)capacity; | |
58 | ||
59 | if (sysctl(mib, 4, NULL, NULL, NULL, 0) < 0) { | |
60 | DEBUG_ONLY(log_msg(ASL_LEVEL_WARNING, "trace facility failure, KERN_KDSETBUF: %s\n", strerror(errno))); | |
61 | return false; | |
62 | } | |
63 | ||
64 | return true; | |
65 | } | |
66 | ||
67 | bool KDBG::set_nowrap(bool is_nowrap) | |
68 | { | |
69 | int mib[4]; | |
70 | ||
71 | mib[0] = CTL_KERN; | |
72 | mib[1] = KERN_KDEBUG; | |
73 | mib[2] = is_nowrap ? KERN_KDEFLAGS : KERN_KDDFLAGS; | |
74 | mib[3] = KDBG_NOWRAP; | |
75 | ||
76 | if (sysctl(mib, 4, NULL, NULL, NULL, 0) < 0) { | |
77 | DEBUG_ONLY(log_msg(ASL_LEVEL_WARNING, "trace facility failure, KDBG_NOWRAP: %s\n", strerror(errno))); | |
78 | return false; | |
79 | } | |
80 | ||
81 | return true; | |
82 | } | |
83 | ||
84 | bool KDBG::initialize_buffers() | |
85 | { | |
86 | int mib[3]; | |
87 | ||
88 | mib[0] = CTL_KERN; | |
89 | mib[1] = KERN_KDEBUG; | |
90 | mib[2] = KERN_KDSETUP; | |
91 | ||
92 | if (sysctl(mib, 3, NULL, NULL, NULL, 0) < 0) { | |
93 | DEBUG_ONLY(log_msg(ASL_LEVEL_WARNING, "trace facility failure, KERN_KDSETUP: %s\n", strerror(errno))); | |
94 | return false; | |
95 | } | |
96 | return true; | |
97 | } | |
98 | ||
99 | ||
100 | // | |
101 | // Legal values are: | |
102 | // | |
103 | // KDEBUG_TRACE (full set of tracepoints) | |
104 | // KDEBUG_PPT (subset of tracepoints to minimize performance impact) | |
105 | // 0 (Disable) | |
106 | // | |
107 | bool KDBG::set_enabled(uint32_t value) | |
108 | { | |
109 | int mib[4]; | |
110 | ||
111 | mib[0] = CTL_KERN; | |
112 | mib[1] = KERN_KDEBUG; | |
113 | mib[2] = KERN_KDENABLE; | |
114 | mib[3] = value; | |
115 | ||
116 | if (sysctl(mib, 4, NULL, NULL, NULL, 0) < 0) { | |
117 | DEBUG_ONLY(log_msg(ASL_LEVEL_WARNING, "trace facility failure, KERN_KDENABLE: %s\n", strerror(errno))); | |
118 | return false; | |
119 | } | |
120 | return true; | |
121 | } | |
122 | ||
123 | std::vector<KDCPUMapEntry> KDBG::cpumap() | |
124 | { | |
125 | std::vector<KDCPUMapEntry> cpumap; | |
126 | ||
127 | /* | |
128 | * To fit in the padding space of a VERSION1 file, the max possible | |
129 | * cpumap size is one page. | |
130 | */ | |
131 | if (kd_cpumap_header* cpumap_header = (kd_cpumap_header*)malloc(PAGE_SIZE)) { | |
132 | int mib[3]; | |
133 | mib[0] = CTL_KERN; | |
134 | mib[1] = KERN_KDEBUG; | |
135 | mib[2] = KERN_KDCPUMAP; | |
136 | ||
137 | size_t temp = PAGE_SIZE; | |
138 | if (sysctl(mib, 3, cpumap_header, &temp, NULL, 0) == 0) { | |
139 | if (PAGE_SIZE >= temp) { | |
140 | if (cpumap_header->version_no == RAW_VERSION1) { | |
141 | cpumap.resize(cpumap_header->cpu_count); | |
142 | memcpy(cpumap.data(), &cpumap_header[1], cpumap_header->cpu_count * sizeof(KDCPUMapEntry)); | |
143 | } | |
144 | } | |
145 | } | |
146 | free(cpumap_header); | |
147 | } | |
148 | ||
149 | return cpumap; | |
150 | } | |
151 | ||
152 | bool KDBG::write_maps(int fd) | |
153 | { | |
154 | int mib[4]; | |
155 | ||
156 | mib[0] = CTL_KERN; | |
157 | mib[1] = KERN_KDEBUG; | |
158 | mib[2] = KERN_KDWRITEMAP; | |
159 | mib[3] = fd; | |
160 | ||
161 | if (sysctl(mib, 4, NULL, NULL, NULL, 0) < 0) | |
162 | return false; | |
163 | ||
164 | return true; | |
165 | } | |
166 | ||
167 | int KDBG::write_events(int fd) | |
168 | { | |
169 | int mib[4]; | |
170 | size_t events_written = 0; | |
171 | ||
172 | mib[0] = CTL_KERN; | |
173 | mib[1] = KERN_KDEBUG; | |
174 | mib[2] = KERN_KDWRITETR; | |
175 | mib[3] = fd; | |
176 | ||
177 | if (sysctl(mib, 4, NULL, &events_written, NULL, 0) < 0) | |
178 | return -1; | |
179 | ||
180 | return (int)events_written; | |
181 | } |