Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 14, 2012, 10:02 AM   #1
alexandre16
macrumors member
 
Join Date: Jan 2011
Check in xcode where is the value of a variable in memory

Hi developers.

I am using xcode to build some simples command line programs, and now i want to check where is the value that i insert to a variable, i am writing in C language.

Exemple:

scanf("%d", &Var);

well, i run the program and insert the value, then i pause the program executation and go to Product -> Debug -> View memory and then i dont know what address i need to choose to see my value of the variable, anyone can give me some help with that?

Thanks people.
alexandre16 is offline   0 Reply With Quote
Old Nov 14, 2012, 10:17 AM   #2
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
You should just set a breakpoint at that line, step over it, and in the debugger you will see the new value for 'Var'.

You could obviously printf out the variable, and its address (&Var) too.
jared_kipe is offline   0 Reply With Quote
Old Nov 14, 2012, 02:51 PM   #3
alexandre16
Thread Starter
macrumors member
 
Join Date: Jan 2011
Quote:
Originally Posted by jared_kipe View Post
You should just set a breakpoint at that line, step over it, and in the debugger you will see the new value for 'Var'.

You could obviously printf out the variable, and its address (&Var) too.
Yes i am using the breakpoint but my problem is to find the right address of the variable, because i only want to know in the memory, where is the value of my variable, and i will see it using the address, and then put this address in Product -> Debug -> View memory, i put the code and see some hexadecimals codes and the correspondent value of this hexadecimal codes, but i dont see the value that i put in my variable...maybe because the address that i use isnt the right address.

How can i get the right address of my variable?

Thanks for the answer
alexandre16 is offline   0 Reply With Quote
Old Nov 14, 2012, 03:32 PM   #4
mfram
macrumors 6502a
 
Join Date: Jan 2010
Location: San Diego, CA USA
Is your variable on the variable list? If so, select it. Right click. One of the options is View Memory of "Var". It will bring up the memory viewer at the correct location.

Otherwise, you can print it out with printf.

Code:
printf("%p\n", &Var);
Put the value into the memory viewer.
mfram is offline   0 Reply With Quote
Old Nov 14, 2012, 04:09 PM   #5
alexandre16
Thread Starter
macrumors member
 
Join Date: Jan 2011
Quote:
Originally Posted by mfram View Post
Is your variable on the variable list? If so, select it. Right click. One of the options is View Memory of "Var". It will bring up the memory viewer at the correct location.

Otherwise, you can print it out with printf.

Code:
printf("%p\n", &Var);
Put the value into the memory viewer.
it is the problem, my var isnt in the list...

i am using this program...


Code:
#include <stdio.h>

int seconds_from_time(int h, int m, int d)
{
    return( h * 3600 + m * 60 + d);
}

void exercise_1_1 (void)
{
    int h;
    int m;
    int d;
    while (scanf ("%d%d%d", &h, &m, &d) != EOF)
    {
        int z = seconds_from_time (h, m, d);
        printf ("%d\n", z);
    }
}

int main()
{
    exercise_1_1();
    return 0;
}

Last edited by grapes911; Nov 14, 2012 at 06:26 PM. Reason: added code tags to make it more readable
alexandre16 is offline   0 Reply With Quote
Old Nov 14, 2012, 04:15 PM   #6
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by alexandre16 View Post
Yes i am using the breakpoint but my problem is to find the right address of the variable, because i only want to know in the memory, where is the value of my variable, and i will see it using the address, and then put this address in Product -> Debug -> View memory, i put the code and see some hexadecimals codes and the correspondent value of this hexadecimal codes, but i dont see the value that i put in my variable...maybe because the address that i use isnt the right address.

How can i get the right address of my variable?

Thanks for the answer
You are trying to make this very, very painful for yourself. Trying to use "View memory" to see the contents of a variable is just daft. Set a breakpoint where the variable is read. Run the program. When Xcode stops at the breakpoint, there is a view at the bottom of the Xcode window showing you all the variables of that function with their values.

"Pause" isn't very helpful because by the time you press "Pause" your program has been running on executing about 2 billion instructions per second, so you can probably see on the left side in the call stack that it is in the middle of the scanf function, waiting for your input. Use a breakpoint.
gnasher729 is offline   0 Reply With Quote
Old Nov 14, 2012, 04:21 PM   #7
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
Which versions of Mac OS and Xcode are you running?
lloyddean is offline   0 Reply With Quote
Old Nov 14, 2012, 04:37 PM   #8
alexandre16
Thread Starter
macrumors member
 
