btrace, test: fix multi-line btrace tests

For testing multi-line test output, gdb.btrace tests used the following
pattern:

  gdb_test "..." "
  ...\r
  ..."

Change this to:

  gdb_test "..." [join [list \
    "..." \
    "..."] "\r\n"]

Also extract repeated tests into a test function and shorten or remove
test messages.

2014-01-16  Markus Metzger  <markus.t.metzger@intel.com>

testsuite/
	* gdb.btrace/function_call_history.exp: Update
	* gdb.btrace/instruction_history.exp: Update.
btrace, linux: fix memory leak when reading branch trace

When it takes more than one iteration to read the BTS trace, the trace from the
previous iteration is leaked.  Fix it.

2014-01-16  Markus Metzger  <markus.t.metzger@intel.com>

	* common/linux-btrace.c (linux_read_btrace): Free trace from
	previous iteration.
btrace: uppercase btrace_read_type

2014-01-16  Markus Metzger  <markus.t.metzger@intel.com>

	* common/btrace-common.h (btrace_read_type) <btrace_read_all>:
	Change to ...
	(btrace_read_type) <BTRACE_READ_ALL>: ... this.  Update users.
	(btrace_read_type) <btrace_read_new>: Change to ...
	(btrace_read_type) <BTRACE_READ_NEW>: ... this.  Update users.
gdbarch: add instruction predicate methods

Add new methods to gdbarch for analyzing the instruction at a given address.
Implement those methods for i386 and amd64 architectures.

This is needed by "record btrace" to detect function calls in the
execution trace.

2014-01-16  Markus Metzger  <markus.t.metzger@intel.com>

	* amd64-tdep.c (amd64_classify_insn_at, amd64_insn_is_call)
	(amd64_insn_is_ret, amd64_insn_is_jump, amd64_jmp_p): New.
	(amd64_init_abi): Add insn_is_call, insn_is_ret, and insn_is_jump
	to gdbarch.
	* i386-tdep.c (i386_insn_is_call, i386_insn_is_ret)
	(i386_insn_is_jump, i386_jmp_p): New.
	(i386_gdbarch_init): Add insn_is_call, insn_is_ret, and
	insn_is_jump to gdbarch.
	* gdbarch.sh (insn_is_call, insn_is_ret, insn_is_jump): New.
	* gdbarch.h: Regenerated.
	* gdbarch.c: Regenerated.
	* arch-utils.h (default_insn_is_call, default_insn_is_ret)
	(default_insn_is_jump): New.
	* arch-utils.c (default_insn_is_call, default_insn_is_ret)
	(default_insn_is_jump): New.
frame: add frame_id_build_unavailable_stack_special

Add a function to build a frame_id for a frame with unavailable stack
and with a special identifier address.

2014-01-16  Markus Metzger  <markus.t.metzger@intel.com>

	* frame.h (frame_id_build_unavailable_stack_special): New.
	* frame.c (frame_id_build_unavailable_stack_special): New.
btrace: change branch trace data structure

The branch trace is represented as 3 vectors:
  - a block vector
  - a instruction vector
  - a function vector

Each vector (except for the first) is computed from the one above.

Change this into a graph where a node represents a sequence of instructions
belonging to the same function and where we have three types of edges to connect
the function segments:
  - control flow
  - same function (instance)
  - call stack

This allows us to navigate in the branch trace.  We will need this for "record
goto" and reverse execution.

This patch introduces the data structure and computes the control flow edges.
It also introduces iterator structs to simplify iterating over the branch trace
in control-flow order.

It also fixes PR gdb/15240 since now recursive calls are handled correctly.
Fix the test that got the number of expected fib instances and also the
function numbers wrong.

The current instruction had been part of the branch trace.  This will look odd
once we start support for reverse execution.  Remove it.  We still keep it in
the trace itself to allow extending the branch trace more easily in the future.

