Displaying debug outputs into MacOS terminal - macos

On Mac OS X the debug outputs go to the system logger that can be read with the Console Application. But when you are developing outside of Xcode (using QtCreator or just the terminal for example), it's not very practical to check the system logs for debugging.
Is there any way to force MacOS to print the backtrace to the terminal (similar to Linux behavior) without any trick on the application side ??
I'm developing a new feature for Clang so it's a C++ program. And I'm using MacOS 10.13.3.

If you are using qt, then include QDebug and then use qDebug() as follows:
qDebug() << debug_qstring;
And if you are using XCode and ObjC, look for NSLog.
void NSLog(NSString *format, ...);
Also printf in case of C and std::out in case of cpp are always your friends!

Related

How to fix permanent EXC_BAD_ACCESS exception in Xcode using SystemC?

I want to write SystemC code using the Xcode IDE. I have set it all up and the code I use for testing my Installation builds just fine, but it runs always into Thread 1: EXC_BAD_ACCESS exception, no matter what I do.
I tried to comment everything out, until only the systemc header include and the sc_main was left. When I tried normal c++ code in a different project, to see if it was a general Xcode problem, it work fine and ran into no exception.
I use this generic Hello World-code to test my Installation:
#include <systemc>
//Hello_world is module name
SC_MODULE (hello_world) {
SC_CTOR (hello_world) {
// Nothing in constructor
}
void say_hello() {
//Print "Hello World" to the console.
std::cout << "Hello World.\n";
}
};
// sc_main in top level function like in C++ main
int sc_main(int argc, char* argv[]) {
hello_world hello("HELLO");
//Print the hello world
hello.say_hello();
return(0);
}
After the code ran for a few seconds, it always crashes with: Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeef3ffff8).
EDIT:
My Config:
- SystemC: v2.3.3
- Xcode: v10.2.1 (10E1001)
- Compiler: (I ran gcc --version)
- LLV: v10.0.1 (clang-1001.0.46.4)
- c++: v4.2.1
Im using std=gnu++98 (Compiler Default) as c++language Dialect, everything else ran into build errors.
EXC_BAD_ACCESS happens when system can not return from an execution block, such as infinite loop or recursion.
you should take a look at stack trace in the Debugger navigator and see which function is getting called and never returns OR which function(s) are calling over and over.
Don't forget to run project in DEBUG mode. (NOT release)
If you're using SystemC 2.3.2, you might be running into the following issue (on macOS 10.13 or later): http://forums.accellera.org/topic/6079-make-check-return-fail/. This issue is assumed to be fixed in SystemC 2.3.3.
You can avoid it by compiling SystemC 2.3.2 with ../configure --disable-async-updates ... or by moving to SystemC 2.3.3 and enabling C++11 (might be the default on a recent Xcode version, you can check the value of the SC_CPLUSPLUS macro in your model).
I did it! I built SystemC with Cmake following the steps provided in this answer: Setting up a SystemC project with CMake: undefined reference to sc_core
. In the CMakeLists.txt I set CMAKE_CXX_STANDARD explicitly to 11 and built the project via the command line and cmake.
Thanks for the help:)

Mac OS: Leaks Sanitizer

Mac OS X Sierra 10.13
I do as wrote here
https://clang.llvm.org/docs/LeakSanitizer.html
I.e. created the small application with memory leak
#include <stdlib.h>
void *p;
int main() {
p = malloc(7);
p = 0; // The memory is leaked here.
return 0;
}
Then build it and run to test how the memory leak is detected:
admins-Mac:test2 admin$ clang -fsanitize=address -g mleak.c ; ASAN_OPTIONS=detect_leaks=1 ./a.out
==556==AddressSanitizer: detect_leaks is not supported on this platform.
Abort trap: 6
admins-Mac:test2 admin$
How I can detect the leak?
I need to use it for my large application.
It seems that the Clang/LLVM shipped by Apple does not have -fsanitize=leak support. I fixed it by installing LLVM on Homebrew. A more detailed fix is on gist
$ brew install llvm#8
# Overwritten default Clang
$ echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >> .zshrc
$ source ~/.zshrc
$ which clang
/usr/local/opt/llvm/bin/clang
Note that you can also use the Leaks Instrument which ships with Xcode to find leaks in your code without having to install anything extra. It's not very well advertised, but it's a very useful tool. From the "Product" menu, choose "Profile". This may rebuild your application, and it will then launch the Instruments.app. You'll be presented with a sheet of different profiling instruments you can use like this:
Once you choose it and press the "record" button, it will run your app and display a track showing you any leaks, like this:
A green check means no leaks in the app at that time. A red "x" means a new leak since the last check. A gray "-" means there are leaks, but no new ones since the last check. The default is to check every 10 seconds.
At the bottom is a list of the current leaks. If you click on one, you'll see a stack trace on the right showing which function allocated the leaked memory.
This is a very powerful tool that has almost no documentation! I'm not sure why Apple keeps it so hidden.
If you use CLion you can try this setting:
in Preferences | Build, Execution, Deployment | CMake -> Cmake options
use
-DCMAKE_BUILD_TYPE=ASAN -DCMAKE_CXX_COMPILER=/usr/local/opt/llvm/bin/clang++
and

