corefile.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Core file generic interface routines for BFD.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. Written by Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program 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.
  9. This program 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
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /*
  18. SECTION
  19. Core files
  20. SUBSECTION
  21. Core file functions
  22. DESCRIPTION
  23. These are functions pertaining to core files.
  24. */
  25. #include "sysdep.h"
  26. #include "bfd.h"
  27. #include "libbfd.h"
  28. /*
  29. FUNCTION
  30. bfd_core_file_failing_command
  31. SYNOPSIS
  32. const char *bfd_core_file_failing_command (bfd *abfd);
  33. DESCRIPTION
  34. Return a read-only string explaining which program was running
  35. when it failed and produced the core file @var{abfd}.
  36. */
  37. const char *
  38. bfd_core_file_failing_command (bfd *abfd)
  39. {
  40. if (abfd->format != bfd_core)
  41. {
  42. bfd_set_error (bfd_error_invalid_operation);
  43. return NULL;
  44. }
  45. return BFD_SEND (abfd, _core_file_failing_command, (abfd));
  46. }
  47. /*
  48. FUNCTION
  49. bfd_core_file_failing_signal
  50. SYNOPSIS
  51. int bfd_core_file_failing_signal (bfd *abfd);
  52. DESCRIPTION
  53. Returns the signal number which caused the core dump which
  54. generated the file the BFD @var{abfd} is attached to.
  55. */
  56. int
  57. bfd_core_file_failing_signal (bfd *abfd)
  58. {
  59. if (abfd->format != bfd_core)
  60. {
  61. bfd_set_error (bfd_error_invalid_operation);
  62. return 0;
  63. }
  64. return BFD_SEND (abfd, _core_file_failing_signal, (abfd));
  65. }
  66. /*
  67. FUNCTION
  68. bfd_core_file_pid
  69. SYNOPSIS
  70. int bfd_core_file_pid (bfd *abfd);
  71. DESCRIPTION
  72. Returns the PID of the process the core dump the BFD
  73. @var{abfd} is attached to was generated from.
  74. */
  75. int
  76. bfd_core_file_pid (bfd *abfd)
  77. {
  78. if (abfd->format != bfd_core)
  79. {
  80. bfd_set_error (bfd_error_invalid_operation);
  81. return 0;
  82. }
  83. return BFD_SEND (abfd, _core_file_pid, (abfd));
  84. }
  85. /*
  86. FUNCTION
  87. core_file_matches_executable_p
  88. SYNOPSIS
  89. bool core_file_matches_executable_p
  90. (bfd *core_bfd, bfd *exec_bfd);
  91. DESCRIPTION
  92. Return <<TRUE>> if the core file attached to @var{core_bfd}
  93. was generated by a run of the executable file attached to
  94. @var{exec_bfd}, <<FALSE>> otherwise.
  95. */
  96. bool
  97. core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
  98. {
  99. if (core_bfd->format != bfd_core || exec_bfd->format != bfd_object)
  100. {
  101. bfd_set_error (bfd_error_wrong_format);
  102. return false;
  103. }
  104. return BFD_SEND (core_bfd, _core_file_matches_executable_p,
  105. (core_bfd, exec_bfd));
  106. }
  107. /*
  108. FUNCTION
  109. generic_core_file_matches_executable_p
  110. SYNOPSIS
  111. bool generic_core_file_matches_executable_p
  112. (bfd *core_bfd, bfd *exec_bfd);
  113. DESCRIPTION
  114. Return TRUE if the core file attached to @var{core_bfd}
  115. was generated by a run of the executable file attached
  116. to @var{exec_bfd}. The match is based on executable
  117. basenames only.
  118. Note: When not able to determine the core file failing
  119. command or the executable name, we still return TRUE even
  120. though we're not sure that core file and executable match.
  121. This is to avoid generating a false warning in situations
  122. where we really don't know whether they match or not.
  123. */
  124. bool
  125. generic_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
  126. {
  127. const char *exec;
  128. const char *core;
  129. const char *last_slash;
  130. if (exec_bfd == NULL || core_bfd == NULL)
  131. return true;
  132. /* The cast below is to avoid a compiler warning due to the assignment
  133. of the const char * returned by bfd_core_file_failing_command to a
  134. non-const char *. In this case, the assignement does not lead to
  135. breaking the const, as we're only reading the string. */
  136. core = bfd_core_file_failing_command (core_bfd);
  137. if (core == NULL)
  138. return true;
  139. exec = bfd_get_filename (exec_bfd);
  140. if (exec == NULL)
  141. return true;
  142. last_slash = strrchr (core, '/');
  143. if (last_slash != NULL)
  144. core = last_slash + 1;
  145. last_slash = strrchr (exec, '/');
  146. if (last_slash != NULL)
  147. exec = last_slash + 1;
  148. return filename_cmp (exec, core) == 0;
  149. }