115

Anyone has experienced this problem? Yesterday I still can run my app in simulator but now I cannot run my app since Xcode prints this error in console:

error: failed to attach to process ID <ID number>

Screenshot for error

I have tried to reinstall, do this post and create new project but returns no luck. Anyone can resolve this issue?

I use Xcode 4.5 Preview 2 and try to run iOS simulator 6

Community
  • 1
  • 1
Rendy
  • 5,572
  • 15
  • 52
  • 95
  • I think I'm facing exactly the same problem (http://stackoverflow.com/questions/11688720/unit-test-target-doesnt-run-on-xcode-4-5#comment15519878_11688720)... the error given is "failed to attach to process ID 0" – pierroz Jul 30 '12 at 08:01
  • 1
    I have described a possible solution here http://stackoverflow.com/a/12504712/647644 – Lindemann Sep 20 '12 at 00:37
  • Maybe some of the answers here may help? http://stackoverflow.com/questions/9702201/xcode-compiles-my-app-but-cant-run-it-in-the-simulator/12810308#12810308 – Jason Oct 10 '12 at 00:31
  • I gave this answer for the same problem. http://stackoverflow.com/a/13682769/187752 – Kimi Dec 03 '12 at 12:13
  • 1
    I would advise people to try the 'Resetting the content and settings in the simulator' answer before trying the actual accepted answer of changing the debugger. – dartacus Jan 17 '13 at 12:17
  • But the 'Resetting the content and settings in the simulator' answer doesn't work for me. After some times, the error comes again and I need to reset the content again. – Rendy Jan 23 '13 at 04:30
  • My issue was solved by creating a new user account solely for development. My own user account contained the Danish letter Ø. I think that was the problem. – SMC Oct 30 '12 at 19:00
  • Having the same problem, and XCode locks up when it happens -- must be forced to terminate. Resetting the simulator, cleaning, and clearing the project derived data didn't work. (In fact, I now have at least one project that won't even build.) (There have been no changes to system config -- the failure happened between runs a few minutes apart.) – Hot Licks Feb 07 '13 at 18:10
  • (Started working mysteriously, after "playing around" -- trying different projects, etc. But then got some other failures, including a "Posix spawn failed" message during a build. Rebooted and all seems fine (so far). I think maybe my Mac (which hadn't been rebooted in a coon's age) ran out of task handles or some such.) – Hot Licks Feb 07 '13 at 19:44
  • After all this solutions its still not working. Other projects run fine. More solutions? – Mark Molina Feb 15 '13 at 15:42
  • This same question appears 3 times: [__one__](http://stackoverflow.com/questions/12741188/error-failed-to-attach-to-process-id-0) [__two__](http://stackoverflow.com/questions/9702201/xcode-compiles-my-app-but-cant-run-it-in-the-simulator/13682769#13682769) [__three__](http://stackoverflow.com/questions/11535844/failed-to-attach-to-process-id-xcode). There are tons of answers under each, so if this one didn't help you, check the other 2. – bobobobo Apr 21 '13 at 22:04
  • Just quit the xcode and simulator...Restart it again. This worked for me. – Jayprakash Dubey Aug 26 '13 at 07:30
  • just run again that work for me – matinict Feb 23 '16 at 09:21

38 Answers38

154

Resetting the content and settings in the simulator worked for me. This is available in the "iOS Simulator" menu.

Robert Zahm
  • 1,877
  • 2
  • 12
  • 8
  • 1
    Didn't worked for me. Still shows a black screen with status bar (even cleaned the code). After changing debugger to GDB as Imran mentioned, my app loads fine. – user427969 Nov 26 '12 at 04:20
  • 1
    After resetting content and settings, quit the simulator and start it again. Worked like a charm. – justinkoh Jul 03 '13 at 09:54
45

go to the Product menu and find the Edit Scheme menu there.

While in Edit Scheme window, select the "Run" option on the left hand side of the screen and then on the right hand side, change the debugger from LLDB to GDB.

Imran
  • 1,070
  • 16
  • 31
  • 6
    I changed LLDB to GDB, but after that it is always showing like "Attaching to MYApp" and showing the following logs. GNU gdb 6.3.50-20050815 (Apple version gdb-1822) (Sun Aug 5 03:00:42 UTC 2012) Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "x86_64-apple-darwin". – iOS_Developer Sep 22 '12 at 07:04
  • Doesn't work for me either. Same behavior as described by KmlHarariyA. @KmlHarariyA: Do you happen to work as a network user? – Tafkadasoh Oct 01 '12 at 10:16
  • 8
    I was having two simulator (5.1 and 6.0), quit the xcode and reset the contents of both the simulators........it is strange but it worked for me....:-) – iOS_Developer Oct 03 '12 at 03:55
  • 32
    -1. This is a big step backwards. Resetting the contents of the simulator fixes the problem without having to change the debugger. – ozz Oct 23 '12 at 22:51
  • What Imran told doesn't work for me, whereas what KmlHarariyA told works – Yogesh Nov 03 '12 at 14:31
  • 2
    @cdo Resetting content did not work for me, changing the simulator yes – Teofilo Israel Vizcaino Rodrig Nov 20 '12 at 14:48
  • 1
    Work for me. But what happens here? – jeswang Nov 23 '12 at 12:04
  • @TeofiloIsraelVizcainoRodrig It may solve the problem for some people, however it is not an optimal solution in the long run - you'd be giving up all the benefits of using LLDB. – ozz Jan 11 '13 at 02:23
  • 1
    So I had this problem for a long while: "failed to attach to process ID " (ID number != 0) and found out that the problem lay in the chmod of /etc/hosts, see answers of amattn and fundtimer. When I changed this the problem turned to: "failed to attach to process ID 0". Then I reset the simulator and threw away the build folder (with alt key pressed) and the problem was resolved. – Vincent Osinga Jan 17 '13 at 09:09
  • 4
    MIGHT HELP: I too changed to GDB, ran the app once, then turned back to LLDB and it worked again! So you don't have to give up the benefits of LLDB! Give it a try. – Charles Feb 21 '13 at 09:49
  • Also -1. Resetting the simulator is the better option. – Greg M. Krsak Jun 29 '13 at 22:29
  • I fix it just by deleting the app in iOS simulator and run again. – Hlung Jul 08 '13 at 04:56
