blob: 26c5b4ac3510efd365dc855c2d38699d4b0e0d4a [file] [log] [blame]
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <fcntl.h>
19#include <getopt.h>
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070020#include <inttypes.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080021#include <signal.h>
22#include <stdarg.h>
23#include <stdbool.h>
24#include <stdio.h>
25#include <stdlib.h>
Elliott Hughes3da5d232015-01-25 08:35:20 -080026#include <string.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080027#include <sys/sendfile.h>
28#include <time.h>
29#include <zlib.h>
30
31#include <binder/IBinder.h>
32#include <binder/IServiceManager.h>
33#include <binder/Parcel.h>
34
35#include <cutils/properties.h>
36
37#include <utils/String8.h>
John Reck469a1942015-03-26 15:31:35 -070038#include <utils/Timers.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080039#include <utils/Trace.h>
40
41using namespace android;
42
43#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
44
Mohamad Ayyash26dbcbe2014-04-08 15:24:11 -070045enum { MAX_SYS_FILES = 10 };
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080046
47const char* k_traceTagsProperty = "debug.atrace.tags.enableflags";
Jamie Gennisf7f29c82013-03-27 15:50:58 -070048const char* k_traceAppCmdlineProperty = "debug.atrace.app_cmdlines";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080049
50typedef enum { OPT, REQ } requiredness ;
51
52struct TracingCategory {
53 // The name identifying the category.
54 const char* name;
55
56 // A longer description of the category.
57 const char* longname;
58
59 // The userland tracing tags that the category enables.
60 uint64_t tags;
61
62 // The fname==NULL terminated list of /sys/ files that the category
63 // enables.
64 struct {
65 // Whether the file must be writable in order to enable the tracing
66 // category.
67 requiredness required;
68
69 // The path to the enable file.
70 const char* path;
71 } sysfiles[MAX_SYS_FILES];
72};
73
74/* Tracing categories */
75static const TracingCategory k_categories[] = {
Jamie Gennisb2a89e32013-03-11 19:37:53 -070076 { "gfx", "Graphics", ATRACE_TAG_GRAPHICS, { } },
77 { "input", "Input", ATRACE_TAG_INPUT, { } },
78 { "view", "View System", ATRACE_TAG_VIEW, { } },
79 { "webview", "WebView", ATRACE_TAG_WEBVIEW, { } },
80 { "wm", "Window Manager", ATRACE_TAG_WINDOW_MANAGER, { } },
81 { "am", "Activity Manager", ATRACE_TAG_ACTIVITY_MANAGER, { } },
Patrick Auchter70ec2942014-09-30 15:38:30 -050082 { "sm", "Sync Manager", ATRACE_TAG_SYNC_MANAGER, { } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -070083 { "audio", "Audio", ATRACE_TAG_AUDIO, { } },
84 { "video", "Video", ATRACE_TAG_VIDEO, { } },
85 { "camera", "Camera", ATRACE_TAG_CAMERA, { } },
86 { "hal", "Hardware Modules", ATRACE_TAG_HAL, { } },
Jeff Brown3200b0b2014-08-14 19:24:47 -070087 { "app", "Application", ATRACE_TAG_APP, { } },
Dianne Hackborn9380d782013-04-12 14:52:35 -070088 { "res", "Resource Loading", ATRACE_TAG_RESOURCES, { } },
Jamie Genniseff2e8d2013-05-07 15:20:39 -070089 { "dalvik", "Dalvik VM", ATRACE_TAG_DALVIK, { } },
Tim Murrayf0f28412013-05-23 14:39:42 -070090 { "rs", "RenderScript", ATRACE_TAG_RS, { } },
Brigid Smith750aa972014-05-28 14:23:24 -070091 { "bionic", "Bionic C Library", ATRACE_TAG_BIONIC, { } },
Jeff Brown3200b0b2014-08-14 19:24:47 -070092 { "power", "Power Management", ATRACE_TAG_POWER, { } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -070093 { "sched", "CPU Scheduling", 0, {
94 { REQ, "/sys/kernel/debug/tracing/events/sched/sched_switch/enable" },
95 { REQ, "/sys/kernel/debug/tracing/events/sched/sched_wakeup/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080096 } },
Dan Willemsenf440d392014-04-11 15:44:09 -070097 { "irq", "IRQ Events", 0, {
98 { REQ, "/sys/kernel/debug/tracing/events/irq/enable" },
99 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700100 { "freq", "CPU Frequency", 0, {
101 { REQ, "/sys/kernel/debug/tracing/events/power/cpu_frequency/enable" },
102 { OPT, "/sys/kernel/debug/tracing/events/power/clock_set_rate/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800103 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700104 { "membus", "Memory Bus Utilization", 0, {
105 { REQ, "/sys/kernel/debug/tracing/events/memory_bus/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800106 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700107 { "idle", "CPU Idle", 0, {
108 { REQ, "/sys/kernel/debug/tracing/events/power/cpu_idle/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800109 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700110 { "disk", "Disk I/O", 0, {
Greg Hackmanne80d32c2014-11-20 12:59:44 -0800111 { OPT, "/sys/kernel/debug/tracing/events/f2fs/f2fs_sync_file_enter/enable" },
112 { OPT, "/sys/kernel/debug/tracing/events/f2fs/f2fs_sync_file_exit/enable" },
113 { OPT, "/sys/kernel/debug/tracing/events/f2fs/f2fs_write_begin/enable" },
114 { OPT, "/sys/kernel/debug/tracing/events/f2fs/f2fs_write_end/enable" },
115 { OPT, "/sys/kernel/debug/tracing/events/ext4/ext4_da_write_begin/enable" },
116 { OPT, "/sys/kernel/debug/tracing/events/ext4/ext4_da_write_end/enable" },
117 { OPT, "/sys/kernel/debug/tracing/events/ext4/ext4_sync_file_enter/enable" },
118 { OPT, "/sys/kernel/debug/tracing/events/ext4/ext4_sync_file_exit/enable" },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700119 { REQ, "/sys/kernel/debug/tracing/events/block/block_rq_issue/enable" },
120 { REQ, "/sys/kernel/debug/tracing/events/block/block_rq_complete/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800121 } },
Ken Sumralld3fa5612013-07-03 12:32:03 -0700122 { "mmc", "eMMC commands", 0, {
123 { REQ, "/sys/kernel/debug/tracing/events/mmc/enable" },
124 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700125 { "load", "CPU Load", 0, {
126 { REQ, "/sys/kernel/debug/tracing/events/cpufreq_interactive/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800127 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700128 { "sync", "Synchronization", 0, {
129 { REQ, "/sys/kernel/debug/tracing/events/sync/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800130 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700131 { "workq", "Kernel Workqueues", 0, {
132 { REQ, "/sys/kernel/debug/tracing/events/workqueue/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800133 } },
Colin Cross580407f2014-08-18 15:22:13 -0700134 { "memreclaim", "Kernel Memory Reclaim", 0, {
135 { REQ, "/sys/kernel/debug/tracing/events/vmscan/mm_vmscan_direct_reclaim_begin/enable" },
136 { REQ, "/sys/kernel/debug/tracing/events/vmscan/mm_vmscan_direct_reclaim_end/enable" },
137 { REQ, "/sys/kernel/debug/tracing/events/vmscan/mm_vmscan_kswapd_wake/enable" },
138 { REQ, "/sys/kernel/debug/tracing/events/vmscan/mm_vmscan_kswapd_sleep/enable" },
139 } },
Aaron Schulmancbe13ef2015-02-25 08:37:09 -0800140 { "regulators", "Voltage and Current Regulators", 0, {
141 { REQ, "/sys/kernel/debug/tracing/events/regulator/enable" },
142 } },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800143};
144
145/* Command line options */
146static int g_traceDurationSeconds = 5;
147static bool g_traceOverwrite = false;
148static int g_traceBufferSizeKB = 2048;
149static bool g_compress = false;
150static bool g_nohup = false;
151static int g_initialSleepSecs = 0;
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700152static const char* g_kernelTraceFuncs = NULL;
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700153static const char* g_debugAppCmdLine = "";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800154
155/* Global state */
156static bool g_traceAborted = false;
157static bool g_categoryEnables[NELEM(k_categories)] = {};
158
159/* Sys file paths */
160static const char* k_traceClockPath =
161 "/sys/kernel/debug/tracing/trace_clock";
162
163static const char* k_traceBufferSizePath =
164 "/sys/kernel/debug/tracing/buffer_size_kb";
165
166static const char* k_tracingOverwriteEnablePath =
167 "/sys/kernel/debug/tracing/options/overwrite";
168
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700169static const char* k_currentTracerPath =
170 "/sys/kernel/debug/tracing/current_tracer";
171
172static const char* k_printTgidPath =
173 "/sys/kernel/debug/tracing/options/print-tgid";
174
175static const char* k_funcgraphAbsTimePath =
176 "/sys/kernel/debug/tracing/options/funcgraph-abstime";
177
178static const char* k_funcgraphCpuPath =
179 "/sys/kernel/debug/tracing/options/funcgraph-cpu";
180
181static const char* k_funcgraphProcPath =
182 "/sys/kernel/debug/tracing/options/funcgraph-proc";
183
184static const char* k_funcgraphFlatPath =
185 "/sys/kernel/debug/tracing/options/funcgraph-flat";
186
187static const char* k_funcgraphDurationPath =
188 "/sys/kernel/debug/tracing/options/funcgraph-duration";
189
190static const char* k_ftraceFilterPath =
191 "/sys/kernel/debug/tracing/set_ftrace_filter";
192
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800193static const char* k_tracingOnPath =
194 "/sys/kernel/debug/tracing/tracing_on";
195
196static const char* k_tracePath =
197 "/sys/kernel/debug/tracing/trace";
198
John Reck469a1942015-03-26 15:31:35 -0700199static const char* k_traceMarkerPath =
200 "/sys/kernel/debug/tracing/trace_marker";
201
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800202// Check whether a file exists.
203static bool fileExists(const char* filename) {
204 return access(filename, F_OK) != -1;
205}
206
207// Check whether a file is writable.
208static bool fileIsWritable(const char* filename) {
209 return access(filename, W_OK) != -1;
210}
211
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700212// Truncate a file.
213static bool truncateFile(const char* path)
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800214{
Jamie Gennis43122e72013-03-21 14:06:31 -0700215 // This uses creat rather than truncate because some of the debug kernel
216 // device nodes (e.g. k_ftraceFilterPath) currently aren't changed by
217 // calls to truncate, but they are cleared by calls to creat.
218 int traceFD = creat(path, 0);
219 if (traceFD == -1) {
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700220 fprintf(stderr, "error truncating %s: %s (%d)\n", path,
Jamie Gennis43122e72013-03-21 14:06:31 -0700221 strerror(errno), errno);
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700222 return false;
223 }
224
Jamie Gennis43122e72013-03-21 14:06:31 -0700225 close(traceFD);
226
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700227 return true;
228}
229
230static bool _writeStr(const char* filename, const char* str, int flags)
231{
232 int fd = open(filename, flags);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800233 if (fd == -1) {
234 fprintf(stderr, "error opening %s: %s (%d)\n", filename,
235 strerror(errno), errno);
236 return false;
237 }
238
239 bool ok = true;
240 ssize_t len = strlen(str);
241 if (write(fd, str, len) != len) {
242 fprintf(stderr, "error writing to %s: %s (%d)\n", filename,
243 strerror(errno), errno);
244 ok = false;
245 }
246
247 close(fd);
248
249 return ok;
250}
251
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700252// Write a string to a file, returning true if the write was successful.
253static bool writeStr(const char* filename, const char* str)
254{
255 return _writeStr(filename, str, O_WRONLY);
256}
257
258// Append a string to a file, returning true if the write was successful.
259static bool appendStr(const char* filename, const char* str)
260{
261 return _writeStr(filename, str, O_APPEND|O_WRONLY);
262}
263
John Reck469a1942015-03-26 15:31:35 -0700264static void writeClockSyncMarker()
265{
266 char buffer[128];
Martijn Coenen0bcd97a2015-07-15 14:25:23 +0200267 int len = 0;
268 int fd = open(k_traceMarkerPath, O_WRONLY);
269 if (fd == -1) {
270 fprintf(stderr, "error opening %s: %s (%d)\n", k_traceMarkerPath,
271 strerror(errno), errno);
272 return;
273 }
John Reck469a1942015-03-26 15:31:35 -0700274 float now_in_seconds = systemTime(CLOCK_MONOTONIC) / 1000000000.0f;
Martijn Coenen0bcd97a2015-07-15 14:25:23 +0200275
276 len = snprintf(buffer, 128, "trace_event_clock_sync: parent_ts=%f\n", now_in_seconds);
277 if (write(fd, buffer, len) != len) {
278 fprintf(stderr, "error writing clock sync marker %s (%d)\n", strerror(errno), errno);
279 }
280
281 int64_t realtime_in_ms = systemTime(CLOCK_REALTIME) / 1000000;
282 len = snprintf(buffer, 128, "trace_event_clock_sync: realtime_ts=%" PRId64 "\n", realtime_in_ms);
283 if (write(fd, buffer, len) != len) {
284 fprintf(stderr, "error writing clock sync marker %s (%d)\n", strerror(errno), errno);
285 }
286
287 close(fd);
John Reck469a1942015-03-26 15:31:35 -0700288}
289
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800290// Enable or disable a kernel option by writing a "1" or a "0" into a /sys
291// file.
292static bool setKernelOptionEnable(const char* filename, bool enable)
293{
294 return writeStr(filename, enable ? "1" : "0");
295}
296
297// Check whether the category is supported on the device with the current
298// rootness. A category is supported only if all its required /sys/ files are
299// writable and if enabling the category will enable one or more tracing tags
300// or /sys/ files.
301static bool isCategorySupported(const TracingCategory& category)
302{
303 bool ok = category.tags != 0;
304 for (int i = 0; i < MAX_SYS_FILES; i++) {
305 const char* path = category.sysfiles[i].path;
306 bool req = category.sysfiles[i].required == REQ;
307 if (path != NULL) {
308 if (req) {
309 if (!fileIsWritable(path)) {
310 return false;
311 } else {
312 ok = true;
313 }
314 } else {
315 ok |= fileIsWritable(path);
316 }
317 }
318 }
319 return ok;
320}
321
322// Check whether the category would be supported on the device if the user
323// were root. This function assumes that root is able to write to any file
324// that exists. It performs the same logic as isCategorySupported, but it
325// uses file existance rather than writability in the /sys/ file checks.
326static bool isCategorySupportedForRoot(const TracingCategory& category)
327{
328 bool ok = category.tags != 0;
329 for (int i = 0; i < MAX_SYS_FILES; i++) {
330 const char* path = category.sysfiles[i].path;
331 bool req = category.sysfiles[i].required == REQ;
332 if (path != NULL) {
333 if (req) {
334 if (!fileExists(path)) {
335 return false;
336 } else {
337 ok = true;
338 }
339 } else {
340 ok |= fileExists(path);
341 }
342 }
343 }
344 return ok;
345}
346
347// Enable or disable overwriting of the kernel trace buffers. Disabling this
348// will cause tracing to stop once the trace buffers have filled up.
349static bool setTraceOverwriteEnable(bool enable)
350{
351 return setKernelOptionEnable(k_tracingOverwriteEnablePath, enable);
352}
353
354// Enable or disable kernel tracing.
355static bool setTracingEnabled(bool enable)
356{
357 return setKernelOptionEnable(k_tracingOnPath, enable);
358}
359
360// Clear the contents of the kernel trace.
361static bool clearTrace()
362{
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700363 return truncateFile(k_tracePath);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800364}
365
366// Set the size of the kernel's trace buffer in kilobytes.
367static bool setTraceBufferSizeKB(int size)
368{
369 char str[32] = "1";
370 int len;
371 if (size < 1) {
372 size = 1;
373 }
374 snprintf(str, 32, "%d", size);
375 return writeStr(k_traceBufferSizePath, str);
376}
377
Colin Crossb1ce49b2014-08-20 14:28:47 -0700378// Read the trace_clock sysfs file and return true if it matches the requested
379// value. The trace_clock file format is:
380// local [global] counter uptime perf
381static bool isTraceClock(const char *mode)
382{
383 int fd = open(k_traceClockPath, O_RDONLY);
384 if (fd == -1) {
385 fprintf(stderr, "error opening %s: %s (%d)\n", k_traceClockPath,
386 strerror(errno), errno);
387 return false;
388 }
389
390 char buf[4097];
391 ssize_t n = read(fd, buf, 4096);
392 close(fd);
393 if (n == -1) {
394 fprintf(stderr, "error reading %s: %s (%d)\n", k_traceClockPath,
395 strerror(errno), errno);
396 return false;
397 }
398 buf[n] = '\0';
399
400 char *start = strchr(buf, '[');
401 if (start == NULL) {
402 return false;
403 }
404 start++;
405
406 char *end = strchr(start, ']');
407 if (end == NULL) {
408 return false;
409 }
410 *end = '\0';
411
412 return strcmp(mode, start) == 0;
413}
414
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800415// Enable or disable the kernel's use of the global clock. Disabling the global
416// clock will result in the kernel using a per-CPU local clock.
Colin Crossb1ce49b2014-08-20 14:28:47 -0700417// Any write to the trace_clock sysfs file will reset the buffer, so only
418// update it if the requested value is not the current value.
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800419static bool setGlobalClockEnable(bool enable)
420{
Colin Crossb1ce49b2014-08-20 14:28:47 -0700421 const char *clock = enable ? "global" : "local";
422
423 if (isTraceClock(clock)) {
424 return true;
425 }
426
427 return writeStr(k_traceClockPath, clock);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800428}
429
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700430static bool setPrintTgidEnableIfPresent(bool enable)
431{
432 if (fileExists(k_printTgidPath)) {
433 return setKernelOptionEnable(k_printTgidPath, enable);
434 }
435 return true;
436}
437
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800438// Poke all the binder-enabled processes in the system to get them to re-read
439// their system properties.
440static bool pokeBinderServices()
441{
442 sp<IServiceManager> sm = defaultServiceManager();
443 Vector<String16> services = sm->listServices();
444 for (size_t i = 0; i < services.size(); i++) {
445 sp<IBinder> obj = sm->checkService(services[i]);
446 if (obj != NULL) {
447 Parcel data;
448 if (obj->transact(IBinder::SYSPROPS_TRANSACTION, data,
449 NULL, 0) != OK) {
450 if (false) {
451 // XXX: For some reason this fails on tablets trying to
452 // poke the "phone" service. It's not clear whether some
453 // are expected to fail.
454 String8 svc(services[i]);
455 fprintf(stderr, "error poking binder service %s\n",
456 svc.string());
457 return false;
458 }
459 }
460 }
461 }
462 return true;
463}
464
465// Set the trace tags that userland tracing uses, and poke the running
466// processes to pick up the new value.
467static bool setTagsProperty(uint64_t tags)
468{
469 char buf[64];
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700470 snprintf(buf, 64, "%#" PRIx64, tags);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800471 if (property_set(k_traceTagsProperty, buf) < 0) {
472 fprintf(stderr, "error setting trace tags system property\n");
473 return false;
474 }
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700475 return true;
476}
477
478// Set the system property that indicates which apps should perform
479// application-level tracing.
480static bool setAppCmdlineProperty(const char* cmdline)
481{
482 if (property_set(k_traceAppCmdlineProperty, cmdline) < 0) {
483 fprintf(stderr, "error setting trace app system property\n");
484 return false;
485 }
486 return true;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800487}
488
489// Disable all /sys/ enable files.
490static bool disableKernelTraceEvents() {
491 bool ok = true;
492 for (int i = 0; i < NELEM(k_categories); i++) {
493 const TracingCategory &c = k_categories[i];
494 for (int j = 0; j < MAX_SYS_FILES; j++) {
495 const char* path = c.sysfiles[j].path;
496 if (path != NULL && fileIsWritable(path)) {
497 ok &= setKernelOptionEnable(path, false);
498 }
499 }
500 }
501 return ok;
502}
503
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700504// Verify that the comma separated list of functions are being traced by the
505// kernel.
506static bool verifyKernelTraceFuncs(const char* funcs)
507{
508 int fd = open(k_ftraceFilterPath, O_RDONLY);
509 if (fd == -1) {
510 fprintf(stderr, "error opening %s: %s (%d)\n", k_ftraceFilterPath,
511 strerror(errno), errno);
512 return false;
513 }
514
515 char buf[4097];
516 ssize_t n = read(fd, buf, 4096);
517 close(fd);
518 if (n == -1) {
519 fprintf(stderr, "error reading %s: %s (%d)\n", k_ftraceFilterPath,
520 strerror(errno), errno);
521 return false;
522 }
523
524 buf[n] = '\0';
525 String8 funcList = String8::format("\n%s", buf);
526
527 // Make sure that every function listed in funcs is in the list we just
528 // read from the kernel.
529 bool ok = true;
530 char* myFuncs = strdup(funcs);
531 char* func = strtok(myFuncs, ",");
532 while (func) {
533 String8 fancyFunc = String8::format("\n%s\n", func);
534 bool found = funcList.find(fancyFunc.string(), 0) >= 0;
535 if (!found || func[0] == '\0') {
536 fprintf(stderr, "error: \"%s\" is not a valid kernel function "
537 "to trace.\n", func);
538 ok = false;
539 }
540 func = strtok(NULL, ",");
541 }
542 free(myFuncs);
543
544 return ok;
545}
546
547// Set the comma separated list of functions that the kernel is to trace.
548static bool setKernelTraceFuncs(const char* funcs)
549{
550 bool ok = true;
551
552 if (funcs == NULL || funcs[0] == '\0') {
553 // Disable kernel function tracing.
Jamie Gennis6f6f3f72013-03-27 15:50:30 -0700554 if (fileIsWritable(k_currentTracerPath)) {
555 ok &= writeStr(k_currentTracerPath, "nop");
556 }
557 if (fileIsWritable(k_ftraceFilterPath)) {
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700558 ok &= truncateFile(k_ftraceFilterPath);
559 }
560 } else {
561 // Enable kernel function tracing.
562 ok &= writeStr(k_currentTracerPath, "function_graph");
563 ok &= setKernelOptionEnable(k_funcgraphAbsTimePath, true);
564 ok &= setKernelOptionEnable(k_funcgraphCpuPath, true);
565 ok &= setKernelOptionEnable(k_funcgraphProcPath, true);
566 ok &= setKernelOptionEnable(k_funcgraphFlatPath, true);
567
568 // Set the requested filter functions.
569 ok &= truncateFile(k_ftraceFilterPath);
570 char* myFuncs = strdup(funcs);
571 char* func = strtok(myFuncs, ",");
572 while (func) {
573 ok &= appendStr(k_ftraceFilterPath, func);
574 func = strtok(NULL, ",");
575 }
576 free(myFuncs);
577
578 // Verify that the set functions are being traced.
579 if (ok) {
580 ok &= verifyKernelTraceFuncs(funcs);
581 }
582 }
583
584 return ok;
585}
586
587// Set all the kernel tracing settings to the desired state for this trace
588// capture.
589static bool setUpTrace()
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800590{
591 bool ok = true;
592
593 // Set up the tracing options.
594 ok &= setTraceOverwriteEnable(g_traceOverwrite);
595 ok &= setTraceBufferSizeKB(g_traceBufferSizeKB);
596 ok &= setGlobalClockEnable(true);
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700597 ok &= setPrintTgidEnableIfPresent(true);
598 ok &= setKernelTraceFuncs(g_kernelTraceFuncs);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800599
600 // Set up the tags property.
601 uint64_t tags = 0;
602 for (int i = 0; i < NELEM(k_categories); i++) {
603 if (g_categoryEnables[i]) {
604 const TracingCategory &c = k_categories[i];
605 tags |= c.tags;
606 }
607 }
608 ok &= setTagsProperty(tags);
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700609 ok &= setAppCmdlineProperty(g_debugAppCmdLine);
610 ok &= pokeBinderServices();
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800611
612 // Disable all the sysfs enables. This is done as a separate loop from
613 // the enables to allow the same enable to exist in multiple categories.
614 ok &= disableKernelTraceEvents();
615
616 // Enable all the sysfs enables that are in an enabled category.
617 for (int i = 0; i < NELEM(k_categories); i++) {
618 if (g_categoryEnables[i]) {
619 const TracingCategory &c = k_categories[i];
620 for (int j = 0; j < MAX_SYS_FILES; j++) {
621 const char* path = c.sysfiles[j].path;
622 bool required = c.sysfiles[j].required == REQ;
623 if (path != NULL) {
624 if (fileIsWritable(path)) {
625 ok &= setKernelOptionEnable(path, true);
626 } else if (required) {
627 fprintf(stderr, "error writing file %s\n", path);
628 ok = false;
629 }
630 }
631 }
632 }
633 }
634
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800635 return ok;
636}
637
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700638// Reset all the kernel tracing settings to their default state.
639static void cleanUpTrace()
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800640{
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800641 // Disable all tracing that we're able to.
642 disableKernelTraceEvents();
643
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700644 // Reset the system properties.
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800645 setTagsProperty(0);
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700646 setAppCmdlineProperty("");
647 pokeBinderServices();
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800648
649 // Set the options back to their defaults.
650 setTraceOverwriteEnable(true);
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700651 setTraceBufferSizeKB(1);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800652 setGlobalClockEnable(false);
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700653 setPrintTgidEnableIfPresent(false);
654 setKernelTraceFuncs(NULL);
655}
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800656
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700657
658// Enable tracing in the kernel.
659static bool startTrace()
660{
661 return setTracingEnabled(true);
662}
663
664// Disable tracing in the kernel.
665static void stopTrace()
666{
667 setTracingEnabled(false);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800668}
669
670// Read the current kernel trace and write it to stdout.
671static void dumpTrace()
672{
673 int traceFD = open(k_tracePath, O_RDWR);
674 if (traceFD == -1) {
675 fprintf(stderr, "error opening %s: %s (%d)\n", k_tracePath,
676 strerror(errno), errno);
677 return;
678 }
679
680 if (g_compress) {
681 z_stream zs;
682 uint8_t *in, *out;
683 int result, flush;
684
Elliott Hughes3da5d232015-01-25 08:35:20 -0800685 memset(&zs, 0, sizeof(zs));
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800686 result = deflateInit(&zs, Z_DEFAULT_COMPRESSION);
687 if (result != Z_OK) {
688 fprintf(stderr, "error initializing zlib: %d\n", result);
689 close(traceFD);
690 return;
691 }
692
693 const size_t bufSize = 64*1024;
694 in = (uint8_t*)malloc(bufSize);
695 out = (uint8_t*)malloc(bufSize);
696 flush = Z_NO_FLUSH;
697
698 zs.next_out = out;
699 zs.avail_out = bufSize;
700
701 do {
702
703 if (zs.avail_in == 0) {
704 // More input is needed.
705 result = read(traceFD, in, bufSize);
706 if (result < 0) {
707 fprintf(stderr, "error reading trace: %s (%d)\n",
708 strerror(errno), errno);
709 result = Z_STREAM_END;
710 break;
711 } else if (result == 0) {
712 flush = Z_FINISH;
713 } else {
714 zs.next_in = in;
715 zs.avail_in = result;
716 }
717 }
718
719 if (zs.avail_out == 0) {
720 // Need to write the output.
721 result = write(STDOUT_FILENO, out, bufSize);
722 if ((size_t)result < bufSize) {
723 fprintf(stderr, "error writing deflated trace: %s (%d)\n",
724 strerror(errno), errno);
725 result = Z_STREAM_END; // skip deflate error message
726 zs.avail_out = bufSize; // skip the final write
727 break;
728 }
729 zs.next_out = out;
730 zs.avail_out = bufSize;
731 }
732
733 } while ((result = deflate(&zs, flush)) == Z_OK);
734
735 if (result != Z_STREAM_END) {
736 fprintf(stderr, "error deflating trace: %s\n", zs.msg);
737 }
738
739 if (zs.avail_out < bufSize) {
740 size_t bytes = bufSize - zs.avail_out;
741 result = write(STDOUT_FILENO, out, bytes);
742 if ((size_t)result < bytes) {
743 fprintf(stderr, "error writing deflated trace: %s (%d)\n",
744 strerror(errno), errno);
745 }
746 }
747
748 result = deflateEnd(&zs);
749 if (result != Z_OK) {
750 fprintf(stderr, "error cleaning up zlib: %d\n", result);
751 }
752
753 free(in);
754 free(out);
755 } else {
756 ssize_t sent = 0;
757 while ((sent = sendfile(STDOUT_FILENO, traceFD, NULL, 64*1024*1024)) > 0);
758 if (sent == -1) {
759 fprintf(stderr, "error dumping trace: %s (%d)\n", strerror(errno),
760 errno);
761 }
762 }
763
764 close(traceFD);
765}
766
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700767static void handleSignal(int /*signo*/)
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800768{
769 if (!g_nohup) {
770 g_traceAborted = true;
771 }
772}
773
774static void registerSigHandler()
775{
776 struct sigaction sa;
777 sigemptyset(&sa.sa_mask);
778 sa.sa_flags = 0;
779 sa.sa_handler = handleSignal;
780 sigaction(SIGHUP, &sa, NULL);
781 sigaction(SIGINT, &sa, NULL);
782 sigaction(SIGQUIT, &sa, NULL);
783 sigaction(SIGTERM, &sa, NULL);
784}
785
786static bool setCategoryEnable(const char* name, bool enable)
787{
788 for (int i = 0; i < NELEM(k_categories); i++) {
789 const TracingCategory& c = k_categories[i];
790 if (strcmp(name, c.name) == 0) {
791 if (isCategorySupported(c)) {
792 g_categoryEnables[i] = enable;
793 return true;
794 } else {
795 if (isCategorySupportedForRoot(c)) {
796 fprintf(stderr, "error: category \"%s\" requires root "
797 "privileges.\n", name);
798 } else {
799 fprintf(stderr, "error: category \"%s\" is not supported "
800 "on this device.\n", name);
801 }
802 return false;
803 }
804 }
805 }
806 fprintf(stderr, "error: unknown tracing category \"%s\"\n", name);
807 return false;
808}
809
810static void listSupportedCategories()
811{
812 for (int i = 0; i < NELEM(k_categories); i++) {
813 const TracingCategory& c = k_categories[i];
814 if (isCategorySupported(c)) {
815 printf(" %10s - %s\n", c.name, c.longname);
816 }
817 }
818}
819
820// Print the command usage help to stderr.
821static void showHelp(const char *cmd)
822{
823 fprintf(stderr, "usage: %s [options] [categories...]\n", cmd);
824 fprintf(stderr, "options include:\n"
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700825 " -a appname enable app-level tracing for a comma "
826 "separated list of cmdlines\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800827 " -b N use a trace buffer size of N KB\n"
828 " -c trace into a circular buffer\n"
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700829 " -k fname,... trace the listed kernel functions\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800830 " -n ignore signals\n"
831 " -s N sleep for N seconds before tracing [default 0]\n"
832 " -t N trace for N seconds [defualt 5]\n"
833 " -z compress the trace dump\n"
834 " --async_start start circular trace and return immediatly\n"
835 " --async_dump dump the current contents of circular trace buffer\n"
836 " --async_stop stop tracing and dump the current contents of circular\n"
837 " trace buffer\n"
Jamie Gennis92573f12012-12-07 16:29:03 -0800838 " --list_categories\n"
839 " list the available tracing categories\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800840 );
841}
842
843int main(int argc, char **argv)
844{
845 bool async = false;
846 bool traceStart = true;
847 bool traceStop = true;
848 bool traceDump = true;
849
850 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
851 showHelp(argv[0]);
852 exit(0);
853 }
854
855 for (;;) {
856 int ret;
857 int option_index = 0;
858 static struct option long_options[] = {
859 {"async_start", no_argument, 0, 0 },
860 {"async_stop", no_argument, 0, 0 },
861 {"async_dump", no_argument, 0, 0 },
862 {"list_categories", no_argument, 0, 0 },
863 { 0, 0, 0, 0 }
864 };
865
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700866 ret = getopt_long(argc, argv, "a:b:ck:ns:t:z",
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800867 long_options, &option_index);
868
869 if (ret < 0) {
870 for (int i = optind; i < argc; i++) {
871 if (!setCategoryEnable(argv[i], true)) {
872 fprintf(stderr, "error enabling tracing category \"%s\"\n", argv[i]);
873 exit(1);
874 }
875 }
876 break;
877 }
878
879 switch(ret) {
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700880 case 'a':
881 g_debugAppCmdLine = optarg;
882 break;
883
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800884 case 'b':
885 g_traceBufferSizeKB = atoi(optarg);
886 break;
887
888 case 'c':
889 g_traceOverwrite = true;
890 break;
891
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700892 case 'k':
893 g_kernelTraceFuncs = optarg;
Jamie Gennis6f6f3f72013-03-27 15:50:30 -0700894 break;
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700895
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800896 case 'n':
897 g_nohup = true;
Jamie Gennis6f6f3f72013-03-27 15:50:30 -0700898 break;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800899
900 case 's':
901 g_initialSleepSecs = atoi(optarg);
902 break;
903
904 case 't':
905 g_traceDurationSeconds = atoi(optarg);
906 break;
907
908 case 'z':
909 g_compress = true;
910 break;
911
912 case 0:
913 if (!strcmp(long_options[option_index].name, "async_start")) {
914 async = true;
915 traceStop = false;
916 traceDump = false;
917 g_traceOverwrite = true;
918 } else if (!strcmp(long_options[option_index].name, "async_stop")) {
919 async = true;
John Reck2c237ee2015-05-15 10:00:34 -0700920 traceStart = false;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800921 } else if (!strcmp(long_options[option_index].name, "async_dump")) {
922 async = true;
923 traceStart = false;
924 traceStop = false;
925 } else if (!strcmp(long_options[option_index].name, "list_categories")) {
926 listSupportedCategories();
927 exit(0);
928 }
Jamie Gennis6f6f3f72013-03-27 15:50:30 -0700929 break;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800930
931 default:
932 fprintf(stderr, "\n");
933 showHelp(argv[0]);
934 exit(-1);
935 break;
936 }
937 }
938
939 registerSigHandler();
940
941 if (g_initialSleepSecs > 0) {
942 sleep(g_initialSleepSecs);
943 }
944
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700945 bool ok = true;
946 ok &= setUpTrace();
947 ok &= startTrace();
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800948
949 if (ok && traceStart) {
950 printf("capturing trace...");
951 fflush(stdout);
952
953 // We clear the trace after starting it because tracing gets enabled for
954 // each CPU individually in the kernel. Having the beginning of the trace
955 // contain entries from only one CPU can cause "begin" entries without a
956 // matching "end" entry to show up if a task gets migrated from one CPU to
957 // another.
958 ok = clearTrace();
959
Martijn Coenen0bcd97a2015-07-15 14:25:23 +0200960 writeClockSyncMarker();
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800961 if (ok && !async) {
962 // Sleep to allow the trace to be captured.
963 struct timespec timeLeft;
964 timeLeft.tv_sec = g_traceDurationSeconds;
965 timeLeft.tv_nsec = 0;
966 do {
967 if (g_traceAborted) {
968 break;
969 }
970 } while (nanosleep(&timeLeft, &timeLeft) == -1 && errno == EINTR);
971 }
972 }
973
974 // Stop the trace and restore the default settings.
975 if (traceStop)
976 stopTrace();
977
978 if (ok && traceDump) {
979 if (!g_traceAborted) {
980 printf(" done\nTRACE:\n");
981 fflush(stdout);
982 dumpTrace();
983 } else {
984 printf("\ntrace aborted.\n");
985 fflush(stdout);
986 }
987 clearTrace();
988 } else if (!ok) {
989 fprintf(stderr, "unable to start tracing\n");
990 }
991
992 // Reset the trace buffer size to 1.
993 if (traceStop)
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700994 cleanUpTrace();
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800995
996 return g_traceAborted ? 1 : 0;
997}