PR15693 - Fix spurious *running events, thread state, dprintf-style call

If one sets a breakpoint with a condition that involves calling a
function in the inferior, and then the condition evaluates false, GDB
outputs one *running event for each time the program hits the
breakpoint.  E.g.,

  $ gdb return-false -i=mi

  (gdb)
  start
  ...
  (gdb)
  b 14 if return_false ()
  &"b 14 if return_false ()\n"
  ~"Breakpoint 2 at 0x4004eb: file return-false.c, line 14.\n"
  ...
  ^done
  (gdb)
  c
  &"c\n"
  ~"Continuing.\n"
  ^running
  *running,thread-id=(...)
  (gdb)
  *running,thread-id=(...)
  *running,thread-id=(...)
  *running,thread-id=(...)
  *running,thread-id=(...)
  *running,thread-id=(...)
  ... repeat forever ...

An easy way a user can trip on this is with a dprintf with "set
dprintf-style call".  In that case, a dprintf is just a breakpoint
that when hit GDB calls the printf function in the inferior, and then
resumes it, just like the case above.

If the breakpoint/dprintf is set in a loop, then these spurious events
can potentially slow down a frontend much, if it decides to refresh
its GUI whenever it sees this event (Eclipse is one such case).

When we run an infcall, we pretend we don't actually run the inferior.
This is already handled for the usual case of calling a function
directly from the CLI:

 (gdb)
 p return_false ()
 &"p return_false ()\n"
 ~"$1 = 0"
 ~"\n"
 ^done
 (gdb)

