[go: up one dir, main page]

File: wrap.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 (754 lines) | stat: -rw-r--r-- 17,943 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
#include <dlfcn.h>
#include <link.h>
#include <pthread.h>
#include <spawn.h>
#include <string.h>
#include <sys/uio.h>
#include <unistd.h>

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

#include "libmcount/dynamic.h"
#include "libmcount/internal.h"
#include "libmcount/mcount.h"
#include "utils/compiler.h"
#include "utils/utils.h"

extern struct uftrace_sym_info mcount_sym_info;

struct dlopen_base_data {
	const char *filename;
	struct mcount_thread_data *mtdp;
	struct uftrace_triggers_info *triggers;
	uint64_t timestamp;
	void *handle;
};

static void send_dlopen_msg(struct mcount_thread_data *mtdp, const char *sess_id,
			    uint64_t timestamp, uint64_t base_addr, const char *libname)
{
	struct uftrace_msg_dlopen dlop = {
		.task = {
			.time = timestamp,
			.pid = getpid(),
			.tid = mcount_gettid(mtdp),
		},
		.base_addr = base_addr,
		.namelen = strlen(libname),
	};
	struct uftrace_msg msg = {
		.magic = UFTRACE_MSG_MAGIC,
		.type = UFTRACE_MSG_DLOPEN,
		.len = sizeof(dlop) + dlop.namelen,
	};
	struct iovec iov[3] = {
		{
			.iov_base = &msg,
			.iov_len = sizeof(msg),
		},
		{
			.iov_base = &dlop,
			.iov_len = sizeof(dlop),
		},
		{
			.iov_base = (void *)libname,
			.iov_len = dlop.namelen,
		},
	};
	int len = sizeof(msg) + msg.len;

	if (mcount_pfd < 0)
		return;

	mcount_memcpy4(dlop.sid, sess_id, sizeof(dlop.sid));

	if (writev(mcount_pfd, iov, 3) != len) {
		if (!mcount_should_stop())
			pr_err("send dlopen msg failed");
	}
}

static int dlopen_base_callback(struct dl_phdr_info *info, size_t size, void *arg)
{
	struct dlopen_base_data *data = arg;
	struct uftrace_mmap *map;
	char buf[PATH_MAX];
	char *p;

	if (info->dlpi_name[0] == '\0')
		return 0;
	if (!strcmp("linux-vdso.so.1", info->dlpi_name))
		return 0;
	if (!strstr(info->dlpi_name, data->filename))
		return 0;

	p = realpath(info->dlpi_name, buf);
	if (p == NULL)
		p = buf;

	if (find_map_by_name(&mcount_sym_info, uftrace_basename(p)))
		return 0;

	/* report a library not found in the session maps */
	send_dlopen_msg(data->mtdp, mcount_session_name(), data->timestamp, info->dlpi_addr,
			info->dlpi_name);

	map = xzalloc(sizeof(*map) + strlen(p) + 1);
	map->len = strlen(p);
	strcpy(map->libname, p);
	mcount_memcpy1(map->prot, "r-xp", 4);
	map->handle = data->handle;

	for (int i = 0; i < info->dlpi_phnum; i++) {
		if (info->dlpi_phdr[i].p_type != PT_LOAD)
			continue;

		if (map->start == 0)
			map->start = info->dlpi_phdr[i].p_vaddr + info->dlpi_addr;

		if (info->dlpi_phdr[i].p_flags & PF_X) {
			map->end = info->dlpi_phdr[i].p_vaddr + info->dlpi_addr;
			map->end += info->dlpi_phdr[i].p_memsz;
			break;
		}
	}

	read_build_id(p, map->build_id, sizeof(map->build_id));
	map->mod = load_module_symtab(&mcount_sym_info, p, map->build_id);

	map->next = mcount_sym_info.maps;
	write_memory_barrier();
	mcount_sym_info.maps = map;

	mcount_dynamic_dlopen(&mcount_sym_info, info, p, map);

	data->triggers = mcount_trigger_init(&mcount_filter_setting);

	return 0;
}

