Sunday, June 2, 2013

Installing Go, Gocode, GDB and LiteIDE

I have been working in windows for 20 years and know the internals of that operating system very well. I am very new to using my Mac and it is always a challenge for me when I need to install software or make configuration changes. I am getting better. It took me about 6 hours over two days to get my Go environment working on my Mac the first time. Here are the steps that will help with installing Go on the Mac.

Step 1: Download Go

Open your favorite browser and go to the following website:

http://golang.org/dl/


This will show you all the latest builds for the different operating systems. Darwin is the name for the Mac OS. Download the installer package for your OS version and architecture.

Once downloaded go to your Downloads folder in Finder and double click on the pkg file to start the installation. The installation will put Go in /usr/local/go. Once the installation is complete you will want to check the installation.

Note: Moving forward we will be using both Terminal and Finder. It helps to be able to see all the files in Finder.  Finder by default will not show you everything that is on your hard drive.

To show all files in Finder:

Open a Terminal session. If you don't know where the Terminal program is go to your Applications folder in Finder and then Utilities. Click on Terminal and a bash shell command window will open.

Execute the following commands in Terminal.

defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder

The killall Finder will reload the Finder program and now you can see all the files.

Step 2:  Check Installation

Open a Terminal session and type the following command

go version

You should see the following if everything installed correctly

go version go1.3.1 darwin/amd64

Now type the which command to verify the installation is in /usr/local/go

which go

You should see that Go can be found in /usr/local/go/bin

/usr/local/go/bin/go

Now Go is installed but we are not ready to start programming just yet. In order to get intellisense when we are using LiteIDE we need GoCode.

Step 3:  Set Your GOPATH

You need a single place to store and work on your Go projects. Create a folder called Projects from inside your home folder:

cd $HOME

mkdir Projects

Now set this as your GOPATH. Open the .bash_profile file from the $HOME folder and add the following items to the end.

nano .bash_profile

export GOPATH="$HOME/Projects"
export PATH=$PATH:$GOPATH/bin

Then exit the Terminal App and open a new Terminal App. Check that the Go environment now has your new GOPATH.

go env

You should see all the Go related environment variables including GOPATH. Here are some of them:

GOARCH="amd64"
GOCHAR="6"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/you/Projects"
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"

Step 4:  Get, Build and Install Gocode

Gocode is a program that provides intellisense for the language. Many editors support using Gocode including LiteIDE. This is going to help with development immeasurably.

We need to download Gocode, build the binary and then install the binary in a place where it can be easily found by LiteIDE and other editors.

Open a Terminal session or use the current one you have open and type the following command. This assumes you setup your GOPATH as instructed in Step 3.

go get github.com/nsf/gocode

Once this is done the source code for Gocode can be found in the folder $HOME/Project/src/github.com/nsf/gocode and the gocode program will be built and placed in $HOME/Project/bin.

To check that everything is working and your go environment is properly setup, run the which command:

which gocode

You should see:

/Users/you/Projects/bin/gocode

Step 5:  Install GDB

Installing the new version of GDB is going to be a unique experience for most. The other problem is that you most likely have a version already installed on your machine.

Run this command in Terminal

gdb --version

If you are like me the following information will be provided

GNU gdb 6.3.50-20050815 (Apple version gdb-1705)

Now run this command in Terminal

which gdb

This version of gdb is installed under

/usr/bin/gdb

So why is this important? When we build and install the new version of GDB it will be installed under /usr/local/bin. Remember Go was installed under /usr/local/go. I don't understand why some programs are installed under /usr/bin or /usr/local/bin or even why you can find different version of the same binary under both. When we are done this is exactly what we will have.

Open your browser again and go to the following url:

http://www.gnu.org/software/gdb/download/

You will see the following on the page


Click the link called http://ftp.gnu.org/gnu/gdb and you will see a list of files you can download


Download gdb-7.8.tar.gz from your browser and then find the file in Finder inside the Downloads folder. Double click on the file in Finder and Finder will unzip the file to a new folder called gdb-7.8.

Go back to your Terminal session and navigate to the gdb-7.8 folder in Downloads

cd ~/downloads/gdb-7.8

You need to run two commands from Terminal that will build the source code.

./configure
make

If the make command does not work then install XCode. I had XCode installed on my machine prior to running these commands. XCode installs the compilers that are used to build code.

Also, I found with version 7.8 the make failed because of some errors. If this is happening, remove the unzipped folder, unzip the tar.gz file again and use this parameter with the configure call.

