1 /* Test program for libdwfl file decriptors leakage.
2 Copyright (C) 2007, 2008 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include <stdio_ext.h>
30 #include <sys/resource.h>
31 #include ELFUTILS_HEADER(dwfl)
35 elfutils_open (pid_t pid, Dwarf_Addr address)
37 static char *debuginfo_path;
38 static const Dwfl_Callbacks proc_callbacks =
40 .find_debuginfo = dwfl_standard_find_debuginfo,
41 .debuginfo_path = &debuginfo_path,
43 .find_elf = dwfl_linux_proc_find_elf,
45 Dwfl *dwfl = dwfl_begin (&proc_callbacks);
47 error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
49 int result = dwfl_linux_proc_report (dwfl, pid);
51 error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1));
53 error (2, result, "dwfl_linux_proc_report");
55 if (dwfl_report_end (dwfl, NULL, NULL) != 0)
56 error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));
59 Dwarf *dbg = dwfl_addrdwarf (dwfl, address, &bias);
62 Elf *elf = dwarf_getelf (dbg);
64 error (2, 0, "dwarf_getelf: %s", dwarf_errmsg (-1));
68 Elf *elf = dwfl_module_getelf (dwfl_addrmodule (dwfl, address), &bias);
70 error (2, 0, "dwfl_module_getelf: %s", dwfl_errmsg (-1));
77 elfutils_close (Dwfl *dwfl)
85 /* We use no threads here which can interfere with handling a stream. */
86 (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
89 (void) setlocale (LC_ALL, "");
91 struct rlimit fd_limit = { .rlim_cur = 32, .rlim_max = 32 };
92 if (setrlimit (RLIMIT_NOFILE, &fd_limit) < 0)
93 error (2, errno, "setrlimit");
95 for (int i = 0; i < 5000; ++i)
97 Dwfl *dwfl = elfutils_open (getpid (), (Dwarf_Addr) (uintptr_t) &main);
98 elfutils_close (dwfl);