aboutsummaryrefslogtreecommitdiff
path: root/daemon/AtraceDriver.cpp
blob: 78fe58a5c46ad882d4b2d444131ccc021f7110e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
 * Copyright (C) ARM Limited 2014-2015. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include "AtraceDriver.h"

#include <unistd.h>

/*
#include <regex.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "DriverSource.h"
#include "Setup.h"
*/

#include "Logging.h"
#include "OlyUtility.h"
#include "SessionData.h"

class AtraceCounter : public DriverCounter {
public:
	AtraceCounter(DriverCounter *next, char *name, int flag);
	~AtraceCounter();

	int getFlag() const { return mFlag; }

private:
	const int mFlag;

	// Intentionally unimplemented
	AtraceCounter(const AtraceCounter &);
	AtraceCounter &operator=(const AtraceCounter &);
};

AtraceCounter::AtraceCounter(DriverCounter *next, char *name, int flag) : DriverCounter(next, name), mFlag(flag) {
}

AtraceCounter::~AtraceCounter() {
}

AtraceDriver::AtraceDriver() : mSupported(false), mNotifyPath() {
}

AtraceDriver::~AtraceDriver() {
}

void AtraceDriver::readEvents(mxml_node_t *const xml) {
	if (!gSessionData->mFtraceDriver.isSupported()) {
		logg->logMessage("Atrace support disabled, ftrace support is required");
		return;
	}
	if (access("/system/bin/setprop", X_OK) != 0) {
		logg->logMessage("Atrace support disabled, setprop is not found, this is not an Android target");
		return;
	}

	if (util->getApplicationFullPath(mNotifyPath, sizeof(mNotifyPath)) != 0) {
		logg->logMessage("Unable to determine the full path of gatord, the cwd will be used");
	}
	strncat(mNotifyPath, "notify.dex", sizeof(mNotifyPath) - strlen(mNotifyPath) - 1);
	if (access(mNotifyPath, W_OK) != 0) {
		logg->logMessage("Atrace support disabled, unable to locate notify.dex");
		return;
	}

	mSupported = true;

	mxml_node_t *node = xml;
	while (true) {
		node = mxmlFindElement(node, xml, "event", NULL, NULL, MXML_DESCEND);
		if (node == NULL) {
			break;
		}
		const char *counter = mxmlElementGetAttr(node, "counter");
		if (counter == NULL) {
			continue;
		}

		if (strncmp(counter, "atrace_", 7) != 0) {
			continue;
		}

		const char *flag = mxmlElementGetAttr(node, "flag");
		if (flag == NULL) {
			logg->logError("The atrace counter %s is missing the required flag attribute", counter);
			handleException();
		}
		setCounters(new AtraceCounter(getCounters(), strdup(counter), strtol(flag, NULL, 16)));
	}
}

void AtraceDriver::setAtrace(const int flags) {
	logg->logMessage("Setting atrace flags to %i\n", flags);
	pid_t pid = fork();
	if (pid < 0) {
		logg->logError("fork failed");
		handleException();
	} else if (pid == 0) {
		char buf[1<<10];
		snprintf(buf, sizeof(buf), "setprop debug.atrace.tags.enableflags %i; "
			 "dalvikvm -cp %s com.android.internal.util.WithFramework Notify", flags, mNotifyPath);
		execlp("sh", "sh", "-c", buf, NULL);
		exit(0);
	}
}

void AtraceDriver::start() {
	if (!mSupported) {
		return;
	}

	int flags = 0;
	for (AtraceCounter *counter = static_cast<AtraceCounter *>(getCounters()); counter != NULL; counter = static_cast<AtraceCounter *>(counter->getNext())) {
		if (!counter->isEnabled()) {
			continue;
		}
		flags |= counter->getFlag();
	}

	setAtrace(flags);
}

void AtraceDriver::stop() {
	if (!mSupported) {
		return;
	}

	setAtrace(0);
}