From 3996f34b46043ed8ea8fdc6f44377d969d64396b Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Mon, 28 Jul 1997 22:35:20 +0000 Subject: Update. 1997-07-28 23:35 Ulrich Drepper * csu/gmon-start.c (__gmon_start__): Call __monstartup not monstartup. * gmon/gmon.c: Rename moncontrol to __moncontrol and monstartup to __monstartup. * gmon/sys/gmon.h: Fix prototypes. * gmon/sys/gmon_out.h: Pretty print. * gmon/bb_exit_func.c (__bb_exit_func): Use memcpy instead of bcopy. * gmon/gmon.c (__monstartup): Likewise. (write_hist): Remove dependency on 32 int. (_mcleanup): Don't call perror, use fprintf. * elf/dl-load.c: Fix handling of current directory in search path. * elf/Makefile (dl-routines): Add dl-profile. * elf/dl-profile.c: New file. * elf/dl-runtime.c (fixup): Add new parameter with address to store relocation result in to elf_machine_relplt. (profile_fixup): New function. * elf/do-rel.h (elf_dynamic_do_rel): Add new parameter with address to store relocation result in to elf_machine_relplt. * elf/dl-support.c: Define _dl_profile and _dl_profile_map. * elf/dynamic-link.h (ELF_DYNAMIC_RELOCATE): Add new parameter and call elf_machine_runtime_setup with extra argument. * elf/dl-reloc.c (_dl_relocate_object): Add new argument to ELF_DYNAMIC_RELOCATE. * elf/link.h: Add prototypes for new functions and variables. * elf/rtld.c: Parse LD_PROFILE and LD_PROFILE_OUTPUT environment variables and call _dl_start_profile if necessary. * include/sys/gmon_out.h: New file. * sysdeps/alpha/dl-machine.h (elf_machine_runtime_setup): Add new parameter to enable profiling. (elf_machine_rela): Add new parameter to specify place to store result in. * sysdeps/m68k/dl-machine.h: Likewise. * sysdeps/mips/dl-machine.h: Likewise. * sysdeps/mips/mips64/dl-machine.h: Likewise. * sysdeps/powerpc/dl-machine.h: Likewise. * sysdeps/sparc/dl-machine.h: Likewise. * sysdeps/sparc64/dl-machine.h: Likewise. * sysdeps/i386/dl-machine.h: Likewise. (elf_machine_runtime_setup): Setup got[2] to _dl_runtime_resolve if we do profiling. (ELF_MACHINE_RUNTIME_TRAMPOLINE): Add code for _dl_runtime_profile. * nis/nss_compat/compat-grp.c (internal_setgrent): Set FD_CLOEXEC for stream on input file. * nis/nss_compat/compat-pwd.c (internal_setpwent): Likewise. * nis/nss_compat/compat-spwd.c (internal_setspent): Likewise. * nss/nss_db/db-XXX.c (internal_setent): Likewise. * nss/nss_db/db-alias.c (internal_setent): Likewise. * nss/nss_db/db-netgrp.c (internal_setent): Likewise. * nss/nss_files/files-XXX.c (internal_setent): Likewise. * nss/nss_files/files-alias.c (internal_setent): Likewise. * nss/nss_files/files-netgrp.c (internal_setent): Likewise. * string/string.h: Pretty print. * sysdeps/i386/fpu/bits/mathinline.h: Major update by John Bowman. Add float and long double versions. 1997-07-27 Andreas Schwab * elf/rtld.c (print_unresolved): Replace empty object name by main program name. 1997-07-27 Andreas Schwab * sysdeps/generic/htons.c (htons): Renamed from __htons. 1997-07-27 Andreas Schwab * sysdeps/m68k/bits/byteswap.h: New file. 1997-07-27 23:50 Philip Blundell * inet/netinet/icmp6.h: Update for new drafts of POSIX.1g and IPv6 advanced API. * inet/netinet/in.h: Likewise. * inet/netinet/ip6.h: Likewise. * sysdeps/unix/sysv/linux/bits/socket.h: Likewise. * posix/sys/types.h: Add socklen_t. * manual/socket.texi: Document some more IPv6 things. * manual/libc.texinfo: Likewise. 1997-07-26 Andreas Jaeger * libc.map: Define missing symbol. 1997-07-27 14:31 Thorsten Kukuk * nis/Makefile: Add nis_findserv. * nis/lckcache.c: Fix typo. * nis/nis_call.c: Fix problems with multihomed servers. * nis/nis_findserv.c: New file. * nis/nis_intern.h: Add more prototypes. * nis/nis_lookup.c (nis_lookup): Don't try the next server if network is unreachable. * nis/nis_table.c (nis_list): Likewise. * nis/nis_ping.c (nis_ping): Use MASTER_ONLY, don't call abort() in error case. * nis/nis_util.c (__nis_finddirectory): Give the right error code back. * nis/ypclnt.c: Make sure, that all sockets are closed. --- elf/dl-profile.c | 267 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 267 insertions(+) create mode 100644 elf/dl-profile.c (limited to 'elf/dl-profile.c') diff --git a/elf/dl-profile.c b/elf/dl-profile.c new file mode 100644 index 0000000..cc25b61 --- /dev/null +++ b/elf/dl-profile.c @@ -0,0 +1,267 @@ +/* Profiling of shared libraries. + Copyright (C) 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* The LD_PROFILE feature has to be implemented different to the + normal profiling using the gmon/ functions. The problem is that an + arbitrary amount of processes simulataneously can be run using + profiling and all write the results in the same file. To provide + this mechanism one could implement a complicated mechanism to merge + the content of two profiling runs or one could extend the file + format to allow more than one data set. For the second solution we + would have the problem that the file can grow in size beyond any + limit and both solutions have the problem that the concurrency of + writing the results is a big problem. + + Another much simpler method is to use mmap to map the same file in + all using programs and modify the data in the mmap'ed area and so + also automatically on the disk. Using the MAP_SHARED option of + mmap(2) this can be done without big problems in more than one + file. + + This approach is very different from the normal profiling. We have + to use the profiling data in exactly the way they are expected to + be written to disk. */ + +extern char *_strerror_internal __P ((int, char *buf, size_t)); + +extern int __profile_frequency __P ((void)); + + +static struct gmonparam param; + +/* We define a special type to address the elements of the arc table. + This is basically the `gmon_cg_arc_record' format but it includes + the room for the tag and it uses real types. */ +struct here_cg_arc_record + { + char tag; + uintptr_t from_pc __attribute__ ((packed)); + uintptr_t self_pc __attribute__ ((packed)); + uint32_t count __attribute__ ((packed)); + }; + +static struct here_cg_arc_record *data; + + +void +_dl_start_profile (struct link_map *map, const char *output_dir) +{ + char *filename; + int fd; + struct stat st; + const ElfW(Phdr) *ph; + ElfW(Addr) mapstart = ~((ElfW(Addr)) 0); + ElfW(Addr) mapend = 0; + off_t expected_size; + struct gmon_hdr gmon_hdr; + struct gmon_hist_hdr hist_hdr; + struct gmon_hdr *addr; + char *hist; + + /* Compute the size of the sections which contain program code. */ + for (ph = map->l_phdr; ph < &map->l_phdr[map->l_phnum]; ++ph) + if (ph->p_type == PT_LOAD && (ph->p_flags & PF_X)) + { + ElfW(Addr) start = (ph->p_vaddr & ~(_dl_pagesize - 1)); + ElfW(Addr) end = ((ph->p_vaddr + ph->p_memsz + _dl_pagesize - 1) + & ~(_dl_pagesize - 1)); + + if (start < mapstart) + mapstart = start; + if (end > mapend) + mapend = end; + } + + /* Now we can compute the size of the profiling data. This is done + with the same formulars as in `monstartup' (see gmon.c). */ + param.state = GMON_PROF_OFF; + param.lowpc = mapstart + map->l_addr; + param.highpc = mapend + map->l_addr; + param.textsize = mapend - mapstart; + param.kcountsize = param.textsize / HISTFRACTION; + param.hashfraction = HASHFRACTION; + param.log_hashfraction = -1; + if ((HASHFRACTION & (HASHFRACTION - 1)) == 0) + /* If HASHFRACTION is a power of two, mcount can use shifting + instead of integer division. Precompute shift amount. */ + param.log_hashfraction = ffs (param.hashfraction + * sizeof (*param.froms)) - 1; + param.fromssize = param.textsize / HASHFRACTION; + param.tolimit = param.textsize * ARCDENSITY / 100; + if (param.tolimit < MINARCS) + param.tolimit = MINARCS; + if (param.tolimit > MAXARCS) + param.tolimit = MAXARCS; + param.tossize = param.tolimit * sizeof (struct tostruct); + + expected_size = (sizeof (struct gmon_hdr) + + 1 + sizeof (struct gmon_hist_hdr) + + ((1 + sizeof (struct gmon_cg_arc_record)) + * (param.fromssize / sizeof (*param.froms)))); + + /* Create the gmon_hdr we expect or write. */ + memset (&gmon_hdr, '\0', sizeof (struct gmon_hdr)); + memcpy (&gmon_hdr.cookie[0], GMON_MAGIC, sizeof (gmon_hdr.cookie)); + *(int32_t *) gmon_hdr.version = GMON_VERSION; + + /* Create the hist_hdr we expect or write. */ + *(char **) hist_hdr.low_pc = (char *) mapstart; + *(char **) hist_hdr.high_pc = (char *) mapend; + *(int32_t *) hist_hdr.hist_size = param.kcountsize / sizeof (HISTCOUNTER); + *(int32_t *) hist_hdr.prof_rate = __profile_frequency (); + strncpy (hist_hdr.dimen, "seconds", sizeof (hist_hdr.dimen)); + hist_hdr.dimen_abbrev = 's'; + + /* First determine the output name. We write in the directory + OUTPUT_DIR and the name is composed from the shared objects + soname (or the file name) and the ending ".profile". */ + filename = (char *) alloca (strlen (output_dir) + 1 + strlen (_dl_profile) + + sizeof ".profile"); + __stpcpy (__stpcpy (__stpcpy (__stpcpy (filename, output_dir), "/"), + _dl_profile), + ".profile"); + + fd = __open (filename, O_RDWR | O_CREAT, 0666); + if (fd == -1) + /* We cannot write the profiling data so don't do anthing. */ + return; + + if (fstat (fd, &st) < 0 || !S_ISREG (st.st_mode)) + { + /* Not stat'able or not a regular file => don't use it. */ + close (fd); + return; + } + + /* Test the size. If it does not match what we expect from the size + values in the map MAP we don't use it and warn the user. */ + if (st.st_size == 0) + { + /* We have to create the file. */ + char buf[_dl_pagesize]; + + memset (buf, '\0', _dl_pagesize); + + if (__lseek (fd, expected_size & ~(_dl_pagesize - 1), SEEK_SET) == -1) + { + char buf[400]; + int errnum; + cannot_create: + errnum = errno; + __close (fd); + fprintf (stderr, "%s: cannot create file: %s\n", filename, + _strerror_internal (errnum, buf, sizeof buf)); + return; + } + + if (TEMP_FAILURE_RETRY (__write (fd, buf, (expected_size + & (_dl_pagesize - 1)))) < 0) + goto cannot_create; + } + else if (st.st_size != expected_size) + { + __close (fd); + wrong_format: + fprintf (stderr, "%s: file is no correct profile data file for `%s'\n", + filename, _dl_profile); + return; + } + + addr = (void *) __mmap (NULL, expected_size, PROT_READ|PROT_WRITE, + MAP_SHARED|MAP_FILE, fd, 0); + if (addr == (void *) -1) + { + char buf[400]; + int errnum = errno; + __close (fd); + fprintf (stderr, "%s: cannot map file: %s\n", filename, + _strerror_internal (errnum, buf, sizeof buf)); + return; + } + + /* We don't need the file desriptor anymore. */ + __close (fd); + + /* Pointer to data after the header. */ + hist = (char *) (addr + 1); + + /* Compute pointer to array of the arc information. */ + data = (struct here_cg_arc_record *) (hist + 1 + + sizeof (struct gmon_hist_hdr)); + + if (st.st_size == 0) + { + /* Create the signature. */ + size_t cnt; + + memcpy (addr, &gmon_hdr, sizeof (struct gmon_hdr)); + + *hist = GMON_TAG_TIME_HIST; + memcpy (hist + 1, &hist_hdr, sizeof (struct gmon_hist_hdr)); + + for (cnt = 0; cnt < param.fromssize / sizeof (*param.froms); ++cnt) + data[cnt].tag = GMON_TAG_CG_ARC; + } + else + { + /* Test the signature in the file. */ + if (memcmp (addr, &gmon_hdr, sizeof (struct gmon_hdr)) != 0 + || *hist != GMON_TAG_TIME_HIST + || memcmp (hist + 1, &hist_hdr, sizeof (struct gmon_hist_hdr)) != 0) + goto wrong_format; + } + + /* Turn on profiling. */ + param.state = GMON_PROF_ON; +} + + +void +_dl_mcount (ElfW(Addr) frompc, ElfW(Addr) selfpc) +{ + if (param.state != GMON_PROF_ON) + return; + param.state = GMON_PROF_BUSY; + + /* Compute relative addresses. The shared object can be loaded at + any address. The value of frompc could be anything. We cannot + restrict it in any way, just set to a fixed value (0) in case it + is outside the allowed range. These calls show up as calls from + in the gprof output. */ + frompc -= param.lowpc; + if (frompc >= param.textsize) + frompc = 0; + selfpc -= param.lowpc; + + param.state = GMON_PROF_ON; +} -- cgit v1.1