Search code examples
gdbbreakpointsfedoraqemuriscv

Does GDB in RISC-V support Program Context aware breakpoint?


I would like to learn if existing GDB for RISC-V supports Program Context aware breakpoints?

By program context aware breakpoints : I mean, when there is JAL or JALR instruction PC changes when there is a function call. in other cases in Function call ==> PC = PC + (Current Program Counter + 4) in Function Return : PC = PC - (Return address (ra register value) ).

I have installed fedora(risc-V) on my ubuntu(virtual machine). Since it is virtual machine I can't print PC register value, that is why I couldn't check if it supports Program Context aware breakpoint or not?

My second question is : How can I print PC register value on my qemu risc-v virtual machine?

#include<stdio.h>

int check_prime(int a)
{
int c; 
    for (c=2;c<a;c++)
    {
        if (a%c == 0 ) return 0; 
        if (c == a-1 ) return 1; 
    }
}

void oddn(int a)
{
    printf("oddn --> %d is an odd number \n",a);
    if (check_prime(a)) printf("oddn --> %d is a prime number\n",a);
}

int main()
{
    int a;
    a=7;
    if (check_prime(a)) printf("%d is a prime number \n",a);
        if (a%2==1) oddn(a);    

}

This is the program I am trying to breakpoint using GDB.

The result of GDB breakpoint

As you see on the picture it breaks twice(which should break once only). It also gives error :

Error in testing breakpoint condition:
Invalid data type for function to be called

Solution

  • What you're looking for is documented here:

    https://sourceware.org/gdb/current/onlinedocs/gdb/Convenience-Funs.html#index-_0024_005fstreq_002c-convenience-function

    You should look at $_caller_is, $_caller_matches, $_any_caller_is, and $_any_caller_matches.

    As an example, to check if the immediate caller is a particular function we could do this:

    break functionD if ($_caller_is ("functionC"))
    

    Then main -> functionD will not trigger the breakpoint, while main -> functionC -> functionD will trigger the breakpoint.

    The convenience functions I listed all take a frame offset that can be used to specify which frame GDB will check (for $_caller_is and $_caller_matches) or to limit the range of frames checked (for $_any_caller_is and $_any_caller_matches).