As usual, attention to detail was the issue. I saw that g++ --version reported clang, but I didn’t think about it enough. I had assumed that GDB was the problem, not my compiler. I’m not sure why Apple linked g++ to clang++, since that’s quite misleading. This little oversight made me spend 8 months writing C++ on my phone (vi on a 60-character wide display is not my ideal IDE).
What I did to get real g++:
Install gcc with Homebrew
Soft-link (
ln -s) gcc-11 and g++-11 from/usr/local/Cellar/to/usr/local/bin/unhash(at least in zsh) gcc and g++, or elsegccwill continue to expand to/usr/bin/clang
GDB is working great now.
Answer from tjcaul on Stack ExchangeAs usual, attention to detail was the issue. I saw that g++ --version reported clang, but I didn’t think about it enough. I had assumed that GDB was the problem, not my compiler. I’m not sure why Apple linked g++ to clang++, since that’s quite misleading. This little oversight made me spend 8 months writing C++ on my phone (vi on a 60-character wide display is not my ideal IDE).
What I did to get real g++:
Install gcc with Homebrew
Soft-link (
ln -s) gcc-11 and g++-11 from/usr/local/Cellar/to/usr/local/bin/unhash(at least in zsh) gcc and g++, or elsegccwill continue to expand to/usr/bin/clang
GDB is working great now.
EDIT: It seems that I was wrong, it still only works sporadically, sometimes it hangs sometimes it doesn't. It just happened to work three times in a row after running this command :(
I had the same problem and fought for hours trying to install gdb on mac. Found this blog that said that I should not have run
sudo DevToolsSecurity -enable
which I never did. So I tried it and it worked. Another possible solution was to run gdb with sudo every time but I felt this was better. No idea why they worked but they did, I hope someone else finds this useful.
The compilers on macOS are confusing. Apple supplies the LLVM compiler as part of Xcode, but it provides a stub at /usr/bin/g++ which leads people to believe they are using GCC, i.e. the GNU Compiler Collection. They then try to use gdb, i.e. the GNU Debugger with the LLVM-produced executables and find it doesn't work.
IMHO, you either need to use wholly Apple-supplied tools, or wholly GNU-supplied tools.
So, let's look at the first option - using Apple tools. In this case you compile with:
/usr/bin/g++ -g main.cpp -o main
and debug with:
/usr/bin/lldb ./main
and the debugger commands seem pretty similar to GDB.
The second option is using GCC and GDB which are normally best installed using homebrew. The installation commands are:
brew install gcc
brew install gdb
Then you will compile with something like:
/usr/local/bin/g++-8 -g main.cpp -o main
and debug with something like:
/usr/local/bin/gdb ./main
However, I cannot get this to work on macOS Mojave for the moment! I read here that GDB v8.1 doesn't work on macOS and you need to install v8.0.1 instead but cannot seem to do that with homebrew.
you can build binutils by yourself
git clone https://sourceware.org/git/binutils-gdb.git
and then use homebrew to install gcc
brew install gcc
remind to use gcc to build it
export PATH=/opt/homebrew/Cellar/gcc/13.2.0/bin:$PATH
export CC=gcc-13
export CXX=g++-13
export LDFLAGS=-Wl,ld_classic
make sure to use this ld_classic flags, dont know the exact reason, but it works on Macos Sonoma.
Videos
Are you positive you are in the Developer Tools group?
Try running this command
sudo dscl . append /Groups/_developer GroupMembership <username>
That should add your user account to the group.
I had the same problem, it wouldn't run, except as root/sudo, which meant I couldn't use it with eclipse, which meant I lost 5 working hours debugging the problem. Here are my results.
Firstly, gdb generated a similar error message when executed under my user account.
Unable to find Mach task port for process-id 4667: (os/kern) failure (0x5).
(please check gdb is codesigned - see taskgated(8))
I tried code signing it by creating a certificate using Keychain, then marking the certificate as trusted for code signing. Then applying it to the executable from the command line.
codesign -s gdb-cert /usr/local/Cellar/gdb/7.6.1/bin/gdb
codesign --verify --verbose /usr/local/Cellar/gdb/7.6.1/bin/gdb
codesign -d --verbose /usr/local/Cellar/gdb/7.6.1/bin/gdb
That didn’t work
I tried adding my user account to the procview, procmod, and _developer groups (probably insecure, but I only deploy my code to github, and don't use the machine for personal use, so I can live with that)
sudo dscl . append /Groups/procmod GroupMembership bryanhunt
sudo dscl . append /Groups/procview GroupMembership bryanhunt
sudo dscl . append /Groups/_developer GroupMembership bryanhunt
That didn't work
Finally, I tried changing the executable's group and group sticky bit.
sudo chgrp procmod /usr/local/Cellar/gdb/7.6.1/bin/gdb
sudo chmod g+s /usr/local/Cellar/gdb/7.6.1/bin/gdb
That worked
I am posting this both on r/Assembly_Language and here. I think the topic involves both fairly strongly.
So, I am going to do a presentation involving buffer overflows and I learned some basic Assembly because of that (more specifically the function prologue). I plan on learning Assembly for real and have installed SASM, but for now I just know basic C. I need help reading something.
In one of my sources (https://www.tenouk.com/Bufferoverflowc/Bufferoverflow4.html), this guy does disass in his vulnerable function in order to show that sub has allocated more space than he declared (4 vs 20).
This is his code:
#include <unistd.h>
void Test()
{
char buff[4];
printf("Some input: ");
gets(buff);
puts(buff);
}
int main(int argc, char *argv[ ])
{
Test();
return 0;
}And this is the output of disass Test:
0x080483d0 <Test+0>: push %ebp 0x080483d1 <Test+1>: mov %esp, %ebp 0x080483d3 <Test+3>: sub $0x8, %esp 0x080483d6 <Test+6>: sub $0xc, %esp
Buff is 4 bytes and 20 bytes are "freed" on the stack. But when I try to do the same with a relatively more complicated function (I hope commentary is enough for the language gap):
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <stdlib.h>
void senha(){
char senha[10];
char senhareal[10]="dddddd";
int teste = 0;
char resposta[3];
printf("\n\n\n\n-----------------------------SISTEMA NUCLEAR DA OTAN------------------------------------ \n\n\n\n");
printf("DIGITE SUA SENHA: ");
gets(senha);
if(0 == strncmp(senha, senhareal, 20))
{
printf ("\nSenha correta! \n");
teste = 1;
}
else
{
printf ("\nSENHA INCORRETA! \n");
}
if(teste)
{
printf ("\nO usuário agora controla a instalação! Deseja destruir o Brasil? \n");
gets(resposta);
}
}
int main(){
system("color 03");
setlocale(LC_ALL, "Portuguese");
senha();
return 0;
}I get this:
0x0000555555555209 <+0>: endbr64 0x000055555555520d <+4>: push %rbp 0x000055555555520e <+5>: mov %rsp,%rbp 0x0000555555555211 <+8>: sub $0x30,%rsp 0x0000555555555215 <+12>: mov %fs:0x28,%rax 0x000055555555521e <+21>: mov %rax,-0x8(%rbp)
No matter my input. So why does Online GDB consistently subtract 48 bytes? What are the instructions following sub and do they apply at all to what I am trying to do? Is it possible to know what's going on in that specific platform? Finally, am I unable to replicate what this person did with my code?
I tested the code of my source in Online GDB as well. It consistently subtracts 16 bytes.
I recently downloaded cygwin and the gdb and gdb-debuginfo packages (ver. 9.2-1) contained there. When I run gdb --version I only get a blank line in the terminal. Using cygcheck gdb says the status is okay and the version is correct. Trying something like g++ --version outputs the correct version. Trying to run gdb, even without arguments, it freezes and I have to kill the process. Any ideas how to get gdb working?
Whoops you have an infinite loop here
//This creates a LCV for the do loop below and sets it equal to 0.
int y = 0;
//This do loop generates the random number.
do
{
//This generates a random time seed.
srand(time(0));
//This generates a random number between 1 and 99.
ran = rand() % 100;
//This while controls the do loop.
}while (y != 9);
y is initialized to 0 and you are looping while y!=9. And that is just the first error, there might be more.
Bug #2
int z = 1;
//This makes the program only create a random number the first pass through the loop.
if (z < 2)
{
//This runs the random number generator function.
return randomgen();
//This sets the result of the function as the random number.
randnum = randomgen();
//This sets the random number to the current number.
num = randnum;
}
After fixing infinite loop in randomgen(), You are immediately returning...
you have two problems in your code: the first one is that you have infinite loop in randomgen() function the condition in check against y - y != 9 but the value never update
int randomgen()
{
//This creates a variable to store the random number.
int ran;
//This creates a LCV for the do loop below and sets it equal to 0.
int y = 0;
//This do loop generates the random number.
do
{
//This generates a random time seed.
srand(time(0));
//This generates a random number between 1 and 99.
ran = rand() % 100;
//This while controls the do loop.
}while (y != 9);
//This returns the random number for use in the rest of the program.
return ran;
}
and the second one is even if you pass the loop in the function you exit main right away because the call to the function randomgen() is with return
if (z < 2)
{
//This runs the random number generator function.
return randomgen(); <-- **second problome**
//This sets the result of the function as the random number.
randnum = randomgen();
//This sets the random number to the current number.
num = randnum;
}
This is how I easily fixed the issue. [Update: based on feedback received and yet to be verified, it seems that this solution works with macOS Sierra 10.12 but not with macOS Sierra 10.12.2]
See video instructions here
- Quit gdb
- Using your text editor e.g. Sublime Text, save a file called
.gdbinitin your user folder. - In the file add the following:
set startup-with-shell off - Save the file
- gdb should now work
Sources
https://discussions.apple.com/thread/7684629?start=0&tstart=0
Where is .gdbinit is located and how can I edit it?
https://sourceware.org/gdb/onlinedocs/gdb/Starting.html
I got the same error after updating to macOS Sierra. Temporarily I changed debugger to LLDB using the library lldbmi2:
1. git clone https://github.com/freedib/lldbmi2.git lldbmi2
2. cd lldbmi2
3. mkdir build
4. cd build
5. cmake ../
6. make
7. sudo make install
Once lldbmi2 is installed, you can debug your application by creating a new C/C++ Application in Debug Configurations... and change the GDB debugger (in Debugger tab) from gdb to lldbmi2. Options to lldbmi2 may be set there. Something like /usr/local/bin/lldbmi2 --log.
Everything seems to be working fine, and even better as GDB.
Can I use GCC and GDB on an M1 Mac? I was wandering if it could support my development of my University assignment (they require GCC). I was also wandering if I could develop application with OpenGL with the Apple Chip?
Someone that is using them can help me?