Running GDB on macOS Sierra failed with the error below:
Starting program: /Users/gaurabb/Desktop/Coding-Projects/CLang/a.out Unable to find Mach task port for process-id 68306: (os/kern) failure (0x5). (please check gdb is codesigned - see taskgated(8))
Steps I followed to address this (based on references at the end):
Step 1: Codesign GDB following steps
Step 1.1

Step 1.2

Step 1.3

Step 1.4

Step 1.5

Step 1.6

Step 2:
Step 2.1: Create a file named .gdbinit in the /Users/<username>
Step 2.2: Add the following to the file: set startup-with-shell off
This disables use of a shell that GDB uses to start the program in Unix based systems
Step 3: Open a terminal and run:
sudo killall taskgated
taskgated is a system daemon that implements a policy for the task_for_pid system service. When the kernel is asked for the task port of a process, and preliminary access control checks pass, it invokes this daemon (via launchd) to make the decision.
Step 4: In the terminal, run:
codesign -f -s "gdb-cert" /usr/local/bin/gdb
These steps should address the error.