[Pdbg] [PATCH 06/10] pdbg: add an expert mode

Nicholas Piggin npiggin at gmail.com
Thu May 3 16:26:58 AEST 2018


Add an --expert mode which contains dangerous options or things which
are "stateful" between pdbg invocations, where the release command and
possibly others need to be run to return the system to a normal state.

If you're not in expert mode, you just run a command and that's it.
More commands can be added to expert mode as we go.

Signed-off-by: Nicholas Piggin <npiggin at gmail.com>
---
 libpdbg/libpdbg.c |  2 ++
 libpdbg/libpdbg.h |  3 +++
 src/main.c        | 59 +++++++++++++++++++++++++++++++++++++----------
 3 files changed, 52 insertions(+), 12 deletions(-)

diff --git a/libpdbg/libpdbg.c b/libpdbg/libpdbg.c
index 0d91cc3..8162d17 100644
--- a/libpdbg/libpdbg.c
+++ b/libpdbg/libpdbg.c
@@ -4,6 +4,8 @@
 #include "device.h"
 #include "libpdbg.h"
 
+bool pdbg_expert_mode = false;
+
 struct pdbg_target *__pdbg_next_target(const char *class, struct pdbg_target *parent, struct pdbg_target *last)
 {
 	struct pdbg_target *next, *tmp;
diff --git a/libpdbg/libpdbg.h b/libpdbg/libpdbg.h
index 50baacc..2fec479 100644
--- a/libpdbg/libpdbg.h
+++ b/libpdbg/libpdbg.h
@@ -1,9 +1,12 @@
 #ifndef __LIBPDBG_H
 #define __LIBPDBG_H
 
+extern bool pdbg_expert_mode;
+
 struct pdbg_target;
 struct pdbg_target_class;
 
+
 struct pdbg_taget *pdbg_root_target;
 
 /* loops/iterators */
diff --git a/src/main.c b/src/main.c
index 293f4e0..b651395 100644
--- a/src/main.c
+++ b/src/main.c
@@ -66,12 +66,14 @@ static int threadsel[MAX_PROCESSORS][MAX_CHIPS][MAX_THREADS];
 static int handle_probe(int optind, int argc, char *argv[]);
 static int handle_release(int optind, int argc, char *argv[]);
 
-static struct {
+struct action {
 	const char *name;
 	const char *args;
 	const char *desc;
 	int (*fn)(int, int, char **);
-} actions[] = {
+};
+
+static struct action expert_actions[] = {
 	{ "getcfam", "<address>", "Read system cfam", &handle_cfams },
 	{ "putcfam", "<address> <value> [<mask>]", "Write system cfam", &handle_cfams },
 	{ "getscom", "<address>", "Read system scom", &handle_scoms },
@@ -90,7 +92,6 @@ static struct {
 	{ "start",   "", "Start thread", &thread_start },
 	{ "step",    "<count>", "Set a thread <count> instructions", &thread_step },
 	{ "stop",    "", "Stop thread", &thread_stop },
-	{ "threadstatus", "", "Print the status of a thread", &thread_status_print },
 	{ "sreset",  "", "Reset", &thread_sreset },
 	{ "htm_start", "", "[deprecated use 'htm nest start'] Start Nest HTM", &run_htm_start },
 	{ "htm_stop", "", "[deprecated use 'htm nest stop'] Stop Nest HTM", &run_htm_stop },
@@ -100,10 +101,15 @@ static struct {
 	{ "htm_trace", "" , "[deprecated use 'htm nest trace'] Configure and start tracing with HTM", &run_htm_trace },
 	{ "htm_analyse", "", "[derepcated use 'htm nest analyse'] Stop and dump buffer to file", &run_htm_analyse },
 	{ "htm", "(core | nest) (start | stop | status | reset | dump | trace | analyse", "Hardware Trace Macro", &run_htm },
-	{ "probe", "", "", &handle_probe },
 	{ "release", "", "Should be called after pdbg work is finished, to release special wakeups and other resources.", &handle_release},
 };
 
+static struct action actions[] = {
+	{ "probe", "", "", &handle_probe },
+	{ "threadstatus", "", "Print the status of a thread", &thread_status_print },
+};
+
+
 static void print_usage(char *pname)
 {
 	int i;
@@ -129,12 +135,18 @@ static void print_usage(char *pname)
 	printf("\t-s, --slave-address=backend device address\n");
 	printf("\t\tDevice slave address to use for the backend. Not used by FSI\n");
 	printf("\t\tand defaults to 0x50 for I2C\n");
+	printf("\t--expert\n");
 	printf("\t-V, --version\n");
 	printf("\t-h, --help\n");
 	printf("\n");
 	printf(" Commands:\n");
 	for (i = 0; i < ARRAY_SIZE(actions); i++)
 		printf("\t%s %s\n", actions[i].name, actions[i].args);
+	if (pdbg_expert_mode) {
+		printf(" Expert Commands:\n");
+		for (i = 0; i < ARRAY_SIZE(expert_actions); i++)
+			printf("\t%s %s\n", expert_actions[i].name, expert_actions[i].args);
+	}
 }
 
 static bool parse_options(int argc, char *argv[])
@@ -152,12 +164,13 @@ static bool parse_options(int argc, char *argv[])
 		{"slave-address",	required_argument,	NULL,	's'},
 		{"thread",		required_argument,	NULL,	't'},
 		{"version",		no_argument,		NULL,	'V'},
+		{"expert",		no_argument,		NULL,	'E'},
 		{NULL,			0,			NULL,     0}
 	};
 	char *endptr;
 
 	do {
-		c = getopt_long(argc, argv, "+ab:c:d:hp:s:t:V", long_opts, NULL);
+		c = getopt_long(argc, argv, "+ab:c:d:hp:s:t:VE", long_opts, NULL);
 		if (c == -1)
 			break;
 
@@ -241,11 +254,15 @@ static bool parse_options(int argc, char *argv[])
 			break;
 
 		case 'V':
-			errno = 0;
 			printf("%s (commit %s)\n", PACKAGE_STRING, GIT_SHA1);
 			exit(1);
 			break;
 
+		case 'E':
+			opt_error = false;
+			pdbg_expert_mode = true;
+			break;
+
 		case '?':
 		case 'h':
 			opt_error = true;
@@ -548,6 +565,14 @@ static int handle_probe(int optind, int argc, char *argv[])
 	return 1;
 }
 
+/*
+ * Release handler for !pdbg_expert_mode
+ */
+static void atexit_release(void)
+{
+	do_release();
+}
+
 static int handle_release(int optind, int argc, char *argv[])
 {
 	do_release();
@@ -589,19 +614,29 @@ int main(int argc, char *argv[])
 	if (target_selection())
 		return 1;
 
+	if (!pdbg_expert_mode)
+		atexit(atexit_release);
+
 	for (i = 0; i < ARRAY_SIZE(actions); i++) {
 		if (strcmp(argv[optind], actions[i].name) == 0) {
 			rc = actions[i].fn(optind, argc, argv);
-			break;
+			goto found_action;
 		}
 	}
-
-	if (i == ARRAY_SIZE(actions)) {
-		PR_ERROR("Unsupported command: %s\n", argv[optind]);
-		print_usage(argv[0]);
-		return 1;
+	if (pdbg_expert_mode) {
+		for (i = 0; i < ARRAY_SIZE(expert_actions); i++) {
+			if (strcmp(argv[optind], expert_actions[i].name) == 0) {
+				rc = expert_actions[i].fn(optind, argc, argv);
+				goto found_action;
+			}
+		}
 	}
 
+	PR_ERROR("Unsupported command: %s\n", argv[optind]);
+	print_usage(argv[0]);
+	return 1;
+
+found_action:
 	if (rc <= 0) {
                 printf("No valid targets found or specified. Try adding -p/-c/-t options to specify a target.\n");
                 printf("Alternatively run %s -a probe to get a list of all valid targets\n", argv[0]);
-- 
2.17.0



More information about the Pdbg mailing list