2014-01-16  Markus Metzger  <markus.t.metzger@intel.com>

	* btrace.h (struct btrace_func_link): New.
	(enum btrace_function_flag): New.
	(struct btrace_inst): Rename to ...
	(struct btrace_insn): ...this. Update all users.
	(struct btrace_func) <ibegin, iend>: Remove.
	(struct btrace_func_link): New.
	(struct btrace_func): Rename to ...
	(struct btrace_function): ...this. Update all users.
	(struct btrace_function) <segment, flow, up, insn, insn_offset)
	(number, level, flags>: New.
	(struct btrace_insn_iterator): Rename to ...
	(struct btrace_insn_history): ...this.
	Update all users.
	(struct btrace_insn_iterator, btrace_call_iterator): New.
	(struct btrace_target_info) <btrace, itrace, ftrace>: Remove.
	(struct btrace_target_info) <begin, end, level>
	<insn_history, call_history>: New.
	(btrace_insn_get, btrace_insn_number, btrace_insn_begin)
	(btrace_insn_end, btrace_insn_prev, btrace_insn_next)
	(btrace_insn_cmp, btrace_find_insn_by_number, btrace_call_get)
	(btrace_call_number, btrace_call_begin, btrace_call_end)
	(btrace_call_prev, btrace_call_next, btrace_call_cmp)
	(btrace_find_function_by_number, btrace_set_insn_history)
	(btrace_set_call_history): New.
	* btrace.c (btrace_init_insn_iterator)
	(btrace_init_func_iterator, compute_itrace): Remove.
	(ftrace_print_function_name, ftrace_print_filename)
	(ftrace_skip_file): Change
	parameter to const.
	(ftrace_init_func): Remove.
	(ftrace_debug): Use new btrace_function fields.
	(ftrace_function_switched): Also consider gaining and
	losing symbol information).
	(ftrace_print_insn_addr, ftrace_new_call, ftrace_new_return)
	(ftrace_new_switch, ftrace_find_caller, ftrace_new_function)
	(ftrace_update_caller, ftrace_fixup_caller, ftrace_new_tailcall):
	New.
	(ftrace_new_function): Move. Remove debug print.
	(ftrace_update_lines, ftrace_update_insns): New.
	(ftrace_update_function): Check for call, ret, and jump.
	(compute_ftrace): Renamed to ...
	(btrace_compute_ftrace): ...this. Rewritten to compute call
	stack.
	(btrace_fetch, btrace_clear): Updated.
	(btrace_insn_get, btrace_insn_number, btrace_insn_begin)
	(btrace_insn_end, btrace_insn_prev, btrace_insn_next)
	(btrace_insn_cmp, btrace_find_insn_by_number, btrace_call_get)
	(btrace_call_number, btrace_call_begin, btrace_call_end)
	(btrace_call_prev, btrace_call_next, btrace_call_cmp)
	(btrace_find_function_by_number, btrace_set_insn_history)
	(btrace_set_call_history): New.
	* record-btrace.c (require_btrace): Use new btrace thread
	info fields.
	(record_btrace_info, btrace_insn_history)
	(record_btrace_insn_history, record_btrace_insn_history_range):
	Use new btrace thread info fields and new iterator.
	(btrace_func_history_src_line): Rename to ...
	(btrace_call_history_src_line): ...this. Use new btrace
	thread info fields.
	(btrace_func_history): Rename to ...
	(btrace_call_history): ...this. Use new btrace thread info
	fields and new iterator.
	(record_btrace_call_history, record_btrace_call_history_range):
	Use new btrace thread info fields and new iterator.

testsuite/
	* gdb.btrace/function_call_history.exp: Fix expected function
	trace.
	* gdb.btrace/instruction_history.exp: Initialize traced.
	Remove traced_functions.
record-btrace: fix insn range in function call history

With the "/i" modifier, we print the instruction number range in the
"record function-call-history" command as [begin, end).

It would be more intuitive if we printed the range as [begin, end].

2014-01-16  Markus Metzger  <markus.t.metzger@intel.com>

	* record-btrace.c (btrace_call_history_insn_range): Print
	insn range as [begin, end].
record-btrace: start counting at one

The record instruction-history and record-function-call-history commands start
counting instructions at zero.  This is somewhat unintuitive when we start
navigating in the recorded instruction history.  Start at one, instead.

2014-01-16  Markus Metzger  <markus.t.metzger@intel.com>

	* btrace.c (ftrace_new_function): Start counting at one.
	* record-btrace.c (record_btrace_info): Adjust number of calls
	and insns.
	* NEWS: Announce it.

testsuite/
    * gdb.btrace/instruction_history.exp: Update.
    * gdb.btrace/function_call_history.exp: Update.
btrace: increase buffer size

Try to allocate as much buffer as we can for each thread with a maximum
of 64KB.

2014-01-16  Markus Metzger  <markus.t.metzger@intel.com>

	* common/linux-btrace.c (linux_enable_btrace): Enlarge buffer.
record-btrace: optionally indent function call history

Add a new modifier /c to the "record function-call-history" command to
indent the function name based on its depth in the call stack.

Also reorder the optional fields to have the indentation at the very beginning.
Prefix the insn range (/i modifier) with "inst ".
Prefix the source line (/l modifier) with "at ".
Change the range syntax from "begin-end" to "begin,end" to allow copy&paste to
the "record instruction-history" and "list" commands.

Adjust the respective tests and add new tests for the /c modifier.

2014-01-16  Markus Metzger  <markus.t.metzger@intel.com>

	* record.h (enum record_print_flag)
	<record_print_indent_calls>: New.
	* record.c (get_call_history_modifiers): Recognize /c modifier.
	(_initialize_record): Document /c modifier.
	* record-btrace.c (btrace_call_history): Add btinfo parameter.
	Reorder fields.  Optionally indent the function name.  Update
	all users.
	* NEWS: Announce changes.