Haskell SDL app on macOS doesn't recognize keyboard/mouse when run with runhaskell

I'm running into a weird problem when running sdl-apps under macOS. Compiled apps run fine, but when i try to run the same code with runhaskell (or more specifically stack runghc) a window opens with no title bar and doesn't react to mouse or keyboard input. Doing the same under Windows or Linux works fine though.
Is there anything like a command line argument or something that i can do about that or do i just have to live with this?
SDL system librarys were installed via homebrew and the haskell libs via stack lts-8.3

Process terminated with status 255

I run Ubuntu to dual boot with Windows 7, I used to work on a C++ project on Windows in VS2013, I imported it to code:: blocks (the original project is in my other partition with Windows on, and I assume I don't have root privileges on it, but I don't really know what I'm talking about). When I try to run my project (which runs fine on Windows) I get the following messages in the "Build log":
-------------- Run: Release Win32 in Huffman (compiler: GNU GCC Compiler)---------------
Checking for existence: /media/shoham/BC5461BE54617C50/Users/Administrator.Shoham-PC/workspace/Huffman/Huffman/Huffman
Executing: /media/shoham/BC5461BE54617C50/Users/Administrator.Shoham-PC/workspace/Huffman/Huffman/Huffman (in /media/shoham/BC5461BE54617C50/Users/Administrator.Shoham-PC/workspace/Huffman/Huffman/.)
Process terminated with status 255 (0 minute(s), 0 second(s))
Most people on the Internet say to install xterm or change the terminal in Settings->Environment..., but it doesn't work for me, even though I have xterm.
What's weird is that it works on a new project I create on my system to test this. By that I mean this:
int main()
{
return 0;
}
In a new project works.
Do I not have privilleges to my project on my other partition?
Do I have to make a new project copying the files? Or can I link them to be the same one?
What do I have to do to fix this?
I got a similar message too when I compile my C++ code in Code::Blocks IDE. To correct the problem, I changed the default console program to "konsole-e".
Settings->Environment-> Terminal to launch console programs: konsole -e
This error occurs as Code::Blocks is trying to start its default application to run console programs. And since this is an app you've not installed on your local machine it stops and outputs the error
Process terminated with status 255 (0 minute(s), 0 second(s)).
Installing xterm could solve this problem if this really is the default app Code::Block is searching for, but there is no need to do so. It is more helpful to look up the current settings and to pick an app that you've already installed.
Go to Settings > Environment and you'll find a dropdown saying Terminal to launch console programs. Here you can select a different default app to launch console programs in.
In Linux Mint this should be: gnome-terminal. In other Linux OS this can be a good choice: konsole-e. If none of them work just set up a very simple C++ program such as:
#include <iostream>
int main(){
std::cout << "Hello world! << std::endl;
return 0;
}
and try out different options Code::Blocks successfully opens up a Terminal and displays Hello world!.

How can I automatically run my programs in a Terminal window & debug with Xcode?

I am building a program on Mac OS that uses the curses library. When I attempt to run it inside Xcode, I get this error message:
Error opening terminal: unknown.
And then curses calls exit.
Obviously, it works from a Terminal window.
Is it possible to launch my program in a terminal window from Xcode? I know that I can use the "Wait for XXX to launch" option, but a lot of Xcode's helpfulness in starting programs vanishes that way, so I would be looking for another way.
Xcode uses GDB, or LLDB for debugging.
You can invoke them directly from a terminal. This way, your executable will be attached to a working one.
Try:
gdb path/to/my/executable
Then, from the GDB prompt, type:
run
To start your program in debugging mode.
Take a look at the GDB manual for learning stuff like debugging commands, breakpoints, etc.

Resources