133965Sjdp/* Core file generic interface routines for BFD.
2218822Sdim   Copyright 1990, 1991, 1992, 1993, 1994, 2000, 2001, 2002, 2003, 2005,
3218822Sdim   2007 Free Software Foundation, Inc.
433965Sjdp   Written by Cygnus Support.
533965Sjdp
633965SjdpThis file is part of BFD, the Binary File Descriptor library.
733965Sjdp
833965SjdpThis program is free software; you can redistribute it and/or modify
933965Sjdpit under the terms of the GNU General Public License as published by
1033965Sjdpthe Free Software Foundation; either version 2 of the License, or
1133965Sjdp(at your option) any later version.
1233965Sjdp
1333965SjdpThis program is distributed in the hope that it will be useful,
1433965Sjdpbut WITHOUT ANY WARRANTY; without even the implied warranty of
1533965SjdpMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1633965SjdpGNU General Public License for more details.
1733965Sjdp
1833965SjdpYou should have received a copy of the GNU General Public License
1933965Sjdpalong with this program; if not, write to the Free Software
20218822SdimFoundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
2133965Sjdp
2233965Sjdp/*
2333965SjdpSECTION
2433965Sjdp	Core files
2533965Sjdp
26218822SdimSUBSECTION
27218822Sdim	Core file functions
28218822Sdim
2933965SjdpDESCRIPTION
3033965Sjdp	These are functions pertaining to core files.
3133965Sjdp*/
3233965Sjdp
33218822Sdim#include "sysdep.h"
3433965Sjdp#include "bfd.h"
3533965Sjdp#include "libbfd.h"
3633965Sjdp
3733965Sjdp/*
3833965SjdpFUNCTION
3933965Sjdp	bfd_core_file_failing_command
4033965Sjdp
4133965SjdpSYNOPSIS
42130561Sobrien	const char *bfd_core_file_failing_command (bfd *abfd);
4333965Sjdp
4433965SjdpDESCRIPTION
4533965Sjdp	Return a read-only string explaining which program was running
4633965Sjdp	when it failed and produced the core file @var{abfd}.
4733965Sjdp
4833965Sjdp*/
4933965Sjdp
50104834Sobrienconst char *
51130561Sobrienbfd_core_file_failing_command (bfd *abfd)
5233965Sjdp{
53130561Sobrien  if (abfd->format != bfd_core)
54130561Sobrien    {
55130561Sobrien      bfd_set_error (bfd_error_invalid_operation);
56130561Sobrien      return NULL;
57130561Sobrien    }
5833965Sjdp  return BFD_SEND (abfd, _core_file_failing_command, (abfd));
5933965Sjdp}
6033965Sjdp
6133965Sjdp/*
6233965SjdpFUNCTION
6333965Sjdp	bfd_core_file_failing_signal
6433965Sjdp
6533965SjdpSYNOPSIS
66130561Sobrien	int bfd_core_file_failing_signal (bfd *abfd);
6733965Sjdp
6833965SjdpDESCRIPTION
6933965Sjdp	Returns the signal number which caused the core dump which
7033965Sjdp	generated the file the BFD @var{abfd} is attached to.
7133965Sjdp*/
7233965Sjdp
7333965Sjdpint
74130561Sobrienbfd_core_file_failing_signal (bfd *abfd)
7533965Sjdp{
76130561Sobrien  if (abfd->format != bfd_core)
77130561Sobrien    {
78130561Sobrien      bfd_set_error (bfd_error_invalid_operation);
79130561Sobrien      return 0;
80130561Sobrien    }
8133965Sjdp  return BFD_SEND (abfd, _core_file_failing_signal, (abfd));
8233965Sjdp}
8333965Sjdp
8433965Sjdp/*
8533965SjdpFUNCTION
8633965Sjdp	core_file_matches_executable_p
8733965Sjdp
8833965SjdpSYNOPSIS
89130561Sobrien	bfd_boolean core_file_matches_executable_p
90130561Sobrien	  (bfd *core_bfd, bfd *exec_bfd);
9133965Sjdp
9233965SjdpDESCRIPTION
93130561Sobrien	Return <<TRUE>> if the core file attached to @var{core_bfd}
9433965Sjdp	was generated by a run of the executable file attached to
95130561Sobrien	@var{exec_bfd}, <<FALSE>> otherwise.
9633965Sjdp*/
97130561Sobrien
98130561Sobrienbfd_boolean
99130561Sobriencore_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
10033965Sjdp{
101130561Sobrien  if (core_bfd->format != bfd_core || exec_bfd->format != bfd_object)
102130561Sobrien    {
103130561Sobrien      bfd_set_error (bfd_error_wrong_format);
104130561Sobrien      return FALSE;
105130561Sobrien    }
10633965Sjdp
107130561Sobrien  return BFD_SEND (core_bfd, _core_file_matches_executable_p,
108130561Sobrien		   (core_bfd, exec_bfd));
10933965Sjdp}
110218822Sdim
111218822Sdim/*
112218822SdimFUNCTION
113218822Sdim        generic_core_file_matches_executable_p
114218822Sdim
115218822SdimSYNOPSIS
116218822Sdim        bfd_boolean generic_core_file_matches_executable_p
117218822Sdim          (bfd *core_bfd, bfd *exec_bfd);
118218822Sdim
119218822SdimDESCRIPTION
120218822Sdim        Return TRUE if the core file attached to @var{core_bfd}
121218822Sdim        was generated by a run of the executable file attached
122218822Sdim        to @var{exec_bfd}.  The match is based on executable
123218822Sdim        basenames only.
124218822Sdim
125218822Sdim        Note: When not able to determine the core file failing
126218822Sdim        command or the executable name, we still return TRUE even
127218822Sdim        though we're not sure that core file and executable match.
128218822Sdim        This is to avoid generating a false warning in situations
129218822Sdim        where we really don't know whether they match or not.
130218822Sdim*/
131218822Sdim
132218822Sdimbfd_boolean
133218822Sdimgeneric_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
134218822Sdim{
135218822Sdim  char *exec;
136218822Sdim  char *core;
137218822Sdim  char *last_slash;
138218822Sdim
139218822Sdim  if (exec_bfd == NULL || core_bfd == NULL)
140218822Sdim    return TRUE;
141218822Sdim
142218822Sdim  /* The cast below is to avoid a compiler warning due to the assignment
143218822Sdim     of the const char * returned by bfd_core_file_failing_command to a
144218822Sdim     non-const char *.  In this case, the assignement does not lead to
145218822Sdim     breaking the const, as we're only reading the string.  */
146218822Sdim
147218822Sdim  core = (char *) bfd_core_file_failing_command (core_bfd);
148218822Sdim  if (core == NULL)
149218822Sdim    return TRUE;
150218822Sdim
151218822Sdim  exec = bfd_get_filename (exec_bfd);
152218822Sdim  if (exec == NULL)
153218822Sdim    return TRUE;
154218822Sdim
155218822Sdim  last_slash = strrchr (core, '/');
156218822Sdim  if (last_slash != NULL)
157218822Sdim    core = last_slash + 1;
158218822Sdim
159218822Sdim  last_slash = strrchr (exec, '/');
160218822Sdim  if (last_slash != NULL)
161218822Sdim    exec = last_slash + 1;
162218822Sdim
163218822Sdim  return strcmp (exec, core) == 0;
164218822Sdim}
165218822Sdim
166