]>
git.saurik.com Git - apple/system_cmds.git/blob - KDBG/KDBG.cpp
5 // Created by James McIlree on 10/24/12.
6 // Copyright (c) 2014 Apple. All rights reserved.
9 #include <CPPUtil/CPPUtil.h>
17 static_assert(sizeof(KDState
) == sizeof(kbufinfo_t
), "Types must be the same size");
21 size_t len
= sizeof(state
);
25 mib
[2] = KERN_KDGETBUF
;
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
));
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
)));
50 bool KDBG::set_buffer_capacity(uint32_t capacity
)
56 mib
[2] = KERN_KDSETBUF
;
57 mib
[3] = (int)capacity
;
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
)));
67 bool KDBG::set_nowrap(bool is_nowrap
)
73 mib
[2] = is_nowrap
? KERN_KDEFLAGS
: KERN_KDDFLAGS
;
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
)));
84 bool KDBG::initialize_buffers()
90 mib
[2] = KERN_KDSETUP
;
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
)));
103 // KDEBUG_TRACE (full set of tracepoints)
104 // KDEBUG_PPT (subset of tracepoints to minimize performance impact)
107 bool KDBG::set_enabled(uint32_t value
)
112 mib
[1] = KERN_KDEBUG
;
113 mib
[2] = KERN_KDENABLE
;
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
)));
123 std::vector
<KDCPUMapEntry
> KDBG::cpumap()
125 std::vector
<KDCPUMapEntry
> cpumap
;
128 * To fit in the padding space of a VERSION1 file, the max possible
129 * cpumap size is one page.
131 if (kd_cpumap_header
* cpumap_header
= (kd_cpumap_header
*)malloc(PAGE_SIZE
)) {
134 mib
[1] = KERN_KDEBUG
;
135 mib
[2] = KERN_KDCPUMAP
;
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
));
152 bool KDBG::write_maps(int fd
)
157 mib
[1] = KERN_KDEBUG
;
158 mib
[2] = KERN_KDWRITEMAP
;
161 if (sysctl(mib
, 4, NULL
, NULL
, NULL
, 0) < 0)
167 int KDBG::write_events(int fd
)
170 size_t events_written
= 0;
173 mib
[1] = KERN_KDEBUG
;
174 mib
[2] = KERN_KDWRITETR
;
177 if (sysctl(mib
, 4, NULL
, &events_written
, NULL
, 0) < 0)
180 return (int)events_written
;