blob: bcbfb290cefda5064f1b83a17e7f9fa9702cc829 [file] [log] [blame]
Ulrich Drepper6258e742007-03-13 06:22:40 +00001/* Test program for libdwfl file decriptors leakage.
Ulrich Dreppere219f1c2008-01-09 05:49:49 +00002 Copyright (C) 2007, 2008 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepper6258e742007-03-13 06:22:40 +00004
Mark Wielaardde2ed972012-06-05 17:15:16 +02005 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.
Ulrich Drepper6258e742007-03-13 06:22:40 +00009
Mark Wielaardde2ed972012-06-05 17:15:16 +020010 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper6258e742007-03-13 06:22:40 +000011 WITHOUT ANY WARRANTY; without even the implied warranty of
Mark Wielaardde2ed972012-06-05 17:15:16 +020012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
Ulrich Drepper6258e742007-03-13 06:22:40 +000014
Mark Wielaardde2ed972012-06-05 17:15:16 +020015 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/>. */
Ulrich Drepper6258e742007-03-13 06:22:40 +000017
18#include <config.h>
19#include <assert.h>
20#include <inttypes.h>
21#include <stdio.h>
22#include <stdio_ext.h>
23#include <locale.h>
24#include <dirent.h>
25#include <stdlib.h>
26#include <errno.h>
27#include <error.h>
28#include <unistd.h>
29#include <dwarf.h>
30#include <sys/resource.h>
31#include ELFUTILS_HEADER(dwfl)
32
33
34static Dwfl *
35elfutils_open (pid_t pid, Dwarf_Addr address)
36{
37 static char *debuginfo_path;
38 static const Dwfl_Callbacks proc_callbacks =
39 {
40 .find_debuginfo = dwfl_standard_find_debuginfo,
41 .debuginfo_path = &debuginfo_path,
42
43 .find_elf = dwfl_linux_proc_find_elf,
44 };
45 Dwfl *dwfl = dwfl_begin (&proc_callbacks);
46 if (dwfl == NULL)
47 error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
48
49 int result = dwfl_linux_proc_report (dwfl, pid);
50 if (result < 0)
51 error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1));
52 else if (result > 0)
53 error (2, result, "dwfl_linux_proc_report");
54
55 if (dwfl_report_end (dwfl, NULL, NULL) != 0)
56 error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));
57
58 Dwarf_Addr bias;
59 Dwarf *dbg = dwfl_addrdwarf (dwfl, address, &bias);
60 if (dbg != NULL)
61 {
62 Elf *elf = dwarf_getelf (dbg);
63 if (elf == NULL)
64 error (2, 0, "dwarf_getelf: %s", dwarf_errmsg (-1));
65 }
66 else
67 {
Pino Toscano349d1cd2015-06-27 18:33:37 +020068 Dwfl_Module *module = dwfl_addrmodule (dwfl, address);
69 if (module == NULL)
70 error (2, 0, "dwfl_addrmodule: no module available for 0x%" PRIx64 "",
71 address);
72 Elf *elf = dwfl_module_getelf (module, &bias);
Ulrich Drepper6258e742007-03-13 06:22:40 +000073 if (elf == NULL)
74 error (2, 0, "dwfl_module_getelf: %s", dwfl_errmsg (-1));
75 }
76
77 return dwfl;
78}
79
80static void
81elfutils_close (Dwfl *dwfl)
82{
83 dwfl_end (dwfl);
84}
85
86int
87main (void)
88{
89 /* We use no threads here which can interfere with handling a stream. */
90 (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
91
92 /* Set locale. */
93 (void) setlocale (LC_ALL, "");
94
95 struct rlimit fd_limit = { .rlim_cur = 32, .rlim_max = 32 };
96 if (setrlimit (RLIMIT_NOFILE, &fd_limit) < 0)
97 error (2, errno, "setrlimit");
98
99 for (int i = 0; i < 5000; ++i)
100 {
Ulrich Dreppere219f1c2008-01-09 05:49:49 +0000101 Dwfl *dwfl = elfutils_open (getpid (), (Dwarf_Addr) (uintptr_t) &main);
Ulrich Drepper6258e742007-03-13 06:22:40 +0000102 elfutils_close (dwfl);
103 }
104
105 return 0;
106}