![]() |
Chapter 6 | ![]() |
(gdb) print a $1 = 134517792 (gdb) print argc $2 = 1 (gdb) print argv $3 = (char **) 0xbffffd04 (gdb) |
Interesting results; variable 'a' is a pretty weird number, argc looks normal, and argv is apparently useless hex gobbledegook. Lets start from the top;
Variable a was never given a value. C programming language says that the results of any variable that has not yet been assigned a value is undefined. In fact, it's not random in any way, though it may appear that way because of the number of factors that affect it.
So, this large number is okay; it's probably based on the memory address of this variable in the data segment of this program in memory. Maybe not! In anycase though, it's acceptable.
Argc is the easy one. It gives a value that not only makes sense, it's right too. Argc is the number of arguments provided to the program, in this case 1. Remember in C, the first argument is the name of the program being run. No problem.
Now, argv is a different story. It looks like utter nonsense, until you remember that argv is a a pointer to an array of pointers. Gdb even tries to help you out here, and gives you that information; (char **). In gdb, the default representation of a pointer, or any memory address for that matter, is a hex string. It seems reasonable then, to have a value of 0xbffffd04. That's not the end of it though, what we really want to do is look through the argv array, and the good news is that we do this just like we would in C. Remember how I said that variables in gdb are evaluated in the same way they are as C code? What would I do if I wanted to see the value of argv[0] in C? Ooops. I just gave away the answer!
(gdb) print argv[0] $4 = 0xbffffe0d "/home/dughi/gdbexamples/gdbex1" (gdb) |
No big secret here, it's got our first argument alright. Everything is peachy. This C evaluation works for other things too; for example, casting a pointer to a data structure type (if you have a void *), or accessing structure members via . or -> controls. You can even perform mathematical operations:
(gdb) print argc + argc $5 = 2 (gdb) |
Now, the more astute of you may wonder what those little $'s are doing to the left of the output lines. They are better than just line counters; they're temporary variables that hold the results of those print statements, for easy later reference. You can use them just like you can use any other variables too.
(gdb) print $3[0] $6 = 0xbffffe0d "/home/dughi/gdbexamples/gdbex1" (gdb) |
There's one more thing you can do with this command, if you prefix the command with a /x where x is a *printf family formatting option (like f,d,s,c,x, etc), it will print out in that format. This can be useful if you're printing out a memory address that you suspect may have been overwritten - you can usually extract strings and chars from it, but this is a bit too advanced to easily cover, so we'll leave that up to you to figure on your own.
![]() |
Index | ![]() |
6.4.1 Examining the Program | 6.4.3 Examining the Stack |