Back to index

cell-binutils  2.17cvs20070401
corefile.c
Go to the documentation of this file.
00001 /* Core file generic interface routines for BFD.
00002    Copyright 1990, 1991, 1992, 1993, 1994, 2000, 2001, 2002, 2003, 2005
00003    Free Software Foundation, Inc.
00004    Written by Cygnus Support.
00005 
00006 This file is part of BFD, the Binary File Descriptor library.
00007 
00008 This program is free software; you can redistribute it and/or modify
00009 it under the terms of the GNU General Public License as published by
00010 the Free Software Foundation; either version 2 of the License, or
00011 (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with this program; if not, write to the Free Software
00020 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00021 
00022 /*
00023 SECTION
00024        Core files
00025 
00026 SUBSECTION
00027        Core file functions
00028 
00029 DESCRIPTION
00030        These are functions pertaining to core files.
00031 */
00032 
00033 #include "bfd.h"
00034 #include "sysdep.h"
00035 #include "libbfd.h"
00036 
00037 /*
00038 FUNCTION
00039        bfd_core_file_failing_command
00040 
00041 SYNOPSIS
00042        const char *bfd_core_file_failing_command (bfd *abfd);
00043 
00044 DESCRIPTION
00045        Return a read-only string explaining which program was running
00046        when it failed and produced the core file @var{abfd}.
00047 
00048 */
00049 
00050 const char *
00051 bfd_core_file_failing_command (bfd *abfd)
00052 {
00053   if (abfd->format != bfd_core)
00054     {
00055       bfd_set_error (bfd_error_invalid_operation);
00056       return NULL;
00057     }
00058   return BFD_SEND (abfd, _core_file_failing_command, (abfd));
00059 }
00060 
00061 /*
00062 FUNCTION
00063        bfd_core_file_failing_signal
00064 
00065 SYNOPSIS
00066        int bfd_core_file_failing_signal (bfd *abfd);
00067 
00068 DESCRIPTION
00069        Returns the signal number which caused the core dump which
00070        generated the file the BFD @var{abfd} is attached to.
00071 */
00072 
00073 int
00074 bfd_core_file_failing_signal (bfd *abfd)
00075 {
00076   if (abfd->format != bfd_core)
00077     {
00078       bfd_set_error (bfd_error_invalid_operation);
00079       return 0;
00080     }
00081   return BFD_SEND (abfd, _core_file_failing_signal, (abfd));
00082 }
00083 
00084 /*
00085 FUNCTION
00086        core_file_matches_executable_p
00087 
00088 SYNOPSIS
00089        bfd_boolean core_file_matches_executable_p
00090          (bfd *core_bfd, bfd *exec_bfd);
00091 
00092 DESCRIPTION
00093        Return <<TRUE>> if the core file attached to @var{core_bfd}
00094        was generated by a run of the executable file attached to
00095        @var{exec_bfd}, <<FALSE>> otherwise.
00096 */
00097 
00098 bfd_boolean
00099 core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
00100 {
00101   if (core_bfd->format != bfd_core || exec_bfd->format != bfd_object)
00102     {
00103       bfd_set_error (bfd_error_wrong_format);
00104       return FALSE;
00105     }
00106 
00107   return BFD_SEND (core_bfd, _core_file_matches_executable_p,
00108                  (core_bfd, exec_bfd));
00109 }
00110 
00111 /*
00112 FUNCTION
00113         generic_core_file_matches_executable_p
00114 
00115 SYNOPSIS
00116         bfd_boolean generic_core_file_matches_executable_p
00117           (bfd *core_bfd, bfd *exec_bfd);
00118 
00119 DESCRIPTION
00120         Return TRUE if the core file attached to @var{core_bfd}
00121         was generated by a run of the executable file attached
00122         to @var{exec_bfd}.  The match is based on executable
00123         basenames only.
00124 
00125         Note: When not able to determine the core file failing
00126         command or the executable name, we still return TRUE even
00127         though we're not sure that core file and executable match.
00128         This is to avoid generating a false warning in situations
00129         where we really don't know whether they match or not.
00130 */
00131 
00132 bfd_boolean
00133 generic_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
00134 {
00135   char *exec;
00136   char *core;
00137   char *last_slash;
00138 
00139   if (exec_bfd == NULL || core_bfd == NULL)
00140     return TRUE;
00141 
00142   /* The cast below is to avoid a compiler warning due to the assignment
00143      of the const char * returned by bfd_core_file_failing_command to a
00144      non-const char *.  In this case, the assignement does not lead to
00145      breaking the const, as we're only reading the string.  */
00146      
00147   core = (char *) bfd_core_file_failing_command (core_bfd);
00148   if (core == NULL)
00149     return TRUE;
00150 
00151   exec = bfd_get_filename (exec_bfd);
00152   if (exec == NULL)
00153     return TRUE;
00154 
00155   last_slash = strrchr (core, '/');
00156   if (last_slash != NULL)
00157     core = last_slash + 1;
00158 
00159   last_slash = strrchr (exec, '/');
00160   if (last_slash != NULL)
00161     exec = last_slash + 1;
00162   
00163   return strcmp (exec, core) == 0;
00164 }
00165