Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gdb throwing error saying program to have a function "malloc"

Tags:

gdb

gdbserver

I executed following commands in gdb and console output is as follows:

Rohan_gdb$ set $var = 15
Rohan_gdb$ p $var
$5 = 0xf
Rohan_gdb$ set $var = (int *)10
Rohan_gdb$ p $var
$6 = (int *) 0xa
Rohan_gdb$ set $char = "abc"
Rohan_gdb$ p $char
$7 = "abc"
Rohan_gdb$ set $char = (char *)"xyz"
evaluation of this expression requires the program to have a function "malloc".

(here I got error)

Rohan_gdb$ p $char
$8 = "abc"
Rohan_gdb$

Here I am debugging with target and not native debugging. I am using GNU gdb (GDB) 7.2 version. Is it possible to solve using scripts.

like image 991
user1235791 Avatar asked Jun 28 '26 02:06

user1235791


1 Answers

I don't know how to solve your specific problem, but I ran across something similar. Given the age of the question, maybe this'll provide a clue.

The problem is that your script is trying to store away a value in a buffer and it must allocated a new buffer for that storage. The storage requirement is likely the result of the cast or because that second string is not in the constant strings within your binary.

To fix, either change your code to not require a malloc (which is a bit of hit or miss, as far as I can tell). Or make the malloc symbol available; load a symbol table that allows gdb to resolve the "_malloc" symbol.

like image 148
bbum Avatar answered Jul 01 '26 00:07

bbum