summaryrefslogtreecommitdiff
path: root/tools/gator/daemon/TtraceDriver.cpp
blob: db06a9d69f6b66698658b90392edc83b39b3a965 (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
137
/**
 * 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 "TtraceDriver.h"

#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

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

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

	int getFlag() const { return mFlag; }

private:
	const int mFlag;

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

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

TtraceCounter::~TtraceCounter() {
}

TtraceDriver::TtraceDriver() : mSupported(false) {
}

TtraceDriver::~TtraceDriver() {
}

void TtraceDriver::readEvents(mxml_node_t *const xml) {
	if (access("/etc/tizen-release", R_OK) != 0) {
		// Reduce warning noise
		//logg.logSetup("Ttrace is disabled\n/etc/tizen-release is not found, this is not a Tizen target");
		return;
	}
	if (!gSessionData.mFtraceDriver.isSupported()) {
		logg.logSetup("Ttrace is disabled\nSupport for ftrace required");
		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, "ttrace_", 7) != 0) {
			continue;
		}

		const char *flagStr = mxmlElementGetAttr(node, "flag");
		if (flagStr == NULL) {
			logg.logError("The ttrace counter %s is missing the required flag attribute", counter);
			handleException();
		}
		int flag;
		if (!stringToInt(&flag, flagStr, 16)) {
			logg.logError("The flag attribute of the ttrace counter %s is not a hex integer", counter);
			handleException();
		}
		setCounters(new TtraceCounter(getCounters(), strdup(counter), flag));
	}
}

void TtraceDriver::setTtrace(const int flags) {
	logg.logMessage("Setting ttrace flags to %i", flags);

	const int fd = open("/tmp/ttrace_tag", O_CREAT | O_RDWR | O_CLOEXEC, 0666);
	if (fd < 0) {
		logg.logError("Unable to open /tmp/ttrace_tag");
		handleException();
	}
	if (ftruncate(fd, sizeof(uint64_t)) != 0) {
		logg.logError("ftruncate failed");
		handleException();
	}

	uint64_t *const buf = (uint64_t *)mmap(NULL, sizeof(uint64_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (buf == MAP_FAILED) {
		logg.logError("mmap failed");
		handleException();
	}
	close(fd);

	*buf = flags;

	munmap(buf, sizeof(uint64_t));
}

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

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

	setTtrace(flags);
}

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

	setTtrace(0);
}