Terminal command-line debug of iOS Simulator apps? - ios

Is it possible to use GDB or LLDB from the Terminal command-line under Mac OS X to debug apps running on the iOS Simulator? (e.g. not from within Xcode's GUI or console, but using an external command-line or process). If so, how?

You'll need to have the app already in the simulator's Springboard; you can't launch the app in the simulator from Xcode and then expect to be able to have a command line instance of gdb attach to it.
So:
Run the iOS Simulator, having already gotten your app into it.
In a terminal window:
% gdb
...
(gdb) attach --waitfor 'Name Of Your App'
Launch your app from the simulator Springboard.
gdb should attach to the process before main() is executed. So you can set some breakpoints, or whatever. Then:
(gdb) continue
The procedure for lldb is similar:
% lldb
(lldb) process attach -n 'Name Of Your App' --waitfor
<launch your app in the simulator>
(lldb) continue
I am not sure why you'd want or need to do this, but as an old command line gdb (and dbx) guy, I can appreciate it. :-)

Related

Attach lldb to iPhone from command line

While attaching lldb to iPhone works from within Xcode. It is flaky at times. I wonder if it is because XCode has some additional checks before attaching to a process in iPhone. I'd like to debug an iPhone app from command line. lldb has some tutorial that suggest using platform select but that command is unsuccessful in even connecting.
$ (lldb) platform select remote-ios
Platform: remote-ios
Connected: no
Am I missing something?
This question has been asked earlier but has no correct answers:
Debugging iPhone App from Command Line
Attach LLDB to app on iPhone connected to computer - Command line
Probably the easiest way to do this is using ios-deploy e.g.
ios-deploy --bundle=<bundle.app> --debug
The above command will install your application bundle to the attached iOS device, and launch it with an lldb session connected.

Viewing console statements from command line in iOS React Native app?

When running an app in the Android Emulator, I can type adb logcat *:S ReactNative:V ReactNativeJS:V to view the console statements in a terminal, which is much faster than viewing them in Chrome or other debugger.
Is there a similar command in macOS to view the console statements of an app running in iOS simulator?
You can just stop the debugger and look for logs in Xcode or you can call this in terminal:
react-native log-ios
Source

Corona SDK and lldb

Whether it possible to debug corona SDK application on iOS simulator?
I build a Corona SDK application for Xcode simulator and wrote next commands:
cd /Users/username/Library/Application\ Support/iPhone\ Simulator/7.0.3/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/app-name.app/
lldb
(lldb) attach --name "app-name" --waitfor
Then I run app in simulator and I get the next message from lldb:
error: attach failed: process did not stop (no such process or permission problem?)
Are you logged in to the Mac remotely (ssh or whatever)? You need to authorize debugging the first time you use the debugger. It's easy to verify if your machine is set up to allow debugging:
% /usr/sbin/DevToolsSecurity -status
Developer mode is currently enabled.
If it's not enabled, I think sudo /usr/sbin/DevToolsSecurity -enable may do the trick. If you're debugging locally, you should see a pop-up window asking you if you want to allow the debugging to happen. But when logged in remotely, you won't see that pop-up of course.

unable to run scripts on simulator from cron tab

We are using sikuli tool for mobile automation on our MAC OSX 10.7.3
I have written a shell script to open simulator and to run scripts on simulator
/Users/ezprintsqa/Downloads/FoneMonkey/bin/iphonesim launch /Users/ezprintsqa/Documents/Xcode/DerivedData/SnapCards- btmjdowsgmixpvdyfxfvhxtoihab/Build/Products/Debug-iphonesimulator/SnapCardsFoneMonkey.app&
/Users/ezprintsqa/Downloads/Sikuli-IDE.app/sikuli-ide.sh -r /Users/ezprintsqa/Downloads/SikuliWorkSpace/SnapcardsTestSuite1.sikuli
When I execute from command line I can see simulator opening and scripts getting executed on simulator.
But when I do same thing using cron tab I can see only simulator opening but the tool can't identify the simulator now to run scripts, the tool throws error message that it is unable to identify the simulator.
When the tool is able to identify the simulator while running from command prompt why is it unable to identify while running from crontab?
Thanks
You are saying:
the tool throws error message that it is unable to identify the simulator.
Do I understand right, that the script throws a FindFailed exception, meaning, that some image (might be the visual identification of the simulator window) is not found on the screen within the standard waiting time of 3 seconds?
I this is the case, you should give more time to the simulator to start up in the crontab case, either by adding a wait(n) at the beginning of the Sikuli script or use wait(image, n) to wait longer for the image to appear

How can I get the console logs from the iOS Simulator?

I want to see what happens in the iOS Simulator if I'm not testing the app in Xcode.
For example, if I open a link in the Safari simulator, see what happens in the console,
or if I install a web-app, see the links that I'm pressing in console.
How can I do this?
I want to see it in Xcode or Terminal, but it's not a problem if I need to use another bit of software.
iOS Simulator > Menu Bar > Debug > Open System Log
Old ways:
iOS Simulator prints its logs directly to stdout, so you can see the logs mixed up with system logs.
Open the Terminal and type: tail -f /var/log/system.log
Then run the simulator.
EDIT:
This stopped working on Mavericks/Xcode 5. Now you can access the simulator logs in its own folder: ~/Library/Logs/iOS Simulator/<sim-version>/system.log
You can either use the Console.app to see this, or just do a tail (iOS 7.0.3 64 bits for example):
tail -f ~/Library/Logs/iOS\ Simulator/7.0.3-64/system.log
EDIT 2:
They are now located in ~/Library/Logs/CoreSimulator/<simulator-hash>/system.log
tail -f ~/Library/Logs/CoreSimulator/<simulator-hash>/system.log
You can view the console for the iOS Simulator via desktop Safari. It's similar to the way you use desktop Safari to view the console for physical iOS devices.
Whenever the simulator is running and there's a webpage open, there'll be an option under the Develop menu in desktop safari that lets you see the iOS simulator console:
Develop -> iPhone Simulator -> site name
There's an option in the Simulator to open the console
Debug > Open System Log
or use the
keyboard shortcut: ⌘/
iOS 8 and iOS 9
Under iOS 8 and iOS 9 this location is now:
~/Library/Logs/CoreSimulator/<DEVICE_CODE>
So, the following will work:
tail -f ~/Library/Logs/CoreSimulator/<DEVICE_CODE>/system.log
The DEVICE_CODE value can be found via the following terminal command:
instruments -s devices
You should not rely on instruments -s. The officially supported tool for working with Simulators from the command line is xcrun simctl.
The log directory for a device can be found with xcrun simctl getenv booted SIMULATOR_LOG_ROOT. This will always be correct even if the location changes.
Now that things are moving to os_log it is easier to open Console.app on the host Mac. Booted simulators should show up as a log source on the left, just like physical devices. You can also run log commands in the booted simulator:
# os_log equivalent of tail -f
xcrun simctl spawn booted log stream --level=debug
# filter log output
xcrun simctl spawn booted log stream --predicate 'processImagePath endswith "myapp"'
xcrun simctl spawn booted log stream --predicate 'eventMessage contains "error" and messageType == info'
# a log dump that Console.app can open
xcrun simctl spawn booted log collect
# open location where log collect will write the dump
cd `xcrun simctl getenv booted SIMULATOR_SHARED_RESOURCES_DIRECTORY`
If you want to use Safari Developer tools (including the JS console) with a webpage in the Simulator: Start one of the simulators, open Safari, then go to Safari on your mac and you should see Simulator in the menu.
You can open a URL in the Simulator by dragging it from the Safari address bar and dropping on the Simulator window. You can also use xcrun simctl openurl booted <url>.
[iOS Logger]
You can use the Console application(select your device in Devices) on your Mac to see a log message that were sent using NSLog, os_log, Logger (you will not see logs from print function).
Also please check (Action -> Include <Info/Debug> Messages)
Please note that if you want to see a log from WebView(UIWebView or WKWebView) you should use Safary -> Develop -> device
[Find crash log]
If you are using Swift, remember that println will only print to the debug log (which appears in xCode's debug area). If you want to print to system.log, you have to use NSLog as in the old days.
Then you can view the simulator log via its menu, Debug > Open System Log... (cmd + /)
tailing /var/log/system.log didn't work for me. I found my logs by using Console.app. They were in
~/Library/Logs/iOS Simulator/{version}/system.log
XCode > 6.0 AND iOS > 8.0
The below script works if you have XCode version > 8.0
I use the below small Script to tail the simulator logs onto the system console.
#!/bin/sh
sim_dir=`xcrun instruments -s | grep "iPhone 6 (8.2 Simulator)" | awk {'print $NF'} | tr -d '[]'`
tail -f ~/Library/Logs/CoreSimulator/$sim_dir/system.log
You can pass in the simulator type used in the Grep as an argument. As mentioned in the above posts, there are simctl and instruments command to view the type of simulators available for use depending on the Xcode version.
To View the list of available devices/simulators.
xcrun instruments -s
OR
xcrun simctl list
Now you can pass in the Device code OR Simulator type as an argument to the script and replace the "iPhone 6 (8.2 Simulator)" inside grep to be $1
You can see the Simulator console window, including Safari Web Inspector and all the Web Development Tools by using the Safari Technology Preview app.
Open your page in Safari on the Simulator and then go to Safari Technology Preview > Develop > Simulator.
In Xcode:
View->Debug Area->Activate Console
I can open the log directly via the iOS simulator: Debug -> Open System Log... Not sure when this was introduced, so it might not be available for earlier versions.
No NSLog or print content will write to system.log, which can be open by Select Simulator -> Debug -> Open System log on Xcode 11.
I figure out a way, write logs into a file and open the xx.log with Terminal.app.Then the logs will present in Terminal.app lively.
I use CocoaLumberjack achieve this.
STEP 1:
Add DDFileLogger DDOSLogger and print logs path. config() should be called when App lunch.
static func config() {
#if DEBUG
DDLog.add(DDOSLogger.sharedInstance) // Uses os_log
let fileLogger: DDFileLogger = DDFileLogger() // File Logger
fileLogger.rollingFrequency = 60 * 60 * 24 // 24 hours
fileLogger.logFileManager.maximumNumberOfLogFiles = 7
DDLog.add(fileLogger)
DDLogInfo("DEBUG LOG PATH: " + (fileLogger.currentLogFileInfo?.filePath ?? ""))
#endif
}
STEP 2:
Replace print or NSLog with DDLogXXX.
STEP 3:
$ tail -f {path of log}
Here, message will present in Terminal.app lively.
One thing more. If there is no any message log out, make sure Environment Variables -> OS_ACTIVITY_MODE ISNOT disable.
Download the safari technology review. With the simulator running, select develop > simulator > localhost
The easiest way to see the console logs is by adding consolelogs as a param:
ionic cordova run ios -l --consolelogs
And you'll able to see the console logs in your terminal.

Resources