15796c8dcSSimon Schubert /* Core file generic interface routines for BFD. 25796c8dcSSimon Schubert Copyright 1990, 1991, 1992, 1993, 1994, 2000, 2001, 2002, 2003, 2005, 35796c8dcSSimon Schubert 2007 Free Software Foundation, Inc. 45796c8dcSSimon Schubert Written by Cygnus Support. 55796c8dcSSimon Schubert 65796c8dcSSimon Schubert This file is part of BFD, the Binary File Descriptor library. 75796c8dcSSimon Schubert 85796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 95796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 105796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 115796c8dcSSimon Schubert (at your option) any later version. 125796c8dcSSimon Schubert 135796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 145796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 155796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 165796c8dcSSimon Schubert GNU General Public License for more details. 175796c8dcSSimon Schubert 185796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 195796c8dcSSimon Schubert along with this program; if not, write to the Free Software 205796c8dcSSimon Schubert Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 215796c8dcSSimon Schubert MA 02110-1301, USA. */ 225796c8dcSSimon Schubert 235796c8dcSSimon Schubert /* 245796c8dcSSimon Schubert SECTION 255796c8dcSSimon Schubert Core files 265796c8dcSSimon Schubert 275796c8dcSSimon Schubert SUBSECTION 285796c8dcSSimon Schubert Core file functions 295796c8dcSSimon Schubert 305796c8dcSSimon Schubert DESCRIPTION 315796c8dcSSimon Schubert These are functions pertaining to core files. 325796c8dcSSimon Schubert */ 335796c8dcSSimon Schubert 345796c8dcSSimon Schubert #include "sysdep.h" 355796c8dcSSimon Schubert #include "bfd.h" 365796c8dcSSimon Schubert #include "libbfd.h" 375796c8dcSSimon Schubert 385796c8dcSSimon Schubert /* 395796c8dcSSimon Schubert FUNCTION 405796c8dcSSimon Schubert bfd_core_file_failing_command 415796c8dcSSimon Schubert 425796c8dcSSimon Schubert SYNOPSIS 435796c8dcSSimon Schubert const char *bfd_core_file_failing_command (bfd *abfd); 445796c8dcSSimon Schubert 455796c8dcSSimon Schubert DESCRIPTION 465796c8dcSSimon Schubert Return a read-only string explaining which program was running 475796c8dcSSimon Schubert when it failed and produced the core file @var{abfd}. 485796c8dcSSimon Schubert 495796c8dcSSimon Schubert */ 505796c8dcSSimon Schubert 515796c8dcSSimon Schubert const char * 525796c8dcSSimon Schubert bfd_core_file_failing_command (bfd *abfd) 535796c8dcSSimon Schubert { 545796c8dcSSimon Schubert if (abfd->format != bfd_core) 555796c8dcSSimon Schubert { 565796c8dcSSimon Schubert bfd_set_error (bfd_error_invalid_operation); 575796c8dcSSimon Schubert return NULL; 585796c8dcSSimon Schubert } 595796c8dcSSimon Schubert return BFD_SEND (abfd, _core_file_failing_command, (abfd)); 605796c8dcSSimon Schubert } 615796c8dcSSimon Schubert 625796c8dcSSimon Schubert /* 635796c8dcSSimon Schubert FUNCTION 645796c8dcSSimon Schubert bfd_core_file_failing_signal 655796c8dcSSimon Schubert 665796c8dcSSimon Schubert SYNOPSIS 675796c8dcSSimon Schubert int bfd_core_file_failing_signal (bfd *abfd); 685796c8dcSSimon Schubert 695796c8dcSSimon Schubert DESCRIPTION 705796c8dcSSimon Schubert Returns the signal number which caused the core dump which 715796c8dcSSimon Schubert generated the file the BFD @var{abfd} is attached to. 725796c8dcSSimon Schubert */ 735796c8dcSSimon Schubert 745796c8dcSSimon Schubert int 755796c8dcSSimon Schubert bfd_core_file_failing_signal (bfd *abfd) 765796c8dcSSimon Schubert { 775796c8dcSSimon Schubert if (abfd->format != bfd_core) 785796c8dcSSimon Schubert { 795796c8dcSSimon Schubert bfd_set_error (bfd_error_invalid_operation); 805796c8dcSSimon Schubert return 0; 815796c8dcSSimon Schubert } 825796c8dcSSimon Schubert return BFD_SEND (abfd, _core_file_failing_signal, (abfd)); 835796c8dcSSimon Schubert } 845796c8dcSSimon Schubert 855796c8dcSSimon Schubert /* 865796c8dcSSimon Schubert FUNCTION 87*c50c785cSJohn Marino bfd_core_file_pid 88*c50c785cSJohn Marino 89*c50c785cSJohn Marino SYNOPSIS 90*c50c785cSJohn Marino int bfd_core_file_pid (bfd *abfd); 91*c50c785cSJohn Marino 92*c50c785cSJohn Marino DESCRIPTION 93*c50c785cSJohn Marino 94*c50c785cSJohn Marino Returns the PID of the process the core dump the BFD 95*c50c785cSJohn Marino @var{abfd} is attached to was generated from. 96*c50c785cSJohn Marino */ 97*c50c785cSJohn Marino 98*c50c785cSJohn Marino int 99*c50c785cSJohn Marino bfd_core_file_pid (bfd *abfd) 100*c50c785cSJohn Marino { 101*c50c785cSJohn Marino if (abfd->format != bfd_core) 102*c50c785cSJohn Marino { 103*c50c785cSJohn Marino bfd_set_error (bfd_error_invalid_operation); 104*c50c785cSJohn Marino return 0; 105*c50c785cSJohn Marino } 106*c50c785cSJohn Marino return BFD_SEND (abfd, _core_file_pid, (abfd)); 107*c50c785cSJohn Marino } 108*c50c785cSJohn Marino 109*c50c785cSJohn Marino 110*c50c785cSJohn Marino /* 111*c50c785cSJohn Marino FUNCTION 1125796c8dcSSimon Schubert core_file_matches_executable_p 1135796c8dcSSimon Schubert 1145796c8dcSSimon Schubert SYNOPSIS 1155796c8dcSSimon Schubert bfd_boolean core_file_matches_executable_p 1165796c8dcSSimon Schubert (bfd *core_bfd, bfd *exec_bfd); 1175796c8dcSSimon Schubert 1185796c8dcSSimon Schubert DESCRIPTION 1195796c8dcSSimon Schubert Return <<TRUE>> if the core file attached to @var{core_bfd} 1205796c8dcSSimon Schubert was generated by a run of the executable file attached to 1215796c8dcSSimon Schubert @var{exec_bfd}, <<FALSE>> otherwise. 1225796c8dcSSimon Schubert */ 1235796c8dcSSimon Schubert 1245796c8dcSSimon Schubert bfd_boolean 1255796c8dcSSimon Schubert core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd) 1265796c8dcSSimon Schubert { 1275796c8dcSSimon Schubert if (core_bfd->format != bfd_core || exec_bfd->format != bfd_object) 1285796c8dcSSimon Schubert { 1295796c8dcSSimon Schubert bfd_set_error (bfd_error_wrong_format); 1305796c8dcSSimon Schubert return FALSE; 1315796c8dcSSimon Schubert } 1325796c8dcSSimon Schubert 1335796c8dcSSimon Schubert return BFD_SEND (core_bfd, _core_file_matches_executable_p, 1345796c8dcSSimon Schubert (core_bfd, exec_bfd)); 1355796c8dcSSimon Schubert } 1365796c8dcSSimon Schubert 1375796c8dcSSimon Schubert /* 1385796c8dcSSimon Schubert FUNCTION 1395796c8dcSSimon Schubert generic_core_file_matches_executable_p 1405796c8dcSSimon Schubert 1415796c8dcSSimon Schubert SYNOPSIS 1425796c8dcSSimon Schubert bfd_boolean generic_core_file_matches_executable_p 1435796c8dcSSimon Schubert (bfd *core_bfd, bfd *exec_bfd); 1445796c8dcSSimon Schubert 1455796c8dcSSimon Schubert DESCRIPTION 1465796c8dcSSimon Schubert Return TRUE if the core file attached to @var{core_bfd} 1475796c8dcSSimon Schubert was generated by a run of the executable file attached 1485796c8dcSSimon Schubert to @var{exec_bfd}. The match is based on executable 1495796c8dcSSimon Schubert basenames only. 1505796c8dcSSimon Schubert 1515796c8dcSSimon Schubert Note: When not able to determine the core file failing 1525796c8dcSSimon Schubert command or the executable name, we still return TRUE even 1535796c8dcSSimon Schubert though we're not sure that core file and executable match. 1545796c8dcSSimon Schubert This is to avoid generating a false warning in situations 1555796c8dcSSimon Schubert where we really don't know whether they match or not. 1565796c8dcSSimon Schubert */ 1575796c8dcSSimon Schubert 1585796c8dcSSimon Schubert bfd_boolean 1595796c8dcSSimon Schubert generic_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd) 1605796c8dcSSimon Schubert { 1615796c8dcSSimon Schubert char *exec; 1625796c8dcSSimon Schubert char *core; 1635796c8dcSSimon Schubert char *last_slash; 1645796c8dcSSimon Schubert 1655796c8dcSSimon Schubert if (exec_bfd == NULL || core_bfd == NULL) 1665796c8dcSSimon Schubert return TRUE; 1675796c8dcSSimon Schubert 1685796c8dcSSimon Schubert /* The cast below is to avoid a compiler warning due to the assignment 1695796c8dcSSimon Schubert of the const char * returned by bfd_core_file_failing_command to a 1705796c8dcSSimon Schubert non-const char *. In this case, the assignement does not lead to 1715796c8dcSSimon Schubert breaking the const, as we're only reading the string. */ 1725796c8dcSSimon Schubert 1735796c8dcSSimon Schubert core = (char *) bfd_core_file_failing_command (core_bfd); 1745796c8dcSSimon Schubert if (core == NULL) 1755796c8dcSSimon Schubert return TRUE; 1765796c8dcSSimon Schubert 1775796c8dcSSimon Schubert exec = bfd_get_filename (exec_bfd); 1785796c8dcSSimon Schubert if (exec == NULL) 1795796c8dcSSimon Schubert return TRUE; 1805796c8dcSSimon Schubert 1815796c8dcSSimon Schubert last_slash = strrchr (core, '/'); 1825796c8dcSSimon Schubert if (last_slash != NULL) 1835796c8dcSSimon Schubert core = last_slash + 1; 1845796c8dcSSimon Schubert 1855796c8dcSSimon Schubert last_slash = strrchr (exec, '/'); 1865796c8dcSSimon Schubert if (last_slash != NULL) 1875796c8dcSSimon Schubert exec = last_slash + 1; 1885796c8dcSSimon Schubert 189*c50c785cSJohn Marino return filename_cmp (exec, core) == 0; 1905796c8dcSSimon Schubert } 1915796c8dcSSimon Schubert 192