./configure --disable-werror

Once the make command is done we need to install this version of GDB to /usr/local/bin.

In Terminal run the following command

sudo make install

Once this command is finished the new version of GDB will almost be ready for use. Remember we now have two version of GDB installed on the machine. From inside of LiteIDE this will not be a problem but from Terminal it is.

Run the following command in Terminal again

gdb --version

You still get the old version

GNU gdb 6.3.50-20050815 (Apple version gdb-1705)

Now run this command in Terminal

which gdb

The old version of gdb is still being used

/usr/bin/gdb

So why is this happening? Because your PATH has /usr/bin before /usr/local/bin. We can fix this for our current Terminal session by running the following Terminal command.

export PATH=/usr/local/bin:$PATH

This command will update the PATH and put /usr/local/bin at the front.  Now run the GDB version command again.

gdb --version

Now you get the new version

GNU gdb (GDB) 7.8

Now run this command in Terminal

which gdb

This version of gdb is being used

/usr/local/bin/gdb

Unfortunately this is not permanent.  If you open a new Terminal window the PATH variable will go back to the original setting. So how do we make /usr/local/bin always come before /usr/bin in the PATH every time we open a Terminal session?

You will need to modify the paths file under the /etc folder.

cd /etc
sudo nano paths

In my original version of the paths file I added the following entries:

/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin

Just move /usr/local/bin to the top and save the file:

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

To see if the change worked, open a new Terminal session and echo the PATH:

echo $PATH

To get more information about how to use the GDB check out this website. You can run these GDB commands from inside of LiteIDE so this web page is helpful.

http://golang.org/doc/gdb

Now that GDB is installed we need to do one more thing, codesign the binary so it can be used to debug the programs we write.

NOTE: Please review the GDB Problems at the end of the article 

Step 6: Codesign GDB

If we don't codesign the GDB executable LiteIDE will run in debug mode but it won't work. The steps I have provided come from the following websites.

http://sourceware.org/gdb/wiki/BuildingOnDarwin

http://iosdevelopertips.com/mac-osx/code-signing-error-object-file-format-unrecognized-invalid-or-unsuitable.html

NOTE: Make sure you have the latest version of Xcode installed before you continue

5a. Creating a Certificate

Start the Keychain Access application
/Applications/Utilities/Keychain Access.app

Open the Menu option
/Keychain Access/Certificate Assistant/Create a Certificate...

In the Create a Certificate dialog box use the following settings

Name: gdb-cert
Identity Type: Self Signed Root
Certificate Type: Code Signing
Let Me Override Default:  Checked

Click Continue several times until you get to the Specify a Location For The Certificate screen

Keychain: System

If you can't store the certificate in the System keychain, create it in the login keychain, then exported it. You can then imported it into the System keychain.

Then find your new certificate in the list and right click and select Get Info. Then expand the Trust item and find the Code Signing drop down. Change the setting for Code Signing to Always Trust. You must quit the Keychain Access application in order to use the certificate so close the program.
5b. Codesigning GDB with new certificate

Before you run the codesign command in Terminal you need to add an export to your Terminal session or you will get the following error

object file format unrecognized, invalid, or unsuitable

Open or reuse your existing Terminal session and run the following export command

export CODESIGN_ALLOCATE="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate"

Now change the directory in your Terminal session to where the GDB binary is located and codesign the binary.

cd /usr/local/bin
codesign -s gdb-cert gdb

There should be no errors and a new Terminal command prompt should just appear. Now everything is ready to go. We have Go, Gocode and GDB installed. Next we need to install LiteIDE.


Step 7: Install LiteIDE

LiteIDE is an excellent IDE and I love working in it. Installing and getting it going requires just a few steps.

Open your browser and navigate to http://sourceforge.net/projects/liteide/files/


Chose the Mac version (liteidex23.2-1.macosx.zip). Download the file and then open Finder and navigate to the Downloads folder.

Unzip the file and copy LiteIDE.app into the Applications folder.

Open your Applications folder and double click on LiteIDE to start it.

Step 8: Test LiteIDE

Let's create a new program and test our installation

Switch to LiteIDE and find the View menu option.  Select the Manage GOPATH option at the bottom of the menu. The Manage GOPATH dialog box will appear.



You need to add the Projects folder you created in step 3 as a custom directory for your GOPATH. This is where Go will look for custom packages and the code you are writing. The Go libraries will always be available. To learn more about the GOPATH go to this web page: http://golang.org/doc/code.html


