I'm trying to figure out how to use the log or debug commands in adding actions to a breakpoint. I can't seem to figure it out. For something like this:
double currentZoom = [self getZoomScale];
How do I print out the currentZoom? I tried using log as my action, and then doing
currentZoom: #(double)currentZoom# // this didn't work
currentZoom: #(double)[self getZoomScale]# // also didn't work
Can someone help me out with this and any other info I may need to log information with breakpoints?
And also a simple example for po an object. Does po always po the description (as in you have to have overridden the description method? Thanks.
If you want to print your double with NSLog, add the following line:
NSLog(#"%f", currentZoom);
Now, if you want to use the debugger console...
If you want to print currentZoom in the console, you don't need po. Plain p would be enough.This is, type
p currentZoom
and it's going to show you currentZoom's value. po is for objects. Let's say you wrap currentZoom in an NSNumber.
NSNumber currentZoomNumber = [NSNumber numberWithDouble:currentZoom];
Then, to print the value, you would have to do
po currentZoomNumber
Like i said, po is to print objects, it means print object. So you can use it to print any type of object, from NSStrings and NSNumbers to NSDictionaries and NSManagedObjects.
You can make the breakpoint execute debugger commands.
Open the edit breakpoint pane:
.
Then type a debugger command (selecting "Automatically continue after evaluating" is advised).
To insert some context around the automated debugger commands, you can add another debugger action of type "Log Message". The "Log Message" action is not capable of inspecting variables itself.
NSLog() is probably easier to implement, but requires you to change the code you are debugging.
I do not know how to add actions to a breakpoint. (I am interested in seeing it in any other answers offered)
The gdb syntax for printing objects is
po objectName
The gdb syntax for printing C variables is
print (int) intNum
print (float) floatNum
Related
I'm unable to understand how the mock-debugger extension controls where the next step is.
For example what if I'd like to step 2 lines if I find the word "banana" in my text? Also, I'd like to do something, like "Step In", where I can walk word-by-word - is it possible?
I've seen the this._currentLine = ln; assign, which looks like it controls where the line is, but it's just a simple local variable. How could it ever control anything in the debugger? I can't find any other uses of the _currentLine varbiable where it passes to anything useful API (except for stack tracing, but I don't think it has any relation with the debugger line-control).
The stack trace is the only source for the debugger step visualization. When the debugger gets a notification to pause it requests the current stack trace. The TOS determines where the next execution point will be located. Hence the debug adapter is reponsible to determine this position precisely.
I have been googling from last couple of hours for finding that is there any way to clear NSLog output using code or not?
Like we have clrscr() in c. So if we are trying to print something which we want to focus most and there is lots of log printin there we can put that code there and get keep our desire log on top for easy searching. This can be done by putting breakpoint on my NSLog line and than click on clear console. but question is is there a way to achive this programatically?
I found few question on stack overflow but I din't satisfied with answer like this is saying that I can disable log for release mode etc.
Or I can use DLog, ALog or ULog as requirement but my question is different..
Any one can help me in this?
Thanks in advance :)
You can use a conditional breakpoint to simulate it. Define a function like this in your code:
int clear_console()
{
NSLog(#"\n\n\n\n\n\n\n\n");
}
Then, when you want to clear the console just add a breakpoint before the NSLog with this condition:
Condition: 1 > 0
Action: Debugger Command expr (int) clear_console()
Options: Automatically continue after evaluating Check it to skip the pause.
Tested with Xcode 4.3.2 and lldb.
Previous answer:
AFAIK, no, there isn't.
Just in case you're not doing it yet, you can create custom macros to format the output to highlight what you want.
Define macros like this:
#define CLEAR(...) NSLog(#"\n\n\n\n\n\n") /* enough \n to "clear" the console */
#define WTF(...) CLEAR();NSLog(#"!!!!!!!!!!!!!!");NSLog(__VA_ARGS__)
#define TRACE(__message__) NSLog(#">>>>>>>>>>>>>>> %# <<<<<<<<<<<<<<<<<<<", __message__)
Then:
WTF(#"This should't be here object: %#", theObject);
...
TRACE(#"Start Encoding");
...
It's not what you want but it pretty much solves the problem. You'll end up with your own set of macros with custom prefixes easily scannable in the console output.
that mean makeUrlforGetBusinessIDLocal is NSString *
I want to know the value of that. without NSLog
how can I know that value? the value is http://isi............
if I do right click and choose edit value, I just can see "0x754cf40"
as we know "0x754cf40" is memory address. I don't want to know it, but the value of that memory address. how can I do that?
Can I do that without NSLog?
You may not be aware that you can type commands into the debugger console to inspect memory locations.
So, if the pointer you want to view is at address 0x754cf40, you would just type:
po [0x754cf40 description]
..into the console. The console is to the right of the variable panel you screen captured above.
Is this possible?
To make a concrete example, consider the following macro:
define pos
po ([self $arg0])
end
So now if I input pos text, it gets turned into po [self text]. But with multiple arguments, it fails, e.g. pos textLabel text gets turned into po [self textLabel] rather than the desired po [[self textLabel]text].
For another example, just as the three commands
po someIvar_
po [self someMethod]
po [[self someMethod]someOtherMethod]
print out the descriptions of the three objects referenced, it would be great to define a macro pi that does the same thing for integers, i.e.
pi [self someMethod]
is the same as calling
print (int)[self someMethod],
and similarly for
pi [[self someMethod]someOtherMethod].
user-defined commands are just a bit more than string-replaces before executing. Your example pos textLabel text passes two parameters to a command which only considers one parameter. The second is thrown away. It should result in po [self textLabel] before executing. What you do is comparable to the following java-sum-function: int sum(int[] args){return args.get(0);}
what you need is sth mentioned here :
define pos
if $argc == 1
po [self $arg0]
end
if $argc == 2
po [[self $arg0] $arg1]
end
..... (as many you need)
end
I have not found any kind of a loop. So this should be the only way to do this except it is possible to to pop arg0 out of args and recursive-calls of user-defined-commands are allowed. But I think its easier to complete the example above.
pi should be implemented the same way
define pi
if $argc == 1
print (int)[self $arg0]
end
if $argc == 2
print (int)[[self $arg0] $arg1]
end
....
end
maybe there is a better solution but this brings you a step further towards your destination.
If you are trying to use
po
pi
command in terminal to see the value of an object or you want to change the value at runtime so here is the solution:
Now here is the best solution to debug and analyze the objective c code in XCode 4. If you want to change the value of a variable at runtime you can change very easily by just Click "Run > Show > Expressions...
and there you can provide value to your variable name.
But I suspect that your problem is not the Objective-C problem, but still I did my best to provide the solution.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Do I need to disable NSLog before release Application?
I wonder if someone could help me setup a number of NSLog statements so they print to console when executing in "Debug Mode" but don't print in "Release Mode". I understand I need to add something like DEBUG = 1 to the debug config in Xcode but I can't find where. Also how do I utilise this in my code?
NSLog(#"Print Always");
if(DEBUG) NSLog(#"Print only in debug");
Is there a simple way of doing this?
EDIT_001:
I tried following this but the keys now seem to be only listed under "All Settings", and are presenting as nice names. The one I should be using is GCC_PREPROCESSOR_DEFINITIONS, so I needed to find "Preprocessor Macros", select edit and add DEBUG=1
When you come to use this its simply a case of adding (see below) or some marco to remove the messy #ifdef / #endif tags.
NSLog(#"You always see me?");
#ifdef DEBUG
NSLog(#"Only in DEBUG");
#endif
This is a popular solution:
http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog
See comments about using either -DDEBUG=1 or DEBUG=1.
The best solution is not to use NSLog in the first place but instead rely on the debugger.
You can set breakpoints that execute debugger commands to print out anything and you can set the breakpoints to execute the debugger commands but not to stop execution. In practice this works just like NSLog.
By using the debugger to do the logging, you don't have to worry about removing the log statements.
Please have a look at the answers of How to print out the method name and line number and conditionally disable NSLog?. There are some nice macros in there that can be very useful.
I use this:
-(void)debugWinLog
{
NSUserDefaults * defaultsDebug = [NSUserDefaults standardUserDefaults];
theDebugWin = [defaultsDebug boolForKey:#"logger"];
}
Which is called in the awakeFromNib.
It checks the apps plist file for a 1 or 0 for the BOOL entry "logger"
The normal state if off, but when debugging you can then turn it on or off at will in terminal. with the normal defaults write.
The NSlog statments look like:
if ( theDebugWin) {NSLog (#"%#", windowState );}