Check in xcode where is the value of a variable in memory

Discussion in 'Mac Programming' started by alexandre16, Nov 14, 2012.

  1. macrumors member

    Joined:
    Jan 21, 2011
    #1
    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.
     
  2. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #2
    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.
     
  3. thread starter macrumors member

    Joined:
    Jan 21, 2011
    #3
    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
     
  4. macrumors 6502a

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #4
    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.
     
  5. alexandre16, Nov 14, 2012
    Last edited by a moderator: Nov 14, 2012

    thread starter macrumors member

    Joined:
    Jan 21, 2011
    #5
    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;
    }
    
     
  6. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    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.
     
  7. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #7
    Which versions of Mac OS and Xcode are you running?
     
  8. thread starter macrumors member

    Joined:
    Jan 21, 2011
    #8
    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?
     
  9. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #9
    Do you see a pane with the debugger variables below the program source?
     
  10. thread starter macrumors member

    Joined:
    Jan 21, 2011
    #10
    in this pane i only have the three things that i said above, dont have variables.
     
  11. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #11
    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.
     
  12. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #12
    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.
     
  13. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #13
    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?
     
  14. thread starter macrumors member

    Joined:
    Jan 21, 2011
    #14
    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
     
  15. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #15
    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!
     
  16. thread starter macrumors member

    Joined:
    Jan 21, 2011
    #16
    Yeh i can use messages, anyone can help?
     
  17. macrumors newbie

    Joined:
    Nov 7, 2012
    #17
    About printing the variable address, other than printf you can do it with gdb: "print &Var".
     
  18. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #18
    Have you tried to help yourself based on my previous post above?
     
  19. thread starter macrumors member

    Joined:
    Jan 21, 2011
    #19
  20. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #20
    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.
     
  21. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #21
    n/m, that may not even be the OPs code.
     
  22. thread starter macrumors member

    Joined:
    Jan 21, 2011
    #22
    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?
     
  23. macrumors 603

    Joined:
    Aug 9, 2009
    #23
    Read the links given here:
    http://forums.macrumors.com/showpost.php?p=15977842
     
  24. thread starter macrumors member

    Joined:
    Jan 21, 2011
    #24

    Attached Files:

  25. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #25
    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.
     

Share This Page