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