blob: 70164eacf68240c379f51002bc37fda58db3508a [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
Paul Lawrence2cd93cc2017-01-17 09:50:18 -080017#define LOG_TAG "atrace"
John Reck40b26b42016-03-30 09:44:36 -070018
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080019#include <errno.h>
20#include <fcntl.h>
21#include <getopt.h>
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070022#include <inttypes.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080023#include <signal.h>
24#include <stdarg.h>
25#include <stdbool.h>
26#include <stdio.h>
27#include <stdlib.h>
Elliott Hughes3da5d232015-01-25 08:35:20 -080028#include <string.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080029#include <time.h>
Martijn Coenend9535872015-11-26 10:00:55 +010030#include <unistd.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080031#include <zlib.h>
32
Carmen Jacksonea826792017-05-05 11:42:32 -070033#include <fstream>
Elliott Hughesa252f4d2016-07-21 17:12:15 -070034#include <memory>
35
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080036#include <binder/IBinder.h>
37#include <binder/IServiceManager.h>
38#include <binder/Parcel.h>
39
Martijn Coenenee9b97e2016-11-16 16:00:26 +010040#include <android/hidl/manager/1.0/IServiceManager.h>
41#include <hidl/ServiceManagement.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080042
Corey Tabakaa6c0a722017-05-31 16:37:40 -070043#include <pdx/default_transport/service_utility.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080044#include <utils/String8.h>
John Reck469a1942015-03-26 15:31:35 -070045#include <utils/Timers.h>
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +090046#include <utils/Tokenizer.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080047#include <utils/Trace.h>
Stephane Gasparinid8419c22016-03-02 13:45:15 +010048#include <android-base/file.h>
Elliott Hughes5fd6ff62017-03-28 14:55:31 -070049#include <android-base/macros.h>
50#include <android-base/properties.h>
51#include <android-base/stringprintf.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080052
53using namespace android;
Corey Tabakaa6c0a722017-05-31 16:37:40 -070054using pdx::default_transport::ServiceUtility;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080055
Martijn Coenenee9b97e2016-11-16 16:00:26 +010056using std::string;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080057
sergeyv4144eff2016-04-28 11:40:04 -070058#define MAX_SYS_FILES 10
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080059
60const char* k_traceTagsProperty = "debug.atrace.tags.enableflags";
sergeyv4144eff2016-04-28 11:40:04 -070061
62const char* k_traceAppsNumberProperty = "debug.atrace.app_number";
63const char* k_traceAppsPropertyTemplate = "debug.atrace.app_%d";
sergeyvdb404152016-05-02 19:26:07 -070064const char* k_coreServiceCategory = "core_services";
Corey Tabakaa6c0a722017-05-31 16:37:40 -070065const char* k_pdxServiceCategory = "pdx";
sergeyvdb404152016-05-02 19:26:07 -070066const char* k_coreServicesProp = "ro.atrace.core.services";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080067
68typedef enum { OPT, REQ } requiredness ;
69
70struct TracingCategory {
71 // The name identifying the category.
72 const char* name;
73
74 // A longer description of the category.
75 const char* longname;
76
77 // The userland tracing tags that the category enables.
78 uint64_t tags;
79
80 // The fname==NULL terminated list of /sys/ files that the category
81 // enables.
82 struct {
83 // Whether the file must be writable in order to enable the tracing
84 // category.
85 requiredness required;
86
87 // The path to the enable file.
88 const char* path;
89 } sysfiles[MAX_SYS_FILES];
90};
91
92/* Tracing categories */
93static const TracingCategory k_categories[] = {
John Reck732a29a2017-05-24 16:09:21 -070094 { "gfx", "Graphics", ATRACE_TAG_GRAPHICS, {
95 { OPT, "events/mdss/enable" },
Wei Wange2c10462018-02-13 10:47:23 -080096 { OPT, "events/sde/enable" },
John Reck732a29a2017-05-24 16:09:21 -070097 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -070098 { "input", "Input", ATRACE_TAG_INPUT, { } },
99 { "view", "View System", ATRACE_TAG_VIEW, { } },
100 { "webview", "WebView", ATRACE_TAG_WEBVIEW, { } },
101 { "wm", "Window Manager", ATRACE_TAG_WINDOW_MANAGER, { } },
102 { "am", "Activity Manager", ATRACE_TAG_ACTIVITY_MANAGER, { } },
Patrick Auchter70ec2942014-09-30 15:38:30 -0500103 { "sm", "Sync Manager", ATRACE_TAG_SYNC_MANAGER, { } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700104 { "audio", "Audio", ATRACE_TAG_AUDIO, { } },
105 { "video", "Video", ATRACE_TAG_VIDEO, { } },
106 { "camera", "Camera", ATRACE_TAG_CAMERA, { } },
107 { "hal", "Hardware Modules", ATRACE_TAG_HAL, { } },
Dianne Hackborn9380d782013-04-12 14:52:35 -0700108 { "res", "Resource Loading", ATRACE_TAG_RESOURCES, { } },
Jamie Genniseff2e8d2013-05-07 15:20:39 -0700109 { "dalvik", "Dalvik VM", ATRACE_TAG_DALVIK, { } },
Tim Murrayf0f28412013-05-23 14:39:42 -0700110 { "rs", "RenderScript", ATRACE_TAG_RS, { } },
Brigid Smith750aa972014-05-28 14:23:24 -0700111 { "bionic", "Bionic C Library", ATRACE_TAG_BIONIC, { } },
Jeff Brown3200b0b2014-08-14 19:24:47 -0700112 { "power", "Power Management", ATRACE_TAG_POWER, { } },
Todd Kennedy01e111b2015-07-31 14:36:20 -0700113 { "pm", "Package Manager", ATRACE_TAG_PACKAGE_MANAGER, { } },
Yasuhiro Matsuda7cc49772015-07-01 01:46:25 +0900114 { "ss", "System Server", ATRACE_TAG_SYSTEM_SERVER, { } },
Greg Hackmannbbd7d992014-12-01 14:43:34 -0800115 { "database", "Database", ATRACE_TAG_DATABASE, { } },
Felipe Leme0f97c1d2016-09-07 11:33:26 -0700116 { "network", "Network", ATRACE_TAG_NETWORK, { } },
Josh Gao468b4cb2016-11-29 10:55:21 -0800117 { "adb", "ADB", ATRACE_TAG_ADB, { } },
Martijn Coenen47791e92018-03-09 09:27:32 +0100118 { "vibrator", "Vibrator", ATRACE_TAG_VIBRATOR, { } },
119 { "aidl", "AIDL calls", ATRACE_TAG_AIDL, { } },
sergeyvdb404152016-05-02 19:26:07 -0700120 { k_coreServiceCategory, "Core services", 0, { } },
Corey Tabakaa6c0a722017-05-31 16:37:40 -0700121 { k_pdxServiceCategory, "PDX services", 0, { } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700122 { "sched", "CPU Scheduling", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800123 { REQ, "events/sched/sched_switch/enable" },
124 { REQ, "events/sched/sched_wakeup/enable" },
Joel Fernandesee593e22017-06-04 13:05:59 -0700125 { OPT, "events/sched/sched_waking/enable" },
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800126 { OPT, "events/sched/sched_blocked_reason/enable" },
127 { OPT, "events/sched/sched_cpu_hotplug/enable" },
Wei Wangca49dfc2018-05-03 15:45:20 -0700128 { OPT, "events/sched/sched_pi_setprio/enable" },
Joel Fernandes4dfca7c2017-06-15 16:53:52 -0700129 { OPT, "events/cgroup/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800130 } },
Dan Willemsenf440d392014-04-11 15:44:09 -0700131 { "irq", "IRQ Events", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800132 { REQ, "events/irq/enable" },
133 { OPT, "events/ipi/enable" },
Dan Willemsenf440d392014-04-11 15:44:09 -0700134 } },
Joel Fernandes0ad0f302017-08-31 08:35:05 -0700135 { "irqoff", "IRQ-disabled code section tracing", 0, {
136 { REQ, "events/preemptirq/irq_enable/enable" },
137 { REQ, "events/preemptirq/irq_disable/enable" },
138 } },
139 { "preemptoff", "Preempt-disabled code section tracing", 0, {
140 { REQ, "events/preemptirq/preempt_enable/enable" },
141 { REQ, "events/preemptirq/preempt_disable/enable" },
142 } },
Michael Wright43fb6782016-08-18 19:56:43 +0100143 { "i2c", "I2C Events", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800144 { REQ, "events/i2c/enable" },
145 { REQ, "events/i2c/i2c_read/enable" },
146 { REQ, "events/i2c/i2c_write/enable" },
147 { REQ, "events/i2c/i2c_result/enable" },
148 { REQ, "events/i2c/i2c_reply/enable" },
149 { OPT, "events/i2c/smbus_read/enable" },
150 { OPT, "events/i2c/smbus_write/enable" },
151 { OPT, "events/i2c/smbus_result/enable" },
152 { OPT, "events/i2c/smbus_reply/enable" },
Michael Wright43fb6782016-08-18 19:56:43 +0100153 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700154 { "freq", "CPU Frequency", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800155 { REQ, "events/power/cpu_frequency/enable" },
156 { OPT, "events/power/clock_set_rate/enable" },
Kevin DuBoisafc2f972017-11-03 15:44:08 -0700157 { OPT, "events/power/clock_disable/enable" },
158 { OPT, "events/power/clock_enable/enable" },
Wei Wang391efc22018-02-22 11:40:07 -0800159 { OPT, "events/clk/clk_set_rate/enable" },
160 { OPT, "events/clk/clk_disable/enable" },
161 { OPT, "events/clk/clk_enable/enable" },
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800162 { OPT, "events/power/cpu_frequency_limits/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800163 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700164 { "membus", "Memory Bus Utilization", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800165 { REQ, "events/memory_bus/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800166 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700167 { "idle", "CPU Idle", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800168 { REQ, "events/power/cpu_idle/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800169 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700170 { "disk", "Disk I/O", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800171 { OPT, "events/f2fs/f2fs_sync_file_enter/enable" },
172 { OPT, "events/f2fs/f2fs_sync_file_exit/enable" },
173 { OPT, "events/f2fs/f2fs_write_begin/enable" },
174 { OPT, "events/f2fs/f2fs_write_end/enable" },
175 { OPT, "events/ext4/ext4_da_write_begin/enable" },
176 { OPT, "events/ext4/ext4_da_write_end/enable" },
177 { OPT, "events/ext4/ext4_sync_file_enter/enable" },
178 { OPT, "events/ext4/ext4_sync_file_exit/enable" },
179 { REQ, "events/block/block_rq_issue/enable" },
180 { REQ, "events/block/block_rq_complete/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800181 } },
Ken Sumralld3fa5612013-07-03 12:32:03 -0700182 { "mmc", "eMMC commands", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800183 { REQ, "events/mmc/enable" },
Ken Sumralld3fa5612013-07-03 12:32:03 -0700184 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700185 { "load", "CPU Load", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800186 { REQ, "events/cpufreq_interactive/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800187 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700188 { "sync", "Synchronization", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800189 { REQ, "events/sync/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800190 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700191 { "workq", "Kernel Workqueues", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800192 { REQ, "events/workqueue/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800193 } },
Colin Cross580407f2014-08-18 15:22:13 -0700194 { "memreclaim", "Kernel Memory Reclaim", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800195 { REQ, "events/vmscan/mm_vmscan_direct_reclaim_begin/enable" },
196 { REQ, "events/vmscan/mm_vmscan_direct_reclaim_end/enable" },
197 { REQ, "events/vmscan/mm_vmscan_kswapd_wake/enable" },
198 { REQ, "events/vmscan/mm_vmscan_kswapd_sleep/enable" },
Marc Hittingerf1f62e32017-05-17 15:57:43 -0700199 { REQ, "events/lowmemorykiller/enable" },
Colin Cross580407f2014-08-18 15:22:13 -0700200 } },
Aaron Schulmanc2c6ecd2015-02-25 08:37:09 -0800201 { "regulators", "Voltage and Current Regulators", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800202 { REQ, "events/regulator/enable" },
Aaron Schulmanc2c6ecd2015-02-25 08:37:09 -0800203 } },
Scott Bauerae473362015-06-08 16:32:36 -0700204 { "binder_driver", "Binder Kernel driver", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800205 { REQ, "events/binder/binder_transaction/enable" },
206 { REQ, "events/binder/binder_transaction_received/enable" },
Martijn Coenen7f3d7a22017-05-26 09:50:55 -0700207 { OPT, "events/binder/binder_set_priority/enable" },
Scott Bauerae473362015-06-08 16:32:36 -0700208 } },
209 { "binder_lock", "Binder global lock trace", 0, {
Howard Cheneb8acbf2017-05-10 16:32:11 +0800210 { OPT, "events/binder/binder_lock/enable" },
211 { OPT, "events/binder/binder_locked/enable" },
212 { OPT, "events/binder/binder_unlock/enable" },
Scott Bauerae473362015-06-08 16:32:36 -0700213 } },
Martijn Coenen70481612015-10-23 13:57:05 +0200214 { "pagecache", "Page cache", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800215 { REQ, "events/filemap/enable" },
Martijn Coenen70481612015-10-23 13:57:05 +0200216 } },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800217};
218
219/* Command line options */
220static int g_traceDurationSeconds = 5;
221static bool g_traceOverwrite = false;
222static int g_traceBufferSizeKB = 2048;
223static bool g_compress = false;
224static bool g_nohup = false;
225static int g_initialSleepSecs = 0;
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +0900226static const char* g_categoriesFile = NULL;
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700227static const char* g_kernelTraceFuncs = NULL;
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700228static const char* g_debugAppCmdLine = "";
John Reck40b26b42016-03-30 09:44:36 -0700229static const char* g_outputFile = nullptr;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800230
231/* Global state */
Corey Tabakaa6c0a722017-05-31 16:37:40 -0700232static bool g_tracePdx = false;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800233static bool g_traceAborted = false;
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700234static bool g_categoryEnables[arraysize(k_categories)] = {};
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800235static std::string g_traceFolder;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800236
237/* Sys file paths */
238static const char* k_traceClockPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800239 "trace_clock";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800240
241static const char* k_traceBufferSizePath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800242 "buffer_size_kb";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800243
Chih-Hung Hsieh734e3782017-10-05 13:44:13 -0700244#if 0
245// TODO: Re-enable after stabilization
Joel Fernandesed80bd02017-06-02 10:19:28 -0700246static const char* k_traceCmdlineSizePath =
247 "saved_cmdlines_size";
Chih-Hung Hsieh734e3782017-10-05 13:44:13 -0700248#endif
Joel Fernandesed80bd02017-06-02 10:19:28 -0700249
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800250static const char* k_tracingOverwriteEnablePath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800251 "options/overwrite";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800252
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700253static const char* k_currentTracerPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800254 "current_tracer";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700255
256static const char* k_printTgidPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800257 "options/print-tgid";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700258
259static const char* k_funcgraphAbsTimePath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800260 "options/funcgraph-abstime";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700261
262static const char* k_funcgraphCpuPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800263 "options/funcgraph-cpu";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700264
265static const char* k_funcgraphProcPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800266 "options/funcgraph-proc";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700267
268static const char* k_funcgraphFlatPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800269 "options/funcgraph-flat";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700270
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700271static const char* k_ftraceFilterPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800272 "set_ftrace_filter";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700273
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800274static const char* k_tracingOnPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800275 "tracing_on";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800276
277static const char* k_tracePath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800278 "trace";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800279
Martijn Coenend9535872015-11-26 10:00:55 +0100280static const char* k_traceStreamPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800281 "trace_pipe";
Martijn Coenend9535872015-11-26 10:00:55 +0100282
John Reck469a1942015-03-26 15:31:35 -0700283static const char* k_traceMarkerPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800284 "trace_marker";
John Reck469a1942015-03-26 15:31:35 -0700285
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800286// Check whether a file exists.
287static bool fileExists(const char* filename) {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800288 return access((g_traceFolder + filename).c_str(), F_OK) != -1;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800289}
290
291// Check whether a file is writable.
292static bool fileIsWritable(const char* filename) {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800293 return access((g_traceFolder + filename).c_str(), W_OK) != -1;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800294}
295
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700296// Truncate a file.
297static bool truncateFile(const char* path)
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800298{
Jamie Gennis43122e72013-03-21 14:06:31 -0700299 // This uses creat rather than truncate because some of the debug kernel
300 // device nodes (e.g. k_ftraceFilterPath) currently aren't changed by
301 // calls to truncate, but they are cleared by calls to creat.
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800302 int traceFD = creat((g_traceFolder + path).c_str(), 0);
Jamie Gennis43122e72013-03-21 14:06:31 -0700303 if (traceFD == -1) {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800304 fprintf(stderr, "error truncating %s: %s (%d)\n", (g_traceFolder + path).c_str(),
Jamie Gennis43122e72013-03-21 14:06:31 -0700305 strerror(errno), errno);
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700306 return false;
307 }
308
Jamie Gennis43122e72013-03-21 14:06:31 -0700309 close(traceFD);
310
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700311 return true;
312}
313
314static bool _writeStr(const char* filename, const char* str, int flags)
315{
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800316 std::string fullFilename = g_traceFolder + filename;
317 int fd = open(fullFilename.c_str(), flags);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800318 if (fd == -1) {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800319 fprintf(stderr, "error opening %s: %s (%d)\n", fullFilename.c_str(),
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800320 strerror(errno), errno);
321 return false;
322 }
323
324 bool ok = true;
325 ssize_t len = strlen(str);
326 if (write(fd, str, len) != len) {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800327 fprintf(stderr, "error writing to %s: %s (%d)\n", fullFilename.c_str(),
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800328 strerror(errno), errno);
329 ok = false;
330 }
331
332 close(fd);
333
334 return ok;
335}
336
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700337// Write a string to a file, returning true if the write was successful.
338static bool writeStr(const char* filename, const char* str)
339{
340 return _writeStr(filename, str, O_WRONLY);
341}
342
343// Append a string to a file, returning true if the write was successful.
344static bool appendStr(const char* filename, const char* str)
345{
346 return _writeStr(filename, str, O_APPEND|O_WRONLY);
347}
348
John Reck469a1942015-03-26 15:31:35 -0700349static void writeClockSyncMarker()
350{
351 char buffer[128];
Martijn Coenen0bcd97a2015-07-15 14:25:23 +0200352 int len = 0;
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800353 int fd = open((g_traceFolder + k_traceMarkerPath).c_str(), O_WRONLY);
Martijn Coenen0bcd97a2015-07-15 14:25:23 +0200354 if (fd == -1) {
355 fprintf(stderr, "error opening %s: %s (%d)\n", k_traceMarkerPath,
356 strerror(errno), errno);
357 return;
358 }
John Reck469a1942015-03-26 15:31:35 -0700359 float now_in_seconds = systemTime(CLOCK_MONOTONIC) / 1000000000.0f;
Martijn Coenen0bcd97a2015-07-15 14:25:23 +0200360
361 len = snprintf(buffer, 128, "trace_event_clock_sync: parent_ts=%f\n", now_in_seconds);
362 if (write(fd, buffer, len) != len) {
363 fprintf(stderr, "error writing clock sync marker %s (%d)\n", strerror(errno), errno);
364 }
365
366 int64_t realtime_in_ms = systemTime(CLOCK_REALTIME) / 1000000;
367 len = snprintf(buffer, 128, "trace_event_clock_sync: realtime_ts=%" PRId64 "\n", realtime_in_ms);
368 if (write(fd, buffer, len) != len) {
369 fprintf(stderr, "error writing clock sync marker %s (%d)\n", strerror(errno), errno);
370 }
371
372 close(fd);
John Reck469a1942015-03-26 15:31:35 -0700373}
374
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800375// Enable or disable a kernel option by writing a "1" or a "0" into a /sys
376// file.
377static bool setKernelOptionEnable(const char* filename, bool enable)
378{
379 return writeStr(filename, enable ? "1" : "0");
380}
381
382// Check whether the category is supported on the device with the current
383// rootness. A category is supported only if all its required /sys/ files are
384// writable and if enabling the category will enable one or more tracing tags
385// or /sys/ files.
386static bool isCategorySupported(const TracingCategory& category)
387{
sergeyvdb404152016-05-02 19:26:07 -0700388 if (strcmp(category.name, k_coreServiceCategory) == 0) {
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700389 return !android::base::GetProperty(k_coreServicesProp, "").empty();
sergeyvdb404152016-05-02 19:26:07 -0700390 }
391
Corey Tabakaa6c0a722017-05-31 16:37:40 -0700392 if (strcmp(category.name, k_pdxServiceCategory) == 0) {
393 return true;
394 }
395
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800396 bool ok = category.tags != 0;
397 for (int i = 0; i < MAX_SYS_FILES; i++) {
398 const char* path = category.sysfiles[i].path;
399 bool req = category.sysfiles[i].required == REQ;
400 if (path != NULL) {
401 if (req) {
402 if (!fileIsWritable(path)) {
403 return false;
404 } else {
405 ok = true;
406 }
407 } else {
Howard Cheneb8acbf2017-05-10 16:32:11 +0800408 ok = true;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800409 }
410 }
411 }
412 return ok;
413}
414
415// Check whether the category would be supported on the device if the user
416// were root. This function assumes that root is able to write to any file
417// that exists. It performs the same logic as isCategorySupported, but it
Fabien Sanglardb5c95472016-06-08 11:40:12 -0700418// uses file existence rather than writability in the /sys/ file checks.
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800419static bool isCategorySupportedForRoot(const TracingCategory& category)
420{
421 bool ok = category.tags != 0;
422 for (int i = 0; i < MAX_SYS_FILES; i++) {
423 const char* path = category.sysfiles[i].path;
424 bool req = category.sysfiles[i].required == REQ;
425 if (path != NULL) {
426 if (req) {
427 if (!fileExists(path)) {
428 return false;
429 } else {
430 ok = true;
431 }
432 } else {
433 ok |= fileExists(path);
434 }
435 }
436 }
437 return ok;
438}
439
440// Enable or disable overwriting of the kernel trace buffers. Disabling this
441// will cause tracing to stop once the trace buffers have filled up.
442static bool setTraceOverwriteEnable(bool enable)
443{
444 return setKernelOptionEnable(k_tracingOverwriteEnablePath, enable);
445}
446
447// Enable or disable kernel tracing.
448static bool setTracingEnabled(bool enable)
449{
450 return setKernelOptionEnable(k_tracingOnPath, enable);
451}
452
453// Clear the contents of the kernel trace.
454static bool clearTrace()
455{
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700456 return truncateFile(k_tracePath);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800457}
458
459// Set the size of the kernel's trace buffer in kilobytes.
460static bool setTraceBufferSizeKB(int size)
461{
462 char str[32] = "1";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800463 if (size < 1) {
464 size = 1;
465 }
466 snprintf(str, 32, "%d", size);
467 return writeStr(k_traceBufferSizePath, str);
468}
469
Chih-Hung Hsieh734e3782017-10-05 13:44:13 -0700470#if 0
471// TODO: Re-enable after stabilization
Joel Fernandesed80bd02017-06-02 10:19:28 -0700472// Set the default size of cmdline hashtable
473static bool setCmdlineSize()
474{
Joel Fernandesce964f22017-06-06 12:20:29 -0700475 if (fileExists(k_traceCmdlineSizePath)) {
476 return writeStr(k_traceCmdlineSizePath, "8192");
477 }
478 return true;
Joel Fernandesed80bd02017-06-02 10:19:28 -0700479}
Chih-Hung Hsieh734e3782017-10-05 13:44:13 -0700480#endif
Joel Fernandesed80bd02017-06-02 10:19:28 -0700481
Carmen Jacksonea826792017-05-05 11:42:32 -0700482// Set the clock to the best available option while tracing. Use 'boot' if it's
483// available; otherwise, use 'mono'. If neither are available use 'global'.
Colin Crossb1ce49b2014-08-20 14:28:47 -0700484// Any write to the trace_clock sysfs file will reset the buffer, so only
485// update it if the requested value is not the current value.
Carmen Jacksonea826792017-05-05 11:42:32 -0700486static bool setClock()
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800487{
Carmen Jacksonea826792017-05-05 11:42:32 -0700488 std::ifstream clockFile((g_traceFolder + k_traceClockPath).c_str(), O_RDONLY);
489 std::string clockStr((std::istreambuf_iterator<char>(clockFile)),
490 std::istreambuf_iterator<char>());
Colin Crossb1ce49b2014-08-20 14:28:47 -0700491
Carmen Jacksonea826792017-05-05 11:42:32 -0700492 std::string newClock;
493 if (clockStr.find("boot") != std::string::npos) {
494 newClock = "boot";
495 } else if (clockStr.find("mono") != std::string::npos) {
496 newClock = "mono";
497 } else {
498 newClock = "global";
Colin Crossb1ce49b2014-08-20 14:28:47 -0700499 }
500
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700501 size_t begin = clockStr.find('[') + 1;
502 size_t end = clockStr.find(']');
Carmen Jacksonea826792017-05-05 11:42:32 -0700503 if (newClock.compare(0, std::string::npos, clockStr, begin, end-begin) == 0) {
504 return true;
505 }
506 return writeStr(k_traceClockPath, newClock.c_str());
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800507}
508
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700509static bool setPrintTgidEnableIfPresent(bool enable)
510{
511 if (fileExists(k_printTgidPath)) {
512 return setKernelOptionEnable(k_printTgidPath, enable);
513 }
514 return true;
515}
516
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800517// Poke all the binder-enabled processes in the system to get them to re-read
518// their system properties.
519static bool pokeBinderServices()
520{
521 sp<IServiceManager> sm = defaultServiceManager();
522 Vector<String16> services = sm->listServices();
523 for (size_t i = 0; i < services.size(); i++) {
524 sp<IBinder> obj = sm->checkService(services[i]);
525 if (obj != NULL) {
526 Parcel data;
527 if (obj->transact(IBinder::SYSPROPS_TRANSACTION, data,
528 NULL, 0) != OK) {
529 if (false) {
530 // XXX: For some reason this fails on tablets trying to
531 // poke the "phone" service. It's not clear whether some
532 // are expected to fail.
533 String8 svc(services[i]);
534 fprintf(stderr, "error poking binder service %s\n",
535 svc.string());
536 return false;
537 }
538 }
539 }
540 }
541 return true;
542}
543
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100544// Poke all the HAL processes in the system to get them to re-read
545// their system properties.
546static void pokeHalServices()
547{
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100548 using ::android::hidl::base::V1_0::IBase;
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100549 using ::android::hidl::manager::V1_0::IServiceManager;
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100550 using ::android::hardware::hidl_string;
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100551 using ::android::hardware::Return;
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100552
553 sp<IServiceManager> sm = ::android::hardware::defaultServiceManager();
Steven Morelanda42866b2016-12-12 15:18:06 -0800554
555 if (sm == nullptr) {
556 fprintf(stderr, "failed to get IServiceManager to poke hal services\n");
557 return;
558 }
559
Yifan Hong8cf4ed12016-11-30 17:28:58 -0800560 auto listRet = sm->list([&](const auto &interfaces) {
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100561 for (size_t i = 0; i < interfaces.size(); i++) {
562 string fqInstanceName = interfaces[i];
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700563 string::size_type n = fqInstanceName.find('/');
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100564 if (n == std::string::npos || interfaces[i].size() == n+1)
565 continue;
566 hidl_string fqInterfaceName = fqInstanceName.substr(0, n);
567 hidl_string instanceName = fqInstanceName.substr(n+1, std::string::npos);
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100568 Return<sp<IBase>> interfaceRet = sm->get(fqInterfaceName, instanceName);
569 if (!interfaceRet.isOk()) {
Martijn Coenen1e4a7fb2017-02-17 14:57:38 +0100570 // ignore
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100571 continue;
572 }
Carmen Jackson73206122017-04-18 15:37:57 -0700573
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100574 sp<IBase> interface = interfaceRet;
Carmen Jackson73206122017-04-18 15:37:57 -0700575 if (interface == nullptr) {
576 // ignore
577 continue;
578 }
579
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100580 auto notifyRet = interface->notifySyspropsChanged();
581 if (!notifyRet.isOk()) {
Martijn Coenen1e4a7fb2017-02-17 14:57:38 +0100582 // ignore
Yifan Hong8cf4ed12016-11-30 17:28:58 -0800583 }
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100584 }
585 });
Yifan Hong8cf4ed12016-11-30 17:28:58 -0800586 if (!listRet.isOk()) {
Martijn Coenen64d54eb2017-01-12 17:16:31 +0100587 // TODO(b/34242478) fix this when we determine the correct ACL
588 //fprintf(stderr, "failed to list services: %s\n", listRet.description().c_str());
Yifan Hong8cf4ed12016-11-30 17:28:58 -0800589 }
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100590}
591
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800592// Set the trace tags that userland tracing uses, and poke the running
593// processes to pick up the new value.
594static bool setTagsProperty(uint64_t tags)
595{
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700596 std::string value = android::base::StringPrintf("%#" PRIx64, tags);
597 if (!android::base::SetProperty(k_traceTagsProperty, value)) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800598 fprintf(stderr, "error setting trace tags system property\n");
599 return false;
600 }
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700601 return true;
602}
603
sergeyv4144eff2016-04-28 11:40:04 -0700604static void clearAppProperties()
605{
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700606 if (!android::base::SetProperty(k_traceAppsNumberProperty, "")) {
sergeyv4144eff2016-04-28 11:40:04 -0700607 fprintf(stderr, "failed to clear system property: %s",
608 k_traceAppsNumberProperty);
609 }
610}
611
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700612// Set the system property that indicates which apps should perform
613// application-level tracing.
Dan Austin09a79872016-05-31 13:27:03 -0700614static bool setAppCmdlineProperty(char* cmdline)
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700615{
sergeyv4144eff2016-04-28 11:40:04 -0700616 int i = 0;
Dan Austin09a79872016-05-31 13:27:03 -0700617 char* start = cmdline;
sergeyv4144eff2016-04-28 11:40:04 -0700618 while (start != NULL) {
sergeyv4144eff2016-04-28 11:40:04 -0700619 char* end = strchr(start, ',');
620 if (end != NULL) {
621 *end = '\0';
622 end++;
623 }
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700624 std::string key = android::base::StringPrintf(k_traceAppsPropertyTemplate, i);
625 if (!android::base::SetProperty(key, start)) {
626 fprintf(stderr, "error setting trace app %d property to %s\n", i, key.c_str());
sergeyv4144eff2016-04-28 11:40:04 -0700627 clearAppProperties();
628 return false;
629 }
630 start = end;
631 i++;
632 }
633
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700634 std::string value = android::base::StringPrintf("%d", i);
635 if (!android::base::SetProperty(k_traceAppsNumberProperty, value)) {
636 fprintf(stderr, "error setting trace app number property to %s\n", value.c_str());
sergeyv4144eff2016-04-28 11:40:04 -0700637 clearAppProperties();
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700638 return false;
639 }
640 return true;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800641}
642
643// Disable all /sys/ enable files.
644static bool disableKernelTraceEvents() {
645 bool ok = true;
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700646 for (size_t i = 0; i < arraysize(k_categories); i++) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800647 const TracingCategory &c = k_categories[i];
648 for (int j = 0; j < MAX_SYS_FILES; j++) {
649 const char* path = c.sysfiles[j].path;
650 if (path != NULL && fileIsWritable(path)) {
651 ok &= setKernelOptionEnable(path, false);
652 }
653 }
654 }
655 return ok;
656}
657
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700658// Verify that the comma separated list of functions are being traced by the
659// kernel.
660static bool verifyKernelTraceFuncs(const char* funcs)
661{
Stephane Gasparinid8419c22016-03-02 13:45:15 +0100662 std::string buf;
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800663 if (!android::base::ReadFileToString(g_traceFolder + k_ftraceFilterPath, &buf)) {
Stephane Gasparinid8419c22016-03-02 13:45:15 +0100664 fprintf(stderr, "error opening %s: %s (%d)\n", k_ftraceFilterPath,
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700665 strerror(errno), errno);
Stephane Gasparinid8419c22016-03-02 13:45:15 +0100666 return false;
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700667 }
668
Stephane Gasparinid8419c22016-03-02 13:45:15 +0100669 String8 funcList = String8::format("\n%s",buf.c_str());
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700670
671 // Make sure that every function listed in funcs is in the list we just
Thomas Buhota2c22872016-01-27 09:44:31 +0100672 // read from the kernel, except for wildcard inputs.
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700673 bool ok = true;
674 char* myFuncs = strdup(funcs);
675 char* func = strtok(myFuncs, ",");
676 while (func) {
Thomas Buhota2c22872016-01-27 09:44:31 +0100677 if (!strchr(func, '*')) {
678 String8 fancyFunc = String8::format("\n%s\n", func);
679 bool found = funcList.find(fancyFunc.string(), 0) >= 0;
680 if (!found || func[0] == '\0') {
681 fprintf(stderr, "error: \"%s\" is not a valid kernel function "
682 "to trace.\n", func);
683 ok = false;
684 }
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700685 }
686 func = strtok(NULL, ",");
687 }
688 free(myFuncs);
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700689 return ok;
690}
691
692// Set the comma separated list of functions that the kernel is to trace.
693static bool setKernelTraceFuncs(const char* funcs)
694{
695 bool ok = true;
696
697 if (funcs == NULL || funcs[0] == '\0') {
698 // Disable kernel function tracing.
Jamie Gennis6f6f3f72013-03-27 15:50:30 -0700699 if (fileIsWritable(k_currentTracerPath)) {
700 ok &= writeStr(k_currentTracerPath, "nop");
701 }
702 if (fileIsWritable(k_ftraceFilterPath)) {
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700703 ok &= truncateFile(k_ftraceFilterPath);
704 }
705 } else {
706 // Enable kernel function tracing.
707 ok &= writeStr(k_currentTracerPath, "function_graph");
708 ok &= setKernelOptionEnable(k_funcgraphAbsTimePath, true);
709 ok &= setKernelOptionEnable(k_funcgraphCpuPath, true);
710 ok &= setKernelOptionEnable(k_funcgraphProcPath, true);
711 ok &= setKernelOptionEnable(k_funcgraphFlatPath, true);
712
713 // Set the requested filter functions.
714 ok &= truncateFile(k_ftraceFilterPath);
715 char* myFuncs = strdup(funcs);
716 char* func = strtok(myFuncs, ",");
717 while (func) {
718 ok &= appendStr(k_ftraceFilterPath, func);
719 func = strtok(NULL, ",");
720 }
721 free(myFuncs);
722
723 // Verify that the set functions are being traced.
724 if (ok) {
725 ok &= verifyKernelTraceFuncs(funcs);
726 }
727 }
728
729 return ok;
730}
731
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +0900732static bool setCategoryEnable(const char* name, bool enable)
733{
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700734 for (size_t i = 0; i < arraysize(k_categories); i++) {
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +0900735 const TracingCategory& c = k_categories[i];
736 if (strcmp(name, c.name) == 0) {
737 if (isCategorySupported(c)) {
738 g_categoryEnables[i] = enable;
739 return true;
740 } else {
741 if (isCategorySupportedForRoot(c)) {
742 fprintf(stderr, "error: category \"%s\" requires root "
743 "privileges.\n", name);
744 } else {
745 fprintf(stderr, "error: category \"%s\" is not supported "
746 "on this device.\n", name);
747 }
748 return false;
749 }
750 }
751 }
752 fprintf(stderr, "error: unknown tracing category \"%s\"\n", name);
753 return false;
754}
755
756static bool setCategoriesEnableFromFile(const char* categories_file)
757{
758 if (!categories_file) {
759 return true;
760 }
761 Tokenizer* tokenizer = NULL;
762 if (Tokenizer::open(String8(categories_file), &tokenizer) != NO_ERROR) {
763 return false;
764 }
765 bool ok = true;
766 while (!tokenizer->isEol()) {
767 String8 token = tokenizer->nextToken(" ");
768 if (token.isEmpty()) {
769 tokenizer->skipDelimiters(" ");
770 continue;
771 }
772 ok &= setCategoryEnable(token.string(), true);
773 }
774 delete tokenizer;
775 return ok;
776}
777
Hector Dearman550b18c2018-03-08 14:35:44 +0000778static bool setUpUserspaceTracing()
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800779{
780 bool ok = true;
781
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800782 // Set up the tags property.
783 uint64_t tags = 0;
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700784 for (size_t i = 0; i < arraysize(k_categories); i++) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800785 if (g_categoryEnables[i]) {
786 const TracingCategory &c = k_categories[i];
787 tags |= c.tags;
788 }
789 }
790 ok &= setTagsProperty(tags);
sergeyvdb404152016-05-02 19:26:07 -0700791
792 bool coreServicesTagEnabled = false;
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700793 for (size_t i = 0; i < arraysize(k_categories); i++) {
sergeyvdb404152016-05-02 19:26:07 -0700794 if (strcmp(k_categories[i].name, k_coreServiceCategory) == 0) {
795 coreServicesTagEnabled = g_categoryEnables[i];
796 }
Corey Tabakaa6c0a722017-05-31 16:37:40 -0700797
798 // Set whether to poke PDX services in this session.
799 if (strcmp(k_categories[i].name, k_pdxServiceCategory) == 0) {
800 g_tracePdx = g_categoryEnables[i];
801 }
sergeyvdb404152016-05-02 19:26:07 -0700802 }
803
804 std::string packageList(g_debugAppCmdLine);
805 if (coreServicesTagEnabled) {
sergeyvdb404152016-05-02 19:26:07 -0700806 if (!packageList.empty()) {
807 packageList += ",";
808 }
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700809 packageList += android::base::GetProperty(k_coreServicesProp, "");
sergeyvdb404152016-05-02 19:26:07 -0700810 }
Dan Austin09a79872016-05-31 13:27:03 -0700811 ok &= setAppCmdlineProperty(&packageList[0]);
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700812 ok &= pokeBinderServices();
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100813 pokeHalServices();
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800814
Corey Tabakaa6c0a722017-05-31 16:37:40 -0700815 if (g_tracePdx) {
816 ok &= ServiceUtility::PokeServices();
817 }
818
Hector Dearman550b18c2018-03-08 14:35:44 +0000819 return ok;
820}
821
822static void cleanUpUserspaceTracing()
823{
824 setTagsProperty(0);
825 clearAppProperties();
826 pokeBinderServices();
827
828 if (g_tracePdx) {
829 ServiceUtility::PokeServices();
830 }
831}
832
833
834// Set all the kernel tracing settings to the desired state for this trace
835// capture.
836static bool setUpKernelTracing()
837{
838 bool ok = true;
839
840 // Set up the tracing options.
841 ok &= setCategoriesEnableFromFile(g_categoriesFile);
842 ok &= setTraceOverwriteEnable(g_traceOverwrite);
843 ok &= setTraceBufferSizeKB(g_traceBufferSizeKB);
844 // TODO: Re-enable after stabilization
845 //ok &= setCmdlineSize();
846 ok &= setClock();
847 ok &= setPrintTgidEnableIfPresent(true);
848 ok &= setKernelTraceFuncs(g_kernelTraceFuncs);
849
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800850 // Disable all the sysfs enables. This is done as a separate loop from
851 // the enables to allow the same enable to exist in multiple categories.
852 ok &= disableKernelTraceEvents();
853
854 // Enable all the sysfs enables that are in an enabled category.
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700855 for (size_t i = 0; i < arraysize(k_categories); i++) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800856 if (g_categoryEnables[i]) {
857 const TracingCategory &c = k_categories[i];
858 for (int j = 0; j < MAX_SYS_FILES; j++) {
859 const char* path = c.sysfiles[j].path;
860 bool required = c.sysfiles[j].required == REQ;
861 if (path != NULL) {
862 if (fileIsWritable(path)) {
863 ok &= setKernelOptionEnable(path, true);
864 } else if (required) {
865 fprintf(stderr, "error writing file %s\n", path);
866 ok = false;
867 }
868 }
869 }
870 }
871 }
872
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800873 return ok;
874}
875
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700876// Reset all the kernel tracing settings to their default state.
Hector Dearman550b18c2018-03-08 14:35:44 +0000877static void cleanUpKernelTracing()
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800878{
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800879 // Disable all tracing that we're able to.
880 disableKernelTraceEvents();
881
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800882 // Set the options back to their defaults.
883 setTraceOverwriteEnable(true);
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700884 setTraceBufferSizeKB(1);
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700885 setPrintTgidEnableIfPresent(false);
886 setKernelTraceFuncs(NULL);
887}
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800888
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700889// Enable tracing in the kernel.
890static bool startTrace()
891{
892 return setTracingEnabled(true);
893}
894
895// Disable tracing in the kernel.
896static void stopTrace()
897{
898 setTracingEnabled(false);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800899}
900
Martijn Coenend9535872015-11-26 10:00:55 +0100901// Read data from the tracing pipe and forward to stdout
902static void streamTrace()
903{
904 char trace_data[4096];
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800905 int traceFD = open((g_traceFolder + k_traceStreamPath).c_str(), O_RDWR);
Martijn Coenend9535872015-11-26 10:00:55 +0100906 if (traceFD == -1) {
907 fprintf(stderr, "error opening %s: %s (%d)\n", k_traceStreamPath,
908 strerror(errno), errno);
909 return;
910 }
911 while (!g_traceAborted) {
912 ssize_t bytes_read = read(traceFD, trace_data, 4096);
913 if (bytes_read > 0) {
914 write(STDOUT_FILENO, trace_data, bytes_read);
915 fflush(stdout);
916 } else {
917 if (!g_traceAborted) {
918 fprintf(stderr, "read returned %zd bytes err %d (%s)\n",
919 bytes_read, errno, strerror(errno));
920 }
921 break;
922 }
923 }
924}
925
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800926// Read the current kernel trace and write it to stdout.
John Reck40b26b42016-03-30 09:44:36 -0700927static void dumpTrace(int outFd)
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800928{
John Reck6c8ac922016-03-28 11:25:30 -0700929 ALOGI("Dumping trace");
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800930 int traceFD = open((g_traceFolder + k_tracePath).c_str(), O_RDWR);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800931 if (traceFD == -1) {
932 fprintf(stderr, "error opening %s: %s (%d)\n", k_tracePath,
933 strerror(errno), errno);
934 return;
935 }
936
937 if (g_compress) {
938 z_stream zs;
Elliott Hughes3da5d232015-01-25 08:35:20 -0800939 memset(&zs, 0, sizeof(zs));
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700940
941 int result = deflateInit(&zs, Z_DEFAULT_COMPRESSION);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800942 if (result != Z_OK) {
943 fprintf(stderr, "error initializing zlib: %d\n", result);
944 close(traceFD);
945 return;
946 }
947
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700948 constexpr size_t bufSize = 64*1024;
949 std::unique_ptr<uint8_t> in(new uint8_t[bufSize]);
950 std::unique_ptr<uint8_t> out(new uint8_t[bufSize]);
951 if (!in || !out) {
952 fprintf(stderr, "couldn't allocate buffers\n");
953 close(traceFD);
954 return;
955 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800956
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700957 int flush = Z_NO_FLUSH;
958
959 zs.next_out = reinterpret_cast<Bytef*>(out.get());
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800960 zs.avail_out = bufSize;
961
962 do {
963
964 if (zs.avail_in == 0) {
965 // More input is needed.
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700966 result = read(traceFD, in.get(), bufSize);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800967 if (result < 0) {
968 fprintf(stderr, "error reading trace: %s (%d)\n",
969 strerror(errno), errno);
970 result = Z_STREAM_END;
971 break;
972 } else if (result == 0) {
973 flush = Z_FINISH;
974 } else {
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700975 zs.next_in = reinterpret_cast<Bytef*>(in.get());
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800976 zs.avail_in = result;
977 }
978 }
979
980 if (zs.avail_out == 0) {
981 // Need to write the output.
Elliott Hughesb59e2962016-07-22 09:00:59 -0700982 result = write(outFd, out.get(), bufSize);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800983 if ((size_t)result < bufSize) {
984 fprintf(stderr, "error writing deflated trace: %s (%d)\n",
985 strerror(errno), errno);
986 result = Z_STREAM_END; // skip deflate error message
987 zs.avail_out = bufSize; // skip the final write
988 break;
989 }
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700990 zs.next_out = reinterpret_cast<Bytef*>(out.get());
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800991 zs.avail_out = bufSize;
992 }
993
994 } while ((result = deflate(&zs, flush)) == Z_OK);
995
996 if (result != Z_STREAM_END) {
997 fprintf(stderr, "error deflating trace: %s\n", zs.msg);
998 }
999
1000 if (zs.avail_out < bufSize) {
1001 size_t bytes = bufSize - zs.avail_out;
Elliott Hughesb59e2962016-07-22 09:00:59 -07001002 result = write(outFd, out.get(), bytes);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001003 if ((size_t)result < bytes) {
1004 fprintf(stderr, "error writing deflated trace: %s (%d)\n",
1005 strerror(errno), errno);
1006 }
1007 }
1008
1009 result = deflateEnd(&zs);
1010 if (result != Z_OK) {
1011 fprintf(stderr, "error cleaning up zlib: %d\n", result);
1012 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001013 } else {
Josh Gaod3d36e72017-04-11 15:21:13 -07001014 char buf[4096];
1015 ssize_t rc;
1016 while ((rc = TEMP_FAILURE_RETRY(read(traceFD, buf, sizeof(buf)))) > 0) {
1017 if (!android::base::WriteFully(outFd, buf, rc)) {
1018 fprintf(stderr, "error writing trace: %s\n", strerror(errno));
1019 break;
1020 }
1021 }
1022 if (rc == -1) {
1023 fprintf(stderr, "error dumping trace: %s\n", strerror(errno));
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001024 }
1025 }
1026
1027 close(traceFD);
1028}
1029
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001030static void handleSignal(int /*signo*/)
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001031{
1032 if (!g_nohup) {
1033 g_traceAborted = true;
1034 }
1035}
1036
1037static void registerSigHandler()
1038{
1039 struct sigaction sa;
1040 sigemptyset(&sa.sa_mask);
1041 sa.sa_flags = 0;
1042 sa.sa_handler = handleSignal;
1043 sigaction(SIGHUP, &sa, NULL);
1044 sigaction(SIGINT, &sa, NULL);
1045 sigaction(SIGQUIT, &sa, NULL);
1046 sigaction(SIGTERM, &sa, NULL);
1047}
1048
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001049static void listSupportedCategories()
1050{
Elliott Hughes5fd6ff62017-03-28 14:55:31 -07001051 for (size_t i = 0; i < arraysize(k_categories); i++) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001052 const TracingCategory& c = k_categories[i];
1053 if (isCategorySupported(c)) {
1054 printf(" %10s - %s\n", c.name, c.longname);
1055 }
1056 }
1057}
1058
1059// Print the command usage help to stderr.
1060static void showHelp(const char *cmd)
1061{
1062 fprintf(stderr, "usage: %s [options] [categories...]\n", cmd);
1063 fprintf(stderr, "options include:\n"
Jamie Gennisf7f29c82013-03-27 15:50:58 -07001064 " -a appname enable app-level tracing for a comma "
Daniel Colascione519a0792018-02-09 20:05:39 -08001065 "separated list of cmdlines; * is a wildcard matching any process\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001066 " -b N use a trace buffer size of N KB\n"
1067 " -c trace into a circular buffer\n"
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +09001068 " -f filename use the categories written in a file as space-separated\n"
1069 " values in a line\n"
Jamie Gennise9b8cfb2013-03-12 16:00:10 -07001070 " -k fname,... trace the listed kernel functions\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001071 " -n ignore signals\n"
1072 " -s N sleep for N seconds before tracing [default 0]\n"
Fabien Sanglardb5c95472016-06-08 11:40:12 -07001073 " -t N trace for N seconds [default 5]\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001074 " -z compress the trace dump\n"
Fabien Sanglardb5c95472016-06-08 11:40:12 -07001075 " --async_start start circular trace and return immediately\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001076 " --async_dump dump the current contents of circular trace buffer\n"
1077 " --async_stop stop tracing and dump the current contents of circular\n"
1078 " trace buffer\n"
Martijn Coenend9535872015-11-26 10:00:55 +01001079 " --stream stream trace to stdout as it enters the trace buffer\n"
1080 " Note: this can take significant CPU time, and is best\n"
1081 " used for measuring things that are not affected by\n"
1082 " CPU performance, like pagecache usage.\n"
Jamie Gennis92573f12012-12-07 16:29:03 -08001083 " --list_categories\n"
1084 " list the available tracing categories\n"
John Reck40b26b42016-03-30 09:44:36 -07001085 " -o filename write the trace to the specified file instead\n"
1086 " of stdout.\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001087 );
1088}
1089
Paul Lawrence2cd93cc2017-01-17 09:50:18 -08001090bool findTraceFiles()
1091{
1092 static const std::string debugfs_path = "/sys/kernel/debug/tracing/";
1093 static const std::string tracefs_path = "/sys/kernel/tracing/";
1094 static const std::string trace_file = "trace_marker";
1095
1096 bool tracefs = access((tracefs_path + trace_file).c_str(), F_OK) != -1;
1097 bool debugfs = access((debugfs_path + trace_file).c_str(), F_OK) != -1;
1098
1099 if (!tracefs && !debugfs) {
1100 fprintf(stderr, "Error: Did not find trace folder\n");
1101 return false;
1102 }
1103
1104 if (tracefs) {
1105 g_traceFolder = tracefs_path;
1106 } else {
1107 g_traceFolder = debugfs_path;
1108 }
1109
1110 return true;
1111}
1112
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001113int main(int argc, char **argv)
1114{
1115 bool async = false;
1116 bool traceStart = true;
1117 bool traceStop = true;
1118 bool traceDump = true;
Martijn Coenend9535872015-11-26 10:00:55 +01001119 bool traceStream = false;
Hector Dearman550b18c2018-03-08 14:35:44 +00001120 bool onlyUserspace = false;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001121
1122 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
1123 showHelp(argv[0]);
1124 exit(0);
1125 }
1126
Paul Lawrence2cd93cc2017-01-17 09:50:18 -08001127 if (!findTraceFiles()) {
1128 fprintf(stderr, "No trace folder found\n");
1129 exit(-1);
1130 }
1131
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001132 for (;;) {
1133 int ret;
1134 int option_index = 0;
1135 static struct option long_options[] = {
Hector Dearman550b18c2018-03-08 14:35:44 +00001136 {"async_start", no_argument, 0, 0 },
1137 {"async_stop", no_argument, 0, 0 },
1138 {"async_dump", no_argument, 0, 0 },
1139 {"only_userspace", no_argument, 0, 0 },
1140 {"list_categories", no_argument, 0, 0 },
1141 {"stream", no_argument, 0, 0 },
1142 { 0, 0, 0, 0 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001143 };
1144
John Reck40b26b42016-03-30 09:44:36 -07001145 ret = getopt_long(argc, argv, "a:b:cf:k:ns:t:zo:",
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001146 long_options, &option_index);
1147
1148 if (ret < 0) {
1149 for (int i = optind; i < argc; i++) {
1150 if (!setCategoryEnable(argv[i], true)) {
1151 fprintf(stderr, "error enabling tracing category \"%s\"\n", argv[i]);
1152 exit(1);
1153 }
1154 }
1155 break;
1156 }
1157
1158 switch(ret) {
Jamie Gennisf7f29c82013-03-27 15:50:58 -07001159 case 'a':
1160 g_debugAppCmdLine = optarg;
1161 break;
1162
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001163 case 'b':
1164 g_traceBufferSizeKB = atoi(optarg);
1165 break;
1166
1167 case 'c':
1168 g_traceOverwrite = true;
1169 break;
1170
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +09001171 case 'f':
1172 g_categoriesFile = optarg;
1173 break;
1174
Jamie Gennise9b8cfb2013-03-12 16:00:10 -07001175 case 'k':
1176 g_kernelTraceFuncs = optarg;
Jamie Gennis6f6f3f72013-03-27 15:50:30 -07001177 break;
Jamie Gennise9b8cfb2013-03-12 16:00:10 -07001178
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001179 case 'n':
1180 g_nohup = true;
Jamie Gennis6f6f3f72013-03-27 15:50:30 -07001181 break;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001182
1183 case 's':
1184 g_initialSleepSecs = atoi(optarg);
1185 break;
1186
1187 case 't':
1188 g_traceDurationSeconds = atoi(optarg);
1189 break;
1190
1191 case 'z':
1192 g_compress = true;
1193 break;
1194
John Reck40b26b42016-03-30 09:44:36 -07001195 case 'o':
1196 g_outputFile = optarg;
1197 break;
1198
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001199 case 0:
1200 if (!strcmp(long_options[option_index].name, "async_start")) {
1201 async = true;
1202 traceStop = false;
1203 traceDump = false;
1204 g_traceOverwrite = true;
1205 } else if (!strcmp(long_options[option_index].name, "async_stop")) {
1206 async = true;
John Reck4ba2b632015-05-15 10:00:34 -07001207 traceStart = false;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001208 } else if (!strcmp(long_options[option_index].name, "async_dump")) {
1209 async = true;
1210 traceStart = false;
1211 traceStop = false;
Hector Dearman550b18c2018-03-08 14:35:44 +00001212 } else if (!strcmp(long_options[option_index].name, "only_userspace")) {
1213 onlyUserspace = true;
Martijn Coenend9535872015-11-26 10:00:55 +01001214 } else if (!strcmp(long_options[option_index].name, "stream")) {
1215 traceStream = true;
1216 traceDump = false;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001217 } else if (!strcmp(long_options[option_index].name, "list_categories")) {
1218 listSupportedCategories();
1219 exit(0);
1220 }
Jamie Gennis6f6f3f72013-03-27 15:50:30 -07001221 break;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001222
1223 default:
1224 fprintf(stderr, "\n");
1225 showHelp(argv[0]);
1226 exit(-1);
1227 break;
1228 }
1229 }
1230
Hector Dearman550b18c2018-03-08 14:35:44 +00001231 if (onlyUserspace) {
1232 if (!async || !(traceStart || traceStop)) {
1233 fprintf(stderr, "--only_userspace can only be used with "
1234 "--async_start or --async_stop\n");
1235 exit(1);
1236 }
1237 }
1238
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001239 registerSigHandler();
1240
1241 if (g_initialSleepSecs > 0) {
1242 sleep(g_initialSleepSecs);
1243 }
1244
Jamie Gennise9b8cfb2013-03-12 16:00:10 -07001245 bool ok = true;
Hector Dearman550b18c2018-03-08 14:35:44 +00001246
Wei Wang190cb7d2018-03-08 13:33:12 -08001247 if (traceStart) {
Hector Dearman550b18c2018-03-08 14:35:44 +00001248 ok &= setUpUserspaceTracing();
1249 }
1250
1251 if (ok && traceStart && !onlyUserspace) {
1252 ok &= setUpKernelTracing();
Wei Wang190cb7d2018-03-08 13:33:12 -08001253 ok &= startTrace();
1254 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001255
1256 if (ok && traceStart) {
Hector Dearman550b18c2018-03-08 14:35:44 +00001257
1258 if (!traceStream && !onlyUserspace) {
Carmen Jacksonac53e732017-05-02 16:55:33 -07001259 printf("capturing trace...");
Martijn Coenend9535872015-11-26 10:00:55 +01001260 fflush(stdout);
1261 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001262
1263 // We clear the trace after starting it because tracing gets enabled for
1264 // each CPU individually in the kernel. Having the beginning of the trace
1265 // contain entries from only one CPU can cause "begin" entries without a
1266 // matching "end" entry to show up if a task gets migrated from one CPU to
1267 // another.
Hector Dearman550b18c2018-03-08 14:35:44 +00001268 if (!onlyUserspace)
1269 ok = clearTrace();
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001270
Hector Dearman550b18c2018-03-08 14:35:44 +00001271 if (!onlyUserspace)
1272 writeClockSyncMarker();
1273
Martijn Coenend9535872015-11-26 10:00:55 +01001274 if (ok && !async && !traceStream) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001275 // Sleep to allow the trace to be captured.
1276 struct timespec timeLeft;
1277 timeLeft.tv_sec = g_traceDurationSeconds;
1278 timeLeft.tv_nsec = 0;
1279 do {
1280 if (g_traceAborted) {
1281 break;
1282 }
1283 } while (nanosleep(&timeLeft, &timeLeft) == -1 && errno == EINTR);
1284 }
Martijn Coenend9535872015-11-26 10:00:55 +01001285
1286 if (traceStream) {
1287 streamTrace();
1288 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001289 }
1290
1291 // Stop the trace and restore the default settings.
Hector Dearman550b18c2018-03-08 14:35:44 +00001292 if (traceStop && !onlyUserspace)
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001293 stopTrace();
1294
Hector Dearman550b18c2018-03-08 14:35:44 +00001295 if (ok && traceDump && !onlyUserspace) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001296 if (!g_traceAborted) {
John Reck40b26b42016-03-30 09:44:36 -07001297 printf(" done\n");
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001298 fflush(stdout);
John Reck40b26b42016-03-30 09:44:36 -07001299 int outFd = STDOUT_FILENO;
1300 if (g_outputFile) {
Martijn Coenenc5791982017-02-22 09:25:31 +01001301 outFd = open(g_outputFile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
John Reck40b26b42016-03-30 09:44:36 -07001302 }
1303 if (outFd == -1) {
1304 printf("Failed to open '%s', err=%d", g_outputFile, errno);
1305 } else {
1306 dprintf(outFd, "TRACE:\n");
1307 dumpTrace(outFd);
1308 if (g_outputFile) {
1309 close(outFd);
1310 }
1311 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001312 } else {
1313 printf("\ntrace aborted.\n");
1314 fflush(stdout);
1315 }
1316 clearTrace();
1317 } else if (!ok) {
1318 fprintf(stderr, "unable to start tracing\n");
1319 }
1320
1321 // Reset the trace buffer size to 1.
Hector Dearman550b18c2018-03-08 14:35:44 +00001322 if (traceStop) {
1323 cleanUpUserspaceTracing();
1324 if (!onlyUserspace)
1325 cleanUpKernelTracing();
1326 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001327
1328 return g_traceAborted ? 1 : 0;
1329}