6

I am working on a c program to add conditional breakpoints or print some values. But when I use some expressions, the gdb failed with Invalid cast.

  • OS: Ubuntu 24.04.1 LTS
  • GCC: gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
  • GDB: GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-git
  • GLIBC: Ubuntu GLIBC 2.39-0ubuntu8.4

Here is my code:

#include<stdio.h>
#include<string.h>

int main(){
    const char *i = "a";
    const char *j = "b";
    int c = strcmp(i, j);
    printf("i: %s\n", i);
    printf("j: %s\n", j);
    printf("c: %d\n", c);
}

Here are some examples:

  1. Compile
gcc -g3 test.c -o test
gdb ./test
  1. Conditional breakpoint: failed with Invalid cast:
(gdb) b 8 if ((int) strcmp(i, "a") == 0) && ((int) strcmp(i, "b") == 0)
Breakpoint 1 at 0x11a1: file test.c, line 8.
(gdb) run
Starting program: /home/xxx/work/osd/hwk89/test 

This GDB supports auto-downloading debuginfo from the following URLs:
  <https://debuginfod.ubuntu.com>
Enable debuginfod for this session? (y or [n]) 
Debuginfod has been disabled.
To make this setting permanent, add 'set debuginfod enabled off' to .gdbinit.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Error in testing condition for breakpoint 1:
Invalid cast.

Breakpoint 1, main () at test.c:8
8           printf("i: %s\n", i);

  1. Print: some examples are failed with Invalid cast:
(gdb) b 8
(gdb) run
Starting program: /home/xuancheng/work/osd/hwk89/test 

This GDB supports auto-downloading debuginfo from the following URLs:
  <https://debuginfod.ubuntu.com>
Enable debuginfod for this session? (y or [n]) 
Debuginfod has been disabled.
To make this setting permanent, add 'set debuginfod enabled off' to .gdbinit.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Breakpoint 1, main () at test.c:8
8           printf("i: %s\n", i);

(gdb) p (int) strcmp(i, "a")
$1 = 0
(gdb) p (int) strcmp(i, "b")
$2 = -1
(gdb) p (int) strcmp(i, "a") && (int) strcmp(i, "b")
Invalid cast.
(gdb) p (int) strcmp(i, "a") && -1
$3 = 0
(gdb) p 0 && (int) strcmp(i, "b")
Invalid cast.
(gdb) p 0 && 1
$4 = 0
(gdb) p (int) 0 && (int) 1
$5 = 0
(gdb) p ((int) strcmp(i, "a")) && ((int) strcmp(i, "b"))
Invalid cast.
(gdb) p (((int) strcmp(i, "a")) && ((int) strcmp(i, "b")))
Invalid cast.
(gdb) p (int) strcmp(i, "a") == 0 && (int) strcmp(i, "b") == 0
Invalid cast.
(gdb) p ((int) strcmp(i, "a") == 0) && ((int) strcmp(i, "b") == 0)
Invalid cast.
(gdb) p (((int) strcmp(i, "a") == 0) && ((int) strcmp(i, "b") == 0))
Invalid cast.
3
  • 1
    I also get a segmentation fault for your example. This is a bug with gdb. Commented Apr 15 at 12:55
  • It would be interesting to know if this can be reproduced with the latest gdb version (currently 16.2, soon 16.3), or maybe even with git master. Commented Apr 15 at 14:14
  • 1
    @ssbssa Thanks for your suggestions. I tried the GDB version 16.2, and the problem was solved. Commented Apr 16 at 2:44

1 Answer 1

2

I tried with your current configuration.Using Temporary Variables, i bypassed the invalid cast error.

(gdb) delete
Delete all breakpoints, watchpoints, tracepoints, and catchpoints? (y or n) y
(gdb) break test.c:8
Breakpoint 27 at 0x5555555551a1: file test.c, line 8.
(gdb) run
Starting program: /home/xxxx/Tmp/c-examples/test 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Breakpoint 27, main () at test.c:8
8       printf("i: %s\n", i);
(gdb) set $is_i_a = ((int)strcmp(i, "a") == 0)
(gdb) set $is_i_b = ((int)strcmp(i, "b") == 0)
(gdb) condition 1 $is_i_a && $is_i_b
No breakpoint number 1.
(gdb) condition 27 $is_i_a && $is_i_b
(gdb) continue
Continuing.
i: a
j: b
c: -1
[Inferior 1 (process 3761) exited normally]
(gdb) 

With your current version i also tried using Explicit Pointer Cast, which also didnt give any invalid cast errors.

(gdb) delete 
Delete all breakpoints, watchpoints, tracepoints, and catchpoints? (y or n) y
(gdb) break test.c:8 if ((int (*)(const char *, const char *))strcmp)(i, "a") == 0 && ((int (*)(const char *, const char *))strcmp)(i, "b") == 0
Breakpoint 28 at 0x5555555551a1: file test.c, line 8.
(gdb) run
Starting program: /home/xxxx/Tmp/c-examples/test 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Error in testing condition for breakpoint 28:
Expression of type other than "Function returning ..." used as function
Breakpoint 28, main () at test.c:8
8       printf("i: %s\n", i);
(gdb) continue
Continuing.
i: a
j: b
c: -1
[Inferior 1 (process 3769) exited normally]
(gdb) 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot! I can use these solutions without changing gdb version :).
the problem might be, but i am sure about it , that the GDB’s parser cannot handle compound expressions that mix casts and logical operators, leading to “Invalid cast” errors.
Here $is_i_a and $is_i_b are calculated at the beginning, so they will always be the same when the condition is checked at the breakpoint. It's fine for this test program, but maybe not what you want to happen in a real program.
@Hermit Interestingly, the "invalid cast" errors always occur when the compound expressions are after AND operators. As examples aforementioned, p (int) strcmp(i, "a") && -1 is valid while p 0 && (int) strcmp(i, "b") isn't. Maybe it is related to this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.