gcore.in 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env bash
  2. # Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. #
  16. # Script to generate a core file of a running program.
  17. # It starts up gdb, attaches to the given PID and invokes the gcore command.
  18. #
  19. # Need to check for -o option, but set default basename to "core".
  20. prefix=core
  21. # When the -a option is present, this may hold additional commands
  22. # to ensure gdb dumps all mappings (OS dependent).
  23. dump_all_cmds=()
  24. while getopts :ao: opt; do
  25. case "$opt" in
  26. a)
  27. case "$OSTYPE" in
  28. linux*)
  29. dump_all_cmds=("-ex" "set use-coredump-filter off")
  30. dump_all_cmds+=("-ex" "set dump-excluded-mappings on")
  31. ;;
  32. esac
  33. ;;
  34. o)
  35. prefix=$OPTARG
  36. ;;
  37. *)
  38. echo "usage: @GCORE_TRANSFORM_NAME@ [-a] [-o prefix] pid1 [pid2...pidN]"
  39. exit 2
  40. ;;
  41. esac
  42. done
  43. shift $((OPTIND-1))
  44. if [ "$#" -eq "0" ]
  45. then
  46. echo "usage: @GCORE_TRANSFORM_NAME@ [-a] [-o prefix] pid1 [pid2...pidN]"
  47. exit 2
  48. fi
  49. # Attempt to fetch the absolute path to the gcore script that was
  50. # called.
  51. binary_path=`dirname "$0"`
  52. if test "x$binary_path" = x. ; then
  53. # We got "." back as a path. This means the user executed
  54. # the gcore script locally (i.e. ./gcore) or called the
  55. # script via a shell interpreter (i.e. sh gcore).
  56. binary_basename=`basename "$0"`
  57. # If the gcore script was called like "sh gcore" and the script
  58. # lives in the current directory, "which" will not give us "gcore".
  59. # So first we check if the script is in the current directory
  60. # before using the output of "which".
  61. if test -f "$binary_basename" ; then
  62. # We have a local gcore script in ".". This covers the case of
  63. # doing "./gcore" or "sh gcore".
  64. binary_path="."
  65. else
  66. # The gcore script was not found in ".", which means the script
  67. # was called from somewhere else in $PATH by "sh gcore".
  68. # Extract the correct path now.
  69. binary_path_from_env=`which "$0"`
  70. binary_path=`dirname "$binary_path_from_env"`
  71. fi
  72. fi
  73. # Check if the GDB binary is in the expected path. If not, just
  74. # quit with a message.
  75. if [ ! -f "$binary_path/@GDB_TRANSFORM_NAME@" ]; then
  76. echo "gcore: GDB binary (${binary_path}/@GDB_TRANSFORM_NAME@) not found"
  77. exit 1
  78. fi
  79. # Initialise return code.
  80. rc=0
  81. # Loop through pids
  82. for pid in "$@"
  83. do
  84. # `</dev/null' to avoid touching interactive terminal if it is
  85. # available but not accessible as GDB would get stopped on SIGTTIN.
  86. "$binary_path/@GDB_TRANSFORM_NAME@" </dev/null \
  87. --nx --batch --readnever -iex 'set debuginfod enabled off' \
  88. -ex "set pagination off" -ex "set height 0" -ex "set width 0" \
  89. "${dump_all_cmds[@]}" \
  90. -ex "attach $pid" -ex "gcore $prefix.$pid" -ex detach -ex quit
  91. if [ -r "$prefix.$pid" ] ; then
  92. rc=0
  93. else
  94. echo "@GCORE_TRANSFORM_NAME@: failed to create $prefix.$pid"
  95. rc=1
  96. break
  97. fi
  98. done
  99. exit $rc