summaryrefslogtreecommitdiff
path: root/tools/gator/daemon/AtraceDriver.cpp
blob: 41a8d7c8ce713f9a9dfa37facaef61302f24a5f2 (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
/**
 * Copyright (C) ARM Limited 2014-2016. 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 "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 (access("/system/bin/setprop", X_OK) != 0) {
		// Reduce warning noise
		//logg.logSetup("Atrace is disabled\nUnable to find setprop, this is not an Android target");
		return;
	}
	if (!gSessionData.mFtraceDriver.isSupported()) {
		logg.logSetup("Atrace is disabled\nSupport for ftrace is required");
		return;
	}
	if (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.logSetup("Atrace is disabled\nUnable 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 *flagStr = mxmlElementGetAttr(node, "flag");
		if (flagStr == NULL) {
			logg.logError("The atrace counter %s is missing the required flag attribute", counter);
			handleException();
		}
		int flag;
		if (!stringToInt(&flag, flagStr, 16)) {
			logg.logError("The flag attribute of the atrace counter %s is not a hex integer", counter);
			handleException();
		}
		setCounters(new AtraceCounter(getCounters(), strdup(counter), flag));
	}
}

void AtraceDriver::setAtrace(const int flags) {
	logg.logMessage("Setting atrace flags to %i", 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; "
			 "CLASSPATH=%s app_process /system/bin 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);
}