[go: up one dir, main page]

File: misc.c

package info (click to toggle)
uftrace 0.18.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,356 kB
  • sloc: ansic: 49,770; python: 11,181; asm: 837; makefile: 769; sh: 637; cpp: 627; javascript: 191
file content (311 lines) | stat: -rw-r--r-- 7,032 bytes parent folder | download
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
#include <time.h>
#include <unistd.h>

/* This should be defined before #include "utils.h" */
#define PR_FMT "mcount"
#define PR_DOMAIN DBG_MCOUNT

#include "libmcount/internal.h"
#include "libmcount/mcount.h"
#include "utils/tracefs.h"
#include "utils/utils.h"

/* old kernel never updates pid filter for a forked child */
void update_kernel_tid(int tid)
{
	char buf[8];

	if (!kernel_pid_update)
		return;

	snprintf(buf, sizeof(buf), "%d", tid);

	/* update pid filter for function tracing */
	if (append_tracing_file("set_ftrace_pid", buf) < 0)
		pr_dbg("write to kernel ftrace pid filter failed\n");

	/* update pid filter for event tracing */
	if (append_tracing_file("set_event_pid", buf) < 0)
		pr_dbg("write to kernel ftrace pid filter failed\n");
}

const char *mcount_session_name(void)
{
	static char session[SESSION_ID_LEN + 1];
	static uint64_t session_id;
	int fd;

	if (!session_id) {
		fd = open("/dev/urandom", O_RDONLY);
		if (fd >= 0) {
			if (read(fd, &session_id, sizeof(session_id)) != 8)
				pr_err("reading from urandom");

			close(fd);
		}
		else {
			srandom(time(NULL));
			session_id = random();
			session_id <<= 32;
			session_id |= random();
		}

		snprintf(session, sizeof(session), "%0*" PRIx64, SESSION_ID_LEN, session_id);
	}
	return session;
}

void uftrace_send_message(int type, void *data, size_t len)
{
	struct uftrace_msg msg = {
		.magic = UFTRACE_MSG_MAGIC,
		.type = type,
		.len = len,
	};
	struct iovec iov[2] = {
		{
			.iov_base = &msg,
			.iov_len = sizeof(msg),
		},
		{
			.iov_base = data,
			.iov_len = len,
		},
	};

	if (mcount_pfd < 0)
		return;

	len += sizeof(msg);
	if (writev(mcount_pfd, iov, 2) != (ssize_t)len) {
		if (!mcount_should_stop())
			pr_err("send msg (type %d) failed", type);
	}
}

void build_debug_domain(char *dbg_domain_str)
{
	int i, len;

	if (dbg_domain_str == NULL)
		return;

	len = strlen(dbg_domain_str);
	for (i = 0; i < len; i += 2) {
		const char *pos;
		char domain = dbg_domain_str[i];
		int level = dbg_domain_str[i + 1] - '0';
		int d;

		pos = strchr(DBG_DOMAIN_STR, domain);
		if (pos == NULL)
			continue;

		d = pos - DBG_DOMAIN_STR;
		dbg_domain[d] = level;
	}
}

bool mcount_rstack_has_plthook(struct mcount_thread_data *mtdp)
{
	int idx;

	for (idx = 0; idx < mtdp->idx; idx++) {
		if (mtdp->rstack[idx].dyn_idx != MCOUNT_INVALID_DYNIDX)
			return true;
	}
	return false;
}

/* restore saved original return address */
void mcount_rstack_restore(struct mcount_thread_data *mtdp)
{
	int idx;
	struct mcount_ret_stack *rstack;

	if (unlikely(mcount_estimate_return))
		return;

	/* reverse order due to tail calls */
	for (idx = mtdp->idx - 1; idx >= 0; idx--) {
		rstack = &mtdp->rstack[idx];

		if (rstack->parent_ip == mcount_return_fn || rstack->parent_ip == plthook_return_fn)
			continue;

		if (!ARCH_CAN_RESTORE_PLTHOOK && rstack->dyn_idx != MCOUNT_INVALID_DYNIDX) {
			/*
			 * We don't know exact location where the return address
			 * was saved (on ARM/AArch64).  But we know that the
			 * return address itself was changed to plthook_return_fn
			 * by the plt_hooker().  So it needs to scan the stack to
			 * look up the value.
			 */
			unsigned long *loc, *end;

			if (idx < mtdp->idx - 1) {
				struct mcount_ret_stack *next_rstack;

				next_rstack = rstack + 1;
				/* skip rstacks for -finstrument-functions */
				while (next_rstack->parent_loc == &mtdp->cygprof_dummy &&
				       next_rstack < &mtdp->rstack[mtdp->idx])
					next_rstack++;

				if (next_rstack == &mtdp->rstack[mtdp->idx])
					goto last_rstack;

				/* special case: same as tail-call */
				if (next_rstack->parent_ip == plthook_return_fn) {
					rstack->parent_loc = next_rstack->parent_loc;
					*rstack->parent_loc = rstack->parent_ip;
					continue;
				}

				end = next_rstack->parent_loc;
			}
			else {
last_rstack:
				/* just check 32 stack slots */
				end = rstack->parent_loc - 32;
			}

			for (loc = rstack->parent_loc; loc < end; loc--) {
				if (*loc != plthook_return_fn)
					continue;

				rstack->parent_loc = loc;
				*loc = rstack->parent_ip;
				break;
			}
			continue;
		}

		*rstack->parent_loc = rstack->parent_ip;
	}
}

