Stack unwind on mingw

Windows specific questions, problems.
Post Reply
Dayman

Stack unwind on mingw

Post by Dayman »

So, I've been trying to resurrect my mingw64 builds. Everything works except backtrace functionality.

This stuff is real mess with mingw. I'm asking if anyone has accomplished printing a meaningful backtrace with mingw on amd64 arch?

This is what I'm getting:

Code: Select all

#  0 qbittorrent.exe      0x004b462e  0x004b462e()
#  1 qbittorrent.exe      0x004b5412  0x004b5412()
#  2 qbittorrent.exe      0x00514f81  qt_plugin_instance()
#  3 ntdll.dll            0x7ffcf30c2316 _C_specific_handler()
#  4 ntdll.dll            0x7ffcf30d398d _chkstk()
#  5 ntdll.dll            0x7ffcf30993a7 RtlRaiseException()
#  6 ntdll.dll            0x7ffcf30d2b1a KiUserExceptionDispatcher()
#  7 qbittorrent.exe      0x00474670  0x00474670()
#  8 qbittorrent.exe      0x00475316  0x00475316()
#  9 qbittorrent.exe      0x00508900  qt_plugin_instance()
# 10 QtCore4.dll          0x5b42368b  ZN11QMetaObject8activateEP7QObjectPKS_iPPv()
# 11 QtGui4.dll           0x5a936833  ZN7QWidget5eventEP6QEvent()
# 12 QtGui4.dll           0x5acb22d0  ZN6QFrame5eventEP6QEvent()
# 13 QtGui4.dll           0x5adcb93b  ZN17QAbstractItemView13viewportEventEP6QEvent()
# 14 QtGui4.dll           0x5ae11501  ZN9QTreeView13viewportEventEP6QEvent()
# 15 QtCore4.dll          0x5b40f388  ZN23QCoreApplicationPrivate29sendThroughObjectEventFiltersEP7QObjectP6QEvent()
# 16 QtGui4.dll           0x5a8e106f  ZN19QApplicationPrivate13notify_helperEP7QObjectP6QEvent()
# 17 QtGui4.dll           0x5a8e7e62  ZN12QApplication6notifyEP7QObjectP6QEvent()
# 18 qbittorrent.exe      0x004f08f3  qt_plugin_instance()
# 19 QtCore4.dll          0x5b40f24c  ZN16QCoreApplication14notifyInternalEP7QObjectP6QEvent()
# 20 QtGui4.dll           0x5a950d33  ZN19QApplicationPrivate10closePopupEP7QWidget()
# 21 QtGui4.dll           0x5a95269e  ZN19QApplicationPrivate10closePopupEP7QWidget()
# 22 USER32.dll           0x7ffcf26b250d DispatchMessageW()
# 23 USER32.dll           0x7ffcf26b2367 NotifyWinEvent()
# 24 QtCore4.dll          0x5b43d599  ZN21QEventDispatcherWin3213processEventsE6QFlagsIN10QEventLoop17ProcessEventsFlagEE()
# 25 QtGui4.dll           0x5a94e686  ZN12QApplication21restoreOverrideCursorEv()
# 26 QtCore4.dll          0x5b40dd76  ZN10QEventLoop13processEventsE6QFlagsINS_17ProcessEventsFlagEE()
# 27 QtCore4.dll          0x5b40e1e4  ZN10QEventLoop4execE6QFlagsINS_17ProcessEventsFlagEE()
# 28 QtCore4.dll          0x5b413e00  ZN16QCoreApplication4execEv()
# 29 qbittorrent.exe      0x004b65fc  0x004b65fc()
# 30 qbittorrent.exe      0x00516124  qt_plugin_instance()
# 31 qbittorrent.exe      0x004013b5  0x004013b5()
# 32 qbittorrent.exe      0x004014c8  0x004014c8()
# 33 KERNEL32.DLL         0x7ffcf28413d2 BaseThreadInitThunk()
# 34 ntdll.dll            0x7ffcf30b03c4 RtlUserThreadStart()
As you can see symbols from loaded libraries resolve correctly, but internal qBt methods are not. If I call GetLastError after failed SymGetSymFromAddr I get code 487 - "Attempt to access invalid address."

