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