• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #define LOG_TAG "atrace"
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <getopt.h>
22 #include <inttypes.h>
23 #include <signal.h>
24 #include <stdarg.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include <zlib.h>
32 
33 #include <fstream>
34 #include <memory>
35 
36 #include <binder/IBinder.h>
37 #include <binder/IServiceManager.h>
38 #include <binder/Parcel.h>
39 
40 #include <android/hardware/atrace/1.0/IAtraceDevice.h>
41 #include <android/hidl/manager/1.0/IServiceManager.h>
42 #include <hidl/ServiceManagement.h>
43 
44 #include <pdx/default_transport/service_utility.h>
45 #include <utils/String8.h>
46 #include <utils/Timers.h>
47 #include <utils/Tokenizer.h>
48 #include <utils/Trace.h>
49 #include <android-base/file.h>
50 #include <android-base/macros.h>
51 #include <android-base/properties.h>
52 #include <android-base/stringprintf.h>
53 
54 using namespace android;
55 using pdx::default_transport::ServiceUtility;
56 using hardware::hidl_vec;
57 using hardware::hidl_string;
58 using hardware::Return;
59 using hardware::atrace::V1_0::IAtraceDevice;
60 using hardware::atrace::V1_0::Status;
61 using hardware::atrace::V1_0::toString;
62 
63 using std::string;
64 
65 #define MAX_SYS_FILES 11
66 
67 const char* k_traceTagsProperty = "debug.atrace.tags.enableflags";
68 const char* k_userInitiatedTraceProperty = "debug.atrace.user_initiated";
69 
70 const char* k_traceAppsNumberProperty = "debug.atrace.app_number";
71 const char* k_traceAppsPropertyTemplate = "debug.atrace.app_%d";
72 const char* k_coreServiceCategory = "core_services";
73 const char* k_pdxServiceCategory = "pdx";
74 const char* k_coreServicesProp = "ro.atrace.core.services";
75 
76 typedef enum { OPT, REQ } requiredness  ;
77 
78 struct TracingCategory {
79     // The name identifying the category.
80     const char* name;
81 
82     // A longer description of the category.
83     const char* longname;
84 
85     // The userland tracing tags that the category enables.
86     uint64_t tags;
87 
88     // The fname==NULL terminated list of /sys/ files that the category
89     // enables.
90     struct {
91         // Whether the file must be writable in order to enable the tracing
92         // category.
93         requiredness required;
94 
95         // The path to the enable file.
96         const char* path;
97     } sysfiles[MAX_SYS_FILES];
98 };
99 
100 /* Tracing categories */
101 static const TracingCategory k_categories[] = {
102     { "gfx",        "Graphics",                 ATRACE_TAG_GRAPHICS, { } },
103     { "input",      "Input",                    ATRACE_TAG_INPUT, { } },
104     { "view",       "View System",              ATRACE_TAG_VIEW, { } },
105     { "webview",    "WebView",                  ATRACE_TAG_WEBVIEW, { } },
106     { "wm",         "Window Manager",           ATRACE_TAG_WINDOW_MANAGER, { } },
107     { "am",         "Activity Manager",         ATRACE_TAG_ACTIVITY_MANAGER, { } },
108     { "sm",         "Sync Manager",             ATRACE_TAG_SYNC_MANAGER, { } },
109     { "audio",      "Audio",                    ATRACE_TAG_AUDIO, { } },
110     { "video",      "Video",                    ATRACE_TAG_VIDEO, { } },
111     { "camera",     "Camera",                   ATRACE_TAG_CAMERA, { } },
112     { "hal",        "Hardware Modules",         ATRACE_TAG_HAL, { } },
113     { "res",        "Resource Loading",         ATRACE_TAG_RESOURCES, { } },
114     { "dalvik",     "Dalvik VM",                ATRACE_TAG_DALVIK, { } },
115     { "rs",         "RenderScript",             ATRACE_TAG_RS, { } },
116     { "bionic",     "Bionic C Library",         ATRACE_TAG_BIONIC, { } },
117     { "power",      "Power Management",         ATRACE_TAG_POWER, { } },
118     { "pm",         "Package Manager",          ATRACE_TAG_PACKAGE_MANAGER, { } },
119     { "ss",         "System Server",            ATRACE_TAG_SYSTEM_SERVER, { } },
120     { "database",   "Database",                 ATRACE_TAG_DATABASE, { } },
121     { "network",    "Network",                  ATRACE_TAG_NETWORK, { } },
122     { "adb",        "ADB",                      ATRACE_TAG_ADB, { } },
123     { "vibrator",   "Vibrator",                 ATRACE_TAG_VIBRATOR, { } },
124     { "aidl",       "AIDL calls",               ATRACE_TAG_AIDL, { } },
125     { "nnapi",      "NNAPI",                    ATRACE_TAG_NNAPI, { } },
126     { "rro",        "Runtime Resource Overlay", ATRACE_TAG_RRO, { } },
127     { k_coreServiceCategory, "Core services", 0, { } },
128     { k_pdxServiceCategory, "PDX services", 0, { } },
129     { "sched",      "CPU Scheduling",   0, {
130         { REQ,      "events/sched/sched_switch/enable" },
131         { REQ,      "events/sched/sched_wakeup/enable" },
132         { OPT,      "events/sched/sched_waking/enable" },
133         { OPT,      "events/sched/sched_blocked_reason/enable" },
134         { OPT,      "events/sched/sched_cpu_hotplug/enable" },
135         { OPT,      "events/sched/sched_pi_setprio/enable" },
136         { OPT,      "events/sched/sched_process_exit/enable" },
137         { OPT,      "events/cgroup/enable" },
138         { OPT,      "events/oom/oom_score_adj_update/enable" },
139         { OPT,      "events/task/task_rename/enable" },
140         { OPT,      "events/task/task_newtask/enable" },
141     } },
142     { "irq",        "IRQ Events",   0, {
143         { REQ,      "events/irq/enable" },
144         { OPT,      "events/ipi/enable" },
145     } },
146     { "irqoff",     "IRQ-disabled code section tracing", 0, {
147         { REQ,      "events/preemptirq/irq_enable/enable" },
148         { REQ,      "events/preemptirq/irq_disable/enable" },
149     } },
150     { "preemptoff", "Preempt-disabled code section tracing", 0, {
151         { REQ,      "events/preemptirq/preempt_enable/enable" },
152         { REQ,      "events/preemptirq/preempt_disable/enable" },
153     } },
154     { "i2c",        "I2C Events",   0, {
155         { REQ,      "events/i2c/enable" },
156         { REQ,      "events/i2c/i2c_read/enable" },
157         { REQ,      "events/i2c/i2c_write/enable" },
158         { REQ,      "events/i2c/i2c_result/enable" },
159         { REQ,      "events/i2c/i2c_reply/enable" },
160         { OPT,      "events/i2c/smbus_read/enable" },
161         { OPT,      "events/i2c/smbus_write/enable" },
162         { OPT,      "events/i2c/smbus_result/enable" },
163         { OPT,      "events/i2c/smbus_reply/enable" },
164     } },
165     { "freq",       "CPU Frequency",    0, {
166         { REQ,      "events/power/cpu_frequency/enable" },
167         { OPT,      "events/power/clock_set_rate/enable" },
168         { OPT,      "events/power/clock_disable/enable" },
169         { OPT,      "events/power/clock_enable/enable" },
170         { OPT,      "events/clk/clk_set_rate/enable" },
171         { OPT,      "events/clk/clk_disable/enable" },
172         { OPT,      "events/clk/clk_enable/enable" },
173         { OPT,      "events/power/cpu_frequency_limits/enable" },
174     } },
175     { "membus",     "Memory Bus Utilization", 0, {
176         { REQ,      "events/memory_bus/enable" },
177     } },
178     { "idle",       "CPU Idle",         0, {
179         { REQ,      "events/power/cpu_idle/enable" },
180     } },
181     { "disk",       "Disk I/O",         0, {
182         { OPT,      "events/f2fs/f2fs_sync_file_enter/enable" },
183         { OPT,      "events/f2fs/f2fs_sync_file_exit/enable" },
184         { OPT,      "events/f2fs/f2fs_write_begin/enable" },
185         { OPT,      "events/f2fs/f2fs_write_end/enable" },
186         { OPT,      "events/ext4/ext4_da_write_begin/enable" },
187         { OPT,      "events/ext4/ext4_da_write_end/enable" },
188         { OPT,      "events/ext4/ext4_sync_file_enter/enable" },
189         { OPT,      "events/ext4/ext4_sync_file_exit/enable" },
190         { REQ,      "events/block/block_rq_issue/enable" },
191         { REQ,      "events/block/block_rq_complete/enable" },
192     } },
193     { "mmc",        "eMMC commands",    0, {
194         { REQ,      "events/mmc/enable" },
195     } },
196     { "load",       "CPU Load",         0, {
197         { REQ,      "events/cpufreq_interactive/enable" },
198     } },
199     { "sync",       "Synchronization",  0, {
200         // linux kernel < 4.9
201         { OPT,      "events/sync/enable" },
202         // linux kernel == 4.9.x
203         { OPT,      "events/fence/enable" },
204         // linux kernel > 4.9
205         { OPT,      "events/dma_fence/enable" },
206     } },
207     { "workq",      "Kernel Workqueues", 0, {
208         { REQ,      "events/workqueue/enable" },
209     } },
210     { "memreclaim", "Kernel Memory Reclaim", 0, {
211         { REQ,      "events/vmscan/mm_vmscan_direct_reclaim_begin/enable" },
212         { REQ,      "events/vmscan/mm_vmscan_direct_reclaim_end/enable" },
213         { REQ,      "events/vmscan/mm_vmscan_kswapd_wake/enable" },
214         { REQ,      "events/vmscan/mm_vmscan_kswapd_sleep/enable" },
215         { OPT,      "events/lowmemorykiller/enable" },
216     } },
217     { "regulators",  "Voltage and Current Regulators", 0, {
218         { REQ,      "events/regulator/enable" },
219     } },
220     { "binder_driver", "Binder Kernel driver", 0, {
221         { REQ,      "events/binder/binder_transaction/enable" },
222         { REQ,      "events/binder/binder_transaction_received/enable" },
223         { REQ,      "events/binder/binder_transaction_alloc_buf/enable" },
224         { OPT,      "events/binder/binder_set_priority/enable" },
225     } },
226     { "binder_lock", "Binder global lock trace", 0, {
227         { OPT,      "events/binder/binder_lock/enable" },
228         { OPT,      "events/binder/binder_locked/enable" },
229         { OPT,      "events/binder/binder_unlock/enable" },
230     } },
231     { "pagecache",  "Page cache", 0, {
232         { REQ,      "events/filemap/enable" },
233     } },
234     { "memory",  "Memory", 0, {
235         { OPT,      "events/kmem/rss_stat/enable" },
236         { OPT,      "events/kmem/ion_heap_grow/enable" },
237         { OPT,      "events/kmem/ion_heap_shrink/enable" },
238     } },
239 };
240 
241 struct TracingVendorCategory {
242     // The name identifying the category.
243     std::string name;
244 
245     // A longer description of the category.
246     std::string description;
247 
248     // If the category is enabled through command.
249     bool enabled;
250 
TracingVendorCategoryTracingVendorCategory251     TracingVendorCategory(string &&name, string &&description, bool enabled)
252             : name(std::move(name))
253             , description(std::move(description))
254             , enabled(enabled)
255     {}
256 };
257 
258 /* Command line options */
259 static int g_traceDurationSeconds = 5;
260 static bool g_traceOverwrite = false;
261 static int g_traceBufferSizeKB = 2048;
262 static bool g_compress = false;
263 static bool g_nohup = false;
264 static int g_initialSleepSecs = 0;
265 static const char* g_categoriesFile = nullptr;
266 static const char* g_kernelTraceFuncs = nullptr;
267 static const char* g_debugAppCmdLine = "";
268 static const char* g_outputFile = nullptr;
269 
270 /* Global state */
271 static bool g_tracePdx = false;
272 static bool g_traceAborted = false;
273 static bool g_categoryEnables[arraysize(k_categories)] = {};
274 static std::string g_traceFolder;
275 static sp<IAtraceDevice> g_atraceHal;
276 static std::vector<TracingVendorCategory> g_vendorCategories;
277 
278 /* Sys file paths */
279 static const char* k_traceClockPath =
280     "trace_clock";
281 
282 static const char* k_traceBufferSizePath =
283     "buffer_size_kb";
284 
285 #if 0
286 // TODO: Re-enable after stabilization
287 static const char* k_traceCmdlineSizePath =
288     "saved_cmdlines_size";
289 #endif
290 
291 static const char* k_tracingOverwriteEnablePath =
292     "options/overwrite";
293 
294 static const char* k_currentTracerPath =
295     "current_tracer";
296 
297 static const char* k_printTgidPath =
298     "options/print-tgid";
299 
300 static const char* k_recordTgidPath =
301     "options/record-tgid";
302 
303 static const char* k_funcgraphAbsTimePath =
304     "options/funcgraph-abstime";
305 
306 static const char* k_funcgraphCpuPath =
307     "options/funcgraph-cpu";
308 
309 static const char* k_funcgraphProcPath =
310     "options/funcgraph-proc";
311 
312 static const char* k_funcgraphFlatPath =
313     "options/funcgraph-flat";
314 
315 static const char* k_ftraceFilterPath =
316     "set_ftrace_filter";
317 
318 static const char* k_tracingOnPath =
319     "tracing_on";
320 
321 static const char* k_tracePath =
322     "trace";
323 
324 static const char* k_traceStreamPath =
325     "trace_pipe";
326 
327 static const char* k_traceMarkerPath =
328     "trace_marker";
329 
330 // Check whether a file exists.
fileExists(const char * filename)331 static bool fileExists(const char* filename) {
332     return access((g_traceFolder + filename).c_str(), F_OK) != -1;
333 }
334 
335 // Check whether a file is writable.
fileIsWritable(const char * filename)336 static bool fileIsWritable(const char* filename) {
337     return access((g_traceFolder + filename).c_str(), W_OK) != -1;
338 }
339 
340 // Truncate a file.
truncateFile(const char * path)341 static bool truncateFile(const char* path)
342 {
343     // This uses creat rather than truncate because some of the debug kernel
344     // device nodes (e.g. k_ftraceFilterPath) currently aren't changed by
345     // calls to truncate, but they are cleared by calls to creat.
346     int traceFD = creat((g_traceFolder + path).c_str(), 0);
347     if (traceFD == -1) {
348         fprintf(stderr, "error truncating %s: %s (%d)\n", (g_traceFolder + path).c_str(),
349             strerror(errno), errno);
350         return false;
351     }
352 
353     close(traceFD);
354 
355     return true;
356 }
357 
_writeStr(const char * filename,const char * str,int flags)358 static bool _writeStr(const char* filename, const char* str, int flags)
359 {
360     std::string fullFilename = g_traceFolder + filename;
361     int fd = open(fullFilename.c_str(), flags);
362     if (fd == -1) {
363         fprintf(stderr, "error opening %s: %s (%d)\n", fullFilename.c_str(),
364                 strerror(errno), errno);
365         return false;
366     }
367 
368     bool ok = true;
369     ssize_t len = strlen(str);
370     if (write(fd, str, len) != len) {
371         fprintf(stderr, "error writing to %s: %s (%d)\n", fullFilename.c_str(),
372                 strerror(errno), errno);
373         ok = false;
374     }
375 
376     close(fd);
377 
378     return ok;
379 }
380 
381 // Write a string to a file, returning true if the write was successful.
writeStr(const char * filename,const char * str)382 static bool writeStr(const char* filename, const char* str)
383 {
384     return _writeStr(filename, str, O_WRONLY);
385 }
386 
387 // Append a string to a file, returning true if the write was successful.
appendStr(const char * filename,const char * str)388 static bool appendStr(const char* filename, const char* str)
389 {
390     return _writeStr(filename, str, O_APPEND|O_WRONLY);
391 }
392 
writeClockSyncMarker()393 static void writeClockSyncMarker()
394 {
395   char buffer[128];
396   int len = 0;
397   int fd = open((g_traceFolder + k_traceMarkerPath).c_str(), O_WRONLY);
398   if (fd == -1) {
399       fprintf(stderr, "error opening %s: %s (%d)\n", k_traceMarkerPath,
400               strerror(errno), errno);
401       return;
402   }
403   float now_in_seconds = systemTime(CLOCK_MONOTONIC) / 1000000000.0f;
404 
405   len = snprintf(buffer, 128, "trace_event_clock_sync: parent_ts=%f\n", now_in_seconds);
406   if (write(fd, buffer, len) != len) {
407       fprintf(stderr, "error writing clock sync marker %s (%d)\n", strerror(errno), errno);
408   }
409 
410   int64_t realtime_in_ms = systemTime(CLOCK_REALTIME) / 1000000;
411   len = snprintf(buffer, 128, "trace_event_clock_sync: realtime_ts=%" PRId64 "\n", realtime_in_ms);
412   if (write(fd, buffer, len) != len) {
413       fprintf(stderr, "error writing clock sync marker %s (%d)\n", strerror(errno), errno);
414   }
415 
416   close(fd);
417 }
418 
419 // Enable or disable a kernel option by writing a "1" or a "0" into a /sys
420 // file.
setKernelOptionEnable(const char * filename,bool enable)421 static bool setKernelOptionEnable(const char* filename, bool enable)
422 {
423     return writeStr(filename, enable ? "1" : "0");
424 }
425 
426 // Check whether the category is supported on the device with the current
427 // rootness.  A category is supported only if all its required /sys/ files are
428 // writable and if enabling the category will enable one or more tracing tags
429 // or /sys/ files.
isCategorySupported(const TracingCategory & category)430 static bool isCategorySupported(const TracingCategory& category)
431 {
432     if (strcmp(category.name, k_coreServiceCategory) == 0) {
433         return !android::base::GetProperty(k_coreServicesProp, "").empty();
434     }
435 
436     if (strcmp(category.name, k_pdxServiceCategory) == 0) {
437         return true;
438     }
439 
440     bool ok = category.tags != 0;
441     for (int i = 0; i < MAX_SYS_FILES; i++) {
442         const char* path = category.sysfiles[i].path;
443         bool req = category.sysfiles[i].required == REQ;
444         if (path != nullptr) {
445             if (fileIsWritable(path)) {
446                 ok = true;
447             } else if (req) {
448                 return false;
449             }
450         }
451     }
452     return ok;
453 }
454 
455 // Check whether the category would be supported on the device if the user
456 // were root.  This function assumes that root is able to write to any file
457 // that exists.  It performs the same logic as isCategorySupported, but it
458 // uses file existence rather than writability in the /sys/ file checks.
isCategorySupportedForRoot(const TracingCategory & category)459 static bool isCategorySupportedForRoot(const TracingCategory& category)
460 {
461     bool ok = category.tags != 0;
462     for (int i = 0; i < MAX_SYS_FILES; i++) {
463         const char* path = category.sysfiles[i].path;
464         bool req = category.sysfiles[i].required == REQ;
465         if (path != nullptr) {
466             if (req) {
467                 if (!fileExists(path)) {
468                     return false;
469                 } else {
470                     ok = true;
471                 }
472             } else {
473                 ok |= fileExists(path);
474             }
475         }
476     }
477     return ok;
478 }
479 
480 // Enable or disable overwriting of the kernel trace buffers.  Disabling this
481 // will cause tracing to stop once the trace buffers have filled up.
setTraceOverwriteEnable(bool enable)482 static bool setTraceOverwriteEnable(bool enable)
483 {
484     return setKernelOptionEnable(k_tracingOverwriteEnablePath, enable);
485 }
486 
487 // Set the user initiated trace property
setUserInitiatedTraceProperty(bool enable)488 static bool setUserInitiatedTraceProperty(bool enable)
489 {
490     if (!android::base::SetProperty(k_userInitiatedTraceProperty, enable ? "1" : "")) {
491         fprintf(stderr, "error setting user initiated strace system property\n");
492         return false;
493     }
494     return true;
495 }
496 
497 // Enable or disable kernel tracing.
setTracingEnabled(bool enable)498 static bool setTracingEnabled(bool enable)
499 {
500     return setKernelOptionEnable(k_tracingOnPath, enable);
501 }
502 
503 // Clear the contents of the kernel trace.
clearTrace()504 static bool clearTrace()
505 {
506     return truncateFile(k_tracePath);
507 }
508 
509 // Set the size of the kernel's trace buffer in kilobytes.
setTraceBufferSizeKB(int size)510 static bool setTraceBufferSizeKB(int size)
511 {
512     char str[32] = "1";
513     if (size < 1) {
514         size = 1;
515     }
516     snprintf(str, 32, "%d", size);
517     return writeStr(k_traceBufferSizePath, str);
518 }
519 
520 #if 0
521 // TODO: Re-enable after stabilization
522 // Set the default size of cmdline hashtable
523 static bool setCmdlineSize()
524 {
525     if (fileExists(k_traceCmdlineSizePath)) {
526         return writeStr(k_traceCmdlineSizePath, "8192");
527     }
528     return true;
529 }
530 #endif
531 
532 // Set the clock to the best available option while tracing. Use 'boot' if it's
533 // available; otherwise, use 'mono'. If neither are available use 'global'.
534 // Any write to the trace_clock sysfs file will reset the buffer, so only
535 // update it if the requested value is not the current value.
setClock()536 static bool setClock()
537 {
538     std::ifstream clockFile((g_traceFolder + k_traceClockPath).c_str(), O_RDONLY);
539     std::string clockStr((std::istreambuf_iterator<char>(clockFile)),
540         std::istreambuf_iterator<char>());
541 
542     std::string newClock;
543     if (clockStr.find("boot") != std::string::npos) {
544         newClock = "boot";
545     } else if (clockStr.find("mono") != std::string::npos) {
546         newClock = "mono";
547     } else {
548         newClock = "global";
549     }
550 
551     size_t begin = clockStr.find('[') + 1;
552     size_t end = clockStr.find(']');
553     if (newClock.compare(0, std::string::npos, clockStr, begin, end-begin) == 0) {
554         return true;
555     }
556     return writeStr(k_traceClockPath, newClock.c_str());
557 }
558 
setPrintTgidEnableIfPresent(bool enable)559 static bool setPrintTgidEnableIfPresent(bool enable)
560 {
561     // Pre-4.13 this was options/print-tgid as an android-specific option.
562     // In 4.13+ this is an upstream option called options/record-tgid
563     // Both options produce the same ftrace format change
564     if (fileExists(k_printTgidPath)) {
565         return setKernelOptionEnable(k_printTgidPath, enable);
566     }
567     if (fileExists(k_recordTgidPath)) {
568         return setKernelOptionEnable(k_recordTgidPath, enable);
569     }
570     return true;
571 }
572 
573 // Poke all the binder-enabled processes in the system to get them to re-read
574 // their system properties.
pokeBinderServices()575 static bool pokeBinderServices()
576 {
577     sp<IServiceManager> sm = defaultServiceManager();
578     Vector<String16> services = sm->listServices();
579     for (size_t i = 0; i < services.size(); i++) {
580         sp<IBinder> obj = sm->checkService(services[i]);
581         if (obj != nullptr) {
582             Parcel data;
583             if (obj->transact(IBinder::SYSPROPS_TRANSACTION, data,
584                     nullptr, 0) != OK) {
585                 if (false) {
586                     // XXX: For some reason this fails on tablets trying to
587                     // poke the "phone" service.  It's not clear whether some
588                     // are expected to fail.
589                     String8 svc(services[i]);
590                     fprintf(stderr, "error poking binder service %s\n",
591                         svc.string());
592                     return false;
593                 }
594             }
595         }
596     }
597     return true;
598 }
599 
600 // Poke all the HAL processes in the system to get them to re-read
601 // their system properties.
pokeHalServices()602 static void pokeHalServices()
603 {
604     using ::android::hidl::base::V1_0::IBase;
605     using ::android::hidl::manager::V1_0::IServiceManager;
606     using ::android::hardware::hidl_string;
607     using ::android::hardware::Return;
608 
609     sp<IServiceManager> sm = ::android::hardware::defaultServiceManager();
610 
611     if (sm == nullptr) {
612         fprintf(stderr, "failed to get IServiceManager to poke hal services\n");
613         return;
614     }
615 
616     auto listRet = sm->list([&](const auto &interfaces) {
617         for (size_t i = 0; i < interfaces.size(); i++) {
618             string fqInstanceName = interfaces[i];
619             string::size_type n = fqInstanceName.find('/');
620             if (n == std::string::npos || interfaces[i].size() == n+1)
621                 continue;
622             hidl_string fqInterfaceName = fqInstanceName.substr(0, n);
623             hidl_string instanceName = fqInstanceName.substr(n+1, std::string::npos);
624             Return<sp<IBase>> interfaceRet = sm->get(fqInterfaceName, instanceName);
625             if (!interfaceRet.isOk()) {
626                 // ignore
627                 continue;
628             }
629 
630             sp<IBase> interface = interfaceRet;
631             if (interface == nullptr) {
632                 // ignore
633                 continue;
634             }
635 
636             auto notifyRet = interface->notifySyspropsChanged();
637             if (!notifyRet.isOk()) {
638                 // ignore
639             }
640         }
641     });
642     if (!listRet.isOk()) {
643         // TODO(b/34242478) fix this when we determine the correct ACL
644         //fprintf(stderr, "failed to list services: %s\n", listRet.description().c_str());
645     }
646 }
647 
648 // Set the trace tags that userland tracing uses, and poke the running
649 // processes to pick up the new value.
setTagsProperty(uint64_t tags)650 static bool setTagsProperty(uint64_t tags)
651 {
652     std::string value = android::base::StringPrintf("%#" PRIx64, tags);
653     if (!android::base::SetProperty(k_traceTagsProperty, value)) {
654         fprintf(stderr, "error setting trace tags system property\n");
655         return false;
656     }
657     return true;
658 }
659 
clearAppProperties()660 static void clearAppProperties()
661 {
662     if (!android::base::SetProperty(k_traceAppsNumberProperty, "")) {
663         fprintf(stderr, "failed to clear system property: %s",
664               k_traceAppsNumberProperty);
665     }
666 }
667 
668 // Set the system property that indicates which apps should perform
669 // application-level tracing.
setAppCmdlineProperty(char * cmdline)670 static bool setAppCmdlineProperty(char* cmdline)
671 {
672     int i = 0;
673     char* start = cmdline;
674     while (start != nullptr) {
675         char* end = strchr(start, ',');
676         if (end != nullptr) {
677             *end = '\0';
678             end++;
679         }
680         std::string key = android::base::StringPrintf(k_traceAppsPropertyTemplate, i);
681         if (!android::base::SetProperty(key, start)) {
682             fprintf(stderr, "error setting trace app %d property to %s\n", i, key.c_str());
683             clearAppProperties();
684             return false;
685         }
686         start = end;
687         i++;
688     }
689 
690     std::string value = android::base::StringPrintf("%d", i);
691     if (!android::base::SetProperty(k_traceAppsNumberProperty, value)) {
692         fprintf(stderr, "error setting trace app number property to %s\n", value.c_str());
693         clearAppProperties();
694         return false;
695     }
696     return true;
697 }
698 
699 // Disable all /sys/ enable files.
disableKernelTraceEvents()700 static bool disableKernelTraceEvents() {
701     bool ok = true;
702     for (size_t i = 0; i < arraysize(k_categories); i++) {
703         const TracingCategory &c = k_categories[i];
704         for (int j = 0; j < MAX_SYS_FILES; j++) {
705             const char* path = c.sysfiles[j].path;
706             if (path != nullptr && fileIsWritable(path)) {
707                 ok &= setKernelOptionEnable(path, false);
708             }
709         }
710     }
711     return ok;
712 }
713 
714 // Verify that the comma separated list of functions are being traced by the
715 // kernel.
verifyKernelTraceFuncs(const char * funcs)716 static bool verifyKernelTraceFuncs(const char* funcs)
717 {
718     std::string buf;
719     if (!android::base::ReadFileToString(g_traceFolder + k_ftraceFilterPath, &buf)) {
720          fprintf(stderr, "error opening %s: %s (%d)\n", k_ftraceFilterPath,
721             strerror(errno), errno);
722          return false;
723     }
724 
725     String8 funcList = String8::format("\n%s",buf.c_str());
726 
727     // Make sure that every function listed in funcs is in the list we just
728     // read from the kernel, except for wildcard inputs.
729     bool ok = true;
730     char* myFuncs = strdup(funcs);
731     char* func = strtok(myFuncs, ",");
732     while (func) {
733         if (!strchr(func, '*')) {
734             String8 fancyFunc = String8::format("\n%s\n", func);
735             bool found = funcList.find(fancyFunc.string(), 0) >= 0;
736             if (!found || func[0] == '\0') {
737                 fprintf(stderr, "error: \"%s\" is not a valid kernel function "
738                         "to trace.\n", func);
739                 ok = false;
740             }
741         }
742         func = strtok(nullptr, ",");
743     }
744     free(myFuncs);
745     return ok;
746 }
747 
748 // Set the comma separated list of functions that the kernel is to trace.
setKernelTraceFuncs(const char * funcs)749 static bool setKernelTraceFuncs(const char* funcs)
750 {
751     bool ok = true;
752 
753     if (funcs == nullptr || funcs[0] == '\0') {
754         // Disable kernel function tracing.
755         if (fileIsWritable(k_currentTracerPath)) {
756             ok &= writeStr(k_currentTracerPath, "nop");
757         }
758         if (fileIsWritable(k_ftraceFilterPath)) {
759             ok &= truncateFile(k_ftraceFilterPath);
760         }
761     } else {
762         // Enable kernel function tracing.
763         ok &= writeStr(k_currentTracerPath, "function_graph");
764         ok &= setKernelOptionEnable(k_funcgraphAbsTimePath, true);
765         ok &= setKernelOptionEnable(k_funcgraphCpuPath, true);
766         ok &= setKernelOptionEnable(k_funcgraphProcPath, true);
767         ok &= setKernelOptionEnable(k_funcgraphFlatPath, true);
768 
769         // Set the requested filter functions.
770         ok &= truncateFile(k_ftraceFilterPath);
771         char* myFuncs = strdup(funcs);
772         char* func = strtok(myFuncs, ",");
773         while (func) {
774             ok &= appendStr(k_ftraceFilterPath, func);
775             func = strtok(nullptr, ",");
776         }
777         free(myFuncs);
778 
779         // Verify that the set functions are being traced.
780         if (ok) {
781             ok &= verifyKernelTraceFuncs(funcs);
782         }
783     }
784 
785     return ok;
786 }
787 
setCategoryEnable(const char * name)788 static bool setCategoryEnable(const char* name)
789 {
790     bool vendor_found = false;
791     for (auto &c : g_vendorCategories) {
792         if (strcmp(name, c.name.c_str()) == 0) {
793             c.enabled = true;
794             vendor_found = true;
795         }
796     }
797     for (size_t i = 0; i < arraysize(k_categories); i++) {
798         const TracingCategory& c = k_categories[i];
799         if (strcmp(name, c.name) == 0) {
800             if (isCategorySupported(c)) {
801                 g_categoryEnables[i] = true;
802                 return true;
803             } else {
804                 if (isCategorySupportedForRoot(c)) {
805                     fprintf(stderr, "error: category \"%s\" requires root "
806                             "privileges.\n", name);
807                 } else {
808                     fprintf(stderr, "error: category \"%s\" is not supported "
809                             "on this device.\n", name);
810                 }
811                 return false;
812             }
813         }
814     }
815     if (vendor_found) {
816         return true;
817     }
818     fprintf(stderr, "error: unknown tracing category \"%s\"\n", name);
819     return false;
820 }
821 
setCategoriesEnableFromFile(const char * categories_file)822 static bool setCategoriesEnableFromFile(const char* categories_file)
823 {
824     if (!categories_file) {
825         return true;
826     }
827     Tokenizer* tokenizer = nullptr;
828     if (Tokenizer::open(String8(categories_file), &tokenizer) != NO_ERROR) {
829         return false;
830     }
831     bool ok = true;
832     while (!tokenizer->isEol()) {
833         String8 token = tokenizer->nextToken(" ");
834         if (token.isEmpty()) {
835             tokenizer->skipDelimiters(" ");
836             continue;
837         }
838         ok &= setCategoryEnable(token.string());
839     }
840     delete tokenizer;
841     return ok;
842 }
843 
setUpUserspaceTracing()844 static bool setUpUserspaceTracing()
845 {
846     bool ok = true;
847 
848     // Set up the tags property.
849     uint64_t tags = 0;
850     for (size_t i = 0; i < arraysize(k_categories); i++) {
851         if (g_categoryEnables[i]) {
852             const TracingCategory &c = k_categories[i];
853             tags |= c.tags;
854         }
855     }
856     ok &= setTagsProperty(tags);
857 
858     bool coreServicesTagEnabled = false;
859     for (size_t i = 0; i < arraysize(k_categories); i++) {
860         if (strcmp(k_categories[i].name, k_coreServiceCategory) == 0) {
861             coreServicesTagEnabled = g_categoryEnables[i];
862         }
863 
864         // Set whether to poke PDX services in this session.
865         if (strcmp(k_categories[i].name, k_pdxServiceCategory) == 0) {
866             g_tracePdx = g_categoryEnables[i];
867         }
868     }
869 
870     std::string packageList(g_debugAppCmdLine);
871     if (coreServicesTagEnabled) {
872         if (!packageList.empty()) {
873             packageList += ",";
874         }
875         packageList += android::base::GetProperty(k_coreServicesProp, "");
876     }
877     ok &= setAppCmdlineProperty(&packageList[0]);
878     ok &= pokeBinderServices();
879     pokeHalServices();
880 
881     if (g_tracePdx) {
882         ok &= ServiceUtility::PokeServices();
883     }
884 
885     return ok;
886 }
887 
cleanUpUserspaceTracing()888 static void cleanUpUserspaceTracing()
889 {
890     setTagsProperty(0);
891     clearAppProperties();
892     pokeBinderServices();
893     pokeHalServices();
894 
895     if (g_tracePdx) {
896         ServiceUtility::PokeServices();
897     }
898 }
899 
900 
901 // Set all the kernel tracing settings to the desired state for this trace
902 // capture.
setUpKernelTracing()903 static bool setUpKernelTracing()
904 {
905     bool ok = true;
906 
907     ok &= setUserInitiatedTraceProperty(true);
908 
909     // Set up the tracing options.
910     ok &= setCategoriesEnableFromFile(g_categoriesFile);
911     ok &= setTraceOverwriteEnable(g_traceOverwrite);
912     ok &= setTraceBufferSizeKB(g_traceBufferSizeKB);
913     // TODO: Re-enable after stabilization
914     //ok &= setCmdlineSize();
915     ok &= setClock();
916     ok &= setPrintTgidEnableIfPresent(true);
917     ok &= setKernelTraceFuncs(g_kernelTraceFuncs);
918 
919     // Disable all the sysfs enables.  This is done as a separate loop from
920     // the enables to allow the same enable to exist in multiple categories.
921     ok &= disableKernelTraceEvents();
922 
923     // Enable all the sysfs enables that are in an enabled category.
924     for (size_t i = 0; i < arraysize(k_categories); i++) {
925         if (g_categoryEnables[i]) {
926             const TracingCategory &c = k_categories[i];
927             for (int j = 0; j < MAX_SYS_FILES; j++) {
928                 const char* path = c.sysfiles[j].path;
929                 bool required = c.sysfiles[j].required == REQ;
930                 if (path != nullptr) {
931                     if (fileIsWritable(path)) {
932                         ok &= setKernelOptionEnable(path, true);
933                     } else if (required) {
934                         fprintf(stderr, "error writing file %s\n", path);
935                         ok = false;
936                     }
937                 }
938             }
939         }
940     }
941 
942     return ok;
943 }
944 
945 // Reset all the kernel tracing settings to their default state.
cleanUpKernelTracing()946 static void cleanUpKernelTracing()
947 {
948     // Disable all tracing that we're able to.
949     disableKernelTraceEvents();
950 
951     // Set the options back to their defaults.
952     setTraceOverwriteEnable(true);
953     setTraceBufferSizeKB(1);
954     setPrintTgidEnableIfPresent(false);
955     setKernelTraceFuncs(nullptr);
956     setUserInitiatedTraceProperty(false);
957 }
958 
959 // Enable tracing in the kernel.
startTrace()960 static bool startTrace()
961 {
962     return setTracingEnabled(true);
963 }
964 
965 // Disable tracing in the kernel.
stopTrace()966 static void stopTrace()
967 {
968     setTracingEnabled(false);
969 }
970 
971 // Read data from the tracing pipe and forward to stdout
streamTrace()972 static void streamTrace()
973 {
974     char trace_data[4096];
975     int traceFD = open((g_traceFolder + k_traceStreamPath).c_str(), O_RDWR);
976     if (traceFD == -1) {
977         fprintf(stderr, "error opening %s: %s (%d)\n", k_traceStreamPath,
978                 strerror(errno), errno);
979         return;
980     }
981     while (!g_traceAborted) {
982         ssize_t bytes_read = read(traceFD, trace_data, 4096);
983         if (bytes_read > 0) {
984             write(STDOUT_FILENO, trace_data, bytes_read);
985             fflush(stdout);
986         } else {
987             if (!g_traceAborted) {
988                 fprintf(stderr, "read returned %zd bytes err %d (%s)\n",
989                         bytes_read, errno, strerror(errno));
990             }
991             break;
992         }
993     }
994 }
995 
996 // Read the current kernel trace and write it to stdout.
dumpTrace(int outFd)997 static void dumpTrace(int outFd)
998 {
999     ALOGI("Dumping trace");
1000     int traceFD = open((g_traceFolder + k_tracePath).c_str(), O_RDWR);
1001     if (traceFD == -1) {
1002         fprintf(stderr, "error opening %s: %s (%d)\n", k_tracePath,
1003                 strerror(errno), errno);
1004         return;
1005     }
1006 
1007     if (g_compress) {
1008         z_stream zs;
1009         memset(&zs, 0, sizeof(zs));
1010 
1011         int result = deflateInit(&zs, Z_DEFAULT_COMPRESSION);
1012         if (result != Z_OK) {
1013             fprintf(stderr, "error initializing zlib: %d\n", result);
1014             close(traceFD);
1015             return;
1016         }
1017 
1018         constexpr size_t bufSize = 64*1024;
1019         std::unique_ptr<uint8_t> in(new uint8_t[bufSize]);
1020         std::unique_ptr<uint8_t> out(new uint8_t[bufSize]);
1021         if (!in || !out) {
1022             fprintf(stderr, "couldn't allocate buffers\n");
1023             close(traceFD);
1024             return;
1025         }
1026 
1027         int flush = Z_NO_FLUSH;
1028 
1029         zs.next_out = reinterpret_cast<Bytef*>(out.get());
1030         zs.avail_out = bufSize;
1031 
1032         do {
1033 
1034             if (zs.avail_in == 0) {
1035                 // More input is needed.
1036                 result = read(traceFD, in.get(), bufSize);
1037                 if (result < 0) {
1038                     fprintf(stderr, "error reading trace: %s (%d)\n",
1039                             strerror(errno), errno);
1040                     result = Z_STREAM_END;
1041                     break;
1042                 } else if (result == 0) {
1043                     flush = Z_FINISH;
1044                 } else {
1045                     zs.next_in = reinterpret_cast<Bytef*>(in.get());
1046                     zs.avail_in = result;
1047                 }
1048             }
1049 
1050             if (zs.avail_out == 0) {
1051                 // Need to write the output.
1052                 result = write(outFd, out.get(), bufSize);
1053                 if ((size_t)result < bufSize) {
1054                     fprintf(stderr, "error writing deflated trace: %s (%d)\n",
1055                             strerror(errno), errno);
1056                     result = Z_STREAM_END; // skip deflate error message
1057                     zs.avail_out = bufSize; // skip the final write
1058                     break;
1059                 }
1060                 zs.next_out = reinterpret_cast<Bytef*>(out.get());
1061                 zs.avail_out = bufSize;
1062             }
1063 
1064         } while ((result = deflate(&zs, flush)) == Z_OK);
1065 
1066         if (result != Z_STREAM_END) {
1067             fprintf(stderr, "error deflating trace: %s\n", zs.msg);
1068         }
1069 
1070         if (zs.avail_out < bufSize) {
1071             size_t bytes = bufSize - zs.avail_out;
1072             result = write(outFd, out.get(), bytes);
1073             if ((size_t)result < bytes) {
1074                 fprintf(stderr, "error writing deflated trace: %s (%d)\n",
1075                         strerror(errno), errno);
1076             }
1077         }
1078 
1079         result = deflateEnd(&zs);
1080         if (result != Z_OK) {
1081             fprintf(stderr, "error cleaning up zlib: %d\n", result);
1082         }
1083     } else {
1084         char buf[4096];
1085         ssize_t rc;
1086         while ((rc = TEMP_FAILURE_RETRY(read(traceFD, buf, sizeof(buf)))) > 0) {
1087             if (!android::base::WriteFully(outFd, buf, rc)) {
1088                 fprintf(stderr, "error writing trace: %s\n", strerror(errno));
1089                 break;
1090             }
1091         }
1092         if (rc == -1) {
1093             fprintf(stderr, "error dumping trace: %s\n", strerror(errno));
1094         }
1095     }
1096 
1097     close(traceFD);
1098 }
1099 
handleSignal(int)1100 static void handleSignal(int /*signo*/)
1101 {
1102     if (!g_nohup) {
1103         g_traceAborted = true;
1104     }
1105 }
1106 
registerSigHandler()1107 static void registerSigHandler()
1108 {
1109     struct sigaction sa;
1110     sigemptyset(&sa.sa_mask);
1111     sa.sa_flags = 0;
1112     sa.sa_handler = handleSignal;
1113     sigaction(SIGHUP, &sa, nullptr);
1114     sigaction(SIGINT, &sa, nullptr);
1115     sigaction(SIGQUIT, &sa, nullptr);
1116     sigaction(SIGTERM, &sa, nullptr);
1117 }
1118 
listSupportedCategories()1119 static void listSupportedCategories()
1120 {
1121     for (size_t i = 0; i < arraysize(k_categories); i++) {
1122         const TracingCategory& c = k_categories[i];
1123         if (isCategorySupported(c)) {
1124             printf("  %10s - %s\n", c.name, c.longname);
1125         }
1126     }
1127     for (const auto &c : g_vendorCategories) {
1128         printf("  %10s - %s (HAL)\n", c.name.c_str(), c.description.c_str());
1129     }
1130 }
1131 
1132 // Print the command usage help to stderr.
showHelp(const char * cmd)1133 static void showHelp(const char *cmd)
1134 {
1135     fprintf(stderr, "usage: %s [options] [categories...]\n", cmd);
1136     fprintf(stderr, "options include:\n"
1137                     "  -a appname      enable app-level tracing for a comma "
1138                         "separated list of cmdlines; * is a wildcard matching any process\n"
1139                     "  -b N            use a trace buffer size of N KB\n"
1140                     "  -c              trace into a circular buffer\n"
1141                     "  -f filename     use the categories written in a file as space-separated\n"
1142                     "                    values in a line\n"
1143                     "  -k fname,...    trace the listed kernel functions\n"
1144                     "  -n              ignore signals\n"
1145                     "  -s N            sleep for N seconds before tracing [default 0]\n"
1146                     "  -t N            trace for N seconds [default 5]\n"
1147                     "  -z              compress the trace dump\n"
1148                     "  --async_start   start circular trace and return immediately\n"
1149                     "  --async_dump    dump the current contents of circular trace buffer\n"
1150                     "  --async_stop    stop tracing and dump the current contents of circular\n"
1151                     "                    trace buffer\n"
1152                     "  --stream        stream trace to stdout as it enters the trace buffer\n"
1153                     "                    Note: this can take significant CPU time, and is best\n"
1154                     "                    used for measuring things that are not affected by\n"
1155                     "                    CPU performance, like pagecache usage.\n"
1156                     "  --list_categories\n"
1157                     "                  list the available tracing categories\n"
1158                     " -o filename      write the trace to the specified file instead\n"
1159                     "                    of stdout.\n"
1160             );
1161 }
1162 
findTraceFiles()1163 bool findTraceFiles()
1164 {
1165     static const std::string debugfs_path = "/sys/kernel/debug/tracing/";
1166     static const std::string tracefs_path = "/sys/kernel/tracing/";
1167     static const std::string trace_file = "trace_marker";
1168 
1169     bool tracefs = access((tracefs_path + trace_file).c_str(), F_OK) != -1;
1170     bool debugfs = access((debugfs_path + trace_file).c_str(), F_OK) != -1;
1171 
1172     if (!tracefs && !debugfs) {
1173         fprintf(stderr, "Error: Did not find trace folder\n");
1174         return false;
1175     }
1176 
1177     if (tracefs) {
1178         g_traceFolder = tracefs_path;
1179     } else {
1180         g_traceFolder = debugfs_path;
1181     }
1182 
1183     return true;
1184 }
1185 
initVendorCategories()1186 void initVendorCategories()
1187 {
1188     g_atraceHal = IAtraceDevice::getService();
1189 
1190     if (g_atraceHal == nullptr) {
1191         // No atrace HAL
1192         return;
1193     }
1194 
1195     Return<void> ret = g_atraceHal->listCategories(
1196         [](const auto& list) {
1197             g_vendorCategories.reserve(list.size());
1198             for (const auto& category : list) {
1199                 g_vendorCategories.emplace_back(category.name, category.description, false);
1200             }
1201         });
1202     if (!ret.isOk()) {
1203         fprintf(stderr, "calling atrace HAL failed: %s\n", ret.description().c_str());
1204     }
1205 }
1206 
setUpVendorTracing()1207 static bool setUpVendorTracing()
1208 {
1209     if (g_atraceHal == nullptr) {
1210         // No atrace HAL
1211         return true;
1212     }
1213 
1214     std::vector<hidl_string> categories;
1215     for (const auto &c : g_vendorCategories) {
1216         if (c.enabled) {
1217             categories.emplace_back(c.name);
1218         }
1219     }
1220 
1221     if (!categories.size()) {
1222         return true;
1223     }
1224 
1225     auto ret = g_atraceHal->enableCategories(categories);
1226     if (!ret.isOk()) {
1227         fprintf(stderr, "calling atrace HAL failed: %s\n", ret.description().c_str());
1228         return false;
1229     } else if (ret != Status::SUCCESS) {
1230         fprintf(stderr, "calling atrace HAL failed: %s\n", toString(ret).c_str());
1231         return false;
1232     }
1233     return true;
1234 }
1235 
cleanUpVendorTracing()1236 static bool cleanUpVendorTracing()
1237 {
1238     if (g_atraceHal == nullptr) {
1239         // No atrace HAL
1240         return true;
1241     }
1242 
1243     if (!g_vendorCategories.size()) {
1244         // No vendor categories
1245         return true;
1246     }
1247 
1248     auto ret = g_atraceHal->disableAllCategories();
1249     if (!ret.isOk()) {
1250         fprintf(stderr, "calling atrace HAL failed: %s\n", ret.description().c_str());
1251         return false;
1252     } else if (ret != Status::SUCCESS) {
1253         fprintf(stderr, "calling atrace HAL failed: %s\n", toString(ret).c_str());
1254         return false;
1255     }
1256     return true;
1257 }
1258 
main(int argc,char ** argv)1259 int main(int argc, char **argv)
1260 {
1261     bool async = false;
1262     bool traceStart = true;
1263     bool traceStop = true;
1264     bool traceDump = true;
1265     bool traceStream = false;
1266     bool onlyUserspace = false;
1267 
1268     if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
1269         showHelp(argv[0]);
1270         exit(0);
1271     }
1272 
1273     if (!findTraceFiles()) {
1274         fprintf(stderr, "No trace folder found\n");
1275         exit(-1);
1276     }
1277 
1278     initVendorCategories();
1279 
1280     for (;;) {
1281         int ret;
1282         int option_index = 0;
1283         static struct option long_options[] = {
1284             {"async_start",       no_argument, nullptr,  0 },
1285             {"async_stop",        no_argument, nullptr,  0 },
1286             {"async_dump",        no_argument, nullptr,  0 },
1287             {"only_userspace",    no_argument, nullptr,  0 },
1288             {"list_categories",   no_argument, nullptr,  0 },
1289             {"stream",            no_argument, nullptr,  0 },
1290             {nullptr,                       0, nullptr,  0 }
1291         };
1292 
1293         ret = getopt_long(argc, argv, "a:b:cf:k:ns:t:zo:",
1294                           long_options, &option_index);
1295 
1296         if (ret < 0) {
1297             for (int i = optind; i < argc; i++) {
1298                 if (!setCategoryEnable(argv[i])) {
1299                     fprintf(stderr, "error enabling tracing category \"%s\"\n", argv[i]);
1300                     exit(1);
1301                 }
1302             }
1303             break;
1304         }
1305 
1306         switch(ret) {
1307             case 'a':
1308                 g_debugAppCmdLine = optarg;
1309             break;
1310 
1311             case 'b':
1312                 g_traceBufferSizeKB = atoi(optarg);
1313             break;
1314 
1315             case 'c':
1316                 g_traceOverwrite = true;
1317             break;
1318 
1319             case 'f':
1320                 g_categoriesFile = optarg;
1321             break;
1322 
1323             case 'k':
1324                 g_kernelTraceFuncs = optarg;
1325             break;
1326 
1327             case 'n':
1328                 g_nohup = true;
1329             break;
1330 
1331             case 's':
1332                 g_initialSleepSecs = atoi(optarg);
1333             break;
1334 
1335             case 't':
1336                 g_traceDurationSeconds = atoi(optarg);
1337             break;
1338 
1339             case 'z':
1340                 g_compress = true;
1341             break;
1342 
1343             case 'o':
1344                 g_outputFile = optarg;
1345             break;
1346 
1347             case 0:
1348                 if (!strcmp(long_options[option_index].name, "async_start")) {
1349                     async = true;
1350                     traceStop = false;
1351                     traceDump = false;
1352                     g_traceOverwrite = true;
1353                 } else if (!strcmp(long_options[option_index].name, "async_stop")) {
1354                     async = true;
1355                     traceStart = false;
1356                 } else if (!strcmp(long_options[option_index].name, "async_dump")) {
1357                     async = true;
1358                     traceStart = false;
1359                     traceStop = false;
1360                 } else if (!strcmp(long_options[option_index].name, "only_userspace")) {
1361                     onlyUserspace = true;
1362                 } else if (!strcmp(long_options[option_index].name, "stream")) {
1363                     traceStream = true;
1364                     traceDump = false;
1365                 } else if (!strcmp(long_options[option_index].name, "list_categories")) {
1366                     listSupportedCategories();
1367                     exit(0);
1368                 }
1369             break;
1370 
1371             default:
1372                 fprintf(stderr, "\n");
1373                 showHelp(argv[0]);
1374                 exit(-1);
1375             break;
1376         }
1377     }
1378 
1379     if (onlyUserspace) {
1380         if (!async || !(traceStart || traceStop)) {
1381             fprintf(stderr, "--only_userspace can only be used with "
1382                     "--async_start or --async_stop\n");
1383             exit(1);
1384         }
1385     }
1386 
1387     registerSigHandler();
1388 
1389     if (g_initialSleepSecs > 0) {
1390         sleep(g_initialSleepSecs);
1391     }
1392 
1393     bool ok = true;
1394 
1395     if (traceStart) {
1396         ok &= setUpUserspaceTracing();
1397     }
1398 
1399     if (ok && traceStart && !onlyUserspace) {
1400         ok &= setUpKernelTracing();
1401         ok &= setUpVendorTracing();
1402         ok &= startTrace();
1403     }
1404 
1405     if (ok && traceStart) {
1406 
1407         if (!traceStream && !onlyUserspace) {
1408             printf("capturing trace...");
1409             fflush(stdout);
1410         }
1411 
1412         // We clear the trace after starting it because tracing gets enabled for
1413         // each CPU individually in the kernel. Having the beginning of the trace
1414         // contain entries from only one CPU can cause "begin" entries without a
1415         // matching "end" entry to show up if a task gets migrated from one CPU to
1416         // another.
1417         if (!onlyUserspace)
1418             ok = clearTrace();
1419 
1420         writeClockSyncMarker();
1421         if (ok && !async && !traceStream) {
1422             // Sleep to allow the trace to be captured.
1423             struct timespec timeLeft;
1424             timeLeft.tv_sec = g_traceDurationSeconds;
1425             timeLeft.tv_nsec = 0;
1426             do {
1427                 if (g_traceAborted) {
1428                     break;
1429                 }
1430             } while (nanosleep(&timeLeft, &timeLeft) == -1 && errno == EINTR);
1431         }
1432 
1433         if (traceStream) {
1434             streamTrace();
1435         }
1436     }
1437 
1438     // Stop the trace and restore the default settings.
1439     if (traceStop && !onlyUserspace)
1440         stopTrace();
1441 
1442     if (ok && traceDump && !onlyUserspace) {
1443         if (!g_traceAborted) {
1444             printf(" done\n");
1445             fflush(stdout);
1446             int outFd = STDOUT_FILENO;
1447             if (g_outputFile) {
1448                 outFd = open(g_outputFile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1449             }
1450             if (outFd == -1) {
1451                 printf("Failed to open '%s', err=%d", g_outputFile, errno);
1452             } else {
1453                 dprintf(outFd, "TRACE:\n");
1454                 dumpTrace(outFd);
1455                 if (g_outputFile) {
1456                     close(outFd);
1457                 }
1458             }
1459         } else {
1460             printf("\ntrace aborted.\n");
1461             fflush(stdout);
1462         }
1463         clearTrace();
1464     } else if (!ok) {
1465         fprintf(stderr, "unable to start tracing\n");
1466     }
1467 
1468     // Reset the trace buffer size to 1.
1469     if (traceStop) {
1470         cleanUpVendorTracing();
1471         cleanUpUserspaceTracing();
1472         if (!onlyUserspace)
1473             cleanUpKernelTracing();
1474     }
1475 
1476     return g_traceAborted ? 1 : 0;
1477 }
1478