Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't get the pretty stack trace, only the basic one #52

Closed
xMAC94x opened this issue Jan 9, 2017 · 6 comments
Closed

Don't get the pretty stack trace, only the basic one #52

xMAC94x opened this issue Jan 9, 2017 · 6 comments

Comments

@xMAC94x
Copy link

xMAC94x commented Jan 9, 2017

Hi, i followed the Readme but i don't get to the pretty stacktrace, only the simple one:

i=0
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
i=11
i=12
i=13
i=14
i=15
i=16
i=17
i=18
i=19
i=20
i=21
i=22
i=23
i=24
i=25
i=26
i=27
i=28
i=29
i=30
i=31
i=32
i=33
i=34
i=35
i=36
i=37
i=38
i=39
i=40
i=41
#0 at unsigned long backward::details::unwind<backward::StackTraceImpl<backward::system_tag::linux_tag>::callback>(backward::StackTraceImpl<backward::system_tag::linux_tag>::callback, unsigned long)
#1 at backward::StackTraceImpl<backward::system_tag::linux_tag>::load_here(unsigned long)
#2 at TracedException::_get_trace[abi:cxx11]()
#3 at TracedException::TracedException()
#4 at f(int)
#5 at f(int)
#6 at f(int)
#7 at f(int)
#8 at f(int)
#9 at f(int)
#10 at f(int)
#11 at f(int)
#12 at f(int)
#13 at f(int)
#14 at f(int)
#15 at f(int)
#16 at f(int)
#17 at f(int)
#18 at f(int)
#19 at f(int)
#20 at f(int)
#21 at f(int)
#22 at f(int)
#23 at f(int)
#24 at f(int)
#25 at f(int)
#26 at f(int)
#27 at f(int)
#28 at f(int)
#29 at f(int)
#30 at f(int)
#31 at f(int)
Stack trace (most recent call last):

I copied the source from the _test_main.cpp file to my project

#include "backward.hpp"

using namespace backward;

class TracedException : public std::runtime_error
{
public:
	TracedException() :
		std::runtime_error(_get_trace())
	{}

private:
	std::string _get_trace()
	{
		std::ostringstream ss;

		StackTrace stackTrace;
		TraceResolver resolver;
		stackTrace.load_here();
		resolver.load_stacktrace(stackTrace);

		for (std::size_t i = 0; i < stackTrace.size(); ++i)
		{
			const ResolvedTrace trace = resolver.resolve(stackTrace[i]);

			ss << "#" << i << " at " << trace.object_function << "\n";
		}

		return ss.str();
	}
};

void f(int i)
{
	if (i >= 42)
	{
		throw TracedException();
	}
	else
	{
		std::cout << "i=" << i << "\n";
		f(i + 1);
	}
}

namespace {

	TEST(pfFiles, TestStackError)
	{
		backward::StackTrace st;
		try
		{
			f(0);
		}
		catch (const TracedException& ex)
		{
			std::cout << ex.what();
		}

		backward::Printer printer;
		//    printer.address = true;
		printer.object = true;
		printer.print(st, stdout);
	}
}

I use the following CMAKE settings.

  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -Wno-long-long -Wpedantic -std=c++14 -g")
  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lbfd")

I have installed backward-cpp as subdirectory in my cmake script.
I installed the gnu binutils package under ubuntu 16.04LTS with gcc 5.4.
I neither set the define (-DBACKWARD_HAS_...=1) flag nor used #define BACKWARD_HAS_BFD 1 because it seems that your cmake script automaticly detects the library:

Looking for IceConnectionNumber in ICE - found
-- Found X11: /usr/lib/x86_64-linux-gnu/libX11.so
-- Could NOT find libdw (missing:  LIBDW_LIBRARY LIBDW_INCLUDE_DIR) 
-- Found libbfd: /usr/lib/x86_64-linux-gnu/libbfd.so  
-- BACKWARD_HAS_UNWIND=1
-- BACKWARD_HAS_BACKTRACE=0
-- BACKWARD_HAS_BACKTRACE_SYMBOL=0
-- BACKWARD_HAS_DW=0
-- BACKWARD_HAS_BFD=1
-- Found Backward: /home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/3rdparty/backward-cpp  

i compile with:

add_executable(PfTEngineTest ${SOURCES} ${BACKWARD_ENABLE})
add_backward(PfTEngineTest)

