Project

General

Profile

« Previous | Next » 

Revision 14713

lib/sh/util.sh: added get_stack_frame() and helpers

View differences:

util.sh
274 274
	echo "$file:$line"
275 275
}
276 276

  
277
### stack traces
277 278

  
279
alias init_i='declare i="${i-0}"'
280
alias next_stack_frame='let! i++'
281

  
282
# makes stack_trace() skip the entry for the caller
283
alias skip_stack_frame='init_i; next_stack_frame'
284

  
285
fi # load new aliases
286
if self_being_included; then
287

  
288
# usage: [i=#] get_stack_frame # i: 0-based index from top of stack
289
# to get all stack frames:
290
# init_i; while get_stack_frame; do use $func $file $line; next_stack_frame;done
291
# better than `caller` builtin because returns info in separate vars
292
get_stack_frame()
293
{
294
	skip_stack_frame; init_i
295
	# the extra FUNCNAME/BASH_LINENO entries have dummy values (main/0)
296
	func="${FUNCNAME[$i]}" # the called function, which = the current function
297
	file="${BASH_SOURCE[$i+1]}" # not the *current* file, but the call's file
298
	line="${BASH_LINENO[$i]}" # the line the function was called at
299
	test "$file" # exit false if invalid index
300
}
301
alias get_stack_frame='declare func file line && "get_stack_frame"'
302
	# && instead of ; so it can be used as a while cond
303

  
304

  
278 305
#### debugging
279 306

  
280 307
debug_fd="$err_fd"

Also available in: Unified diff