void mcount_rstack_rehook_exception(struct mcount_thread_data *mtdp, unsigned long frame_addr)
{
	int idx;
	struct mcount_ret_stack *rstack;

	if (unlikely(mcount_estimate_return))
		return;

	/* it needs to find how much stack frame unwinds */
	for (idx = mtdp->idx - 1; idx >= 0; idx--) {
		rstack = &mtdp->rstack[idx];

		pr_dbg3("%s: [%d] parent at %p\n", __func__, idx, rstack->parent_loc);
		if (rstack->parent_loc == &mtdp->cygprof_dummy)
			break;

		if ((unsigned long)rstack->parent_loc > frame_addr) {
			/*
			 * there might be tail call optimizations in the
			 * middle of the exception handling path.
			 * In that case, we need to keep the original
			 * mtdp->idx but update parent address of the
			 * first rstack of the tail call chain.
			 */
			int orig_idx = idx;

			while (idx > 0) {
				struct mcount_ret_stack *tail_call;
				tail_call = &mtdp->rstack[idx - 1];

				if (rstack->parent_loc != tail_call->parent_loc)
					break;

				idx--;
				rstack = tail_call;
				pr_dbg3("%s: exception in tail call at [%d]\n", __func__, idx + 1);
			}
			idx = orig_idx;

			/* do not overwrite current return address */
			rstack->parent_ip = *rstack->parent_loc;
			break;
		}

		/* record unwound functions */
		if (!(rstack->flags & MCOUNT_FL_NORECORD))
			rstack->end_time = mcount_gettime();

		mcount_exit_filter_record(mtdp, rstack, NULL);
	}

	/* we're in ENTER state, so add 1 to the index */
	mtdp->idx = idx + 1;
	pr_dbg3("%s: exception returned to [%d]\n", __func__, mtdp->idx);

	mcount_rstack_rehook(mtdp);
}

static char **collect_uftrace_envp(void)
{
	size_t n = 0;
	size_t i, k;
	char **envp;

#define ENV(_name) "UFTRACE_" #_name

	const char *const uftrace_env[] = {
		ENV(FILTER),
		ENV(TRIGGER),
		ENV(ARGUMENT),
		ENV(RETVAL),
		ENV(AUTO_ARGS),
		ENV(DEPTH),
		ENV(DISABLED),
		ENV(PIPE),
		ENV(LOGFD),
		ENV(DEBUG),
		ENV(BUFFER),
		ENV(MAX_STACK),
		ENV(COLOR),
		ENV(THRESHOLD),
		ENV(DEMANGLE),
		ENV(PLTHOOK),
		ENV(PATCH),
		ENV(EVENT),
		ENV(SCRIPT),
		ENV(NEST_LIBCALL),
		ENV(DEBUG_DOMAIN),
		ENV(LIST_EVENT),
		ENV(DIR),
		ENV(KERNEL_PID_UPDATE),
		ENV(PATTERN),
		/* not uftrace-specific, but necessary to run */
		"LD_PRELOAD",
		"LD_LIBRARY_PATH",
	};

#undef ENV

	for (i = 0; i < ARRAY_SIZE(uftrace_env); i++) {
		if (getenv(uftrace_env[i]))
			n++;
	}

	envp = xcalloc(n + 2, sizeof(*envp));

	for (i = k = 0; i < ARRAY_SIZE(uftrace_env); i++) {
		char *env_str;
		char *env_val;

		env_val = getenv(uftrace_env[i]);
		if (env_val == NULL)
			continue;

		xasprintf(&env_str, "%s=%s", uftrace_env[i], env_val);
		envp[k++] = env_str;
	}

	return envp;
}

static int count_envp(char *const *env)
{
	int i, n = 0;

	for (i = 0; env && env[i]; i++)
		n++;

	return n;
}

static char **merge_envp(char *const *env1, char **env2)
{
	int i, n = 0;
	char **envp;

	n += count_envp(env1);
	n += count_envp(env2);

	envp = xcalloc(n + 1, sizeof(*envp));

	n = 0;
	for (i = 0; env1 && env1[i]; i++)
		envp[n++] = env1[i];
	for (i = 0; env2 && env2[i]; i++)
		envp[n++] = env2[i];

	return envp;
}

/*
 * hooking functions
 */
static int (*real_backtrace)(void **buffer, int sz);
static void (*real_cxa_throw)(void *exc, void *type, void *dest);
static void (*real_cxa_rethrow)(void);
static void *(*real_cxa_begin_catch)(void *exc);
static void (*real_cxa_end_catch)(void);
static void (*real_cxa_guard_abort)(void *guard_obj);
static void *(*real_dlopen)(const char *filename, int flags);
static int (*real_dlclose)(void *handle);
static __noreturn void (*real_pthread_exit)(void *retval);
static void (*real_unwind_resume)(void *exc);
static int (*real_posix_spawn)(pid_t *pid, const char *path,
			       const posix_spawn_file_actions_t *actions,
			       const posix_spawnattr_t *attr, char *const argv[],
			       char *const envp[]);
static int (*real_posix_spawnp)(pid_t *pid, const char *file,
				const posix_spawn_file_actions_t *actions,
				const posix_spawnattr_t *attr, char *const argv[],
				char *const envp[]);
/* TODO: support execle() */
static int (*real_execve)(const char *path, char *const argv[], char *const envp[]);
static int (*real_execvpe)(const char *file, char *const argv[], char *const envp[]);
static int (*real_fexecve)(int fd, char *const argv[], char *const envp[]);
static int (*real_close)(int fd);

void mcount_hook_functions(void)
{
	real_backtrace = dlsym(RTLD_NEXT, "backtrace");
	real_cxa_throw = dlsym(RTLD_NEXT, "__cxa_throw");
	real_cxa_rethrow = dlsym(RTLD_NEXT, "__cxa_rethrow");
	real_cxa_begin_catch = dlsym(RTLD_NEXT, "__cxa_begin_catch");
	real_cxa_end_catch = dlsym(RTLD_NEXT, "__cxa_end_catch");
	real_cxa_guard_abort = dlsym(RTLD_NEXT, "__cxa_guard_abort");
	real_dlopen = dlsym(RTLD_NEXT, "dlopen");
	real_dlclose = dlsym(RTLD_NEXT, "dlclose");
	real_pthread_exit = dlsym(RTLD_NEXT, "pthread_exit");
	real_unwind_resume = dlsym(RTLD_NEXT, "_Unwind_Resume");
	real_posix_spawn = dlsym(RTLD_NEXT, "posix_spawn");
	real_posix_spawnp = dlsym(RTLD_NEXT, "posix_spawnp");
	real_execve = dlsym(RTLD_NEXT, "execve");
	real_execvpe = dlsym(RTLD_NEXT, "execvpe");
	real_fexecve = dlsym(RTLD_NEXT, "fexecve");
	real_close = dlsym(RTLD_NEXT, "close");
}

__visible_default int backtrace(void **buffer, int sz)
{
	int ret;
	struct mcount_thread_data *mtdp;

	if (unlikely(real_backtrace == NULL))
		mcount_hook_functions();

	mtdp = get_thread_data();
	if (!check_thread_data(mtdp)) {
		mcount_rstack_restore(mtdp);
		pr_dbg("%s is called from [%d]\n", __func__, mtdp->idx);
	}

	ret = real_backtrace(buffer, sz);

	if (!check_thread_data(mtdp))
		mcount_rstack_rehook(mtdp);

	return ret;
}

__visible_default void __cxa_throw(void *exception, void *type, void *dest)
{
	struct mcount_thread_data *mtdp;

	if (unlikely(real_cxa_throw == NULL))
		mcount_hook_functions();

	mtdp = get_thread_data();
	if (!check_thread_data(mtdp)) {
		pr_dbg2("%s: exception thrown from [%d]\n", __func__, mtdp->idx);

		mtdp->in_exception = true;

		/*
		 * restore return addresses so that it can unwind stack
		 * frames safely during the exception handling.
		 * It pairs to mcount_rstack_rehook_exception().
		 */
		mcount_rstack_restore(mtdp);
	}

	real_cxa_throw(exception, type, dest);
}

__visible_default void __cxa_rethrow(void)
{
	struct mcount_thread_data *mtdp;

	if (unlikely(real_cxa_rethrow == NULL))
		mcount_hook_functions();

	mtdp = get_thread_data();
	if (!check_thread_data(mtdp)) {
		pr_dbg2("%s: exception rethrown from [%d]\n", __func__, mtdp->idx);

		mtdp->in_exception = true;

		/*
		 * restore return addresses so that it can unwind stack
		 * frames safely during the exception handling.
		 * It pairs to mcount_rstack_rehook_exception()
		 */
		mcount_rstack_restore(mtdp);
	}

	real_cxa_rethrow();
}

__visible_default void _Unwind_Resume(void *exception)
{
	struct mcount_thread_data *mtdp;

	if (unlikely(real_unwind_resume == NULL))
		mcount_hook_functions();

	mtdp = get_thread_data();
	if (!check_thread_data(mtdp)) {
		pr_dbg2("%s: exception resumed on [%d]\n", __func__, mtdp->idx);

		mtdp->in_exception = true;

		/*
		 * restore return addresses so that it can unwind stack
		 * frames safely during the exception handling.
		 * It pairs to mcount_rstack_rehook_exception().
		 */
		mcount_rstack_restore(mtdp);
	}

	real_unwind_resume(exception);
}

__visible_default void *__cxa_begin_catch(void *exception)
{
	struct mcount_thread_data *mtdp;
	void *obj;

	if (unlikely(real_cxa_begin_catch == NULL))
		mcount_hook_functions();

	obj = real_cxa_begin_catch(exception);

	mtdp = get_thread_data();
	if (!check_thread_data(mtdp) && unlikely(mtdp->in_exception)) {
		unsigned long *frame_ptr;
		unsigned long frame_addr;

		frame_ptr = __builtin_frame_address(0);
		frame_addr = *frame_ptr; /* XXX: probably dangerous */

		/* basic sanity check */
		if (frame_addr < (unsigned long)frame_ptr)
			frame_addr = (unsigned long)frame_ptr;

		mcount_rstack_rehook_exception(mtdp, frame_addr);
		mtdp->in_exception = false;
		pr_dbg2("%s: exception caught begin on [%d]\n", __func__, mtdp->idx);
	}

	return obj;
}

__visible_default void __cxa_end_catch(void)
{
	if (unlikely(real_cxa_end_catch == NULL))
		mcount_hook_functions();

	pr_dbg2("%s: exception caught end\n", __func__);
	real_cxa_end_catch();
}

__visible_default void __cxa_guard_abort(void *guard_obj)
{
	struct mcount_thread_data *mtdp;

	if (unlikely(real_cxa_guard_abort == NULL))
		mcount_hook_functions();

	real_cxa_guard_abort(guard_obj);

	mtdp = get_thread_data();
	if (!check_thread_data(mtdp) && unlikely(mtdp->in_exception)) {
		unsigned long *frame_ptr;
		unsigned long frame_addr;
		struct mcount_ret_stack *rstack;

		frame_ptr = __builtin_frame_address(0);
		frame_addr = *frame_ptr; /* XXX: probably dangerous */

		/* basic sanity check */
		if (frame_addr < (unsigned long)frame_ptr)
			frame_addr = (unsigned long)frame_ptr;

		mcount_rstack_rehook_exception(mtdp, frame_addr);
		mtdp->in_exception = false;

		/*
		 * It seems __cxa_guard_abort() is called after the
		 * exception is thrown, so the return address was
		 * not restored, let's do that right here.
		 *
		 * The mtdp->idx should pointer the first function
		 * right after the exception frame, so it won't
		 * have tailcalls (i.e. not pointer libmcount hook).
		 */
		rstack = &mtdp->rstack[mtdp->idx];
		*rstack->parent_loc = rstack->parent_ip;
	}
}

__visible_default void *dlopen(const char *filename, int flags)
{
	struct mcount_thread_data *mtdp;
	struct dlopen_base_data data = {
		.timestamp = mcount_gettime(),
		.filename = filename,
	};
	void *ret;

	/*
	 * get timestamp before calling dlopen() so that
	 * it can have symbols in static initializers which
	 * called during the dlopen.
	 */

	if (unlikely(real_dlopen == NULL))
		mcount_hook_functions();

	ret = real_dlopen(filename, flags);

	if (filename == NULL)
		return ret;
	pr_dbg("%s is called for '%s'\n", __func__, filename);

	mtdp = get_thread_data();
	if (unlikely(check_thread_data(mtdp))) {
		mtdp = mcount_prepare();
		if (mtdp == NULL)
			return ret;
	}
	else {
		if (!mcount_guard_recursion(mtdp))
			return ret;
	}

	data.mtdp = mtdp;
	data.handle = ret;
	dl_iterate_phdr(dlopen_base_callback, &data);

	if (data.triggers)
		swap_triggers(&mcount_triggers, data.triggers);

	mcount_unguard_recursion(mtdp);
	return ret;
}

__visible_default int dlclose(void *handle)
{
	struct mcount_thread_data *mtdp;
	struct uftrace_mmap *map;
	int ret;

	if (unlikely(real_dlopen == NULL))
		mcount_hook_functions();

	ret = real_dlclose(handle);

	mtdp = get_thread_data();
	if (unlikely(check_thread_data(mtdp))) {
		mtdp = mcount_prepare();
		if (mtdp == NULL)
			return ret;
	}
	else {
		if (!mcount_guard_recursion(mtdp))
			return ret;
	}

	for_each_map(&mcount_sym_info, map) {
		if (map->mod == NULL)
			continue;

		if (map->handle == handle) {
			map->mod = NULL;
			break;
		}
	}

	mcount_unguard_recursion(mtdp);

	return ret;
}