qBt is built in release mode against release libraries with the following flags:

Code: Select all

CFLAGS = -g -ggdb -O0 -fno-inline -fno-omit-frame-pointer -fno-stack-protector -UQT_NO_DEBUG_OUTPUT -fexceptions
LFLAGS = -fno-stack-protector
sledgehammer_999
Administrator
Administrator
Posts: 2443
Joined: Sun Jan 23, 2011 1:17 pm

Re: Stack unwind on mingw

Post by sledgehammer_999 »

I have been playing with this a few days ago but I quickly gave up. Here is some info that may help you.
Background:
I use msvc for official builds and mingw for testing and releasing unofficial builds. That's because the linking stage with mingw is much faster than msvc. msvc uses the ltcg which kills my crappy machine.
Anyways I always edit the mkspec of qt before building it. And I use them to define cflags/lflags to make the libs as small as possible. (release and static builds).

Here is what I have found:
1. Even if you use the -g switch for debug symbols during building you need to make sure that the mkspec's ldflags don't contain the switches that strip debug symbols from the final binary. Unfortunately my ldflags have those switches.
2. Sometimes I observed that the mkspec's CFLAGS came after qbt's CFLAGS, thus they could invalidate the -g switch.

Can you share a sample command that qmake issues when compiling a random qbt source file, so we can examine the switches?
How about the final linking commands?
If you use the strip.exe from mingw to strip the binary does it reduce in size? If yes, then you already have debug symbols and you should disregard all the above nonsense.
sledgehammer_999
Administrator
Administrator
Posts: 2443
Joined: Sun Jan 23, 2011 1:17 pm

Re: Stack unwind on mingw

Post by sledgehammer_999 »

Well the small answer from StackOverflow, hints that it might not be possible dbghelp doesn't support other compilers: http://stackoverflow.com/questions/1467 ... e-in-mingw

Something counterintuitive: We can actually use the linux code for mingw. aka backtrace() and backtrace_symbols(). -I am refering to the stacktrace.h file-
SO: http://stackoverflow.com/questions/1380 ... e-in-mingw
sledgehammer_999
Administrator
Administrator
Posts: 2443
Joined: Sun Jan 23, 2011 1:17 pm

Re: Stack unwind on mingw

Post by sledgehammer_999 »

