I'd like to automate the following in my .gdbinit:

break boost::uuids::detail::sha1::process_bytes

# When execution stops at the above breakpoint,
# I want to display the contents of `rcx` as a string:
x/s $rcx
c  # do not stop here

How do I automate this?

UPDATE: Here's a better .gdbinit example:

# Our custom-built libcurl, with debugging symbols enabled:
set environment LD_PRELOAD=./curl/curl-7.34.0/lib/.libs/libcurl.so

# File that connects to the evil server:
file ./evil

# Make sure we get notified when it connects!
break curl_easy_setopt
commands $bpnum
clear curl_easy_setopt  # to avoid recursion
call curl_easy_setopt(curl, CURLOPT_VERBOSE)
continue
end

This hooks in to the evil binary, and when it initialises its curl handle, we set set it to verbose so we get lots of output about what's going on.

Thanks for the answer.

up vote 6 down vote accepted

Easy enough. In your case what you most likely want is commands which can be used to create "routines" that run whenever a breakpoint is hit. For your case roughly:

break boost::uuids::detail::sha1::process_bytes
commands 1
x/s $rcx
continue
end

Problem is that you need to hardcode the breakpoint number. Depending on the GDB version you may get around this using the $bpnum convenience variable. So:

break boost::uuids::detail::sha1::process_bytes
commands $bpnum
x/s $rcx
continue
end

Also see this concerning the last example.

Note: using this method can be quite taxing on the CPU depending on how often this gets called and whether a hardware breakpoint could be used by GDB.


You can also use the conditional form of breakpoints. Check out the actual authoritative reference here. The form looks like this:

break ... if cond

You can also set the condition independent of setting the breakpoint, if you know the breakpoint number. Use info break to get the number of the breakpoint and then use that as bnum in:

condition bnum expression

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.