[PATCH] dynamic printf

Stan Shebs stanshebs@earthlink.net
Wed Feb 29 09:14:00 GMT 2012


This patch implements a "dynamic printf", which is basically a 
breakpoint with a printf;continue as its command list - but with 
additional features that make it more interesting.  First, there is an 
option to send the result to the inferior's output stream rather than to 
the GDB console; second, there is an option to do the printing in a 
remote agent/stub; and third, the agent version can continue printf'ing 
even after GDB detaches(!).

(For those who pay attention to the competitive landscape, this is an 
Ericsson-requested GDBification of a Wind River feature.)

The syntax of the command is

     dprintf <location> <format>,<arg>,<arg>...

where the location is as for breakpoints, while the format and args are 
as for the printf command.  So you could have something like

     dprintf myfun "myfun args are %d,%d\n",arg1,arg2

A setshow variable dprintf-style controls whether the printing is done 
by GDB, the inferior, or by a remote agent, and another variable 
disconnected-dprintf controls persistence upon disconnection, similarly 
to disconnected tracing.

The patch itself is somewhat of a hack-n-slash through the middle of 
GDB, and there is much to critique. :-)  Partly this is because I wanted 
to push through to a genuinely useful example of target-side breakpoint 
commands, rather than doing limited-functionality "commands" in the 
abstract.  It also points up our continuing need for being able to 
define types of breakpoints by properties rather than adding enums...  I 
borrowed some of Luis' target-side conditional code, and got some 
inspiration from Hui Zhu's previous try at a printf bytecode.

Anyway, without further ado, here's the patch itself.  I've skipped the 
testsuite for now, as I expect everybody will want nontrivial 
modifications. :-)

Stan

2012-02-28  Stan Shebs <stan@codesourcery.com>

     Add dynamic printf.
     * breakpoint.h (enum bptype): New type bp_dprintf.
     (struct bp_target_info): New field commands and persist.
     (struct bp_location): New field cmd_bytecode.
     (struct breakpoint): New field extra_string.
     * breakpoint.c (dprintf_breakpoint_ops): New.
     (disconnected_dprintf): New global.
     (is_breakpoint): Add bp_dprintf.
     (parse_format_string): New function.
     (parse_cmd_to_aexpr): New function.
     (build_target_command_list): New function.
     (insert_bp_location): Call it.
     (remove_breakpoints_pid): Skip dprintf breakpoints.
     (bpstat_what): Add dprintf case.
     (bptype_string): Ditto.
     (print_one_breakpoint_location): Ditto.
     (init_bp_location): Ditto.
     (bkpt_print_mention): Ditto.
     (dprintf_style_enums): New array.
     (dprintf_style): New global.
     (glob_extra_string): New global.
     (init_breakpoint_sal): Handle extra string.
     (addr_string_to_sals): Ditto.
     (find_condition_and_thread): Add extra argument.
     (create_breakpoint): Save away additional text at end of command.
     (dprintf_command): New function.
     (_initialize_breakpoint): Add new commands.
     * common/ax.def (printf): New bytecode.
     * ax-gdb.c (gen_printf): New function.
     (agent_print_command): New function.
     (_initialize_ax_gdb): Add maint agent-printf command.
     * ax-gdb.h (gen_printf): Declare.
     * ax-general.c (ax_print): Add printf disassembly.
     * remote.c (remote_state) <cond_breakpoints>: New field.
     (PACKET_BreakpointCommandss): New enum.
     (remote_breakpoint_commands_feature): New function.
     (remote_protocol_features): Add new BreakpointCommands entry.
     (remote_can_run_breakpoint_commands): New function.
     (remote_add_target_side_commands): New function.
     (remote_insert_breakpoint): Call it.
     (remote_insert_hw_breakpoint): Ditto.
     (_initialize_remote): Add new packet configuration for
     target-side breakpoint commands.
     * target.h (struct target_ops): New field
     to_can_run_breakpoint_commands.
     (target_can_run_breakpoint_commands): New macro.
     * target.c (update_current_target): Handle
     to_can_run_breakpoint_commands.

     [gdbserver]
     * server.c (handle_query): Claim support for breakpoint commands.
     (process_point_options): Add command case.
     (process_serial_event): Leave running if there are printfs in
     effect.
     * mem-break.c (struct point_command_list): New struct.
     (struct breakpoint): New field command_list.
     (any_persistent_commands): New function.
     (add_commands_to_breakpoint): New function.
     (add_breakpoint_commands): New function.
     (gdb_no_commands_at_breakpoint): New function.
     (run_breakpoint_commands): New function.
     * mem-break.h (any_persistent_commands): Declare.
     (add_breakpoint_commands): Declare.
     (gdb_no_commands_at_breakpoint): Declare.
     (run_breakpoint_commands): Declare.
     * linux-low.c (linux_wait_1): Test for and run breakpoint commands
     locally.
     * ax.c (gdb_eval_agent_expr): Add printf opcode.

     [doc]
     * gdb.texinfo (Dynamic Printf): New subsection.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: dprintf-patch-1
URL: <http://sourceware.org/pipermail/gdb-patches/attachments/20120229/35ce9f9b/attachment.ksh>


More information about the Gdb-patches mailing list