175fd0b74Schristos /* Core file generic interface routines for BFD.
2*e992f068Schristos Copyright (C) 1990-2022 Free Software Foundation, Inc.
375fd0b74Schristos Written by Cygnus Support.
475fd0b74Schristos
575fd0b74Schristos This file is part of BFD, the Binary File Descriptor library.
675fd0b74Schristos
775fd0b74Schristos This program is free software; you can redistribute it and/or modify
875fd0b74Schristos it under the terms of the GNU General Public License as published by
975fd0b74Schristos the Free Software Foundation; either version 3 of the License, or
1075fd0b74Schristos (at your option) any later version.
1175fd0b74Schristos
1275fd0b74Schristos This program is distributed in the hope that it will be useful,
1375fd0b74Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1475fd0b74Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1575fd0b74Schristos GNU General Public License for more details.
1675fd0b74Schristos
1775fd0b74Schristos You should have received a copy of the GNU General Public License
1875fd0b74Schristos along with this program; if not, write to the Free Software
1975fd0b74Schristos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
2075fd0b74Schristos MA 02110-1301, USA. */
2175fd0b74Schristos
2275fd0b74Schristos /*
2375fd0b74Schristos SECTION
2475fd0b74Schristos Core files
2575fd0b74Schristos
2675fd0b74Schristos SUBSECTION
2775fd0b74Schristos Core file functions
2875fd0b74Schristos
2975fd0b74Schristos DESCRIPTION
3075fd0b74Schristos These are functions pertaining to core files.
3175fd0b74Schristos */
3275fd0b74Schristos
3375fd0b74Schristos #include "sysdep.h"
3475fd0b74Schristos #include "bfd.h"
3575fd0b74Schristos #include "libbfd.h"
3675fd0b74Schristos
3775fd0b74Schristos /*
3875fd0b74Schristos FUNCTION
3975fd0b74Schristos bfd_core_file_failing_command
4075fd0b74Schristos
4175fd0b74Schristos SYNOPSIS
4275fd0b74Schristos const char *bfd_core_file_failing_command (bfd *abfd);
4375fd0b74Schristos
4475fd0b74Schristos DESCRIPTION
4575fd0b74Schristos Return a read-only string explaining which program was running
4675fd0b74Schristos when it failed and produced the core file @var{abfd}.
4775fd0b74Schristos
4875fd0b74Schristos */
4975fd0b74Schristos
5075fd0b74Schristos const char *
bfd_core_file_failing_command(bfd * abfd)5175fd0b74Schristos bfd_core_file_failing_command (bfd *abfd)
5275fd0b74Schristos {
5375fd0b74Schristos if (abfd->format != bfd_core)
5475fd0b74Schristos {
5575fd0b74Schristos bfd_set_error (bfd_error_invalid_operation);
5675fd0b74Schristos return NULL;
5775fd0b74Schristos }
5875fd0b74Schristos return BFD_SEND (abfd, _core_file_failing_command, (abfd));
5975fd0b74Schristos }
6075fd0b74Schristos
6175fd0b74Schristos /*
6275fd0b74Schristos FUNCTION
6375fd0b74Schristos bfd_core_file_failing_signal
6475fd0b74Schristos
6575fd0b74Schristos SYNOPSIS
6675fd0b74Schristos int bfd_core_file_failing_signal (bfd *abfd);
6775fd0b74Schristos
6875fd0b74Schristos DESCRIPTION
6975fd0b74Schristos Returns the signal number which caused the core dump which
7075fd0b74Schristos generated the file the BFD @var{abfd} is attached to.
7175fd0b74Schristos */
7275fd0b74Schristos
7375fd0b74Schristos int
bfd_core_file_failing_signal(bfd * abfd)7475fd0b74Schristos bfd_core_file_failing_signal (bfd *abfd)
7575fd0b74Schristos {
7675fd0b74Schristos if (abfd->format != bfd_core)
7775fd0b74Schristos {
7875fd0b74Schristos bfd_set_error (bfd_error_invalid_operation);
7975fd0b74Schristos return 0;
8075fd0b74Schristos }
8175fd0b74Schristos return BFD_SEND (abfd, _core_file_failing_signal, (abfd));
8275fd0b74Schristos }
8375fd0b74Schristos
8475fd0b74Schristos /*
8575fd0b74Schristos FUNCTION
8675fd0b74Schristos bfd_core_file_pid
8775fd0b74Schristos
8875fd0b74Schristos SYNOPSIS
8975fd0b74Schristos int bfd_core_file_pid (bfd *abfd);
9075fd0b74Schristos
9175fd0b74Schristos DESCRIPTION
9275fd0b74Schristos
9375fd0b74Schristos Returns the PID of the process the core dump the BFD
9475fd0b74Schristos @var{abfd} is attached to was generated from.
9575fd0b74Schristos */
9675fd0b74Schristos
9775fd0b74Schristos int
bfd_core_file_pid(bfd * abfd)9875fd0b74Schristos bfd_core_file_pid (bfd *abfd)
9975fd0b74Schristos {
10075fd0b74Schristos if (abfd->format != bfd_core)
10175fd0b74Schristos {
10275fd0b74Schristos bfd_set_error (bfd_error_invalid_operation);
10375fd0b74Schristos return 0;
10475fd0b74Schristos }
10575fd0b74Schristos return BFD_SEND (abfd, _core_file_pid, (abfd));
10675fd0b74Schristos }
10775fd0b74Schristos
10875fd0b74Schristos
10975fd0b74Schristos /*
11075fd0b74Schristos FUNCTION
11175fd0b74Schristos core_file_matches_executable_p
11275fd0b74Schristos
11375fd0b74Schristos SYNOPSIS
114*e992f068Schristos bool core_file_matches_executable_p
11575fd0b74Schristos (bfd *core_bfd, bfd *exec_bfd);
11675fd0b74Schristos
11775fd0b74Schristos DESCRIPTION
11875fd0b74Schristos Return <<TRUE>> if the core file attached to @var{core_bfd}
11975fd0b74Schristos was generated by a run of the executable file attached to
12075fd0b74Schristos @var{exec_bfd}, <<FALSE>> otherwise.
12175fd0b74Schristos */
12275fd0b74Schristos
123*e992f068Schristos bool
core_file_matches_executable_p(bfd * core_bfd,bfd * exec_bfd)12475fd0b74Schristos core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
12575fd0b74Schristos {
12675fd0b74Schristos if (core_bfd->format != bfd_core || exec_bfd->format != bfd_object)
12775fd0b74Schristos {
12875fd0b74Schristos bfd_set_error (bfd_error_wrong_format);
129*e992f068Schristos return false;
13075fd0b74Schristos }
13175fd0b74Schristos
13275fd0b74Schristos return BFD_SEND (core_bfd, _core_file_matches_executable_p,
13375fd0b74Schristos (core_bfd, exec_bfd));
13475fd0b74Schristos }
13575fd0b74Schristos
13675fd0b74Schristos /*
13775fd0b74Schristos FUNCTION
13875fd0b74Schristos generic_core_file_matches_executable_p
13975fd0b74Schristos
14075fd0b74Schristos SYNOPSIS
141*e992f068Schristos bool generic_core_file_matches_executable_p
14275fd0b74Schristos (bfd *core_bfd, bfd *exec_bfd);
14375fd0b74Schristos
14475fd0b74Schristos DESCRIPTION
14575fd0b74Schristos Return TRUE if the core file attached to @var{core_bfd}
14675fd0b74Schristos was generated by a run of the executable file attached
14775fd0b74Schristos to @var{exec_bfd}. The match is based on executable
14875fd0b74Schristos basenames only.
14975fd0b74Schristos
15075fd0b74Schristos Note: When not able to determine the core file failing
15175fd0b74Schristos command or the executable name, we still return TRUE even
15275fd0b74Schristos though we're not sure that core file and executable match.
15375fd0b74Schristos This is to avoid generating a false warning in situations
15475fd0b74Schristos where we really don't know whether they match or not.
15575fd0b74Schristos */
15675fd0b74Schristos
157*e992f068Schristos bool
generic_core_file_matches_executable_p(bfd * core_bfd,bfd * exec_bfd)15875fd0b74Schristos generic_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
15975fd0b74Schristos {
160012573ebSchristos const char *exec;
161012573ebSchristos const char *core;
162012573ebSchristos const char *last_slash;
16375fd0b74Schristos
16475fd0b74Schristos if (exec_bfd == NULL || core_bfd == NULL)
165*e992f068Schristos return true;
16675fd0b74Schristos
16775fd0b74Schristos /* The cast below is to avoid a compiler warning due to the assignment
16875fd0b74Schristos of the const char * returned by bfd_core_file_failing_command to a
16975fd0b74Schristos non-const char *. In this case, the assignement does not lead to
17075fd0b74Schristos breaking the const, as we're only reading the string. */
17175fd0b74Schristos
172012573ebSchristos core = bfd_core_file_failing_command (core_bfd);
17375fd0b74Schristos if (core == NULL)
174*e992f068Schristos return true;
17575fd0b74Schristos
17675fd0b74Schristos exec = bfd_get_filename (exec_bfd);
17775fd0b74Schristos if (exec == NULL)
178*e992f068Schristos return true;
17975fd0b74Schristos
18075fd0b74Schristos last_slash = strrchr (core, '/');
18175fd0b74Schristos if (last_slash != NULL)
18275fd0b74Schristos core = last_slash + 1;
18375fd0b74Schristos
18475fd0b74Schristos last_slash = strrchr (exec, '/');
18575fd0b74Schristos if (last_slash != NULL)
18675fd0b74Schristos exec = last_slash + 1;
18775fd0b74Schristos
18875fd0b74Schristos return filename_cmp (exec, core) == 0;
18975fd0b74Schristos }
19075fd0b74Schristos
191