* limitations under the License.
*/
+ #define LOG_TAG "atrace"
+
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
using namespace android;
-#define LOG_TAG "atrace"
-
#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
enum { MAX_SYS_FILES = 10 };
static const char* g_categoriesFile = NULL;
static const char* g_kernelTraceFuncs = NULL;
static const char* g_debugAppCmdLine = "";
+static const char* g_outputFile = nullptr;
/* Global state */
static bool g_traceAborted = false;
}
// Read the current kernel trace and write it to stdout.
-static void dumpTrace()
+static void dumpTrace(int outFd)
{
ALOGI("Dumping trace");
int traceFD = open(k_tracePath, O_RDWR);
if (zs.avail_out == 0) {
// Need to write the output.
- result = write(STDOUT_FILENO, out, bufSize);
+ result = write(outFd, out, bufSize);
if ((size_t)result < bufSize) {
fprintf(stderr, "error writing deflated trace: %s (%d)\n",
strerror(errno), errno);
if (zs.avail_out < bufSize) {
size_t bytes = bufSize - zs.avail_out;
- result = write(STDOUT_FILENO, out, bytes);
+ result = write(outFd, out, bytes);
if ((size_t)result < bytes) {
fprintf(stderr, "error writing deflated trace: %s (%d)\n",
strerror(errno), errno);
free(out);
} else {
ssize_t sent = 0;
- while ((sent = sendfile(STDOUT_FILENO, traceFD, NULL, 64*1024*1024)) > 0);
+ while ((sent = sendfile(outFd, traceFD, NULL, 64*1024*1024)) > 0);
if (sent == -1) {
fprintf(stderr, "error dumping trace: %s (%d)\n", strerror(errno),
errno);
" CPU performance, like pagecache usage.\n"
" --list_categories\n"
" list the available tracing categories\n"
+ " -o filename write the trace to the specified file instead\n"
+ " of stdout.\n"
);
}
{ 0, 0, 0, 0 }
};
- ret = getopt_long(argc, argv, "a:b:cf:k:ns:t:z",
+ ret = getopt_long(argc, argv, "a:b:cf:k:ns:t:zo:",
long_options, &option_index);
if (ret < 0) {
g_compress = true;
break;
+ case 'o':
+ g_outputFile = optarg;
+ break;
+
case 0:
if (!strcmp(long_options[option_index].name, "async_start")) {
async = true;
if (ok && traceDump) {
if (!g_traceAborted) {
- printf(" done\nTRACE:\n");
+ printf(" done\n");
fflush(stdout);
- dumpTrace();
+ int outFd = STDOUT_FILENO;
+ if (g_outputFile) {
+ outFd = open(g_outputFile, O_WRONLY | O_CREAT);
+ }
+ if (outFd == -1) {
+ printf("Failed to open '%s', err=%d", g_outputFile, errno);
+ } else {
+ dprintf(outFd, "TRACE:\n");
+ dumpTrace(outFd);
+ if (g_outputFile) {
+ close(outFd);
+ }
+ }
} else {
printf("\ntrace aborted.\n");
fflush(stdout);