Join Date: Jan 2011
Quote:
Originally Posted by lloyddean View Post
Which versions of Mac OS and Xcode are you running?
I am using the last version of OSX and Xcode.

i am using the lldb debugger.

i am using the breakpoint at the line of scanf, using the code that i posted above, when i run the the code and it stop at the breakpoint i have 3 things in the debugger navigation, exercise_1_1, main and start.

What i am doing wrong?
alexandre16 is offline   0 Reply With Quote
Old Nov 14, 2012, 05:35 PM   #9
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
Do you see a pane with the debugger variables below the program source?
lloyddean is offline   0 Reply With Quote
Old Nov 14, 2012, 05:46 PM   #10
alexandre16
Thread Starter
macrumors member
 
Join Date: Jan 2011
Quote:
Originally Posted by lloyddean View Post
Do you see a pane with the debugger variables below the program source?
in this pane i only have the three things that i said above, dont have variables.
alexandre16 is offline   0 Reply With Quote
Old Nov 14, 2012, 05:53 PM   #11
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
That's the "stack trace" pane off to the left side of the "source" pane.

So that's a big "no" on my question.

At the top right of the window above the "source" pane is a collection of buttons. One of which is called "View". Select the center button. A "debug" pane should appear "BELOW" your projects source.

All the above is accurate assuming you haven't done any custom setup.
lloyddean is offline   0 Reply With Quote
Old Nov 14, 2012, 07:18 PM   #12
firewood
macrumors 603
 
Join Date: Jul 2003
Location: Silicon Valley
To find the address of variable foo, you can just type:

Code:
print &foo
in Xcode's debug console, after hitting some breakpoint or stopping the app with that variable in scope. Then you can use:

Code:
me r -b <start_address> <end_address>
to dump out hex bytes at that (or any other nearby) address.
firewood is offline   0 Reply With Quote
Old Nov 14, 2012, 08:59 PM   #13
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by alexandre16 View Post
I am using the last version of OSX and Xcode.

i am using the lldb debugger.

i am using the breakpoint at the line of scanf, using the code that i posted above, when i run the the code and it stop at the breakpoint i have 3 things in the debugger navigation, exercise_1_1, main and start.

What i am doing wrong?
What you are doing wrong is that you haven't learned what all the little clickety icons on the screen are there for and what all the menu items are doing.

Can you tell us what the "Show Toolbar" or "Hide Toolbar" menu does? That's the first step. Next, what do the three icons with "View" under them do? That's the next step. What do the three little icons to the right of the "Clear" button do?
gnasher729 is offline   0 Reply With Quote
Old Nov 15, 2012, 08:34 AM   #14
alexandre16
Thread Starter
macrumors member
 
Join Date: Jan 2011
Quote:
Originally Posted by gnasher729 View Post
What you are doing wrong is that you haven't learned what all the little clickety icons on the screen are there for and what all the menu items are doing.

Can you tell us what the "Show Toolbar" or "Hide Toolbar" menu does? That's the first step. Next, what do the three icons with "View" under them do? That's the next step. What do the three little icons to the right of the "Clear" button do?
i am not an expert in xcode because i start using it maybe 1 month ago, but until this problem everything is ok, anyone can talk with me by skype? it will resolve the problem in 1 minute because i can show you some pictures....

Thanks
alexandre16 is offline   0 Reply With Quote
Old Nov 15, 2012, 12:37 PM   #15
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
Why Skype when there a native solutions already a part of your "last version of OSX" - Messages.

What you haven't clearly made apparent yet is if you've got it figured out yet or not!
lloyddean is offline   0 Reply With Quote
Old Nov 16, 2012, 04:33 PM   #16
alexandre16
Thread Starter
macrumors member
 
Join Date: Jan 2011
Quote:
Originally Posted by lloyddean View Post
Why Skype when there a native solutions already a part of your "last version of OSX" - Messages.

What you haven't clearly made apparent yet is if you've got it figured out yet or not!
Yeh i can use messages, anyone can help?
alexandre16 is offline   0 Reply With Quote
Old Nov 16, 2012, 06:25 PM   #17
ramy1989
macrumors newbie
 
