Skip to content

Commit

Permalink
fix ash -jvm-debugger on java 8
Browse files Browse the repository at this point in the history
  • Loading branch information
qwe2 committed Nov 27, 2024
1 parent ff40ca8 commit a3dc087
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ addResidual () {
}

addDebugger () {
addJava "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:$1"
local major_version=$(get_java_major_version)
local address="*:$1"
if [ $major_version -le 8 ]; then
address="$1"
fi
addJava "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$address"
}

require_arg () {
Expand All @@ -69,6 +74,24 @@ require_arg () {
fi
}

get_java_major_version () {
readonly java_version=$("$java_cmd" -version 2>&1 | awk -F '"' '/version/ {print $2}')
if [[ "$java_version" == "" ]]; then
echoerr "Could not determine Java version. Assuming 8."
echo 8
else
local major=$(echo "$java_version" | cut -d'.' -f1)
if [[ "$major" -eq "1" ]]; then
local major=$(echo "$java_version" | cut -d'.' -f2)
fi
echo $major
fi
}

echoerr () {
echo 1>&2 "$@"
}

# Allow user to specify java options. These get listed first per bash-template.
if [ -n "$JAVA_OPTS" ]
then
Expand All @@ -95,13 +118,16 @@ get_java_cmd() {
# Processes incoming arguments and places them in appropriate global variables. called by the run method.
process_args () {
local no_more_snp_opts=0
# this depends on -java-home, so we need to delay calling addDebugger until all opts have been parsed
local should_add_debugger=

while [ $# -gt 0 ]; do
case "$1" in
--) shift && no_more_snp_opts=1 && break ;;
-h|-help) usage; exit 1 ;;

-mem) echo "!! WARNING !! -mem option is ignored. Please use -J-Xmx and -J-Xms" && shift 2 ;;
-jvm-debug) require_arg port "$1" "$2" && addDebugger $2 && shift 2 ;;
-jvm-debug) require_arg port "$1" "$2" && should_add_debugger=$2 && shift 2 ;;

-main) custom_mainclass="$2" && shift 2 ;;

Expand All @@ -113,6 +139,10 @@ process_args () {
esac
done

if [[ $should_add_debugger ]]; then
addDebugger $should_add_debugger
fi

if [ $no_more_snp_opts ]; then
while [ $# -gt 0 ]; do
addResidual "$1" && shift
Expand Down Expand Up @@ -158,6 +188,7 @@ app_mainclass=${{app_mainclass}}

${{template_declares}}

java_cmd="$(get_java_cmd)"
process_args "$@"

# Fallback to custom mainclass if main class is not provided (this is the case if the JAR contains multiple apps)
Expand All @@ -170,8 +201,6 @@ if [ "$app_mainclass" = "" ] || [ $custom_mainclass ];then
app_mainclass=$custom_mainclass
fi

java_cmd="$(get_java_cmd)"

# If a configuration file exist, read the contents to $opts
[ -f "$script_conf_file" ] && opts=$(loadConfigFile "$script_conf_file")

Expand Down
2 changes: 0 additions & 2 deletions src/sbt-test/ash/script-debug/build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import java.net.ServerSocket

enablePlugins(JavaAppPackaging, AshScriptPlugin)

name := "script-debug"
Expand Down
2 changes: 0 additions & 2 deletions src/sbt-test/bash/script-debug/build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import java.net.ServerSocket

enablePlugins(JavaAppPackaging)

name := "script-debug"
Expand Down

0 comments on commit a3dc087

Please sign in to comment.