1*7304104dSAndroid Build Coastguard Worker /* Test dwfl_linux_proc_attach works without any modules.
2*7304104dSAndroid Build Coastguard Worker Copyright (C) 2015 Red Hat, Inc.
3*7304104dSAndroid Build Coastguard Worker This file is part of elfutils.
4*7304104dSAndroid Build Coastguard Worker
5*7304104dSAndroid Build Coastguard Worker This file is free software; you can redistribute it and/or modify
6*7304104dSAndroid Build Coastguard Worker it under the terms of the GNU General Public License as published by
7*7304104dSAndroid Build Coastguard Worker the Free Software Foundation; either version 3 of the License, or
8*7304104dSAndroid Build Coastguard Worker (at your option) any later version.
9*7304104dSAndroid Build Coastguard Worker
10*7304104dSAndroid Build Coastguard Worker elfutils is distributed in the hope that it will be useful, but
11*7304104dSAndroid Build Coastguard Worker WITHOUT ANY WARRANTY; without even the implied warranty of
12*7304104dSAndroid Build Coastguard Worker MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*7304104dSAndroid Build Coastguard Worker GNU General Public License for more details.
14*7304104dSAndroid Build Coastguard Worker
15*7304104dSAndroid Build Coastguard Worker You should have received a copy of the GNU General Public License
16*7304104dSAndroid Build Coastguard Worker along with this program. If not, see <http://www.gnu.org/licenses/>. */
17*7304104dSAndroid Build Coastguard Worker
18*7304104dSAndroid Build Coastguard Worker #include <config.h>
19*7304104dSAndroid Build Coastguard Worker #include <inttypes.h>
20*7304104dSAndroid Build Coastguard Worker #include <stdio.h>
21*7304104dSAndroid Build Coastguard Worker #include <stdlib.h>
22*7304104dSAndroid Build Coastguard Worker #include <errno.h>
23*7304104dSAndroid Build Coastguard Worker #include <unistd.h>
24*7304104dSAndroid Build Coastguard Worker #ifdef __linux__
25*7304104dSAndroid Build Coastguard Worker #include <sys/types.h>
26*7304104dSAndroid Build Coastguard Worker #include <sys/stat.h>
27*7304104dSAndroid Build Coastguard Worker #include <sys/user.h>
28*7304104dSAndroid Build Coastguard Worker #include <fcntl.h>
29*7304104dSAndroid Build Coastguard Worker #include <string.h>
30*7304104dSAndroid Build Coastguard Worker #include ELFUTILS_HEADER(dwfl)
31*7304104dSAndroid Build Coastguard Worker #include <pthread.h>
32*7304104dSAndroid Build Coastguard Worker #endif
33*7304104dSAndroid Build Coastguard Worker #include "system.h"
34*7304104dSAndroid Build Coastguard Worker
35*7304104dSAndroid Build Coastguard Worker #ifndef __linux__
36*7304104dSAndroid Build Coastguard Worker int
main(int argc,char ** argv)37*7304104dSAndroid Build Coastguard Worker main (int argc __attribute__ ((unused)), char **argv __attribute__ ((unused)))
38*7304104dSAndroid Build Coastguard Worker {
39*7304104dSAndroid Build Coastguard Worker printf ("dwfl_linux_proc_attach unsupported.\n");
40*7304104dSAndroid Build Coastguard Worker return 77;
41*7304104dSAndroid Build Coastguard Worker }
42*7304104dSAndroid Build Coastguard Worker #else /* __linux__ */
43*7304104dSAndroid Build Coastguard Worker
44*7304104dSAndroid Build Coastguard Worker static pthread_t thread1;
45*7304104dSAndroid Build Coastguard Worker static pthread_t thread2;
46*7304104dSAndroid Build Coastguard Worker
47*7304104dSAndroid Build Coastguard Worker static void *
sleeper(void * d)48*7304104dSAndroid Build Coastguard Worker sleeper (void* d __attribute__ ((unused)))
49*7304104dSAndroid Build Coastguard Worker {
50*7304104dSAndroid Build Coastguard Worker sleep (60);
51*7304104dSAndroid Build Coastguard Worker return NULL;
52*7304104dSAndroid Build Coastguard Worker }
53*7304104dSAndroid Build Coastguard Worker
54*7304104dSAndroid Build Coastguard Worker static char *debuginfo_path = NULL;
55*7304104dSAndroid Build Coastguard Worker
56*7304104dSAndroid Build Coastguard Worker static const Dwfl_Callbacks proc_callbacks =
57*7304104dSAndroid Build Coastguard Worker {
58*7304104dSAndroid Build Coastguard Worker .find_elf = dwfl_linux_proc_find_elf,
59*7304104dSAndroid Build Coastguard Worker .find_debuginfo = dwfl_standard_find_debuginfo,
60*7304104dSAndroid Build Coastguard Worker .debuginfo_path = &debuginfo_path,
61*7304104dSAndroid Build Coastguard Worker };
62*7304104dSAndroid Build Coastguard Worker
63*7304104dSAndroid Build Coastguard Worker static int
thread_callback(Dwfl_Thread * thread,void * thread_arg)64*7304104dSAndroid Build Coastguard Worker thread_callback (Dwfl_Thread *thread, void *thread_arg)
65*7304104dSAndroid Build Coastguard Worker {
66*7304104dSAndroid Build Coastguard Worker int *threads = (int *) thread_arg;
67*7304104dSAndroid Build Coastguard Worker pid_t tid = dwfl_thread_tid (thread);
68*7304104dSAndroid Build Coastguard Worker printf ("thread tid: %d\n", tid);
69*7304104dSAndroid Build Coastguard Worker (*threads)++;
70*7304104dSAndroid Build Coastguard Worker
71*7304104dSAndroid Build Coastguard Worker return DWARF_CB_OK;
72*7304104dSAndroid Build Coastguard Worker }
73*7304104dSAndroid Build Coastguard Worker
74*7304104dSAndroid Build Coastguard Worker int
main(int argc,char ** argv)75*7304104dSAndroid Build Coastguard Worker main (int argc __attribute__ ((unused)),
76*7304104dSAndroid Build Coastguard Worker char **argv __attribute__ ((unused)))
77*7304104dSAndroid Build Coastguard Worker {
78*7304104dSAndroid Build Coastguard Worker /* Create two extra threads to iterate through. */
79*7304104dSAndroid Build Coastguard Worker int err;
80*7304104dSAndroid Build Coastguard Worker if ((err = pthread_create (&thread1, NULL, sleeper, NULL)) != 0)
81*7304104dSAndroid Build Coastguard Worker error (-1, err, "Couldn't create thread1");
82*7304104dSAndroid Build Coastguard Worker if ((err = pthread_create (&thread2, NULL, sleeper, NULL)) != 0)
83*7304104dSAndroid Build Coastguard Worker error (-1, err, "Couldn't create thread2");
84*7304104dSAndroid Build Coastguard Worker
85*7304104dSAndroid Build Coastguard Worker Dwfl *dwfl = dwfl_begin (&proc_callbacks);
86*7304104dSAndroid Build Coastguard Worker if (dwfl == NULL)
87*7304104dSAndroid Build Coastguard Worker error (-1, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
88*7304104dSAndroid Build Coastguard Worker
89*7304104dSAndroid Build Coastguard Worker pid_t pid = getpid ();
90*7304104dSAndroid Build Coastguard Worker /* This used to fail, since we don't have any modules yet. */
91*7304104dSAndroid Build Coastguard Worker if (dwfl_linux_proc_attach (dwfl, pid, false) < 0)
92*7304104dSAndroid Build Coastguard Worker error (-1, 0, "dwfl_linux_proc_attach pid %d: %s", pid,
93*7304104dSAndroid Build Coastguard Worker dwfl_errmsg (-1));
94*7304104dSAndroid Build Coastguard Worker
95*7304104dSAndroid Build Coastguard Worker /* Did we see all 3 threads? */
96*7304104dSAndroid Build Coastguard Worker int threads = 0;
97*7304104dSAndroid Build Coastguard Worker if (dwfl_getthreads (dwfl, thread_callback, &threads) != DWARF_CB_OK)
98*7304104dSAndroid Build Coastguard Worker error (-1, 0, "dwfl_getthreads failed: %s", dwfl_errmsg (-1));
99*7304104dSAndroid Build Coastguard Worker
100*7304104dSAndroid Build Coastguard Worker dwfl_end (dwfl);
101*7304104dSAndroid Build Coastguard Worker
102*7304104dSAndroid Build Coastguard Worker pthread_cancel (thread1);
103*7304104dSAndroid Build Coastguard Worker pthread_cancel (thread2);
104*7304104dSAndroid Build Coastguard Worker pthread_join (thread1, NULL);
105*7304104dSAndroid Build Coastguard Worker pthread_join (thread2, NULL);
106*7304104dSAndroid Build Coastguard Worker
107*7304104dSAndroid Build Coastguard Worker return (threads == 3) ? 0 : -1;
108*7304104dSAndroid Build Coastguard Worker }
109*7304104dSAndroid Build Coastguard Worker
110*7304104dSAndroid Build Coastguard Worker /* HACK. This is a simple workaround for a combination of old glibc
111*7304104dSAndroid Build Coastguard Worker and valgrind. libdw will try to dlopen libdebuginfod this causes
112*7304104dSAndroid Build Coastguard Worker some unsuppressable memory leak warnings when the process is
113*7304104dSAndroid Build Coastguard Worker multi-threaded under valgrind because of some bad backtraces.
114*7304104dSAndroid Build Coastguard Worker So simply override dlopen and always return NULL so libdebuginfod
115*7304104dSAndroid Build Coastguard Worker (and libcurl) are never loaded. This test doesn't rely on
116*7304104dSAndroid Build Coastguard Worker libdebuginfod anyway. */
dlopen(void)117*7304104dSAndroid Build Coastguard Worker void *dlopen (void)
118*7304104dSAndroid Build Coastguard Worker {
119*7304104dSAndroid Build Coastguard Worker return NULL;
120*7304104dSAndroid Build Coastguard Worker }
121*7304104dSAndroid Build Coastguard Worker
122*7304104dSAndroid Build Coastguard Worker #endif /* __linux__ */
123