Join Date: Nov 2012
About printing the variable address, other than printf you can do it with gdb: "print &Var".
ramy1989 is offline   0 Reply With Quote
Old Nov 16, 2012, 07:11 PM   #18
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
Have you tried to help yourself based on my previous post above?
lloyddean is offline   0 Reply With Quote
Old Nov 17, 2012, 06:23 PM   #19
alexandre16
Thread Starter
macrumors member
 
Join Date: Jan 2011
Quote:
Originally Posted by lloyddean View Post
Have you tried to help yourself based on my previous post above?
yes.
i want to see the values of my variables in "stack trace", i already know how to go to stack trace using the address, but i cant find the right address of a variable, if i have this:
http://bencollier.net/wp-content/upl...t-22.36.27.png
it will be very easy.
alexandre16 is offline   0 Reply With Quote
Old Nov 17, 2012, 10:45 PM   #20
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
Are we both speaking english or is it just me.

I see your variables "below" the "source pane" in that image.

Perhaps you could say explicitly what it is you need to see that isn't in that image.

If you're looking for the addresses of 'argc', 'argv' and 'retVal' then 'argc' and 'argv' will most likely be in 'registers', which don't have an address and 'retVal' may've been mapped directly to the 'register' in which function results are returned bypassing the stack altogether.
lloyddean is offline   0 Reply With Quote
Old Nov 17, 2012, 11:35 PM   #21
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
n/m, that may not even be the OPs code.
lee1210 is offline   0 Reply With Quote
Old Nov 18, 2012, 05:08 AM   #22
alexandre16
Thread Starter
macrumors member
 
Join Date: Jan 2011
Quote:
Originally Posted by lloyddean View Post
Are we both speaking english or is it just me.

I see your variables "below" the "source pane" in that image.

Perhaps you could say explicitly what it is you need to see that isn't in that image.

If you're looking for the addresses of 'argc', 'argv' and 'retVal' then 'argc' and 'argv' will most likely be in 'registers', which don't have an address and 'retVal' may've been mapped directly to the 'register' in which function results are returned bypassing the stack altogether.
this image is not mine, i said if i have this image it will be easy to find the values of variables in the stack strace, but i dont have this.

I posted a simple code above, and i want to share a simple image to show you, what i have in my xcode.

Can i send you a image via email, or other way?
alexandre16 is offline   0 Reply With Quote
Old Nov 18, 2012, 10:28 AM   #23
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by alexandre16 View Post
I posted a simple code above, and i want to share a simple image to show you, what i have in my xcode.

Can i send you a image via email, or other way?
Read the links given here:
http://forums.macrumors.com/showpost.php?p=15977842
chown33 is offline   0 Reply With Quote
Old Nov 18, 2012, 11:18 AM   #24
alexandre16
Thread Starter
macrumors member
 
Join Date: Jan 2011
Quote:
Originally Posted by chown33 View Post
Thanks for the links.

Now i need to check in the stack trace, the values of my variables...before and after the breakpoint.
Attached Thumbnails
Click image for larger version

Name:	Captura de ecrã 2012-11-18, às 17.16.17.png
Views:	43
Size:	122.1 KB
ID:	378143   Click image for larger version

Name:	Captura de ecrã 2012-11-18, às 17.16.34.png
Views:	49
Size:	199.5 KB
ID:	378144  
alexandre16 is offline   0 Reply With Quote
Old Nov 18, 2012, 01:03 PM   #25
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
The picture helps.

In the "debug pane" you're only displaying the "console".

See the "clear" button in the "debug pane"? Their's a group of three buttons next to it. Press the center button and your variables sub pane will display what you're wanting to see.
lloyddean is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
How to check log files to date/time system RAM was changed? Any way to check this? asterix2k10 OS X 1 Mar 7, 2014 08:19 PM
How to check video memory usage ngeil OS X 10.8 Mountain Lion 2 Sep 12, 2013 12:45 PM
Moving from xcode 3.2 to xcode 4.5.2 compilation error for 'abs' macro in stdlib.h hrishidev Mac Programming 7 Jun 2, 2013 10:36 AM
Variable into Method how ? mngoose iPhone/iPad Programming 9 Oct 10, 2012 03:59 PM
The I may go to bed, and just check back in tomorrow to check my order status thread lsutigerfan1976 iPhone 0 Sep 14, 2012 10:50 PM

Forum Jump

All times are GMT -5. The time now is 09:30 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC