20

I have seen a lot of answers for this question:

error: failed to attach to process ID

as switch to GDB. But no one addresses the reason of why it happens?

Attaching works fine with the GDB debugger but the default and recommended project setting is LLDB.

Can anybody explain why LLDB fails? Is it a common bug or am I doing something wrong?

Alternatively, how can I set GDB as my default debugger without changing it manually when creating the new projects?

System Info:

OS: Lion
RAM: 5GB
XCode: Version 4.6 (4H127)
Device: Mac mini

My localhost setting: enter image description here

Ron
  • 3,055
  • 1
  • 20
  • 21
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • This is the same problem with me but it stars when I try to install some frameworks (third party) for xcode. It requirers some specific environment variable or some specific system file to be generated and after generating that file or installation my xcode start to show the same error and have to every time change the debugger to GDB for every project to compile and run it :(. It seams there is not other way then re-installing the OS X from scratch, as everything else is tried by me. All including the Xcode re-installation. And I don't think that other then the re-installation of OS X. – The iOSDev Mar 12 '13 at 11:27
  • What version of Xcode are you using? – trojanfoe Mar 13 '13 at 16:13
  • 1
    On the very rare occasion that this actually happens to me, a reboot has fixed it 100% of the time. Have you tried that? – Mick MacCallum Mar 13 '13 at 16:14
  • @0x7fffffff : Sometimes rebooting fixes .Some times not – Lithu T.V Mar 13 '13 at 16:15
  • Can you check if `localhost` is defined in your `/etc/hosts` file please. From the command line `grep localhost /etc/hosts`. – trojanfoe Mar 13 '13 at 16:17
  • # localhost is used to configure the loopback interface ::1 localhost fe80::1%lo0 localhost – Lithu T.V Mar 13 '13 at 16:22

2 Answers2

38

Make sure you have localhost mapped to 127.0.0.1 in your /etc/hosts file:

$ grep localhost /etc/hosts

If grep doesn't show 127.0.0.1 then add it:

$ sudo -i
# echo "127.0.0.1 localhost" >> /etc/hosts

^ That '#' is root's command prompt; don't type it otherwise you will comment-out the statement and nothing will happen

NOTE Use >> and not >! (better is to edit it using vi or mate or whatever).

My /etc/hosts file shows (ignoring comments):

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
fe80::1%lo0     localhost
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • # localhost is used to configure the loopback interface ::1 localhost fe80::1%lo0 localhost – Lithu T.V Mar 13 '13 at 16:26
  • Can you please explain this.What difference does this make? – Lithu T.V Mar 14 '13 at 03:24
  • I ll definitly try it can you verify the response i commented earlier.That is what i get – Lithu T.V Mar 14 '13 at 09:57
  • @LithuT.V It's a networking issue; lldb doesn't know how to connect to the iPhone Simulator using IPv4. If you don't have a 127.0.0.1 <=> localhost mapping (verified by `grep`) then you will need to fix your `/etc/hosts` file using the details in this answer. – trojanfoe Mar 14 '13 at 09:57
  • hmm?withhout a network connection cannot run simulator?:O I m a newbie .can you please explain more – Lithu T.V Mar 14 '13 at 10:00
  • 1
    @LithuT.V Don't get carried away; this doesn't mean you need an internet connection in order to debug, as 127.0.0.1 is the *loopback interface* (Google that for details) and it's simply that `lldb` (and `gdb`) uses TCP for its interprocess communication mechanism. – trojanfoe Mar 14 '13 at 10:05
  • Updated the question :) This is my current setting – Lithu T.V Mar 14 '13 at 10:07
  • @LithuT.V Then you need to apply the fix in my answer. – trojanfoe Mar 14 '13 at 10:08
  • @LithuT.V Great; you can award the bounty as well then. – trojanfoe Mar 17 '13 at 09:29
  • ooh..sry .I thought marking the answer will automatically give you bounty.here you go.Thanks a ton :) – Lithu T.V Mar 17 '13 at 12:02
  • @LithuT.V No problem; glad the solution helped. – trojanfoe Mar 17 '13 at 13:06
  • I think I love you. Thank you. – Oh Danny Boy Apr 02 '13 at 20:50
  • Thanks for this answer! Something must've screwed up my hosts file… I have no idea why 127.0.0.1 wasn't pointing to localhost. Putting it back fixed the issue, though! – Jacob Pritchett Apr 27 '13 at 17:07
  • Unfortunately this does not work for me, I'm trying to attach to coreaudiod, and I get the "lost connection" error :( – Robert Nov 16 '15 at 06:54
2

Apple likes to move forward. So setting gdb as the debugger for all new projects is not an option.

Sometimes, you have to reset the iOS Simulator to clean up the debugger.

Black Frog
  • 11,595
  • 1
  • 35
  • 66