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