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