Aleksandar Simonović
Aleksandar Simonović

Reputation: 19

GDB breaking at unwanted position

When I place a breakpoint at a certain function, and type run, function parameters aren't correct. Example:

(gdb) break add_numbers
Breakpoint 1 at 0x1149: file program.c, line 4.
(gdb) run
Starting program: /home/aleksandar/Desktop/program 

Breakpoint 1, add_numbers (n1=21845, n2=1431654909) at program.c:4
4   {

I expect it to say n1=5, n2=6 instead of n1=21845, n2=1431654909. Everything works fine, program gives correct output, but dont know why parameters aren't right when gdb breaks at function add_numbers

I'm new to debugging with GBD. Can someone explain?

Here is C code:

#include <stdio.h>

int add_numbers(int n1, int n2)
{
    int sum = n1 + n2;
    return sum;
}

int main(){
    int x = 5;
    int y = 6;
    int suma = add_numbers(x,y);
    printf("Suma je %d\n", suma);
    return 0;
}

Disassembly output:

(gdb) disas
Dump of assembler code for function add_numbers:
=> 0x0000555555555149 <+0>: endbr64 
   0x000055555555514d <+4>: push   %rbp
   0x000055555555514e <+5>: mov    %rsp,%rbp
   0x0000555555555151 <+8>: mov    %edi,-0x14(%rbp)
   0x0000555555555154 <+11>:    mov    %esi,-0x18(%rbp)
   0x0000555555555157 <+14>:    mov    -0x14(%rbp),%edx
   0x000055555555515a <+17>:    mov    -0x18(%rbp),%eax
   0x000055555555515d <+20>:    add    %edx,%eax
   0x000055555555515f <+22>:    mov    %eax,-0x4(%rbp)
   0x0000555555555162 <+25>:    mov    -0x4(%rbp),%eax
   0x0000555555555165 <+28>:    pop    %rbp
   0x0000555555555166 <+29>:    retq 

GDB version

aleksandar@ubuntu:~/Desktop$ gdb -version
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Upvotes: 0

Views: 130

Answers (1)

Employed Russian
Employed Russian

Reputation: 213877

Reproduced using 9.2-0ubuntu1~20.04 and gcc 9.3.0-17ubuntu1~20.04.

This is a bug in GDB, fixed on trunk (11.0.50.20210307-git).

You can work around it by building your program with -fcf-protection=none flag.

Upvotes: 1

Related Questions