Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6871961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:52:03+00:00 2026-05-27T03:52:03+00:00

In Linux, with C/C++ code, using gdb, how can you add a gdb breakpoint

  • 0

In Linux, with C/C++ code, using gdb, how can you add a gdb breakpoint to scan the incoming strings in order to break on a particular string?

I don’t have access to a specific library’s code, but I want to break as soon as that library sends a specific string to standard out so I can go back up the stack and investigate the part of my code that is calling the library. Of course I don’t want to wait until a buffer flush occurs. Can this be done? Perhaps a routine in libstdc++ ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:52:03+00:00Added an answer on May 27, 2026 at 3:52 am

    This question might be a good starting point: how can I put a breakpoint on "something is printed to the terminal" in gdb?

    So you could at least break whenever something is written to stdout. The method basically involves setting a breakpoint on the write syscall with a condition that the first argument is 1 (i.e. STDOUT). In the comments, there is also a hint as to how you could inspect the string parameter of the write call as well.

    x86 32-bit mode

    I came up with the following and tested it with gdb 7.0.1-debian. It seems to work quite well. $esp + 8 contains a pointer to the memory location of the string passed to write, so first you cast it to an integral, then to a pointer to char. $esp + 4 contains the file descriptor to write to (1 for STDOUT).

    $ gdb break write if 1 == *(int*)($esp + 4) && strcmp((char*)*(int*)($esp + 8), "your string") == 0
    

    x86 64-bit mode

    If your process is running in x86-64 mode, then the parameters are passed through scratch registers %rdi and %rsi

    $ gdb break write if 1 == $rdi && strcmp((char*)($rsi), "your string") == 0
    

    Note that one level of indirection is removed since we’re using scratch registers rather than variables on the stack.

    Variants

    Functions other than strcmp can be used in the above snippets:

    • strncmp is useful if you want match the first n number of characters of the string being written
    • strstr can be used to find matches within a string, since you can’t always be certain that the string you’re looking for is at the beginning of string being written through the write function.

    Edit: I enjoyed this question and finding it’s subsequent answer. I decided to do a blog post about it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been using objdump to look at assembly code in Linux ELF binaries. Sometimes
I am trying to compile code that works on os x and linux using
Is there significant problem, if I write the code for embedded linux system using
In Windows, I can set the processor affinity of driver code using KeSetSystemAffinityThread, and
What flags do I need to add to g++ in order to compile code
I know it is possible to remotely debug code using gdb's server mode and
I'm a linux noob wanting to migrate this piece of C# code: using System;
I've tried using GDB and Valgrind, but I can't seem to pinpoint the problem.
I'm using gcc4.4.4 and gdb on 64bit Linux centos 5.7, compiling to ansi C.
I have some Linux code that monitors our hardware by collecting temperatures, voltages, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.