A hint on how to use the addresses for mingw: http://sourceforge.net/p/mingw-w64/mail ... 23gen.com/
well, you have here different ways to achieve this.  First thing to
start about is the backtrace.  You can find such sample code in our
experimental tree (or at stackoverflow as you've shown).
By those addresses you can either use binutils' addr2line tool to get
more detailed information, or you can try to use libbfd (part of
binutils project) for this.
-I'll stop the spamming now-
Dayman

Re: Stack unwind on mingw

Post by Dayman »

I've managed to make it work.

needs

Code: Select all

QMAKE_LFLAGS += -Wl,--export-all-symbols
everything else is irrelevant except -fno-omit-frame-pointer for i686
Now the problem is demangling:

Code: Select all

#  0 qbittorrent.exe      0x0000000000a30cc9 ZN9straceWin12getBacktraceEv()
#  1 qbittorrent.exe      0x0000000000a32312 Z14sigsegvHandleri()
#  2 qbittorrent.exe      0x0000000000aad661 Z23qCleanupResources_geoipv()
#  3 ntdll.dll            0x00007ffcf30c2316 _C_specific_handler()
#  4 ntdll.dll            0x00007ffcf30d398d _chkstk()
#  5 ntdll.dll            0x00007ffcf30993a7 RtlRaiseException()
#  6 ntdll.dll            0x00007ffcf30d2b1a KiUserExceptionDispatcher()
#  7 qbittorrent.exe      0x0000000000b14d67 ZN5aboutC1EP7QWidget()
#  8 qbittorrent.exe      0x0000000000a682f8 ZN10MainWindow24on_actionAbout_triggeredEv()
#  9 qbittorrent.exe      0x0000000000aa7c0e ZN10MainWindow18qt_static_metacallEP7QObjectN11QMetaObject4CallEiPPv()
# 10 qbittorrent.exe      0x0000000000aa7ee4 ZN10MainWindow11qt_metacallEN11QMetaObject4CallEiPPv()
# 11 QtCore4.dll          0x0000000061293891 ZN11QMetaObject8activateEP7QObjectPKS_iPPv()
# 12 QtGui4.dll           0x000000006074ae95 ZN7QAction8activateENS_11ActionEventE()
# 13 QtGui4.dll           0x0000000060b5efe6 ZN5QMenu7setIconERK5QIcon()
# 14 QtGui4.dll           0x0000000060b6438c ZN5QMenu20internalDelayedPopupEv()
# 15 QtGui4.dll           0x00000000607a653f ZN7QWidget5eventEP6QEvent()
# 16 QtGui4.dll           0x0000000060b689db ZN5QMenu5eventEP6QEvent()
# 17 QtGui4.dll           0x00000000607510ac ZN19QApplicationPrivate13notify_helperEP7QObjectP6QEvent()
# 18 QtGui4.dll           0x0000000060758881 ZN12QApplication6notifyEP7QObjectP6QEvent()
# 19 qbittorrent.exe      0x0000000000a83af6 ZN18SessionApplication6notifyEP7QObjectP6QEvent()
# 20 QtCore4.dll          0x000000006127f24c ZN16QCoreApplication14notifyInternalEP7QObjectP6QEvent()
# 21 QtGui4.dll           0x0000000060757420 ZN19QApplicationPrivate14sendMouseEventEP7QWidgetP11QMouseEventS1_S1_PS1_R8QPointerIS0_Eb()
# 22 QtGui4.dll           0x00000000607c06ca ZN19QApplicationPrivate10closePopupEP7QWidget()
# 23 QtGui4.dll           0x00000000607c269e ZN19QApplicationPrivate10closePopupEP7QWidget()
# 24 USER32.dll           0x00007ffcf26b250d DispatchMessageW()
# 25 USER32.dll           0x00007ffcf26b2367 NotifyWinEvent()
# 26 QtCore4.dll          0x00000000612ad599 ZN21QEventDispatcherWin3213processEventsE6QFlagsIN10QEventLoop17ProcessEventsFlagEE()
# 27 QtGui4.dll           0x00000000607be686 ZN12QApplication21restoreOverrideCursorEv()
# 28 QtCore4.dll          0x000000006127dd76 ZN10QEventLoop13processEventsE6QFlagsINS_17ProcessEventsFlagEE()
# 29 QtCore4.dll          0x000000006127e1e4 ZN10QEventLoop4execE6QFlagsINS_17ProcessEventsFlagEE()
# 30 QtCore4.dll          0x0000000061283e00 ZN16QCoreApplication4execEv()
# 31 qbittorrent.exe      0x0000000000a33b9b Z5qMainiPPc()
# 32 qbittorrent.exe      0x0000000000aae804 WinMain()
# 33 qbittorrent.exe      0x00000000009313b5 0x009313b5()
# 34 qbittorrent.exe      0x00000000009314c8 0x009314c8()
# 35 KERNEL32.DLL         0x00007ffcf28413d2 BaseThreadInitThunk()
# 36 ntdll.dll            0x00007ffcf30b03c4 RtlUserThreadStart()
sledgehammer_999
Administrator
Administrator
Posts: 2443
Joined: Sun Jan 23, 2011 1:17 pm

Re: Stack unwind on mingw

Post by sledgehammer_999 »

Dayman

Re: Stack unwind on mingw

Post by Dayman »

Yep. Already did this. Much faster than trying to do it manually.

Code: Select all

#  0 qbittorrent.exe      0x0000000000380e79 straceWin::getBacktrace()
#  1 qbittorrent.exe      0x00000000003822e2 sigsegvHandler(int)
#  2 qbittorrent.exe      0x00000000003fd631 qCleanupResources_geoip()
#  3 ntdll.dll            0x00007ffcf30c2316 _C_specific_handler
#  4 ntdll.dll            0x00007ffcf30d398d _chkstk
#  5 ntdll.dll            0x00007ffcf30993a7 RtlRaiseException
#  6 ntdll.dll            0x00007ffcf30d2b1a KiUserExceptionDispatcher
#  7 qbittorrent.exe      0x0000000000464d37 about::about(QWidget*)
#  8 qbittorrent.exe      0x00000000003b82c8 MainWindow::on_actionAbout_triggered()
#  9 qbittorrent.exe      0x00000000003f7bde MainWindow::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)
# 10 qbittorrent.exe      0x00000000003f7eb4 MainWindow::qt_metacall(QMetaObject::Call, int, void**)
# 11 QtCore4.dll          0x00000000605c3891 QMetaObject::activate(QObject*, QMetaObject const*, int, void**)
# 12 QtGui4.dll           0x000000005fa7ae95 QAction::activate(QAction::ActionEvent)
# 13 QtGui4.dll           0x000000005fe8efe6 QMenu::setIcon(QIcon const&)
# 14 QtGui4.dll           0x000000005fe9438c QMenu::internalDelayedPopup()
# 15 QtGui4.dll           0x000000005fad653f QWidget::event(QEvent*)
# 16 QtGui4.dll           0x000000005fe989db QMenu::event(QEvent*)
# 17 QtGui4.dll           0x000000005fa810ac QApplicationPrivate::notify_helper(QObject*, QEvent*)
# 18 QtGui4.dll           0x000000005fa88881 QApplication::notify(QObject*, QEvent*)
# 19 qbittorrent.exe      0x00000000003d3ac6 SessionApplication::notify(QObject*, QEvent*)
# 20 QtCore4.dll          0x00000000605af24c QCoreApplication::notifyInternal(QObject*, QEvent*)
# 21 QtGui4.dll           0x000000005fa87420 QApplicationPrivate::sendMouseEvent(QWidget*, QMouseEvent*, QWidget*, QWidget*, QWidget**, QPointer&, bool)
# 22 QtGui4.dll           0x000000005faf06ca QApplicationPrivate::closePopup(QWidget*)
# 23 QtGui4.dll           0x000000005faf269e QApplicationPrivate::closePopup(QWidget*)
# 24 USER32.dll           0x00007ffcf26b250d DispatchMessageW
# 25 USER32.dll           0x00007ffcf26b2367 NotifyWinEvent
# 26 QtCore4.dll          0x00000000605dd599 QEventDispatcherWin32::processEvents(QFlags)
# 27 QtGui4.dll           0x000000005faee686 QApplication::restoreOverrideCursor()
# 28 QtCore4.dll          0x00000000605add76 QEventLoop::processEvents(QFlags)
# 29 QtCore4.dll          0x00000000605ae1e4 QEventLoop::exec(QFlags)
# 30 QtCore4.dll          0x00000000605b3e00 QCoreApplication::exec()
# 31 qbittorrent.exe      0x0000000000383b6b qMain(int, char**)
# 32 qbittorrent.exe      0x00000000003fe7d4 WinMain
# 33 qbittorrent.exe      0x00000000002813b5 0x002813b5
# 34 qbittorrent.exe      0x00000000002814c8 0x002814c8
# 35 KERNEL32.DLL         0x00007ffcf28413d2 BaseThreadInitThunk
# 36 ntdll.dll            0x00007ffcf30b03c4 RtlUserThreadStart
Post Reply