22

I experienced the same problem, and after a while I found out that it was due to the fact that my XCode project included a Folder Reference to a folder named Resources.

It seems that XCode doesn't like that name.

Just removing the folder reference or renaming the folder solved the issue.

Mirko Luchi
  • 321
  • 3
  • 4
20

TWO METHODS

Method 1 : Inside the folder Application Support -> iPhone Simulator -> In the each simulator version of iOS -> Applications -> Delete all the files present here.

Now go to Products -> Clean

Now run it on any simulator and it will work.

Method 2 :

Tap on Reset

Ambili B Menon
  • 1,249
  • 2
  • 14
  • 26
  • 2
    This is the only method that worked for me. I caused the issue by naming one of my folder Resources and it killed my project, just about had a heart attach. Apple should really do better. – Zack Morris Apr 11 '13 at 18:21
9

Step1: ping

$ ping localhost

This should return something like

PING localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.028 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.048 ms
...

If this works, this answer won't fix your problem, try something else.

If ping return something else for example: ping: cannot resolve localhost: Unknown host something is screwed up with your /etc/hosts file, go to Step 2

Step2: Check /etc/hosts

Check that the top of your /etc/hosts file looks like this

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost 
fe80::1%lo0     localhost

If it doesn't have these entries in the file, enter them at the top of the file, flush the dns cache using $ dscacheutil -flushcache and go back to Step 1, otherwise continue to Step 3.

Step3: Correct File Format: It should be unix or LF *

$ file /etc/hosts

This should return: /etc/hosts: ASCII English text

