Sams
Sams

Reputation: 197

Errors with arrays in GDB (written in C)

I'm trying to find the memory locations of the elements of an array. The function is basically this (in C):

int function(struct MyStruct *s)
{
    char myArray[16];

    printf("\n");
    printf("\n");
    gets(myArray);
    return strcasecmp(s->a,guess);
}

According to GDB guides online I should be able to do "x myArray" or "x myArray[0]" or "p myArray" or "p myArray[0]" to see the elements or memory locations. I set up breakpoints at function and gets (and continued until the gets breakpoint), but when I tried the GDB commands, I got "no such file." I also tried "b 15" to set a breakpoint at the array and "b 16." but I still got the same error. Why is this happening and how can I fix it?

Upvotes: 1

Views: 124

Answers (1)

Employed Russian
Employed Russian

Reputation: 213799

"break main, break function, break gets, break 15 (which didn't work), break 16 (which didn't work), run, s (until I reach the gets breakpoint), x myArray, x myArray[0], p myArray, p myArray[0]"

You are doing it wrong.

When you hit the gets breakpoint, you are stopped inside gets.

In there, the myArray variable is not visible -- it's inside function, not inside gets.

You can't break 15 while inside gets either, because you don't have debug info for libc (which is where gets is implemented).

What you want to do is finish from the breakpoint in gets (which will bring you back into function). Now you should be able to look at myArray, or break 15, etc.

Upvotes: 3

Related Questions