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