If it returns something like /etc/hosts: ASCII English text, with CR line terminators then the file is in the wrong format and is likely being ignored.

Change the file line endings to unix or LF using your favorite text editor.

  • In Sublime Text 2 this can be done throught the view menue: View > Line Endings > Unix

Flush the dns cache ($ dscacheutil -flushcache) and go back to step 1

Community
  • 1
  • 1
user160917
  • 9,211
  • 4
  • 53
  • 63
5

Similar to fundtimer's answer, the local hosts file seems to have been the culprit.

I had to change my /etc/hosts permissions back to the default after an inadvertent change:

sudo chmod 644 /etc/hosts

After that, simulator/LLDB worked for me.

amattn
  • 10,045
  • 1
  • 36
  • 33
5

This is an old topic, but I believe things have changed a bit for the latest version of macOS.

I've stumbled on the issue while trying to run an UI test suite for an iOS app in the simulator on macOS Mojave Beta and Xcode 9.4.1.

Turns out, on macOS Mojave the codesign tool (which is a part of the OS itself and not a part of the Command Line Tools or Xcode) signs processes into a hardened runtime environment which includes runtime code signing enforcement, library validation, hard, kill, and debugging restrictions. This, together with the System Integrity Protection in macOS Mojave, prevents the lldb debugger from connecting to the running target.

To resolve this, you have to options:

  1. An obvious one, probably, is just to use Xcode 10. It does seem to handle the connection gracefully.

  2. Reboot the OS into the Recovery Mode and turn off the System Integrity Protection from the Terminal with csrutil disable. The debugger then connects happily.

I've spent quite an amount of time banging my head on this and was able to resolve it all after reading the following article: http://www.lapcatsoftware.com/articles/debugging-mojave.html

Just my two cents. Hope this helps someone.

eploko
  • 5,347
  • 2
  • 28
  • 23
4

I had somehow removed or deleted records from my /etc/hosts file and I got the same error. You should check whether you have in your /etc/hosts file the line "127.0.0.1 localhost". This helped me!

pedrouan
  • 12,762
  • 3
  • 58
  • 74
  • 2
    Thanks. I had this error and tried everything, nothing worked. I checked my hosts file and it was empty except for one line from Avast anti-virus. Avast actually wrote over my previously customized hosts file (WTH?)! I added the line '127.0.0.1 localhost' to the hosts file, saved it. Then I reset the iPhone simulator, did a 'Clean' in Xcode and it started right up the next time I ran the app. No more Avast for me. – Ryan Feb 25 '13 at 08:25
  • Yeah, I don't like avast either – pedrouan Mar 09 '13 at 18:36
  • Took me a while to find out how to get to the hosts folder. From the Finder, select the "Go" menu and choose "Go to folder..." Then enter: /private/etc/hosts.txt – Jonah Apr 19 '13 at 21:25
  • Actually you may need to enter $sudo /etc/hosts – pedrouan May 07 '14 at 20:13
  • I know this is a year's ago answer. But I would like to know if is there a difference between '127.0.0.1 localhost' and '127.0.0.1 serverchat.local'. Should I remove the serverchat.local and replace it with the localhost one? Advice please... – Melvin Lai Jun 23 '14 at 07:09
3

I know this is an old question, but it came up first for me when I did a google search for "failed to attach to process ID". I'm using Xcode 7. Happened after iOS9 was released and I had updated my swift app for swift 2.

Deleting my app in the simulator and doing a new build and run cleared it up.

Christopher Larsen
  • 1,375
  • 15
  • 22
  • 1
    Similar here too, except I just clicked the app IN the simulator and it came up, but then had issues stopping. In the end I just reset the simulator. – MCB Oct 11 '15 at 20:39
2

I've had the same problem when debugging on the device. The app crashed on start but didn't appear as a running process.

My solution is to use some process viewer app (e.g. System Status or similar). Find your stale app and then find the parent process id which usually is debugserver.