Choose File/New and perform the following tasks.

1. Double click on your custom GOPATH. It will be cut off in the dialog box but it should show above the system folder /usr/local/go. This should change the Location field to the GOPATH.

2. Select Go1 Command Project.

3. Name your new program test_program.

4. Click OK and agree to open main.go.
Before you build and test the program make sure you are using the correct build environment (darwin64-local).

Test if you can build and run the program. Find the blue "BR" button and select it. If LiteIDE is configured correctly the program should build and run. The Build Output should look like the image below.

You should see the Hello World! print in the Build Output window.





Next test if the debugger is working. Select line 9 of the program and click on the orange button to add a breakpoint.



Next you need to change the Build configuration a bit. Add these build arguments. They help support the GDB. The debugging is not perfect but this makes it better. The debugger will work without it but my experience is that the debugger can pick up more information.

-gcflags "-N -l"
(Capital N and Lowercase L)


To learn more about debugging in Go click on this link:

http://golang.org/doc/gdb



Now from the Debug menu select Start Debugging.






If the debugger is working a green arrow will stop at line 8. You may be asked to enter your password before debugging begins.

If this is happening you are good to go.
If things stop working always double check that you have the right environment selected (darwin64-local). I can't tell you how much time I have lost because the environment changed without me realizing it.

Possible Build Errors

"can't open output file for writing: a.out, errno=13 for architecture x86_64"
Solution By: Alejandro Gaviria
Version: Xcode 4.2, Mac OS X (10.7.1), Mac OS X (10.7.2)

This is due to the gcc compiler that comes with XCode ~4.1.

Solution comes from this Apple discussion thread:
https://discussions.apple.com/thread/3406578

Possible GDB Errors

"Unable to find Mach task port for process-id 12383: (os/kern) protection failure (0x2).\n (please check gdb is codesigned - see taskgated(8)"
Solution By: Karl Tuhkanen
Version: Mac OS X (10.8)

1. sudo chgrp procmod /usr/local/bin/gdb
2. sudo chmod g+s /usr/local/bin/gdb
3. add the legacy switch -p to taskgated by modifying the file System/Library/LaunchAgents/com.apple.taskgated.plist
4. Force kill taskgated process (it will restart) or reboot if necessary

Solution comes from this stackoverflow thread:
http://stackoverflow.com/questions/12050257/gdb-fails-on-mountain-lion

ALERT! Be sure to use standard .plist markup with the file modification. Otherwise OSX won't start next time you reboot. This happened to me. Solution to this is to reboot with recovery option (Cmd-R) and modifying the file with vi to match the standard.

Version 7.6.2 still has a bug that prevents it from loading the Go runtime integration!
Solution By: Chris McGee
Version: Mac OS X

You can see the problem when you fire up GDB and it does not have the special "Loading Go Runtime Support" message. There is a patch for this:

http://sourceware-org.1504.n7.nabble.com/Path-Add-support-for-mach-o-reader-to-be-aware-of-debug-gdb-scripts-td238372.html

GDB Freezes And Consumes CPU When Using "Info Locals"
Solution By: Chris McGee
Version: Mac OS X

GDB freezes. Only Ctrl-C seems to bring it back. When using an application that uses gdb/MI for a GUI interface gdb is totally unresponsive.

https://code.google.com/p/go/issues/detail?id=6598

Conclusion


I hope these instructions get you up and running with Go quickly on your Mac. Now that you can write code using LiteIDE I suggest building some test programs and learning how all the different windows in the IDE work. There are a lot of great goodies in there.

Check out this web page: http://go-lang.cat-v.org/books

It is the Go language resources page and contains a lot of great links to information. The Book Go In Action by Brian Ketelsen, Erik St. Martin and myself is available and being written for developers new to Go.

Here are other links to web pages that will be very helpful:

http://golang.org/
https://plus.google.com/+golang/posts
http://blog.golang.org/
http://golang.org/doc/gdb
http://www.youtube.com/user/gocoding
http://dave.cheney.net/
http://gophervids.appspot.com/

You must watch these videos on Go Concurrency Patterns

http://www.youtube.com/watch?v=QDDwwePbDtw
http://www.youtube.com/watch?v=f6kdp27TYZs

