Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print the string a pointer points to while debugging using GDB?

How do I inspect a string a pointer is pointing to when stepping through a program using GDB?

I can see the a pointer is pointing to 0x82c6e10. I know it is a string. How do I print it?

Using printf("%s\n", 0x82c6e10) gives Bad format string, missing '"'. The fact that gdb does not complain of unknown command tells me that the solution is some variation of what I am doing. Am I right? I tried escaping the quotes but that did not help.

like image 777
341008 Avatar asked Nov 24 '10 08:11

341008


People also ask

What is print command in gdb?

The usual way to examine data in your program is with the print command (abbreviated p ), or its synonym inspect . It evaluates and prints the value of an expression of the language your program is written in (see section Using GDB with Different Languages).

Which command in gdb is used to find the type of variable?

The ptype [ARG] command will print the type. Show activity on this post.

How do you call a function in gdb?

To execute one line of code, type "step" or "s". If the line to be executed is a function call, gdb will step into that function and start executing its code one line at a time. If you want to execute the entire function with one keypress, type "next" or "n".

How do I set variables in gdb?

Use the set variable (gdb) and the assign (dbx) commands to change the value associated with a variable, memory address, or expression that is accessible according to the scope and visibility rules of the language. The expression can be any expression that is valid in the current context.


2 Answers

Use x rather than p:

x /s 0x82c6e10
like image 54
Paul R Avatar answered Sep 24 '22 16:09

Paul R


Try:

print (char *)pointer
like image 21
Jacek Konieczny Avatar answered Sep 24 '22 16:09

Jacek Konieczny