Go on Xcode, Product -> Attach to Process -> By process id and attach to the debugserver process PID. Wait a second and then press the Stop button.

The stale debugserver and app processes disappear.

(When using the simulator, the equivalent would be to use the terminal, ps, and kill the App process. Haven't tried that, though).

Ramon Poca
  • 1,889
  • 1
  • 10
  • 19
  • I've run into this bug before and after banging my head against this for the past half hour or so this led to my fix. Thanks a ton man. ' – Gowiem Feb 10 '13 at 22:55
2

I just removed the application from the simulator (just like you would on an actual device), and when I re-ran it everything worked fine.

Nic Foster
  • 2,864
  • 1
  • 27
  • 45
2

I have also same problem and for me the solution is :-

Just delete the app from the Simulator and then clear and Run the project.

May it help also to someone else.

Muhammad Rizwan
  • 3,470
  • 1
  • 27
  • 35
2

I fixed this problem by removing Xcode Derived data contents.

In finder press SHIFT+CMD+G and type below path and remove all data inside DerivedData:

  ~/Library/Developer/Xcode/DerivedData

enter image description here

Also reset simulator builds

     ~/Library/Application Support/iPhone Simulator/6.0/Applications 
Guru
  • 21,652
  • 10
  • 63
  • 102
1

I just posted this on the Developer Forum. It was a simple solution and may help someone here:

I had exactly the same problem: when using any current version of Xcode 4.xx and the LLDB debugger, plus any version of the Simulator, XCode consistently crashed with 'failed to attach to process ...' My only resolution was to switch to the GDB debugger. This was disappointing since I really wanted to try the new, robust LLDB in XCode 4.6-DP.

And then I remembered; I had changed my network/server host file at ...etc/hosts some time ago to accomodate several Apache2 server virtual hosts, i.e., I removed the line:

127.0.0.1 locahost

That was it. Removing that line, XCode crashed on attaching to the Simulator with LLDB. Restoring that line, XCode worked just fine with LLDB and the Simulator.

Anyway, this may not be your problem but it's worth exploring. As far as I'm concerned, it's a serious undocumented bug.

fundtimer
  • 809
  • 7
  • 5
1

In my case the solution was different: the run mode was "Release". Changing to "Debug" fixed it. Xcode 4.5.x had no problem with it.

orip
  • 73,323
  • 21
  • 116
  • 148
1

The issue for me was caused by a pretty stupid mistake: I created a new target for my project by duplicating an existing target and forgot to change the Bundle Identifier in the target's info.plist file.

mwidmann
  • 479
  • 4
  • 10
1

Above answer really helped me. https://stackoverflow.com/a/12819757/1752988 Because in my XCode I used story board and custom view controller. and in its warning list that I saw some of the nib files are missing. I was miss leaded by seeing that. However, I saw this answer, and even though I didn't like to reset my App simulator content, I did it and it really worked for me.

And also Changing the Debugger from Product -> Scheme -> Edit Scheme (Run) from LLDB to GDB didn't work for me either.

P.s.:- Pardon me,I added this answer as a support comment to above, as I don't have 15 reputation upvote the above answer or 50 points of reputation to comment on above.

Community
  • 1
  • 1
Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
1

I solved this problem by quitting XCode and Simulator. Reopen the Project. Done.

Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
1

The following worked for me on iOS 9.1 :

  1. Just uninstall the app from the simulator
  2. Quit the simulator (Cmd+Q)
  3. Clean your Xcode project
  4. Run on simulator

Note: No need to reset the simulator.

Arjun Shukla
  • 337
  • 7
  • 10
1

I am working on Ionic 2 application, I am new to this and even Xcode and simulator.
Initially my app was working on simulator but after some time it start generating error like-
xcode error: failed to attach to process id

I searched for, tried all above answers but my issue was not resolved.
Then I updated (Ionic, npm, cordova) my machine setup-

******************************************************
 Dependency warning - for the CLI to run correctly,      
 it is highly recommended to install/upgrade the following:     

 Install ios-deploy to deploy iOS applications to devices.  `npm install -g ios-deploy` (may require sudo)

******************************************************

Your system information:

Cordova CLI: 6.4.0 
Ionic Framework Version: 2.0.0-beta.10
Ionic CLI Version: 2.1.8
Ionic App Lib Version: 2.1.4
ios-deploy version: Not installed
ios-sim version: 5.0.8 
OS: OS X Yosemite
Node Version: v6.2.2
Xcode version: Xcode 7.2 Build version 7C68



******************************************************

After this I just run a command from my application home directory

  1. ionic run -l
  2. ionic build ios.
  3. imported xcode changes to xcode.
  4. Reset content and setting as-
    enter image description here
  5. clear product from xcode,
    enter image description here
  6. Run Xcode and now it is working on simulator.

Happy to see this!!!

S.Yadav
  • 4,273
  • 3
  • 37
  • 44
1

On Xcode 9+ do the following: Go to the Xcode menu: Product-Scheme - Edit Scheme and under Run tab be sure that the Debug Executable checkbox is checked! That's it!

enter image description here

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
joan
  • 2,407
  • 4
  • 29
  • 35
1

So many answers to this and I didn't see what I believe is the correct one. It's an issue with authorization. Type this in the Terminal:

sudo DevToolsSecurity -enable

crewshin
  • 765
  • 9
  • 22
0

Just reset content and settings of iOS simulator and then clean code by pressing command+shift+k keys to clean code.This worked for me

Desert Rose
  • 3,376
  • 1
  • 30
  • 36
0

As lame as it is, restarting my computer was the only solution that worked. I tried switching from LLDB to GDB, resetting the contents of the Simulator, and restarting Xcode. The only thing that worked was restarting my computer, reopening Xcode, and re-running the app.

Jeff Grimes
  • 2,300
  • 1
  • 23
  • 23
0

I had the same problem. I did some combinations of answers given to make it work. :)

