1#!/bin/sh
2# Copyright (C) 2011 O.S. Systems Software LTDA.
3# Licensed on MIT
4
5# Adds support to dynamic debugging of initramfs using bootparam in
6# following format:
7#   shell                 : starts a shell before and after each module
8#   shell=before:<module> : starts a shell before <module> is loaded and run
9#   shell=after:<module>  : starts a shell after <module> is loaded and run
10#
11#   shell-debug                 : run set -x as soon as possible
12#   shell-debug=before:<module> : run set -x before <module> is loaded and run
13#   shell-debug=after:<module>  : run set -x after <module> is loaded and run
14
15DEBUG_SHELL="false"
16
17debug_hook_handler() {
18	status=$1
19	module=$2
20
21	if [ -n "$bootparam_shell" ] && [ "$bootparam_shell" != "true" ]; then
22		shell_wanted_status=`expr $bootparam_shell : '\(.*\):.*'`
23		shell_wanted_module=`expr $bootparam_shell : '.*:\(.*\)'`
24
25		if [ "$shell_wanted_status" = "before" ]; then
26			shell_wanted_status=pre
27		else
28			shell_wanted_status=post
29		fi
30	fi
31
32	if [ "$bootparam_shell" = "true" ] ||
33		( [ "$status" = "$shell_wanted_status" ] &&
34			[ "$module" = "$shell_wanted_module" ] ); then
35		if [ "$status" = "pre" ]; then
36			status_msg="before"
37		else
38			status_msg="after"
39		fi
40
41		msg "Starting shell $status_msg $module..."
42		sh
43	fi
44
45	if [ -n "$bootparam_shell_debug" ] && [ "$bootparam_shell_debug" != "true" ]; then
46		shell_debug_wanted_status=`expr $bootparam_shell_debug : '\(.*\):.*'`
47		shell_debug_wanted_module=`expr $bootparam_shell_debug : '.*:\(.*\)'`
48
49		if [ "$shell_debug_wanted_status" = "before" ]; then
50			shell_debug_wanted_status=pre
51		else
52			shell_debug_wanted_status=post
53		fi
54	fi
55
56	if [ "$bootparam_shell_debug" = "true" ] ||
57		( [ "$status" = "$shell_debug_wanted_status" ] &&
58			[ "$module" = "$shell_debug_wanted_module" ] ); then
59		if [ "$DEBUG_SHELL" = "true" ]; then
60			return 0
61		fi
62
63		if [ "$status" = "pre" ]; then
64			status_msg="before"
65		else
66			status_msg="after"
67		fi
68
69		msg "Starting shell debugging $status_msg $module..."
70		DEBUG_SHELL="true"
71		set -x
72	fi
73}
74
75debug_enabled() {
76	return 0
77}
78
79debug_run() {
80	add_module_pre_hook "debug_hook_handler"
81	add_module_post_hook "debug_hook_handler"
82}
83