Note no *running, nor *stopped events.  That's handled by:

 static void
 mi_on_resume (ptid_t ptid)
 {
...
   /* Suppress output while calling an inferior function.  */
   if (tp->control.in_infcall)
     return;

and equivalent code on normal_stop.

However, in the cases of the PR, after finishing the infcall there's
one more resume, and mi_on_resume doesn't know that it should suppress
output then too, somehow.

The "running/stopped" state is a high level user/frontend state.
Internal stops are invisible to the frontend.  If follows from that
that we should be setting the thread to running at a higher level
where we still know the set of threads the user _intends_ to resume.

Currently we mark a thread as running from within target_resume, a low
level target operation.  As consequence, today, if we resume a
multi-threaded program while stopped at a breakpoint, we see this:

 -exec-continue
 ^running
 *running,thread-id="1"
 (gdb)
 *running,thread-id="all"

The first *running was GDB stepping over the breakpoint, and the
second is GDB finally resuming everything.

Between those two *running's, threads other than "1" still have their
state set to stopped.  That's bogus -- in async mode, this opens a
tiny window between both resumes where the user might try to run
another execution command to threads other than thread 1, and very
much confuse GDB.

That is, the "step" below should fail the "step", complaining that the
thread is running:

  (gdb) c -a &
  (gdb) thread 2
  (gdb) step

IOW, threads that GDB happens to not resume immediately (say, because
it needs to step over a breakpoint) shall still be marked as running.

Then, if we move marking threads as running to a higher layer,
decoupled from target_resume, plus skip marking threads as running
when running an infcall, the spurious *running events disappear,
because there will be no state transitions at all.

I think we might end up adding a new thread state -- THREAD_INFCALL or
some such, however since infcalls are always synchronous today, I
didn't find a need.  There's no way to execute a CLI/MI command
directly from the prompt if some thread is running an infcall.

Tested on x86_64 Fedora 20.

gdb/
2014-05-29  Pedro Alves  <palves@redhat.com>

	PR PR15693
	* infrun.c (resume): Determine how much to resume depending on
	whether the caller wanted a step, not whether we can hardware step
	the target.  Mark all threads that we intend to run as running,
	unless we're calling an inferior function.
	(normal_stop): If the thread is running an infcall, don't finish
	thread state.
	* target.c (target_resume): Don't mark threads as running here.

gdb/testsuite/
2014-05-29  Pedro Alves  <palves@redhat.com>
	    Hui Zhu  <hui@codesourcery.com>

	PR PR15693
	* gdb.mi/mi-condbreak-call-thr-state-mt.c: New file.
	* gdb.mi/mi-condbreak-call-thr-state-st.c: New file.
	* gdb.mi/mi-condbreak-call-thr-state.c: New file.
	* gdb.mi/mi-condbreak-call-thr-state.exp: New file.
PR gdb/15713 - errors from i386_linux_resume lead to lock-up

linux_nat_resume is not considering that linux_ops->to_resume may throw:

  /* Mark LWP as not stopped to prevent it from being continued by
     linux_nat_resume_callback.  */
  lp->stopped = 0;

  if (resume_many)
    iterate_over_lwps (ptid, linux_nat_resume_callback, NULL);

If something within linux_nat_resume_callback throws, GDB leaves the
lwp_info as if the inferior was resumed, while it actually wasn't.

A couple examples, there are possibly others:

 - i386_linux_resume calls target_read which calls QUIT.
 - if the actual ptrace resumption fails in inf_ptrace_resume,
   perror_with_name is called.

If the user tries to kill the inferior at this point (or quit, which
offers to kill), GDB locks up trying to stop the lwp -- if it is
already stopped no new waitpid event gets generated for it.

Fix this by setting the stopped flag earlier, as soon as we collect a
stop event with waitpid, and clearing it always only after resuming
the lwp successfully.

Tested on x86_64 Fedora 20.  Confirmed the lock-up disappears using a
local hack that forces an error in inf_ptrace_resume.

Also fixes a little "set debug lin-lwp" annoyance.  Currently we always see:

 Continuing.
 LLR: Preparing to resume process 6802, 0, inferior_ptid Thread 0x7ffff7fc7740 (LWP 6802)
                                                                                ^^^^^^^^
 RC: Resuming sibling Thread 0x7ffff77c5700 (LWP 6807), 0, resume
 RC: Resuming sibling Thread 0x7ffff7fc6700 (LWP 6806), 0, resume
 RC: Not resuming sibling Thread 0x7ffff7fc7740 (LWP 6802) (not stopped)
                                                 ^^^^^^^^^^^^^^^^^^^^^^^
 LLR: PTRACE_CONT process 6802, 0 (resume event thread)

This patch gets rid of the "Not resuming sibling" line.

2014-05-29  Pedro Alves  <palves@redhat.com>

	PR gdb/15713
	* linux-nat.c (linux_nat_resume_callback): Rename the second
	parameter to 'except'.  Skip LP if it points to EXCEPT.
	(linux_nat_resume): Don't mark the event lwp as not stopped
	before resuming sibling lwps.  Instead ask
	linux_nat_resume_callback to skip the event lwp.  Mark it as not
	stopped after actually resuming it.
	(linux_handle_syscall_trap): Mark the lwp as not stopped after
	resuming it.
	(wait_lwp): Mark the lwp as stopped here.
	(stop_wait_callback): Mark the lwp as not stopped right after
	resuming it.  Don't mark lwps as stopped here.
	(linux_nat_filter_event): Mark the lwp as stopped earlier.
	(linux_nat_wait_1): Don't mark dead lwps as stopped here.
PR gdb/13860 - Make MI sync vs async output (closer to) the same.

Ignoring expected and desired differences like whether the prompt is
output after *stoppped records, GDB MI output is still different in
sync and async modes.

In sync mode, when a CLI execution command is entered, the "reason"
field is missing in the *stopped async record.  And in async mode, for
some events, like program exits, the corresponding CLI output is
missing in the CLI channel.

Vis, diff between sync vs async modes:

   run
   ^running
   *running,thread-id="1"
   (gdb)
   ...
 - ~"[Inferior 1 (process 15882) exited normally]\n"
   =thread-exited,id="1",group-id="i1"
   =thread-group-exited,id="i1",exit-code="0"
 - *stopped
 + *stopped,reason="exited-normally"

   si
   ...
   (gdb)
   ~"0x000000000045e033\t29\t  memset (&args, 0, sizeof args);\n"
 - *stopped,frame=...,thread-id="1",stopped-threads="all",core="0"
 + *stopped,reason="end-stepping-range",frame=...,thread-id="1",stopped-threads="all",core="0"
   (gdb)

In addition, in both cases, when a MI execution command is entered,
and a breakpoint triggers, the event is sent to the console too.  But
some events like program exits have the CLI output missing in the CLI
channel:

   -exec-run
   ^running
   *running,thread-id="1"
   (gdb)
   ...
   =thread-exited,id="1",group-id="i1"
   =thread-group-exited,id="i1",exit-code="0"
 - *stopped
 + *stopped,reason="exited-normally"

We'll want to make background commands always possible by default.
IOW, make target-async be the default.  But, in order to do that,
we'll need to emulate MI sync on top of an async target.  That means
we'll have yet another combination to care for in the testsuite.

Rather than making the testsuite cope with all these differences, I
thought it better to just fix GDB to always have the complete output,
no matter whether it's in sync or async mode.

This is all related to interpreter-exec, and the corresponding uiout
switching.  (Typing a CLI command directly in MI is shorthand for
running it through -interpreter-exec console.)

In sync mode, when a CLI command is active, normal_stop is called when
the current interpreter and uiout are CLI's.  So print_XXX_reason
prints the stop reason to CLI uiout (only), and we don't show it in
MI.

In async mode the stop event is processed when we're back in the MI
interpreter, so the stop reason is printed directly to the MI uiout.

Fix this by making run control event printing roughly independent of
whatever is the current interpreter or uiout.  That is, move these
prints to interpreter observers, that know whether to print or be
quiet, and if printing, which uiout to print to.  In the case of the
console/tui interpreters, only print if the top interpreter.  For MI,
always print.

Breakpoint hits / normal stops are already handled similarly -- MI has
a normal_stop observer that prints the event to both MI and the CLI,
though that could be cleaned up further in the direction of this
patch.

This also makes all of:

 (gdb) foo
and
 (gdb) interpreter-exec MI "-exec-foo"
and
 (gdb)
 -exec-foo
and
 (gdb)
 -interpreter-exec console "foo"

print as expected.

Tested on x86_64 Fedora 20, sync and async modes.

gdb/
2014-05-29  Pedro Alves  <palves@redhat.com>

	PR gdb/13860
	* cli/cli-interp.c: Include infrun.h and observer.h.
	(cli_uiout, cli_interp): New globals.
	(cli_on_signal_received, cli_on_end_stepping_range)
	(cli_on_signal_exited, cli_on_exited, cli_on_no_history): New
	functions.
	(cli_interpreter_init): Install them as 'end_stepping_range',
	'signal_received' 'signal_exited', 'exited' and 'no_history'
	observers.
	(_initialize_cli_interp): Remove cli_interp local.
	* infrun.c (handle_inferior_event): Call the several stop reason
	observers instead of printing the stop reason directly.
	(end_stepping_range): New function.
	(print_end_stepping_range_reason, print_signal_exited_reason)
	(print_exited_reason, print_signal_received_reason)
	(print_no_history_reason): Make static, and add an uiout
	parameter.  Print to that instead of to CURRENT_UIOUT.
	* infrun.h (print_end_stepping_range_reason)
	(print_signal_exited_reason, print_exited_reason)
	(print_signal_received_reason print_no_history_reason): New
	declarations.
	* mi/mi-common.h (struct mi_interp): Rename 'uiout' field to
	'mi_uiout'.
	<cli_uiout>: New field.
	* mi/mi-interp.c (mi_interpreter_init): Adjust.  Create the new
	uiout for CLI output.  Install 'signal_received',
	'end_stepping_range', 'signal_exited', 'exited' and 'no_history'
	observers.
	(find_mi_interpreter, mi_interp_data, mi_on_signal_received)
	(mi_on_end_stepping_range, mi_on_signal_exited, mi_on_exited)
	(mi_on_no_history): New functions.
	(ui_out_free_cleanup): Delete function.
	(mi_on_normal_stop): Don't allocate a new uiout for CLI output,
	instead use the one already stored in the MI interpreter data.
	(mi_ui_out): Adjust.
	* tui/tui-interp.c: Include infrun.h and observer.h.
	(tui_interp): New global.
	(tui_on_signal_received, tui_on_end_stepping_range)
	(tui_on_signal_exited, tui_on_exited)
	(tui_on_no_history): New functions.
	(tui_init): Install them as 'end_stepping_range',
	'signal_received' 'signal_exited', 'exited' and 'no_history'
	observers.
	(_initialize_tui_interp): Delete tui_interp local.

gdb/doc/
2014-05-29  Pedro Alves  <palves@redhat.com>

	PR gdb/13860
	* observer.texi (signal_received, end_stepping_range)
	(signal_exited, exited, no_history): New observer subjects.

gdb/testsuite/
2014-05-29  Pedro Alves  <palves@redhat.com>

	PR gdb/13860
	* gdb.mi/mi-cli.exp: Always expect "end-stepping-range" stop
	reason, even in sync mode.
Make display_gdb_prompt CLI-only.

Enabling target-async by default will require implementing sync
execution on top of an async target, much like foreground command are
implemented on the CLI in async mode.

In order to do that, we will need better control of when to print the
MI prompt.  Currently the interp->display_prompt_p hook is all we
have, and MI just always returns false, meaning, make
display_gdb_prompt a no-op.  We'll need to be able to know to print
the MI prompt in some of the conditions that display_gdb_prompt is
called from the core, but not all.

This is all a litte twisted currently.  As we can see,
display_gdb_prompt is really CLI specific, so make the console
interpreters (console/tui) themselves call it.  To be able to do that,
and add a few different observers that the interpreters can use to
distinguish when or why the the prompt is being printed:

#1 - one called whenever a command is cancelled due to an error.
#2 - another for when a foreground command just finished.

In both cases, CLI wants to print the prompt, while MI doesn't.

MI will want to print the prompt in the second case when in a special
MI mode.

The display_gdb_prompt call in interp_set made me pause.  The comment
there reads:

  /* Finally, put up the new prompt to show that we are indeed here.
     Also, display_gdb_prompt for the console does some readline magic
     which is needed for the console interpreter, at least...  */

But, that looks very much like a no-op to me currently:

 - the MI interpreter always return false in the prompt hook, meaning
   actually display no prompt.

 - the interpreter used at that point is still quiet.  And the
   console/tui interpreters return false in the prompt hook if they're
   quiet, meaning actually display no prompt.

The only remaining possible use would then be the readline magic.  But
whatever that might have been, it's not reacheable today either,
because display_gdb_prompt returns early, before touching readline if
the interpreter returns false in the display_prompt_p hook.

Tested on x86_64 Fedora 20, sync and async modes.

gdb/
2014-05-29  Pedro Alves  <palves@redhat.com>

	* cli/cli-interp.c (cli_interpreter_display_prompt_p): Delete.
	(_initialize_cli_interp): Adjust.
	* event-loop.c: Include "observer.h".
	(start_event_loop): Notify 'command_error' observers instead of
	calling display_gdb_prompt.  Remove FIXME comment.
	* event-top.c (display_gdb_prompt): Remove call into the
	interpreters.
	* inf-loop.c: Include "observer.h".
	(inferior_event_handler): Notify 'command_error' observers instead
	of calling display_gdb_prompt.
	* infrun.c (fetch_inferior_event): Notify 'sync_execution_done'
	observers instead of calling display_gdb_prompt.
	* interps.c (interp_set): Don't call display_gdb_prompt.
	(current_interp_display_prompt_p): Delete.
	* interps.h (interp_prompt_p): Delete declaration.
	(interp_prompt_p_ftype): Delete.
	(struct interp_procs) <prompt_proc_p>: Delete field.
	(current_interp_display_prompt_p): Delete declaration.
	* mi-interp.c (mi_interpreter_prompt_p): Delete.
	(_initialize_mi_interp): Adjust.
	* tui-interp.c (tui_init): Install 'sync_execution_done' and
	'command_error' observers.
	(tui_on_sync_execution_done, tui_on_command_error): New
	functions.
	(tui_display_prompt_p): Delete.
	(_initialize_tui_interp): Adjust.

gdb/doc/
2014-05-29  Pedro Alves  <palves@redhat.com>

	* observer.texi (sync_execution_done, command_error): New
	subjects.
enable target async by default; separate MI and target notions of async

This finally makes background execution commands possible by default.

However, in order to do that, there's one last thing we need to do --
we need to separate the MI and target notions of "async".  Unlike the
CLI, where the user explicitly requests foreground vs background
execution in the execution command itself (c vs c&), MI chose to treat
"set target-async" specially -- setting it changes the default
behavior of execution commands.

So, we can't simply "set target-async" default to on, as that would
affect MI frontends.  Instead we have to make the setting MI-specific,
and teach MI about sync commands on top of an async target.

Because the "target" word in "set target-async" ends up as a potential
source of confusion, the patch adds a "set mi-async" option, and makes
"set target-async" a deprecated alias.

Rather than make the targets always async, this patch introduces a new
"maint set target-async" option so that the GDB developer can control
whether the target is async.  This makes it simpler to debug issues
arising only in the synchronous mode; important because sync mode
seems unlikely to go away.

Unlike in previous revisions, "set target-async" does not affect this
new maint parameter.  The rationale for this is that then one can
easily run the test suite in the "maint set target-async off" mode and
have tests that enable mi-async fail just like they fail on
non-async-capable targets.  This emulation is exactly the point of the
maint option.

I had asked Tom in a previous iteration to split the actual change of
the target async default to a separate patch, but it turns out that
that is quite awkward in this version of the patch, because with MI
async and target async decoupled (unlike in previous versions), if we
don't flip the default at the same time, then just "set target-async
on" alone never actually manages to do anything.  It's best to not
have that transitory state in the tree.

Given "set target-async on" now only has effect for MI, the patch goes
through the testsuite removing it from non-MI tests.  MI tests are
adjusted to use the new and less confusing "mi-async" spelling.

2014-05-29  Pedro Alves  <palves@redhat.com>
	    Tom Tromey  <tromey@redhat.com>

	* NEWS: Mention "maint set target-async", "set mi-async", and that
	background execution commands are now always available.
	* target.h (target_async_permitted): Update comment.
	* target.c (target_async_permitted, target_async_permitted_1):
	Default to 1.
	(set_target_async_command): Rename to ...
	(maint_set_target_async_command): ... this.
	(show_target_async_command): Rename to ...
	(maint_show_target_async_command): ... this.
	(_initialize_target): Adjust.
	* infcmd.c (prepare_execution_command): Make extern.
	* inferior.h (prepare_execution_command): Declare.
	* infrun.c (set_observer_mode): Leave target async alone.
	* mi/mi-interp.c (mi_interpreter_init): Install
	mi_on_sync_execution_done as sync_execution_done observer.
	(mi_on_sync_execution_done): New function.
	(mi_execute_command_input_handler): Don't print the prompt if we
	just started a synchronous command with an async target.
	(mi_on_resume): Check sync_execution before printing prompt.
	* mi/mi-main.h (mi_async_p): Declare.
	* mi/mi-main.c: Include gdbcmd.h.
	(mi_async_p): New function.
	(mi_async, mi_async_1): New globals.
	(set_mi_async_command, show_mi_async_command, mi_async): New
	functions.
	(exec_continue): Call prepare_execution_command.
	(run_one_inferior, mi_cmd_exec_run, mi_cmd_list_target_features)
	(mi_execute_async_cli_command): Use mi_async_p.
	(_initialize_mi_main): Install "set mi-async".  Make
	"target-async" a deprecated alias.

2014-05-29  Pedro Alves  <palves@redhat.com>
	    Tom Tromey  <tromey@redhat.com>

	* gdb.texinfo (Non-Stop Mode): Remove "set target-async 1"
	from example.
	(Asynchronous and non-stop modes): Document '-gdb-set mi-async'.
	Mention that target-async is now deprecated.
	(Maintenance Commands): Document maint set/show target-async.

2014-05-29  Pedro Alves  <palves@redhat.com>
	    Tom Tromey  <tromey@redhat.com>

	* gdb.base/async-shell.exp: Don't enable target-async.
	* gdb.base/async.exp
	* gdb.base/corefile.exp (corefile_test_attach): Remove 'async'
	parameter.  Adjust.
	(top level): Don't test with "target-async".
	* gdb.base/dprintf-non-stop.exp: Don't enable target-async.
	* gdb.base/gdb-sigterm.exp: Don't test with "target-async".
	* gdb.base/inferior-died.exp: Don't enable target-async.
	* gdb.base/interrupt-noterm.exp: Likewise.
	* gdb.mi/mi-async.exp: Use "mi-async" instead of "target-async".
	* gdb.mi/mi-nonstop-exit.exp: Likewise.
	* gdb.mi/mi-nonstop.exp: Likewise.
	* gdb.mi/mi-ns-stale-regcache.exp: Likewise.
	* gdb.mi/mi-nsintrall.exp: Likewise.
	* gdb.mi/mi-nsmoribund.exp: Likewise.
	* gdb.mi/mi-nsthrexec.exp: Likewise.
	* gdb.mi/mi-watch-nonstop.exp: Likewise.
	* gdb.multi/watchpoint-multi.exp: Adjust comment.
	* gdb.python/py-evsignal.exp: Don't enable target-async.
	* gdb.python/py-evthreads.exp: Likewise.
	* gdb.python/py-prompt.exp: Likewise.
	* gdb.reverse/break-precsave.exp: Don't test with "target-async".
	* gdb.server/solib-list.exp: Don't enable target-async.
	* gdb.threads/thread-specific-bp.exp: Likewise.
	* lib/mi-support.exp: Adjust to use mi-async.
unbreak infcalls

I managed to miss an interaction between the recent *running patch,
and target-async, which resulted in infcalls being completely broken
on GNU/Linux and remote targets (that is, the async-capable targets).

 Temporary breakpoint 1, main () at threads.c:35
 35          long i = 0;
 (gdb) p malloc (0)
 The program being debugged stopped while in a function called from GDB.
 Evaluation of the expression containing the function
 (malloc) will be abandoned.
 When the function is done executing, GDB will silently stop.
 (gdb) p malloc (0)

 Program received signal SIGSEGV, Segmentation fault.
 0x000000000058d7e8 in get_regcache_aspace (regcache=0x0) at ../../src/gdb/regcache.c:281
 281       return regcache->aspace;
 (top-gdb)

The issue is that when running an infcall, the thread is no longer
marked as running, so run_inferior_call is not calling
wait_for_inferior anymore.

Fix this by doing what the comment actually says we do:

  "Do here what `proceed' itself does in sync mode."

And proceed doesn't check whether the target is running.

I notice this is broken in case of the early return in proceed, but we
were broken before in that case anyway, because run_inferior_call will
think the call actually ran.  Seems like we should make proceed have a
boolean return, and go through all callers making use of it, if
necessary.

But for now, just fix the regression.

Tested on x86_64 Fedora 20.

gdb/
2014-05-29  Pedro Alves  <palves@redhat.com>

	* infcall.c (run_inferior_call): Don't check whether the current
	thread is running after the proceed call.
infrun.c: stop_stepping -> stop_waiting.

stop_stepping is called even when we weren't stepping.  It's job really is:

  static void
  stop_waiting (struct execution_control_state *ecs)
  {
  ...
    /* Let callers know we don't want to wait for the inferior anymore.  */
    ecs->wait_some_more = 0;
  }

So rename it for clarity.

gdb/
2014-05-29  Pedro Alves  <palves@redhat.com>

	* infrun.c (stop_stepping): Rename to ...
	(stop_waiting): ... this.
	(proceed): Update comment.
	(process_event_stop_test, handle_inferior_event)
	(handle_signal_stop, handle_step_into_function)
	(handle_step_into_function_backward): Update.
infrun.c: simplify "end stepping range" code a bit.

 - all end_stepping_range callers also set stop_step.

 - all places that set stop_step call end_stepping_range and
   stop_waiting too.

IOW, all places where we handle "end stepping range" do:

      ecs->event_thread->control.stop_step = 1;
      end_stepping_range ();
      stop_waiting (ecs);

Factor that out into end_stepping_range itself.

Tested on x86_64 Fedora 20.

gdb/
2014-05-29  Pedro Alves  <palves@redhat.com>

	* infrun.c (process_event_stop_test, handle_step_into_function)
	(handle_step_into_function_backward): Adjust.
	Don't set the even thread's stop_step and call stop_waiting before
	calling end_stepping_range.  Instead do that ...
	(end_stepping_range): ... here.  Take an ecs pointer parameter.
Running the current tree against my software-single-step-on-x86_64
branch showed some extra assertions I have in place triggering.  Turns
out my previous change to 'resume' was incomplete, and we mishandle
the 'hw_step' / 'step' variable pair.  (I swear I had fixed this, but
I guess I lost that in some local branch...)

Tested on x86_64 Fedora 20.

gdb/
2014-05-29  Pedro Alves  <palves@redhat.com>

	* infrun.c (resume): Rename local 'hw_step' to 'entry_step'
	and make it const.  When a single-step decays to a continue,
	clear 'step', not 'hw_step'.  Pass whether the caller wanted
	to step to user_visible_resume_ptid, not what we ask the
	target to do.
daily update