From 7d2cd69791a0251c9b619f6b6a70823adc42ccb3 Mon Sep 17 00:00:00 2001 From: rocky Date: Thu, 5 Dec 2024 20:35:55 -0500 Subject: [PATCH 1/3] Expand filename shown in backtrace --- command/backtrace.sh | 23 ++++++++++++++++------- lib/file.sh | 19 ++++++++++++------- lib/hook.sh | 3 ++- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/command/backtrace.sh b/command/backtrace.sh index 69c933ee..1ccf236f 100644 --- a/command/backtrace.sh +++ b/command/backtrace.sh @@ -2,7 +2,7 @@ # gdb-like "backtrace" debugger command # # Copyright (C) 2002-2006, 2008, 2010-2011, 2018-2019 -# Rocky Bernstein +# 2024 Rocky Bernstein # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -124,12 +124,17 @@ function _Dbg_do_backtrace { typeset filename typeset -i adjusted_pos - # Position 0 is special in that get the line number not from the - # stack but ultimately from LINENO which was saved in the hook call. if (( frame_start == 0 )) ; then ((count--)) ; adjusted_pos=$(_Dbg_frame_adjusted_pos 0) - filename=$(_Dbg_file_canonic "${BASH_SOURCE[$adjusted_pos]}") + filename="${BASH_SOURCE[$adjusted_pos]}" + resolved_filename="${_Dbg_file2canonic[$filename]}" + if [[ ! -z "${resolved_filename}" ]] ; then + filename="$resolved_filename" + else + filename=$(_Dbg_file_canonic "$filename") + fi + _Dbg_frame_print $(_Dbg_frame_prefix 0) '0' '' "$filename" "$_Dbg_frame_last_lineno" '' fi @@ -153,8 +158,6 @@ function _Dbg_do_backtrace { adjusted_pos=$(_Dbg_frame_adjusted_pos $i) _Dbg_msg_nocr $(_Dbg_frame_prefix $i)$i ${FUNCNAME[$adjusted_pos-1]} - typeset parms='' - # Print out parameter list. if (( 0 != ${#BASH_ARGC[@]} )) ; then _Dbg_frame_fn_param_str @@ -170,7 +173,13 @@ function _Dbg_do_backtrace { else lineno=${BASH_LINENO[$adjusted_pos-1]} fi - filename=$(_Dbg_file_canonic "${BASH_SOURCE[$adjusted_pos]}") + + filename="${BASH_SOURCE[$adjusted_pos]}" + resolved_filename="${_Dbg_file2canonic[$filename]}" + if [[ ! -z "${resolved_filename}" ]] ; then + filename="$resolved_filename" + fi + _Dbg_msg "($_Dbg_parm_str) called from file \`$filename'" "at line $lineno" if (( show_source )) ; then _Dbg_get_source_line $lineno "${BASH_SOURCE[$adjusted_pos]}" diff --git a/lib/file.sh b/lib/file.sh index fb239e78..da8aad3f 100644 --- a/lib/file.sh +++ b/lib/file.sh @@ -1,8 +1,8 @@ # -*- shell-script -*- # Things related to file handling. # -# Copyright (C) 2002-2004, 2006, 2008-2010, 2014, 2018, 2020 Rocky -# Bernstein rocky@gnu.org +# Copyright (C) 2002-2004, 2006, 2008-2010, 2014, 2018, 2020 +# 2024 Bernstein rocky@gnu.org # # bashdb is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free @@ -98,6 +98,9 @@ function _Dbg_resolve_expand_filename { # Try using cwd rather that Dbg_init_cwd full_find_file=$(_Dbg_expand_filename "$find_file") fi + if [[ ! -z "$full_find_file" ]]; then + _Dbg_file2canonic["$find_file"]="$full_find_file" + fi echo "$full_find_file" return 0 else @@ -105,14 +108,16 @@ function _Dbg_resolve_expand_filename { typeset -i n=${#_Dbg_dir[@]} typeset -i i for (( i=0 ; i < n; i++ )) ; do - typeset basename="${_Dbg_dir[i]}" + typeset dirname="${_Dbg_dir[i]}" if [[ "$basename" == '\$cdir' ]] ; then - basename=$_Dbg_cdir + dirname=$_Dbg_cdir elif [[ "$basename" == '\$cwd' ]] ; then - basename=$(pwd) + dirname=$(pwd) fi - if [[ -f "$basename/$find_file" ]] ; then - echo "$basename/$find_file" + if [[ -f "$dirname/$find_file" ]] ; then + full_find_file="$dirname/$find_file" + _Dbg_file2canonic["$find_file"]="$full_find_file" + echo "$full_find_file" return 0 fi done diff --git a/lib/hook.sh b/lib/hook.sh index 6653cd53..df77e79a 100644 --- a/lib/hook.sh +++ b/lib/hook.sh @@ -2,7 +2,7 @@ # hook.sh - Debugger trap hook # # Copyright (C) 2002-2011, 2014, 2017-2019 -# Rocky Bernstein +# 2024 Rocky Bernstein # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -150,6 +150,7 @@ _Dbg_debug_trap_handler() { _Dbg_full_filename=$(_Dbg_is_file "$_Dbg_frame_last_filename") if [[ -r "$_Dbg_full_filename" ]] ; then _Dbg_file2canonic["$_Dbg_frame_last_filename"]="$_Dbg_full_filename" + _Dbg_file2canonic["${BASH_SOURCE[1]}"]="$_Dbg_full_filename" fi # Run applicable action statement From b66975978d69fb2d9e878a7303d11a30380ff64e Mon Sep 17 00:00:00 2001 From: rocky Date: Thu, 5 Dec 2024 21:44:16 -0500 Subject: [PATCH 2/3] An attempt to solve problem/screenshot 2 --- command/load.sh | 1 + lib/break.sh | 7 ++++--- lib/filecache.sh | 14 ++++++++++---- lib/hook.sh | 1 + lib/list.sh | 10 +++++++--- lib/save-restore.sh | 7 +++++-- 6 files changed, 28 insertions(+), 12 deletions(-) diff --git a/command/load.sh b/command/load.sh index cd3591ee..a50364aa 100644 --- a/command/load.sh +++ b/command/load.sh @@ -53,6 +53,7 @@ _Dbg_do_load() { done _Dbg_readin "$_Dbg_full_filename" + _Dbg_file2canonic["${_Dbg_filename}"]="$_Dbg_full_filename" _Dbg_msg "File $_Dbg_full_filename loaded." else _Dbg_errmsg "Couldn't resolve or read $_Dbg_filename" diff --git a/lib/break.sh b/lib/break.sh index 449a3aa7..94dc3e80 100644 --- a/lib/break.sh +++ b/lib/break.sh @@ -247,11 +247,12 @@ _Dbg_set_brkpt() { _Dbg_write_journal_eval "_Dbg_brkpt_file2linenos[\"$source_file\"]+=\" $lineno \"" _Dbg_write_journal_eval "_Dbg_brkpt_file2brkpt[\"$source_file\"]+=\" $_Dbg_brkpt_max \"" - source_file=$(_Dbg_adjust_filename "$source_file") + resolved_source_file=$(_Dbg_adjust_filename "$source_file") + _Dbg_file2canonic["$source_file"]="$resolved_source_file" if (( is_temp == 0 )) ; then - _Dbg_msg "Breakpoint $_Dbg_brkpt_max set in file ${source_file}, line $lineno." + _Dbg_msg "Breakpoint $_Dbg_brkpt_max set in file ${resolved_source_file}, line $lineno." else - _Dbg_msg "One-time breakpoint $_Dbg_brkpt_max set in file ${source_file}, line $lineno." + _Dbg_msg "One-time breakpoint $_Dbg_brkpt_max set in file ${resolved_source_file}, line $lineno." fi _Dbg_write_journal "_Dbg_brkpt_max=$_Dbg_brkpt_max" return 0 diff --git a/lib/filecache.sh b/lib/filecache.sh index 3ee92a9f..bbef2399 100644 --- a/lib/filecache.sh +++ b/lib/filecache.sh @@ -2,7 +2,7 @@ # filecache.sh - cache file information # # Copyright (C) 2008-2011, 2013-2015, 2018-2019 Rocky Bernstein -# +# 2024 # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -63,7 +63,7 @@ _Dbg_check_line() { # Error message for file not read in function _Dbg_file_not_read_in { typeset -r filename=$(_Dbg_adjust_filename "$1") - _Dbg_errmsg "File \"$filename\" not found in read-in files." + _Dbg_errmsg "File \"$filename\" ($1) not found in read-in files." _Dbg_errmsg "See 'info files' for a list of known files and" _Dbg_errmsg "'load' to read in a file." } @@ -106,7 +106,9 @@ _Dbg_get_source_line() { filename="$1" fi _Dbg_readin_if_new "$filename" - if [[ -n $_Dbg_set_highlight ]] && [[ -n $_Dbg_highlight_array_var ]]; then + if [[ -z "$_Dbg_source_array_var" ]]; then + _Dbg_source_line="??" + elif [[ -n $_Dbg_set_highlight ]] && [[ -n $_Dbg_highlight_array_var ]]; then eval "typeset -i count=\${#$_Dbg_highlight_array_var[@]}" if (( count )) ; then eval "_Dbg_source_line=\${$_Dbg_highlight_array_var[lineno]}" @@ -244,7 +246,11 @@ function _Dbg_readin { (( line_count >= NOT_SMALLFILE )) && _Dbg_msg "done." # Add $filename to list of all filenames - _Dbg_filenames["$fullname"]=$_Dbg_source_array_var; + if [[ ! -z $fullname ]] ; then + _Dbg_filenames["$fullname"]=$_Dbg_source_array_var; + else + echo "XXX ${filename}" + fi return 0 } diff --git a/lib/hook.sh b/lib/hook.sh index df77e79a..0406233f 100644 --- a/lib/hook.sh +++ b/lib/hook.sh @@ -148,6 +148,7 @@ _Dbg_debug_trap_handler() { typeset _Dbg_full_filename _Dbg_full_filename=$(_Dbg_is_file "$_Dbg_frame_last_filename") + if [[ -r "$_Dbg_full_filename" ]] ; then _Dbg_file2canonic["$_Dbg_frame_last_filename"]="$_Dbg_full_filename" _Dbg_file2canonic["${BASH_SOURCE[1]}"]="$_Dbg_full_filename" diff --git a/lib/list.sh b/lib/list.sh index 947458b7..4b224312 100644 --- a/lib/list.sh +++ b/lib/list.sh @@ -36,10 +36,14 @@ typeset _Dbg_source_line function _Dbg_print_location_and_command { typeset line_number=${1:-$_Dbg_frame_last_lineno} typeset filename=${2:-$_Dbg_frame_last_filename} - _Dbg_get_source_line $line_number "$filename" - filename=$(_Dbg_adjust_filename "$filename") - _Dbg_msg "(${filename}:${line_number}): + if [[ -z "$filename" ]] ; then + _Dbg_msg "(??:${line_number}):" + else + _Dbg_get_source_line $line_number "$filename" + filename=$(_Dbg_adjust_filename "$filename") + _Dbg_msg "(${filename}:${line_number}): ${line_number}:\t${_Dbg_source_line}" + fi # If we are at the same place in the file but the command has changed, # then we have multiple commands on the line. So print which one we are diff --git a/lib/save-restore.sh b/lib/save-restore.sh index e84e524c..4e6e090f 100644 --- a/lib/save-restore.sh +++ b/lib/save-restore.sh @@ -87,8 +87,11 @@ function _Dbg_set_debugger_entry { _Dbg_stack_pos=_0 _Dbg_listline=_Dbg_frame_last_lineno _Dbg_set_debugger_internal - _Dbg_frame_last_filename=${BASH_SOURCE[$discard_top_fn_count]:-$_Dbg_bogus_file} - _Dbg_frame_last_filename=$(_Dbg_resolve_expand_filename "$_Dbg_frame_last_filename") + typeset -l frame_last_filename=${BASH_SOURCE[$discard_top_fn_count]:-$_Dbg_bogus_file} + _Dbg_frame_last_filename=$(_Dbg_resolve_expand_filename "$frame_last_filename") + if [[ -z "$_Dbg_frame_last_filename" ]]; then + _Dbg_frame_last_filename="$frame_last_filename" + fi # Read in the journal to pick up variable settings that might have # been left from a subshell. From 6a8ed0362e47bd930c88bef05076654545fbb1f6 Mon Sep 17 00:00:00 2001 From: rocky Date: Sat, 7 Dec 2024 12:31:48 -0500 Subject: [PATCH 3/3] jansorg's suggestions: quotes and typeset --- command/backtrace.sh | 2 +- lib/file.sh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/command/backtrace.sh b/command/backtrace.sh index 1ccf236f..65b6678f 100644 --- a/command/backtrace.sh +++ b/command/backtrace.sh @@ -128,7 +128,7 @@ function _Dbg_do_backtrace { ((count--)) ; adjusted_pos=$(_Dbg_frame_adjusted_pos 0) filename="${BASH_SOURCE[$adjusted_pos]}" - resolved_filename="${_Dbg_file2canonic[$filename]}" + typeset -l resolved_filename="${_Dbg_file2canonic[$filename]}" if [[ ! -z "${resolved_filename}" ]] ; then filename="$resolved_filename" else diff --git a/lib/file.sh b/lib/file.sh index da8aad3f..8f8aa53c 100644 --- a/lib/file.sh +++ b/lib/file.sh @@ -110,9 +110,9 @@ function _Dbg_resolve_expand_filename { for (( i=0 ; i < n; i++ )) ; do typeset dirname="${_Dbg_dir[i]}" if [[ "$basename" == '\$cdir' ]] ; then - dirname=$_Dbg_cdir + dirname="$_Dbg_cdir" elif [[ "$basename" == '\$cwd' ]] ; then - dirname=$(pwd) + dirname="$(pwd)" fi if [[ -f "$dirname/$find_file" ]] ; then full_find_file="$dirname/$find_file"