clean the Build Folder by clicking "Product" in the MenuBar. while pressing the Alt-key click on "Clean Build Folder". Now restart your xcode and simulator. Now it should work. if not, also try changing bundle identifier and Simulator "Content Reset" and then restart xcode and simulator.

0

Ran across this today. I accidentally removed my Launch images from my Target Settings.

PWiggin
  • 956
  • 2
  • 12
  • 24
0

try to do next steps:

  • Go to Product/Scheme/Edit Scheme then select Run section.
  • Select Info Tan and set Debugger None.
  • Run project. It must be working (without debugger)
  • Go back to Edit Scheme and selecet debugger LLDB
  • Run project. It is working.

Maybe additionally you need to delete derived data and do Product/Clean

Philip J. Fry
  • 1,165
  • 1
  • 8
  • 10
0

Do you have multiple users logged in to your machine? I have "personal" and "work" accounts on my laptop. I got this error when I was trying to debug from my personal account, but I had left the simulator running in my work account. Quitting the simulator in the other account fixed my problem.

bugloaf
  • 2,890
  • 3
  • 30
  • 49
0

and another one for the record: (none of the suggestions worked for me)

Mountain Lion: XCode 4.6.3

  • I moved the whole Application to Trash and deleted following other folders
  • /Users/[USERNAME]/Library/Developer
  • /Users/[USERNAME]/Library/Caches/com.apple.dt.XCode
  • /Users/[USERNAME]/Library/Application Support/iPhone Simulator

  • reboot System and reinstall XCode

longi
  • 11,104
  • 10
  • 55
  • 89
0

Also had same problem today. i was using Xcode 4.6.3. i closed my project and open it in Xcode 5 and it is running perfect.

Strange but True

Sergio
  • 6,900
  • 5
  • 31
  • 55
Mohit
  • 3,708
  • 2
  • 27
  • 30
0

I had the same problem, in my case the issue was :-

Have set "Build active architectures : NO". Just changed it to YES and it worked.

