From patchwork Wed Oct 19 12:56:19 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [2/5] runstate: Print state transition when invalid From: Luiz Capitulino X-Patchwork-Id: 120621 Message-Id: <20111019105619.4e0813fa@doriath> To: Wen Congyang Cc: aliguori@us.ibm.com, qemu-devel@nongnu.org Date: Wed, 19 Oct 2011 10:56:19 -0200 On Wed, 19 Oct 2011 08:43:33 +0800 Wen Congyang wrote: > At 10/15/2011 01:26 AM, Luiz Capitulino Write: > > Makes it easier to debug. > > > > Signed-off-by: Luiz Capitulino > > --- > > vl.c | 4 +++- > > 1 files changed, 3 insertions(+), 1 deletions(-) > > > > diff --git a/vl.c b/vl.c > > index dbf7778..6645720 100644 > > --- a/vl.c > > +++ b/vl.c > > @@ -397,7 +397,9 @@ void runstate_set(RunState new_state) > > { > > if (new_state >= RUN_STATE_MAX || > > !runstate_valid_transitions[current_run_state][new_state]) { > > - fprintf(stderr, "invalid runstate transition\n"); > > + fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n", > > + RunState_lookup[current_run_state], > > + RunState_lookup[new_state]); > > If new_state >= RUN_STATE_MAX, we can not use RunState_lookup. Good catch! > I think it's better to use: > new_state >= RUN_STATE_MAX ? "invalid state" : RunState_lookup[new_state] I prefer to do the following instead: diff --git a/vl.c b/vl.c index 2dce3ae..2a634a7 100644 --- a/vl.c +++ b/vl.c @@ -393,9 +393,12 @@ void runstate_init(void) /* This function will abort() on invalid state transitions */ void runstate_set(RunState new_state) { - if (new_state >= RUN_STATE_MAX || - !runstate_valid_transitions[current_run_state][new_state]) { - fprintf(stderr, "invalid runstate transition\n"); + assert(new_state < RUN_STATE_MAX); + + if (!runstate_valid_transitions[current_run_state][new_state]) { + fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n", + RunState_lookup[current_run_state], + RunState_lookup[new_state]); abort(); }