]> git.saurik.com Git - apple/system_cmds.git/blame - msa/WriteTraceFileAction.cpp
system_cmds-670.1.2.tar.gz
[apple/system_cmds.git] / msa / WriteTraceFileAction.cpp
CommitLineData
bd6521f0
A
1//
2// WriteTraceFileAction.cpp
3// system_cmds
4//
5// Created by James McIlree on 4/29/14.
6//
7//
8
9#include "global.h"
10
11static bool shouldProcessEvents;
12static uint32_t sigintCount;
13
14static bool start_tracing(Globals& globals)
15{
16 if (!KDBG::reset()) return false;
17 if (!KDBG::set_buffer_capacity(globals.trace_buffer_size())) return false;
18 if (!KDBG::set_nowrap(false)) return false;
19 if (!KDBG::initialize_buffers()) return false;
20 if (!KDBG::set_enabled(KDEBUG_ENABLE_TRACE)) return false;
21
22 return true;
23}
24
25static void end_tracing(void)
26{
27 KDBG::reset();
28}
29
30static void signal_handler_ctrl_C(int sig)
31{
32 shouldProcessEvents = false;
33 if (++sigintCount >= 5) {
34 // Not responding, nuke it from orbit.
35 exit(1);
36 }
37}
38
39void WriteTraceFileAction::execute(Globals& globals) {
40 FileDescriptor fd(open(_path.c_str(), O_TRUNC|O_WRONLY|O_CREAT, 0777));
41 if (!fd.is_open()) {
42 log_msg(ASL_LEVEL_ERR, "Unable to write to %s\n", _path.c_str());
43 return;
44 }
45
46 shouldProcessEvents = true;
47 sigintCount = 0;
48
49 VoucherContentSysctl contents(globals.should_trace_voucher_contents());
50
51 AbsTime t1 = AbsTime::now();
52 if (start_tracing(globals)) {
53 // We cannot write the "maps" until after tracing has started.
54 if (KDBG::write_maps(fd)) {
55 signal(SIGINT, signal_handler_ctrl_C);
56
57 while (shouldProcessEvents) {
58 int events_written = KDBG::write_events(fd);
59 AbsTime t2 = AbsTime::now();
60 if (events_written != -1) {
61 printf("wrote %d events - elapsed time = %.1f secs\n", events_written, (double)(t2 - t1).nano_time().value() / (double)NANOSECONDS_PER_SECOND);
62 } else {
63 log_msg(ASL_LEVEL_WARNING, "write events returned -1\n");
64 break;
65 }
66 t1 = t2;
67 }
68
69 signal(SIGINT, SIG_DFL);
70 }
71 }
72
73 end_tracing();
74}