__visible_default __noreturn void pthread_exit(void *retval)
{
	struct mcount_thread_data *mtdp;

	if (unlikely(real_pthread_exit == NULL))
		mcount_hook_functions();

	mtdp = get_thread_data();
	if (check_thread_data(mtdp))
		goto out;

	pr_dbg("%s: pthread exited on [%d]\n", __func__, mtdp->idx);

	if (!mcount_estimate_return) {
		struct mcount_ret_stack *rstack = &mtdp->rstack[mtdp->idx - 1];

		/* record the final call */
		rstack->end_time = mcount_gettime();
		mcount_exit_filter_record(mtdp, rstack, NULL);

		mcount_rstack_restore(mtdp);
		mtdp->idx--;
	}

	/* To skip duplicate mcount_rstack_restore() in mtd_dtor() */
	mtdp->exited = true;

out:
	real_pthread_exit(retval);
}

__visible_default int posix_spawn(pid_t *pid, const char *path,
				  const posix_spawn_file_actions_t *actions,
				  const posix_spawnattr_t *attr, char *const argv[],
				  char *const envp[])
{
	char **uftrace_envp;
	char **new_envp;

	if (unlikely(real_posix_spawn == NULL))
		mcount_hook_functions();

	uftrace_envp = collect_uftrace_envp();
	new_envp = merge_envp(envp, uftrace_envp);

	pr_dbg("%s is called for '%s'\n", __func__, path);
	return real_posix_spawn(pid, path, actions, attr, argv, new_envp);
}

__visible_default int posix_spawnp(pid_t *pid, const char *file,
				   const posix_spawn_file_actions_t *actions,
				   const posix_spawnattr_t *attr, char *const argv[],
				   char *const envp[])
{
	char **uftrace_envp;
	char **new_envp;

	if (unlikely(real_posix_spawnp == NULL))
		mcount_hook_functions();

	uftrace_envp = collect_uftrace_envp();
	new_envp = merge_envp(envp, uftrace_envp);

	pr_dbg("%s is called for '%s'\n", __func__, file);
	return real_posix_spawnp(pid, file, actions, attr, argv, new_envp);
}

__visible_default int execve(const char *path, char *const argv[], char *const envp[])
{
	char **uftrace_envp;
	char **new_envp;

	if (unlikely(real_execve == NULL))
		mcount_hook_functions();

	uftrace_envp = collect_uftrace_envp();
	new_envp = merge_envp(envp, uftrace_envp);

	pr_dbg("%s is called for '%s'\n", __func__, path);
	return real_execve(path, argv, new_envp);
}

__visible_default int execvpe(const char *file, char *const argv[], char *const envp[])
{
	char **uftrace_envp;
	char **new_envp;

	if (unlikely(real_execvpe == NULL))
		mcount_hook_functions();

	uftrace_envp = collect_uftrace_envp();
	new_envp = merge_envp(envp, uftrace_envp);

	pr_dbg("%s is called for '%s'\n", __func__, file);
	return real_execvpe(file, argv, new_envp);
}

__visible_default int fexecve(int fd, char *const argv[], char *const envp[])
{
	char **uftrace_envp;
	char **new_envp;

	if (unlikely(real_fexecve == NULL))
		mcount_hook_functions();

	uftrace_envp = collect_uftrace_envp();
	new_envp = merge_envp(envp, uftrace_envp);

	pr_dbg("%s is called for fd %d\n", __func__, fd);
	return real_fexecve(fd, argv, new_envp);
}

__visible_default int close(int fd)
{
	if (unlikely(real_close == NULL))
		mcount_hook_functions();

	if (unlikely(fd == mcount_pfd)) {
		return 0;
	}

	return real_close(fd);
}

#ifdef UNIT_TEST

TEST_CASE(mcount_wrap_dlopen)
{
	void *handle;

	/* In some environment, dlopen() is called already */
	if (unlikely(real_dlopen != NULL))
		real_dlopen = NULL;

	pr_dbg("calling %s (%s) should init all the wrappers\n", "dlopen",
	       "or other wrapped function");
	handle = dlopen(NULL, RTLD_LAZY);

	TEST_NE(handle, NULL);
	TEST_NE(real_dlopen, NULL);

	return TEST_OK;
}

TEST_CASE(mcount_env_check)
{
	char **uftrace_envp;
	char **new_envp;
	int old1_cnt, old2_cnt, new_cnt;
	int i;

	pr_dbg("collecting environ related to uftrace\n");
	uftrace_envp = collect_uftrace_envp();
	old1_cnt = count_envp(uftrace_envp);
	old2_cnt = count_envp(environ);

	pr_dbg("merging uftrace envp to the existing one\n");
	new_envp = merge_envp(environ, uftrace_envp);
	new_cnt = count_envp(new_envp);

	TEST_EQ(old1_cnt + old2_cnt, new_cnt);

	for (i = 0; i < old1_cnt; i++)
		free(uftrace_envp[i]);
	free(uftrace_envp);
	free(new_envp);

	return TEST_OK;
}

#endif /* UNIT_TEST */