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:
- Compile
gcc -g3 test.c -o test
gdb ./test
- 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);
- 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.