pcprofile.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Profile PC and write result to FIFO.
  2. Copyright (C) 1999, 2000 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #define _GNU_SOURCE
  18. #include <features.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. /* Nonzero if we are actually doing something. */
  25. static int active;
  26. /* The file descriptor of the FIFO. */
  27. static int fd;
  28. static void __attribute__ ((constructor))
  29. install (void)
  30. {
  31. /* See whether the environment variable `PCPROFILE_OUTPUT' is defined.
  32. If yes, it should name a FIFO. We open it and mark ourself as active. */
  33. const char *outfile = getenv ("PCPROFILE_OUTPUT");
  34. if (outfile != NULL && *outfile != '\0')
  35. {
  36. fd = open (outfile, O_RDWR | O_CREAT, 0666);
  37. if (fd != -1)
  38. {
  39. uint32_t word;
  40. active = 1;
  41. /* Write a magic word which tells the reader about the byte
  42. order and the size of the following entries. */
  43. word = 0xdeb00000 | sizeof (void *);
  44. if (TEMP_FAILURE_RETRY (write (fd, &word, 4)) != 4)
  45. {
  46. /* If even this fails we shouldn't try further. */
  47. close (fd);
  48. fd = -1;
  49. active = 0;
  50. }
  51. }
  52. }
  53. }
  54. static void __attribute__ ((destructor))
  55. uninstall (void)
  56. {
  57. if (active)
  58. close (fd);
  59. }
  60. void __cyg_profile_func_enter (void *this_fn, void *call_site)
  61. {
  62. void *buf[2];
  63. if (! active)
  64. return;
  65. /* Now write out the current position and that of the caller. We do
  66. this now, and don't cache the because we want real-time output. */
  67. buf[0] = this_fn;
  68. buf[1] = call_site;
  69. write (fd, buf, sizeof buf);
  70. }
  71. /* We don't handle entry and exit differently here. */
  72. strong_alias (__cyg_profile_func_enter, __cyg_profile_func_exit)