Ashish
  • 1,899
  • 20
  • 22
0

I know this is old - but the solution for me was the following:

Goto : Targets > Build settings > Linking -> Mach-O Type = Executable

TejAces
  • 551
  • 5
  • 11
0

As @eploko mentioned in his answer in Mojave the debuggability policy changes. This answer is only for those having problems running the test frameworks using Xcode 9.4.1 and Mojave.

According to Apple that's an intended behavior so you have two solutions:

  1. Use Xcode 10 and your problems will disappear.
  2. Disabling the debugger in the scheme editor for your Test target.

To do that select your Test target and then select Edit Scheme. Once there select the Test section in the left and uncheck Debug executable.

enter image description here

More information in this radar. I hope this helps you.

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
0

If you're using Cocoapods, make sure you're building dynamic frameworks by having use_frameworks! in your Podfile.

When I was building static libraries of my Cocoapods dependencies on Xcode 10.2.1 and Cocoapods 1.6.1, I couldn't debug the target because of this "could not attach" error. With use_frameworks!, the error was gone and I could debug again.


Details

In my Podfile, I was building a static framework, which is the default, with a mix of swift and objective-c based pods. I used the :modular_headers => true with one of the pods. My Podfile looked something like this:

swift_version = '5.0'

target 'TargetName' do
  platform :ios, '12.2'

  pod 'IGListKit', :modular_headers => true
  pod 'PromiseKit/CorePromise', '~> 6.0'
end

The above Podfile couldn't be debugged with Xcode 10.2.1, always resulting in the "Could not attach to PID" modal.

But, when I added use_frameworks!, it worked! I was able to repro it with and without. Here is the Podfile that worked:

swift_version = '5.0'
use_frameworks!

target 'TargetName' do
  platform :ios, '12.2'

  pod 'IGListKit'
  pod 'PromiseKit/CorePromise', '~> 6.0'
end
yood
  • 6,650
  • 3
  • 26
  • 24
0

None of the answers on this page or on the related page Could not attach to pid : “####” unable to attach worked for me. (Also, the symptom I was seeing had an added detail that didn't see mentioned on any of these pages, which is that Gatekeeper would come up and say it was "verifying" the app, even though it was an iOS app running on the simulator. That always preceded the PID error.)

Here is what eventually did work:

I let xcode try to launch and attach to the app in the simulator. It failed as usual with the gatekeeper "verifying" and then the PID error. But then the app was installed on the simulator so I just started it there and it started fine (as expected). But then I used xcode's Debug > Attach to process ... menu item and attached to the simulator running the app. That worked, and now it seems to have fixed the problem and I can start the app from xcode and it attaches fine to the simulator and lets me debug, and I no longer get the Gatekeeper "verifying" dialog box.

M Katz
  • 5,098
  • 3
  • 44
  • 66
0

If your app uses one or more XPC services when running, then try to disable debugging of these services in the Debug Scheme of your app:

  1. In Xcode, click on menu-item Product > Scheme > Edit Scheme.
  2. Click on the Options tab.
  3. Disable setting Debug XPC services used by the app.

In my case, the XPC services are related to the Sparkle update framework.

Ely
  • 8,259
  • 1
  • 54
  • 67
0

Steps -

  1. Click on Edit Scheme

enter image description here

  1. De Select Debug executable option

enter image description here

Ramesh Vishnoi
  • 1,299
  • 1
  • 12
  • 19
0

I ran into this issue running Xcode 14.0.1.

In my case it turns out to be an issue related to which user started the process. I was getting an error sating that I was trying to start it with my user, but the root user had already created the process. I still don't quite understand what the issue is.

Eventually I found this answer from the apple developer forms

You can run sudo lldb to start an lldb REPL then type in the following but instead to hitting enter hit tab process attach -n <pid from error> This will give you the name of the process.

Go to Xcode and enter that into Debug > Attach to Process by PID or Name.

TMin
  • 2,280
  • 1
  • 25
  • 34