diff options
author | Elliott Hughes <enh@google.com> | 2015-02-24 19:29:20 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2015-02-24 19:29:20 +0000 |
commit | 7401a30e8b3a0de25d7e8a6b635fc33f45894118 (patch) | |
tree | a63e40b5dd9927bd04ec7427c8797975c2a71f85 /src/tests/dwfl-bug-fd-leak.c | |
parent | 2c9b0ee07430feec65a3934aeab4d5e66fdbaabf (diff) | |
parent | 36e62782f45c6b004d370a79fbc895e0f321181d (diff) | |
download | android_external_elfutils-7401a30e8b3a0de25d7e8a6b635fc33f45894118.tar.gz android_external_elfutils-7401a30e8b3a0de25d7e8a6b635fc33f45894118.tar.bz2 android_external_elfutils-7401a30e8b3a0de25d7e8a6b635fc33f45894118.zip |
am 36e62782: Merge "Upgrade to elfutils 0.161."
* commit '36e62782f45c6b004d370a79fbc895e0f321181d':
Upgrade to elfutils 0.161.
Diffstat (limited to 'src/tests/dwfl-bug-fd-leak.c')
-rw-r--r-- | src/tests/dwfl-bug-fd-leak.c | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/tests/dwfl-bug-fd-leak.c b/src/tests/dwfl-bug-fd-leak.c new file mode 100644 index 00000000..170a61a7 --- /dev/null +++ b/src/tests/dwfl-bug-fd-leak.c @@ -0,0 +1,102 @@ +/* Test program for libdwfl file decriptors leakage. + Copyright (C) 2007, 2008 Red Hat, Inc. + This file is part of elfutils. + + This file is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include <config.h> +#include <assert.h> +#include <inttypes.h> +#include <stdio.h> +#include <stdio_ext.h> +#include <locale.h> +#include <dirent.h> +#include <stdlib.h> +#include <errno.h> +#include <error.h> +#include <unistd.h> +#include <dwarf.h> +#include <sys/resource.h> +#include ELFUTILS_HEADER(dwfl) + + +static Dwfl * +elfutils_open (pid_t pid, Dwarf_Addr address) +{ + static char *debuginfo_path; + static const Dwfl_Callbacks proc_callbacks = + { + .find_debuginfo = dwfl_standard_find_debuginfo, + .debuginfo_path = &debuginfo_path, + + .find_elf = dwfl_linux_proc_find_elf, + }; + Dwfl *dwfl = dwfl_begin (&proc_callbacks); + if (dwfl == NULL) + error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1)); + + int result = dwfl_linux_proc_report (dwfl, pid); + if (result < 0) + error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1)); + else if (result > 0) + error (2, result, "dwfl_linux_proc_report"); + + if (dwfl_report_end (dwfl, NULL, NULL) != 0) + error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1)); + + Dwarf_Addr bias; + Dwarf *dbg = dwfl_addrdwarf (dwfl, address, &bias); + if (dbg != NULL) + { + Elf *elf = dwarf_getelf (dbg); + if (elf == NULL) + error (2, 0, "dwarf_getelf: %s", dwarf_errmsg (-1)); + } + else + { + Elf *elf = dwfl_module_getelf (dwfl_addrmodule (dwfl, address), &bias); + if (elf == NULL) + error (2, 0, "dwfl_module_getelf: %s", dwfl_errmsg (-1)); + } + + return dwfl; +} + +static void +elfutils_close (Dwfl *dwfl) +{ + dwfl_end (dwfl); +} + +int +main (void) +{ + /* We use no threads here which can interfere with handling a stream. */ + (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER); + + /* Set locale. */ + (void) setlocale (LC_ALL, ""); + + struct rlimit fd_limit = { .rlim_cur = 32, .rlim_max = 32 }; + if (setrlimit (RLIMIT_NOFILE, &fd_limit) < 0) + error (2, errno, "setrlimit"); + + for (int i = 0; i < 5000; ++i) + { + Dwfl *dwfl = elfutils_open (getpid (), (Dwarf_Addr) (uintptr_t) &main); + elfutils_close (dwfl); + } + + return 0; +} |