/* hook return address again (used after mcount_rstack_restore) */
void mcount_rstack_rehook(struct mcount_thread_data *mtdp)
{
	int idx;
	struct mcount_ret_stack *rstack;

	if (unlikely(mcount_estimate_return))
		return;

	for (idx = mtdp->idx - 1; idx >= 0; idx--) {
		rstack = &mtdp->rstack[idx];

		if (rstack->dyn_idx == MCOUNT_INVALID_DYNIDX)
			*rstack->parent_loc = mcount_return_fn;
		else if (ARCH_CAN_RESTORE_PLTHOOK)
			*rstack->parent_loc = plthook_return_fn;
	}
}

void mcount_auto_restore(struct mcount_thread_data *mtdp)
{
	struct mcount_ret_stack *curr_rstack;
	struct mcount_ret_stack *prev_rstack;

	/* auto recover is meaningful only if parent rstack is hooked */
	if (mtdp->idx < 2)
		return;

	if (mtdp->in_exception)
		return;

	curr_rstack = &mtdp->rstack[mtdp->idx - 1];
	prev_rstack = &mtdp->rstack[mtdp->idx - 2];

	if (!ARCH_CAN_RESTORE_PLTHOOK && prev_rstack->dyn_idx != MCOUNT_INVALID_DYNIDX)
		return;

	/* ignore tail calls */
	if (curr_rstack->parent_loc == prev_rstack->parent_loc)
		return;

	while (prev_rstack >= mtdp->rstack) {
		unsigned long parent_ip = prev_rstack->parent_ip;

		/* parent also can be tail-called; skip */
		if (parent_ip == mcount_return_fn || parent_ip == plthook_return_fn) {
			prev_rstack--;
			continue;
		}

		*prev_rstack->parent_loc = parent_ip;
		return;
	}
}

void mcount_auto_rehook(struct mcount_thread_data *mtdp)
{
	struct mcount_ret_stack *curr_rstack;
	struct mcount_ret_stack *prev_rstack;

	/* auto recover is meaningful only if parent rstack is hooked */
	if (mtdp->idx < 2)
		return;

	if (mtdp->in_exception)
		return;

	curr_rstack = &mtdp->rstack[mtdp->idx - 1];
	prev_rstack = &mtdp->rstack[mtdp->idx - 2];

	if (!ARCH_CAN_RESTORE_PLTHOOK && prev_rstack->dyn_idx != MCOUNT_INVALID_DYNIDX)
		return;

	/* ignore tail calls */
	if (curr_rstack->parent_loc == prev_rstack->parent_loc)
		return;

	if (prev_rstack->dyn_idx == MCOUNT_INVALID_DYNIDX)
		*prev_rstack->parent_loc = mcount_return_fn;
	else
		*prev_rstack->parent_loc = plthook_return_fn;
}

#ifdef UNIT_TEST

TEST_CASE(mcount_debug_domain)
{
	int i;
	char dbg_str[DBG_DOMAIN_MAX * 2 + 1];

	/* ensure domain string matches to current domain bit */
	TEST_EQ(DBG_DOMAIN_MAX, (int)strlen(DBG_DOMAIN_STR));

	pr_dbg("initially all domains are off\n");
	for (i = 0; i < DBG_DOMAIN_MAX; i++) {
		if (i != PR_DOMAIN)
			TEST_EQ(dbg_domain[i], 0);
	}

	pr_dbg("turn on all domains\n");
	for (i = 0; i < DBG_DOMAIN_MAX; i++) {
		dbg_str[i * 2] = DBG_DOMAIN_STR[i];
		dbg_str[i * 2 + 1] = '1';
	}
	dbg_str[i * 2] = '\0';

	build_debug_domain(dbg_str);

	for (i = 0; i < DBG_DOMAIN_MAX; i++)
		TEST_EQ(dbg_domain[i], 1);

	/* increase mcount debug domain to 2 */
	strcpy(dbg_str, "M2");
	build_debug_domain(dbg_str);

	TEST_EQ(dbg_domain[PR_DOMAIN], 2);

	return TEST_OK;
}

#endif /* UNIT_TEST */