So my question is: What do i have to do to get the full pretty code style?
Have i installed all dependencys correclty ?
Have i forgot something ?
The README doesn't specify what needs to be in code for pretty code ? is it enough just to compile backward.cpp with my target ?
Or do i have to create some classes ?
Do i need to throw special Exceptions or is everything catched ?
Do i need a TRY/CATCH block in main which covers everything ?

Thanks for any help :)

PS: I used the changed code from #51

@bombela
Copy link
Owner

bombela commented Jan 9, 2017

It looks like you are not collecting all the trace details in your _get_trace method. Try calling abort() with backward-cpp signal handler installed for comparison.

The README doesn't specify what needs to be in code for pretty code ?

It does, read again :) And as far as I can tell, your defines are correct in your cmake output. (But I am not experienced with CMake, so take my affirmation with a grain of salt). You have unwind and BFD.

Do i need to throw special Exceptions or is everything catched ?
Do i need a TRY/CATCH block in main which covers everything ?

backward-cpp doesn't offer any function to attach stacktrace onto Exceptions (I had a work in progress branch at some point, but never took the time to finish the work).

If you never catch an exception, eventually your program will call abort(). This will effectively get you a stacktrace from backward-cpp assuming signal handlers are registered. But this stackstrace will start from the abort() call not were the exception was raised.

So your idea of storing a stacktrace alongside the Exception is perfectly good, but make sure to collect all the details :) I would advise you to collect the stacktrace in the exception without resolving anything yet, and use the stacktrace printer whenever you want to resolve and print details about the exception stacktrace.

Best,

@xMAC94x
Copy link
Author

xMAC94x commented Jan 9, 2017

I was not sure if

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lbfd")

is the correct way to link against the library, so i added:

target_link_libraries(PfTEngineTest "libbfd.so")

now when calling abort in f() i get a pretty nice stacktrace.

`i=0
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
i=11
i=12
i=13
i=14
i=15
i=16
i=17
i=18
i=19
i=20
i=21
i=22
i=23
i=24
i=25
i=26
i=27
i=28
i=29
i=30
i=31
i=32
i=33
i=34
i=35
i=36
i=37
i=38
i=39
i=40
i=41
Stack trace (most recent call last):
#31   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#30   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#29   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#28   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#27   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#26   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#25   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#24   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#23   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#22   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#21   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#20   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#19   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#18   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#17   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#16   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#15   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#14   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#13   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#12   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#11   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#10   Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#9    Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#8    Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#7    Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#6    Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#5    Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#4    Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#3    Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 50, in f(int) [0x4be574]
         47: 	else
         48: 	{
         49: 		std::cout << "i=" << i << "\n";
      >  50: 		f(i + 1);
         51: 	}
         52: }
#2    Source "/home/gitlab-runner/builds/0c2717af/0/PfTTech/PfTEngine/src/test/pfthread.cpp", line 44, in f(int) [0x4be53b]
         41: {
         42: 	if (i >= 42)
         43: 	{
      >  44: 		abort();
         45: 		throw TracedException();
         46: 	}
         47: 	else
#1    Object "/lib/x86_64-linux-gnu/libc.so.6", at 0x7faf86b13029, in abort
#0    Object "/lib/x86_64-linux-gnu/libc.so.6", at 0x7faf86b11428, in gsignal
Aborted (Signal sent by tkill() 19122 999)
Aborted

@bombela
Copy link
Owner

bombela commented Jan 12, 2017

I am glad that it works now :)

@bombela bombela closed this as completed Jan 12, 2017
@Manu343726
Copy link
Collaborator

So the pretty output only shows up if you link against the right libraries? I think there's a bug in the conan recipe then :(

@bombela
Copy link
Owner

bombela commented Jan 13, 2017 via email

@Manu343726
Copy link
Collaborator

Manu343726 commented Jan 14, 2017

From the package recipe perspective, conan is just a python script that tells how to get your project sources, build them, and what build artifacts must be packaged (In this way it works very similar to some system package managers). The only thing that makes it special is that conan is specifically designed for C++, so it keeps track of compiler versioning, abi, platform, etc when building and resolving the dependencies.

With this in mind, if there's a bug in the way optional libraries are handled is because I didn't take this right into account when writing the recipe. Specifically, I put one conan option (A package can define multiple options that result in different "variants" of the same package, for example "MyFooLib" Debug vs Release) for each backward-cpp option. Then conan build() method just invokes your CMakeLists.txt with those options. What I forgot is to tell the library (Maybe by modifying how the cmake library target is handled in the CMakeLists.txt) to link against different optional libraries depending on the options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants