]>
git.saurik.com Git - apple/xnu.git/blob - san/tools/ksancov.c
4 $
(MAKEFILE_LIST
:.c
= ):
11 * Copyright (c) 2019 Apple Inc. All rights reserved.
13 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
15 * This file contains Original Code and/or Modifications of Original Code
16 * as defined in and that are subject to the Apple Public Source License
17 * Version 2.0 (the 'License'). You may not use this file except in
18 * compliance with the License. The rights granted to you under the License
19 * may not be used to create, or enable the creation or redistribution of,
20 * unlawful or unlicensed copies of an Apple operating system, or to
21 * circumvent, violate, or enable the circumvention or violation of, any
22 * terms of an Apple operating system software license agreement.
24 * Please obtain a copy of the License at
25 * http://www.opensource.apple.com/apsl/ and read it before using this file.
27 * The Original Code and all software distributed under the License are
28 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
29 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
30 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
32 * Please see the License for the specific language governing rights and
33 * limitations under the License.
35 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
42 #include <stdatomic.h>
46 #include <sys/ioctl.h>
50 #include "../ksancov.h"
56 "usage: ./ksancov [OPTIONS]\n\n"
57 " -t | --trace use trace (PC log) mode [default]\n"
58 " -c | --counters use edge counter mode\n"
59 " -n | --entries <n> override max entries in trace log\n"
60 " -x | --exec <path> instrument execution of binary at <path>\n");
65 main(int argc
, char *argv
[])
67 struct ksancov_trace
*trace
= NULL
;
68 struct ksancov_counters
*counters
= NULL
;
69 struct ksancov_header
*header
= NULL
;
72 size_t max_entries
= 64UL * 1024;
74 bool docounters
= false;
76 struct option opts
[] = {
77 { "entries", required_argument
, NULL
, 'n' },
78 { "exec", required_argument
, NULL
, 'x' },
80 { "trace", no_argument
, NULL
, 't' },
81 { "counters", no_argument
, NULL
, 'c' },
87 while ((ch
= getopt_long(argc
, argv
, "tn:x:c", opts
, NULL
)) != -1) {
90 max_entries
= strtoul(optarg
, NULL
, 0);
113 perror("ksancov_open");
116 fprintf(stderr
, "opened ksancov on fd %i\n", fd
);
119 ret
= ksancov_map_edgemap(fd
, &e
, NULL
);
121 perror("ksancov map counters\n");
124 struct ksancov_edgemap
*map
= (void *)e
;
125 fprintf(stderr
, "nedges (edgemap) = %u\n", map
->nedges
);
128 ret
= ksancov_mode_counters(fd
);
130 perror("ksancov set mode\n");
134 ret
= ksancov_mode_trace(fd
, max_entries
);
136 perror("ksancov set mode\n");
141 ret
= ksancov_map(fd
, &addr
, &sz
);
143 perror("ksancov map");
146 fprintf(stderr
, "mapped to 0x%lx + %lu\n", addr
, sz
);
149 counters
= (void *)addr
;
150 fprintf(stderr
, "nedges (counters) = %u\n", counters
->nedges
);
152 trace
= (void *)addr
;
153 fprintf(stderr
, "maxpcs = %lu\n", ksancov_trace_max_pcs(trace
));
155 header
= (void *)addr
;
162 ret
= ksancov_thread_self(fd
);
164 perror("ksancov thread");
168 ksancov_reset(header
);
169 ksancov_start(header
);
170 ret
= execl(path
, path
, 0);
176 waitpid(pid
, NULL
, 0);
177 ksancov_stop(header
);
180 ret
= ksancov_thread_self(fd
);
182 perror("ksancov thread");
186 ksancov_reset(header
);
187 ksancov_start(header
);
188 int ppid
= getppid();
189 ksancov_stop(header
);
190 fprintf(stderr
, "ppid = %i\n", ppid
);
194 for (size_t i
= 0; i
< counters
->nedges
; i
++) {
195 size_t hits
= counters
->hits
[i
];
197 fprintf(stderr
, "0x%lx: %lu hits [idx %lu]\n", ksancov_edge_addr(map
, i
), hits
, i
);
201 size_t head
= ksancov_trace_head(trace
);
202 fprintf(stderr
, "head = %lu\n", head
);
203 for (uint32_t i
= 0; i
< head
; i
++) {
204 uintptr_t pc
= ksancov_trace_entry(trace
, i
);
205 fprintf(stderr
, "0x%lx\n", pc
);
210 fprintf(stderr
, "close = %i\n", ret
);