18 comments:

  1. Debugging worked after a few additional steps to these instructions.
    1. sudo chgrp procmod /usr/local/bin/gdb
    2. sudo chmod g+s /usr/local/bin/gdb
    3. add the legacy switch -p to taskgated by modifying the file /System/Library/LaunchAgents/com.apple.taskgated.plist
    4. Force kill taskgated process (it will restart) or reboot if necessary
    5. Try again

    Solution comes from this stackoverflow thread: http://stackoverflow.com/questions/12050257/gdb-fails-on-mountain-lion

    ALERT! Be sure to use standard .plist markup with the file modification. Otherwise osx won't start next time you reboot. This happened to me. Solution to this is to reboot with recovery option (Cmd-R) and modifying the file with vi to match the standard.

    ReplyDelete
    Replies
    1. Karl, can you tell me what problems you experienced or what didn't work for you that required these steps. I would like to add these steps to the document. I didn't run into these problems. I am running version 10.8.4 of the Mac OS.

      Delete
  2. I got this error from debug console from Lite-IDE --> "Unable to find Mach task port for process-id 12383: (os/kern) protection failure (0x2).\n (please check gdb is codesigned - see taskgated(8)". This error came even though I had codesigned gdb 7.6 with the exact steps of your instructions. Now I can reproduce the error by unloading the /System/Library/LaunchDaemons/com.apple.taskgated.plist, deleting the -p switch and reloading taskgated. My Mac OS is same as yours.

    The problem has to do with codesigning, but I can't figure out in what way.

    ReplyDelete
    Replies
    1. Thank Karl, I have added the error your identified and the solution to the post

      Delete
  3. Great article! I only ran into one problem when builing and running LiteIDE test_program. "Errno 13." This is due to the gcc compiler that comes with XCode ~4.1.

    If anyone runs into Errno 13 please go here "Apple Discussion"

    ReplyDelete
    Replies
    1. Thank Alejabdro, I have added the error you identified and the solution to the post

      Delete
  4. Thanks this was a great help.
    Martyn

    ReplyDelete
  5. I ran into one small problem with the codesign command: gdb: cannot find code object on disk.
    Turns out codesign_allocate moved:
    export CODESIGN_ALLOCATE="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate"

    The locate command found it for me (as well as regular use of /usr/libexec/locate.updatedb).

    ReplyDelete
    Replies
    1. Thanks for the update. I have made the change. The latest update of Xcode changed the location. I really appreciate it !!

      Delete
  6. Thanks for the outstanding walk-through. I tripped over the nomenclature for "users" folder a couple of times, and simply guessed. Also, I was unable to get the line-8 green arrow rendered, and I have some work to do on getting the debugger working. I am using an early-1989 iMac, so darwin32-local was necessary for me to do a build; and I was using OSX 10.8.3 with the latest compatible XCode.

    Overall, really extraordinary job.

    Many thanks,

    Greg

    ReplyDelete
    Replies
    1. I did focus the article on a 64-bit architecture, sorry about that. I am glad it had enough to get you going. If you have any questions send them to me. bill@ardanstudios.com

      Delete
    2. I wrote this back when I knew nothing about the os. I fixed all the references to user directory to home. Thanks again !!

      Delete
  7. If you are into brew[0], the installation of go is dead simple[1] and I've found the upgrade process painless. GDB[2] is still a bit of a pain.

    [0] : http://brew.sh
    [1] : "brew install go gdb"
    [1] ; http://sourceware.org/gdb/wiki/BuildingOnDarwin
    https://gist.github.com/Pallinder/6426936

    ReplyDelete
  8. Thanks for this walk-through. Having an integrated debugger is my main reason for using an IDE, instead of just an editor and Make, and after this I was up and running!

    You spent a big chunk of the post fiddling with the .bashrc file and PATH variable. There's a much easier way to do this on *nix systems (including OS X): edit the /etc/paths file. In this case you want the line "/usr/local/bin" to be before "/usr/bin". And you can add an entry for /usr/local/bin if it doesn't exist.

    ReplyDelete
    Replies
    1. Thanks for the tip on the /etc/paths file. I knew about that file. Beware that debugging on the Mac using GDB is not complete, though basic debugging works.

      Delete
    2. Updated the post to use the paths file. Thanks Again !!

      Delete
  9. Ouch, that's a lot of work just to get Go installed. For me on Linux, all I had to do was run `sudo pacman -S go` (`apt-get install go` for Debian/Ubuntu users) and done, could just open up my Kate text editor and enable the built-in terminal, save the file as go source code and have go syntax highlighting, write the code and save, type go run source-code.go and done.

    ReplyDelete