Stoogy
Stoogy

Reputation: 1351

gdb: Cast depends on the language compiled

I was debugging a program compiled in Rust using GDB (arm-none-eabi-gdb). At one point, I wanted to write to a memory address as follow:

(gdb) set *((int *) 0x24040000) = 0x0000CAFE
syntax error in expression, near `) 0x24040000) = 0x0000CAFE'.

After multiple tentative, I found out that I was casting the C style and I had to cast it the Rust style as follow:

set *(0x24040000 as *mut i32) = 0x0000CAFE

My question is how GDB is interpreting the different commands and why I get this error. Is it because the symbol (int) is not recognized, but in this case, how gdb load the symbols? Does gdb need to compile the instruction to the correct language of the binary running on the target?

Upvotes: 5

Views: 1279

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385325

Yes, it depends on the language, and the language is deduced from the filename of the loaded source file.


Quoting the manual:

print and many other GDB commands accept an expression and compute its value. Any kind of constant, variable or operator defined by the programming language you are using is valid in an expression in GDB. This includes conditional expressions, function calls, casts, and string constants.

And:

If you are not interested in seeing the value of the assignment, use the set command instead of the print command. set is really the same as print except that the expression’s value is not printed and is not put in the value history (see Value History). The expression is evaluated only for its effects.

And:

Language-specific information is built into GDB for some languages, allowing you to express operations like the above in your program’s native language, and allowing GDB to output values in a manner consistent with the syntax of your program’s native language. The language you use to build expressions is called the working language.

And:

There are two ways to control the working language—either have GDB set it automatically, or select it manually yourself. You can use the set language command for either purpose. On startup, GDB defaults to setting the language automatically.

[..] most of the time GDB infers the language from the name of the file.

Upvotes: 2

Related Questions