115

I have an issue I am troubleshooting which occurs very infrequently and doesn't seem to happen when I have things running under Xcode.

Is it possible to run an app normally (i.e. from Springboard) until my issue occurs, and then attach a debugger at that point?

I would prefer to do this without jailbreaking if possible.

pkamb
  • 33,281
  • 23
  • 160
  • 191
Locksleyu
  • 5,192
  • 8
  • 52
  • 77
  • **See also [How to debug an iOS extension (.appex)?](https://stackoverflow.com/questions/33524051/how-to-debug-an-ios-extension-appex) (for a more complete answer)** – Top-Master Sep 28 '21 at 12:06
  • **And: [Xcode's Profiler does not show Symbol names](https://stackoverflow.com/a/43967070/8740349) fix** (which can be caused by Attach approach) – Top-Master Sep 28 '21 at 12:25

5 Answers5

147
  • Attach your device connected your Mac
  • Debug > Attach to Process by PID or Name
  • In the dialog sheet, enter the name of your App as it appears in the Debug navigator when started via Xcode (e.g. Target's name not bundle-id).

If the app is already running, the debugger will attach to the running process. If it isn't running, it will wait for the app to launch and then attach.

Top-Master
  • 7,611
  • 5
  • 39
  • 71
Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • Ole, thanks for the answer. I was looking for a way to do this. When I go to attach the debugger, it looks like it attaches but then I get this message: warning: Unable to read symbols for /var/mobile/Applications/ {GUID}/{APPNAME}.app/{APPNAME} (file not found). When I press the "stop" button in XCode, the app terminates. Looks like it's not finding the debug symbols, though it is being built in debug mode. Any ideas? – Brian Dec 15 '12 at 16:52
  • 4
    I have a problem with the **If it isn't running, it will wait for the app to launch and then attach.** part. It doesn't work. I use the process name to wait for the app to call. I launch the app on the iPhone, but the debugger never realizes the app has started. – Jonny Feb 07 '13 at 07:59
  • 1
    @BrianV -- you need to make sure you're compiling with -ggdb if you want to be able to attach to the process. – itfische May 17 '13 at 18:57
  • 4
    I agree with Jonny; my experience is that the attach works on the simulator, but won't attach to the actual device. – James Moore Feb 25 '14 at 20:24
  • @JamesMoore: Attach worked for me on device just now, no problem. – Ben Jackson Oct 27 '14 at 18:23
  • 4
    NOTE: In Xcode 5.0.1+ it is `Debug > Attach to Process > By Process Identifier (PID) or Name` http://stackoverflow.com/a/20193088/175830 – Jason Axelson Dec 09 '15 at 20:30
  • Looks like they went back to "Debug > Attach to Process by PID or Name..." in Xcode 7.2 – J2N Apr 25 '16 at 16:34
127

I'll leave this here since neither of the other 2 answers gave me quite enough detail without a little bit of a struggle.

  1. Run your app in the simulator and take note of the name in the Debug navigator enter image description here

  2. Plug in your device and don't forget to select your device as the target enter image description here

  3. Debug > Attach to Process > By Process Identifier (PID) or Name enter image description here
  4. Enter the name from step 1 and attach. That should be all you have to do. enter image description here
Adam Johns
  • 35,397
  • 25
  • 123
  • 176
  • 11
    You should have 1000 upvotes. Debugging things that start via a cutom url scheme can be a pain in the ass without this neat trick. Many thanks! – txulu Feb 11 '15 at 16:28
  • 2
    I wish I saw this answer before I spent 20 minutes figuring this out on my own :( – sam_smith Apr 01 '15 at 00:48
  • 9
    This works in that it attaches to the process, but I can't seen to get any log output which is what I really need. Any ideas? – Orlando Aug 08 '15 at 20:39
  • 2
    @Orlando - If you're looking for log output, I highly recommend DeviceConsole: https://github.com/rpetrich/deviceconsole – orion elenzil Oct 08 '15 at 05:46
  • 2
    I did exactly as you suggested. and I get error: `Could not attach to PID` I tried both the name and its ID. I tried while the app is running and while it's stopped.. – mfaani Sep 09 '16 at 20:52
  • Same issue as @Orlando – David Apr 18 '18 at 09:00
49

In Xcode 5.0.1 and 6 it's the menu bar items:

Debug > Attach to Process > By Process Identifier (PID) or Name...
pkamb
  • 33,281
  • 23
  • 160
  • 191
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
8

In Xcode 7 it's just:

Debug > Attach to Process by PID or Name...
pkamb
  • 33,281
  • 23
  • 160
  • 191
Japes
  • 393
  • 5
  • 11
-1

I was able to debug the app by adding a breakpoint on the AppCoordinator file init() method on the super.init() line.

I was able to turn off the wifi/internet and then by pass the developer verification.