Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Extending gdbserver to support an strace client

March 16, 2020
Stan Cox
Related topics:
Linux

    The strace command traces system calls and signals, deciding them and their corresponding arguments into a symbolic form. A frequent debugging request from developers is the ability to allow strace to trace system calls for a program that is also being debugged by GDB, like this:

    % gdb --args test-program
    (gdb) b main
    Breakpoint 1 at 0x40128e: file test-program.c, line 22.
    (gdb) run
    Starting program: test-program
    Breakpoint 1, main (argc=3, argv=0x7fffffffdb98) at test-program.c:22
    22 int thread_count = 2;
    (gdb)

    In another terminal window, we invoke strace on the same process GDB is debugging:

    % strace -p $(pgrep -f test-program)
    strace: attach: ptrace(PTRACE_SEIZE, 27882): Operation not permitted

    The culprit here is that the ptrace system call, which is used by both GDB and strace to control the execution of programs, and does not allow both strace and GDB to control the same process.

    One solution is to use gdbserver to support both the gdb client and the strace client.

    Extending strace to support gdbserver

    The first step in this process is to extend strace to support gdbserver. The strace command is implemented using the ptrace call to intercept system calls. Gdbserver is also able to intercept system calls. To enable strace to use gdbserver, it is necessary to add a new back end that uses gdbserver instead of ptrace to do the system call interception. This gdbserver back end provides similar functionality to the ptrace back end.

    Now, let's look at some examples.

    Example of strace using a gdbserver

    An example of running strace with an alternative gdbserver back end is:

    % strace -G '|/usr/bin/gdbserver --once --multi stdio' test-program

    This example says to:

    1. Connect to gdbserver using the -G option.
    2. Run gdbserver only once.
    3. Communicate with strace with stdio.
    4. Use gdbserver to trace test-program.

    When run, the output provides (some output omitted):

    ...
    [pid 15603] close(-1) = -1 EBADF (Bad file descriptor)
    [pid 15603] chroot(".") = -1 EPERM (Operation not permitted)
    [pid 15603] pipe([3, 4]) = 0
    [pid 15603] write(4, "a\0", 2) = 2
    [pid 15603] read(3, "a\0", 2) = 2
    [pid 15603] madvise(0x7ffff75bb000, 8368128, MADV_DONTNEED) = 0
    [pid 15595] --- stopped by 255 ---
    [pid 15595] +++ exited with 0 +++

    Example of strace using a standalone gdbserver

    Alternately strace can use a standalone gdbserver:

    1. Start gdbserver:
      % gdbserver --multi :65432
    2. Second, start the strace client and communicate with gdbserver using the TCP port 65432.
    3. Tell strace to trace the test program:
      % strace -G localhost${i} test-program

    Example of strace using a remote gdbserver

    An example of running strace with a standalone gdbserver on a remote machine is to first start gdbserver on the remote machine:

    % gdbserver --once :65432 test-program

    Second, start the strace client and communicate with gdbserver on the remote host using the TCP port given in the gdbserver command:

    % strace -G remote-host.org.com:65432

    Note that we give neither a -p PID option nor a test program option. That information was specified by gdbserver, and strace will inherit that test program connection:

    ...
    brk(NULL) = 0x63a000
    arch_prctl(0x3001 /* ARCH_??? */, 0x7fffffffdb40) = -1 EINVAL (Invalid argument)
    --- stopped by 255 ---
    +++ exited with 0 +++

    Extending gdbserver to support both a gdb and an strace client

    Gdbserver currently handles a single client connection. To support both a gdb and strace client, it is necessary to enable gdbserver to handle multiple client connections. To do this, gdbserver must alternate between:

    1. The gdb client waits.
    2. Gdbserver sends a syscall packet to strace.
    3. Strace continues running via gdbserver.
    4. Gdbserver interacts with the gdb client while the strace client waits.

    Gdbserver currently keeps state information for a single client. Making gdbserver multiple client-aware requires adding state information for both the gdb client and the strace client—for example, the file descriptor for the connection and state information regarding the packets sent over the connection.

    As is the case with time-sharing, one client will always be the active client. Client packet requests (continue, step, get memory, etc.) and the current client state (active or waiter) determine the client's next state. For instance:

    1. State: GDB client is active, strace client is waiting.
    2. Request: GDB client makes the next request over a syscall.
    3. State: GDB client is waiting, strace client is active.
    4. Request: Strace receives syscall and continues running via gdbserver.
    5. State: GDB client is active, strace client is waiting.

    Example of a gdbserver and strace client

    This example says to start gdbserver and use TCP port 65432 to communicate with clients:

    1. Start gdbserver:
    % gdbserver --multi :65432
    1. Do the following for GDB:
      1. Start the gdb client in another terminal window.
      2. Tell GDB to communicate with the gdbserver using the TCP port 65432.
      3. Set a breakpoint and run the test program:
    % gdb
    (gdb) file test-program
    (gdb) target extended-remote localhost:65432
    (gdb) set remote exec-file test-program
    (gdb) b thread_worker
    (gdb) run
    (gdb) Thread 1 "test-program" hit Breakpoint 1, thread_worker () ...
    52 pthread_barrier_wait (&barrier);
    1. Start the strace client in another terminal window.
    2. Do the following for strace:
      1. Tell strace to communicate with gdbserver using the TCP port 65432.
      2. Tell strace to trace the test program that was started by the gdb client.
        % strace -G localhost:65432 -p $(pgrep test-program)
      3. The following would be displayed in the strace window:
        ...
        
        brk(0x426000) = 0x426000
    1. [GDB window] Tell the gdb client to use thread two, set a breakpoint, and advance the test program:
    (gdb) thread 2
    [Switching to thread 2 (Thread 7464.7490)]
    #0 thread_worker () at test-program.c:59
    (gdb) b 52
    (gdb) continue
    59 close (-1);
    (gdb) next
    61 chroot (".");
    (gdb) next
    1. [Strace window] Notice that strace has traced the close and chroot system calls:
    [pid 7490] close(-1) = -1 EBADF (Bad file descriptor)
    [pid 7490] chroot(".") = -1 EPERM (Operation not permitted)
    1. [GDB window] Advance the test program:
    (gdb) next
    63 pipe (fd);
    (gdb) next
    65 write (fd[1], buf1, sizeof (buf1));
    (gdb) next
    67 read (fd[0], buf2, sizeof (buf2));
    (gdb) next
    1. [Strace window] Notice that strace has traced the pipe, write, and read calls:
    [pid 7490] pipe([3, 4]) = 0
    [pid 7490] write(4, "a\0", 2) = 2
    [pid 7490] read(3, "a\0", 2) = 2
    1. [GDB window] Continue the test program to completion:
    (gdb) continue
    [Inferior 1 (process 7464) exited normally]
    (gdb) quit
    Remote connection closed
    1. [Strace window] Notice that the attached process has exited:
    [pid 7490] madvise(0x7ffff7475000, 8368128, MADV_DONTNEED) = 0
    [pid 7464] --- stopped by 255 ---
    [pid 7464] +++ exited with 0 +++
    strace: Process 7490 detached

    Current tool status

    Versions of the GDB remote protocol, strace, and corresponding gdbserver are currently under review. Initial versions of the gdbserver and strace tools are available for experimentation at gdbserver_copr and strace_copr.

    Summary

    Extending strace to additionally support gdbserver provides an additional means to run strace. For example, to trace a program that is running on a remote machine. Additionally, this extended strace, along with a corresponding extended gdbserver, enables tracing system calls in a program while a gdb client is debugging the same program.

    Last updated: June 29, 2020

    Recent Posts

    • Protect data offloaded to GPU-accelerated environments with OpenShift sandboxed containers

    • Case study: Measuring energy efficiency on the x64 platform

    • How to prevent AI inference stack silent failures

    • Preventing GPU waste: A guide to JIT checkpointing with Kubeflow Trainer on OpenShift AI

    • How to manage TLS certificates used by OpenShift GitOps operator

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.