Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'tag' a location in a C source file for a later breakpoint definition?

Problem: I want to be able to put different potentially unique or repeated "tags" across my C code, such that I can use them in gdb to create breakpoints.

Similar Work:

  • Breakpoints to line-numbers: The main difference with breakpoints on source lines, is that if the code previous to the tag is modified in such a way that it results in more or less lines, a reference to the tag would still be semantically correct, a reference to the source line would not.

  • Labels: I am coming from my previous question, How to tell gcc to keep my unused labels?, in which I preconceived the idea that the answer was to insert labels. Upon discussion with knowledgeable members of the platform, I was taught that label's names are not preserved after compilation. Labels not used within C are removed by the compiler.

  • Injecting asm labels: Related to the previous approach, if I inject asm code in the C source, certain problems arise, due to inline functions, compiler optimizations, and lack of scoping. This makes this approach not robust.

  • Define a dummy function: On this other question, Set GDB breakpoint in C file, there is an interesting approach, in which a "dummy" function can be placed in the code, and then add a breakpoint to the function call. The problem with this approach is that the definition of such function must be replicated for each different tag.

Is there a better solution to accomplish this? Or a different angle to attack the presented problem?

like image 732
onlycparra Avatar asked Nov 24 '21 01:11

onlycparra


People also ask

Where is the data stored in a C program?

So far the operations using C program are done on a prompt / terminal which is not stored anywhere. But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information in a file.

What does the text in the brackets mean in C programming?

The text in the brackets denotes the functions used for performing those operations. Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.

How to identify location words using the gazetteers Corpus?

As it uses the gazetteers corpus to identify location words. The gazetteers corpus is a WordListCorpusReader class that contains the following location words: LocationChunker class looking for words that are found in the gazetteers corpus by iterating over a tagged sentence.

How to store the information fetched from a program?

But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information in a file. Different operations that can be performed on a file are: The text in the brackets denotes the functions used for performing those operations.


4 Answers

Using SDT (Statically Defined Tracing) probe points appears to satisfy all the requirements.

GDB documentation links to examples of how to define the probes.

Example use: (gdb) break -probe-stap my_probe (this should be documented in the GDB breakpoints section, but currently isn't).

like image 70
Employed Russian Avatar answered Nov 01 '22 15:11

Employed Russian


You could create a dummy variable and set it to different values. Then you can use conditional watchpoints. Example:

#include <stdio.h>
static volatile int loc;
int main()
{
    loc = 1;
    puts("hello world");
    loc = 2;
    return 0;
}

(gdb) watch loc if loc == 2
Hardware watchpoint 1: loc
(gdb) r
Starting program: /tmp/a.out 
hello world

Hardware watchpoint 1: loc

Old value = 1
New value = 2
main () at test.c:8
8       return 0;

You can of course wrap the assignment in a macro so you only get it in debug builds. Usual caveats apply: optimizations and inlining may be affected.

like image 20
Jester Avatar answered Nov 01 '22 14:11

Jester


Use python to search a source file for some predefined labels, and set breakpoints there:

def break_on_labels(source, label):
    """add breakpoint on each SOURCE line containing LABEL"""
    with open(source) as file:
        l = 0
        for line in file:
            l = l + 1
            if label in line:
                gdb.Breakpoint(source=source, line=l)

main_file = gdb.lookup_global_symbol("main").symtab.fullname()
break_on_labels(main_file, "BREAK-HERE")

Example:

int main(void)
{
  int a = 15;
  a = a + 23; // BREAK-HERE
  return a;
}
like image 45
ssbssa Avatar answered Nov 01 '22 15:11

ssbssa


You could insert a #warning at each line where you want a breakpoint, then have a script to parse the file and line numbers from the compiler messages and write a .gdbinit file placing breakpoints at those locations.

like image 38
Nate Eldredge Avatar answered Nov 01 '22 16:11

Nate Eldredge