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.
[Patch, microblaze]: Add wdc.ext.clear and wdc.ext.flush insns

Added two new instructions, wdc.ext.clear and wdc.ext.flush,
to enable MicroBlaze to flush an external cache, which is
used with the new coherency support for multiprocessing.

Signed-off-by:nagaraju <nmekala@xilix.com>
Signed-off-by: David Holsgrove <david.holsgrove@xilinx.com>
[Patch, microblaze]: Add mlittle-endian and mbig-endian flags

Added support in gas for mlittle-endian and mbig-endian flags
as options.

Updated show usage for MicroBlaze specific assembler options
to include new entries.

Signed-off-by:nagaraju <nmekala@xilix.com>
Signed-off-by: David Holsgrove <david.holsgrove@xilinx.com>
[Patch, microblaze]: Initial port of microblaze core reading support

Added support for reading notes in linux core dumps
Support for reading of PRSTATUS and PSINFO information for rebuilding
".reg" sections of core dumps at run time.

Signed-off-by: David Holsgrove <david.holsgrove@xilinx.com>
Signed-off-by: Nathan Rossi <nathan.rossi@xilinx.com>
[Patch, microblaze]: Communicate in larger blocks with the target

Signed-off-by: Edgar E. Iglesias <edgar.iglesias@gmail.com>
[Patch, microblaze]: Add mb singlestepping.

Fix prologue analysis for little-endian.
Always provide a frame base.

Signed-off-by: Edgar E. Iglesias <edgar@axis.com>
[Patch, microblaze]: Add initial port of linux gdbserver

add gdb_proc_service_h to gdbserver microblaze-linux

gdbserver needs to initialise the microblaze registers

other archs use this step to run a *_arch_setup() to carry out all
architecture specific setup - may need to add in future

 * add linux-ptrace.o to gdbserver configure
 * gdb/configure.tgt: Set build_gdbserver=yes
 * Update breakpoint opcode
 * fix segfault on connecting gdbserver
 * add microblaze_linux_memory_remove_breakpoint
 * add set_solib_svr4_fetch_link_map_offsets
 * add set_gdbarch_fetch_tls_load_module_address
 * Force reading of r0 as 0, prevent stores

Signed-off-by: David Holsgrove <david.holsgrove@petalogix.com>
Signed-off-by: Nathan Rossi <nathan.rossi@petalogix.com>
[Patch, microblaze]: Add slr and shr regs and little-endian breakpoint

Signed-off-by: David Holsgrove <david.holsgrove@xilinx.com>
[Patch, microblaze]: Added Backtrace support to GDB

Added backtrace support without debugging information.

Earlier backtrace used to work only when debug information is available.
Previous pc & sp values are calculated incorrectly due to which backtrace
was failing.

In this patch, previous frame sp & pc values are calculated correctly
in microblaze_frame_cache function and supplied them to
microblaze_frame_prev_register function

Signed-off-by:nagaraju <nmekala@xilix.com>
[Patch, microblaze]: Initial support for native gdb

microblaze: Follow PPC method of getting setting registers
using PTRACE PEEK/POKE

Signed-off-by: David Holsgrove <david.holsgrove@petalogix.com>
[Patch, gdb]: Robustify inline function support

Signed-off-by: Edgar E. Iglesias <edgar@axis.com>
[Patch, gdb]: Fix debug message when register is unavailable

Signed-off-by: Nathan Rossi <nathan.rossi@xilinx.com>
Signed-off-by: David Holsgrove <david.holsgrove@xilinx.com>
[LOCAL]: Disable the warning message for eh_frame_hdr

Signed-off-by: Edgar E. Iglesias <edgar.iglesias@gmail.com>
[LOCAL]: Fix relaxation of assembler resolved references
[LOCAL]: Fixup debug_loc sections after linker relaxation

Adds a new reloctype R_MICROBLAZE_32_NONE, used for passing
reloc info from the assembler to the linker when the linker
manages to fully resolve a local symbol reference.

This is a workaround for design flaws in the assembler to
linker interface with regards to linker relaxation.

Signed-off-by: Edgar E. Iglesias <edgar.iglesias@gmail.com>
Signed-off-by: David Holsgrove <david.holsgrove@xilinx.com>
[LOCAL]: upstream change to garbage collection sweep causes mb regression

Upstream change for PR13177 now clears the def_regular during gc_sweep of a
section. (All other archs in binutils/bfd/elf32-*.c received an update
to a warning about unresolvable relocations - this warning is not present
in binutils/bfd/elf32-microblaze.c, but this warning check would not
prevent the error being seen)

The visible issue with this change is when running a c++ application
in Petalinux which links libstdc++.so for exception handling it segfaults
on execution.

This does not occur if static linking libstdc++.a, so its during the
relocations for a shared lib with garbage collection this occurs

Signed-off-by: David Holsgrove <david.holsgrove@xilinx.com>
[LOCAL]: Add Support for cfi asm insn

Signed-off-by: David Holsgrove <david.holsgrove@xilinx.com>