testsuite/
	* gdb.btrace/function_call_history.exp: Fix expected field
	order for "record function-call-history".
	Add new tests for "record function-call-history /c".
	* gdb.btrace/exception.cc: New.
	* gdb.btrace/exception.exp: New.
	* gdb.btrace/tailcall.exp: New.
	* gdb.btrace/x86-tailcall.S: New.
	* gdb.btrace/x86-tailcall.c: New.
	* gdb.btrace/unknown_functions.c: New.
	* gdb.btrace/unknown_functions.exp: New.
	* gdb.btrace/Makefile.in (EXECUTABLES): Add new.

doc/
	* gdb.texinfo (Process Record and Replay): Document new /c
	modifier accepted by "record function-call-history".
	Add /i modifier to "record function-call-history" example.
record-btrace: make ranges include begin and end

The "record function-call-history" and "record instruction-history" commands
accept a range "begin, end".  End is not included in both cases.  Include it.

2014-01-16  Markus Metzger  <markus.t.metzger@intel.com>

	* record-btrace.c (record_btrace_insn_history_range): Include
	end.
	(record_btrace_insn_history_from): Adjust range.
	(record_btrace_call_history_range): Include
	end.
	(record_btrace_call_history_from): Adjust range.
	* NEWS: Announce changes.

testsuite/
	* gdb.btrace/function_call_history.exp: Update tests.
	* gdb.btrace/instruction_history.exp: Update tests.

doc/
	* gdb.texinfo (Process Record and Replay): Update documentation.
btrace: add replay position to btrace thread info

Add a branch trace instruction iterator pointing to the current replay position
to the branch trace thread info struct.

Free the iterator when btrace is cleared.

Start at the replay position for the instruction and function-call histories.

2014-01-16  Markus Metzger  <markus.t.metzger@intel.com>

	* btrace.h (replay) <replay>: New.
	(btrace_is_replaying): New.
	* btrace.c (btrace_clear): Free replay iterator.
	(btrace_is_replaying): New.
	* record-btrace.c (record_btrace_is_replaying): New.
	(record_btrace_info): Print insn number if replaying.
	(record_btrace_insn_history): Start at replay position.
	(record_btrace_call_history): Start at replay position.
	(init_record_btrace_ops): Init to_record_is_replaying.
Add target_ops argument to to_prepare_to_store

2013-12-17  Tom Tromey  <tromey@redhat.com>

	* windows-nat.c (windows_prepare_to_store): Add 'self' argument.
	* target.h (struct target_ops) <to_prepare_to_store>: Add
	argument.
	(target_prepare_to_store): Add argument.
	* target.c (debug_to_prepare_to_store): Add argument.
	(update_current_target): Update.
	* remote.c (remote_prepare_to_store): Add 'self' argument.
	* remote-sim.c (gdbsim_prepare_to_store): Add 'self' argument.
	* remote-mips.c (mips_prepare_to_store): Add 'self' argument.
	* remote-m32r-sdi.c (m32r_prepare_to_store): Add 'self' argument.
	* record-full.c (record_full_core_prepare_to_store): Add 'self'
	argument.
	* ravenscar-thread.c (ravenscar_prepare_to_store): Add argument.
	* nto-procfs.c (procfs_prepare_to_store): Add 'self' argument.
	* monitor.c (monitor_prepare_to_store): Add 'self' argument.
	* inf-child.c (inf_child_prepare_to_store): Add 'self' argument.
	* go32-nat.c (go32_prepare_to_store): Add 'self' argument.
record-btrace: supply register target methods

Supply target methods to allow reading the PC.  Forbid anything else.

2014-01-16  Markus Metzger  <markus.t.metzger@intel.com>

	* record-btrace.c (record_btrace_fetch_registers)
	(record_btrace_store_registers)
	(record_btrace_to_prepare_to_store): New.
	(init_record_btrace_ops): Add the above.
frame, backtrace: allow targets to supply a frame unwinder

Allow targets to supply their own target-specific frame unwinders; one for
normal frames and one for tailcall frames.  If a target-specific unwinder
is supplied, it will be chosen before any other unwinder.

The original patch has been split into this and the next two patches.

gdb/
2013-02-11  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* frame-unwind.c: Include target.h.
	(frame_unwind_try_unwinder): New function with code from ...
	(frame_unwind_find_by_frame): ... here.  New variable
	unwinder_from_target, call also target_get_unwinder)
	(target_get_tailcall_unwinder, and frame_unwind_try_unwinder for it.
	* target.c (target_get_unwinder, target_get_tailcall_unwinder): New.
	* target.h (struct target_ops): New fields to_get_unwinder and
	to_get_tailcall_unwinder.
	(target_get_unwinder, target_get_tailcall_unwinder): New declarations.
frame, cfa: check unwind stop reason first

Swap the unwind stop reason check and the unwinder check to allow
non-dwarf2 frame types to fail with a recoverable error.

gdb/
2013-02-11  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* dwarf2-frame.c (dwarf2_frame_cfa): Move UNWIND_UNAVAILABLE check
	earlier.
frame: do not assume unwinding will succeed

In get_frame_unwind_stop_reason, remove the assumption that further frame
unwinding will succeed.

gdb/
2013-02-11  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* frame.c (get_frame_unwind_stop_reason): Unconditionally call
	get_prev_frame_1.
record-btrace, frame: supply target-specific unwinder

Supply a target-specific frame unwinder for the record-btrace target that does
not allow unwinding while replaying.

2013-02-11  Jan Kratochvil  <jan.kratochvil@redhat.com>
            Markus Metzger  <markus.t.metzger@intel.com>

gdb/
	* record-btrace.c: Include frame-unwind.h.
	(record_btrace_frame_unwind_stop_reason)
	(record_btrace_frame_this_id, record_btrace_frame_prev_register)
	(record_btrace_frame_sniffer, record_btrace_frame_unwind):
	New.
	(init_record_btrace_ops): Install it.
target, breakpoint: allow insert/remove breakpoint to be forwarded

2014-01-16  Markus Metzger  <markus.t.metzger@intel.com>

	* target.h (target_ops) <to_insert_breakpoint>
	<to_remove_breakpoint>: Add target_ops parameter.
	(forward_target_insert_breakpoint): New.
	(forward_target_remove_breakpoint): New.
	(memory_remove_breakpoint, memory_insert_breakpoint):
	Add target_ops parameter.
	* target.c (target_insert_breakpoint): Split into this and ...
	(forward_target_insert_breakpoint): ... this.
	(target_remove_breakpoint): Split into this and ...
	(forward_target_remove_breakpoint): ... this.
	(debug_to_insert_breakpoint): Add target_ops parameter.
	Call forward_target_insert_breakpoint.
	(debug_to_remove_breakpoint): Add target_ops parameter.
	Call forward_target_remove_breakpoint.
	(update_current_target): Do not inherit or default to_insert_breakpoint
	and to_remove_breakpoint.
	* corelow.c (ignore): Add target_ops parameter.
	* exec.c (ignore): Add target_ops parameter.
	* mem-break.c (memory_insert_breakpoint, memory_remove_breakpoint):
	Add target_ops parameter.
	* monitor.c (monitor_insert_breakpoint, monitor_remove_breakpoint):
	Add target_ops parameter.
	* nto-procfs.c (procfs_insert_breakpoint, procfs_remove_breakpoint):
	Add target_ops parameter.
	* record-full.c (record_full_beneath_to_insert_breakpoint)
	(record_full_beneath_to_remove_breakpoint, tmp_to_insert_breakpoint)
	(tmp_to_remove_breakpoint, record_full_insert_breakpoint)
	(record_full_remove_breakpoint, record_full_core_insert_breakpoint)
	(record_full_core_remove_breakpoint): Add target_ops parameter.
	Update users.
	(record_full_beneath_to_insert_breakpoint_ops)
	(record_full_beneath_to_remove_breakpoint_ops)
	(tmp_to_insert_breakpoint_ops, tmp_to_remove_breakpoint_ops): New.
	(record_full_open): Initialize tmp_to_insert_breakpoint_ops,
	tmp_to_remove_breakpoint_ops,
	record_full_beneath_to_insert_breakpoint_ops, and
	record_full_beneath_to_remove_breakpoint_ops.
	* remote-m32r-sdi.c (m32r_insert_breakpoint)
	(m32r_remove_breakpoint): Add target_ops parameter.
	* remote-mips.c (mips_insert_breakpoint, mips_remove_breakpoint):
	Add target_ops parameter.
	* remote.c (remote_insert_breakpoint, remote_remove_breakpoint):
	Add target_ops parameter.
record-btrace: provide xfer_partial target method

Provide the xfer_partial target method for the btrace record target.

Only allow memory read accesses to readonly memory while we're replaying,
except for inserting and removing breakpoints.

2014-01-16  Markus Metzger  <markus.t.metzger@intel.com>

	* record-btrace.c (record_btrace_xfer_partial)
	(record_btrace_insert_breakpoint, record_btrace_remove_breakpoint)
	(record_btrace_allow_memory_access): New.
	(init_record_btrace_ops): Initialize new methods.
	* target.c (raw_memory_xfer_partial): Bail out if target reports
	that this memory is not available.