Sunday, November 30, 2014

Debugging with GDB: Examining Call Stack and Call Stack Frames


If you are debugging a programming with gdb and want to see the call stack, and the call stack is really big (spanning the whole page), it makes sense to see only the top (innermost) 5-6 stack frames rather than the entire stack.


(gdb) backtrace
#0  0x00007ffff7bc2620 in thirdPartyFFTransform () from ./liblzo2.so
#1  0x00007ffff7108e43 in useMD5Checksum (karlCoef=0x7ffff2cd0ff0, ffCoef=0x7ffff2cd10c0) at ThirdParty/HardFFTrf.c:99
#2  0x00007ffff71091d5 in FastCheckSum (gamma=3426, delta=0x7ffff730e4c8 <acc_no> "x^2+5y  ", capillaryRise=0x7ffff2cd1120, 
    pCheckSumList=0x7ffff2cd10c0) at ThirdParty/HardFFTrf.c:152
#3  0x00007ffff7107349 in compute_fourier (gamma=40127, delta=0x7ffff2cd1120, CoefA=0x7ffff2cd123a) at ThirdPartyFF.c:1125
#4  0x0000000000491c6e in example::MathCalculator::FastFourierHelper::getCoefficient(FastFourier::Coefficient const*) ()
#5  0x0000000000492a44 in example::MathCalculator::FastFourierHelper::createFFDescriptor(std::vector<example::MathCalculator::ComputeIterationN*, std::allocator<example::MathCalculator::ComputeIterationN*> > const&, int) ()
#6  0x0000000000478767 in example::MathCalculator::FTransform::FTransform(example::MathCalculator::MessageQueue<example::MathCalculator::QMessg, 1024ul>*, example::MathCalculator::MessageQueue<example::MathCalculator::QueueMessage, 1024ul>*, std::vector<example::MathCalculator::ComputeIterationN*, std::allocator<example::MathCalculator::ComputeIterationN*> >&, example::MathCalculator::Accumulator*) ()
#7  0x0000000000474f23 in example::MathCalculator::MathEngine::createJobFactory(FastFourier::EquationDescription*) ()
#8  0x0000000000476856 in example::MathCalculator::MathEngine::processQueue() ()
#9  0x00000000004770b1 in example::MathCalculator::MathEngine::checkAssignments() ()
#10 0x00000000004a7d2e in example::MathCalculator::MathComputeEngine::Calculate() ()
#11 0x000000000046072c in example::MathCalculator::MMain::runMathLibThread(int) ()
#12 0x00007ffff7787b53 in thread_proxy () from ./libboost_thread.so.1.48.0
#13 0x00007ffff5ecadc5 in start_thread () from /lib64/libpthread.so.0
#14 0x00007ffff63de73d in clone () from /lib64/libc.so.6
(gdb) 
(gdb) 
(gdb) bt 4
#0  0x00007ffff7bc2620 in thirdPartyFFTransform () from ./liblzo2.so
#1  0x00007ffff7108e43 in useMD5Checksum (karlCoef=0x7ffff2cd0ff0, ffCoef=0x7ffff2cd10c0) at ThirdParty/HardFFTrf.c:99
#2  0x00007ffff71091d5 in FastCheckSum (gamma=3426, delta=0x7ffff730e4c8 <acc_no> "x^2+5y  ", capillaryRise=0x7ffff2cd1120, 
    pCheckSumList=0x7ffff2cd10c0) at ThirdParty/HardFFTrf.c:152
#3  0x00007ffff7107349 in compute_fourier (gamma=40127, delta=0x7ffff2cd1120, CoefA=0x7ffff2cd123a) at ThirdPartyFF.c:1125
#4  0x0000000000491c6e in example::MathCalculator::FastFourierHelper::getCoefficient(FastFourier::Coefficient const*) ()
(gdb) 
(gdb) 
(gdb) bt -4
#11 0x000000000046072c in example::MathCalculator::MMain::runMathLibThread(int) ()
#12 0x00007ffff7787b53 in thread_proxy () from ./libboost_thread.so.1.48.0
#13 0x00007ffff5ecadc5 in start_thread () from /lib64/libpthread.so.0
#14 0x00007ffff63de73d in clone () from /lib64/libc.so.6

Stack frames

Consider the following stack dump


(gdb) backtrace
#0  0x0000000000401449 in autoPtrExample (rm=3) at MultiUseHeader.hpp:81
#1  0x0000000000401254 in registerQuickExitHandlers (k=3, comment="called from main") at MultiUseHeader.hpp:66
#2  0x00000000004015aa in registeringExitHandlers () at samAutoPtr.cpp:22
#3  0x0000000000401610 in main () at samAutoPtr.cpp:30
(gdb) backtrace -2
#2  0x00000000004015aa in registeringExitHandlers () at samAutoPtr.cpp:22
#3  0x0000000000401610 in main () at samAutoPtr.cpp:30
(gdb) bt 2
#0  0x0000000000401449 in autoPtrExample (rm=3) at MultiUseHeader.hpp:81
#1  0x0000000000401254 in registerQuickExitHandlers (k=3, comment="called from main") at MultiUseHeader.hpp:66
(More stack frames follow...)

You can select your frame of interest using the frame command.


(gdb) frame 0
#0  0x0000000000401449 in autoPtrExample (rm=3) at MultiUseHeader.hpp:81
81  cout << "Printing Rollno: p " << m->roll << " q " << n->roll << "\n"; // <- Seg fault
(gdb) frame 1
#1  0x0000000000401254 in registerQuickExitHandlers (k=3, comment="called from main") at MultiUseHeader.hpp:66
66  autoPtrExample(3);
(gdb) 

Similarly to traverse one frame in either direction one at a time use the up/down command


(gdb) up
#2  0x00000000004015aa in registeringExitHandlers () at samAutoPtr.cpp:22
22  registerQuickExitHandlers(3, "called from main");
(gdb) up
#3  0x0000000000401610 in main () at samAutoPtr.cpp:30
30  registeringExitHandlers();
(gdb) up
Initial frame selected; you cannot go up.
(gdb) down
#2  0x00000000004015aa in registeringExitHandlers () at samAutoPtr.cpp:22
22  registerQuickExitHandlers(3, "called from main");
(gdb) down
#1  0x0000000000401254 in registerQuickExitHandlers (k=3, comment="called from main") at MultiUseHeader.hpp:66
66  autoPtrExample(3);
(gdb) down
#0  0x0000000000401449 in autoPtrExample (rm=3) at MultiUseHeader.hpp:81
81  cout << "Printing Rollno: p " << m->roll << " q " << n->roll << "\n"; // <- Seg fault
(gdb) down
Bottom (innermost) frame selected; you cannot go down.

To see the arguments passed in a particular frame use the info args command once the frame of interest is selected

(gdb) frame
#0  0x0000000000401449 in autoPtrExample (rm=3) at MultiUseHeader.hpp:81
81  cout << "Printing Rollno: p " << m->roll << " q " << n->roll << "\n"; // <- Seg fault
(gdb) up
#1  0x0000000000401254 in registerQuickExitHandlers (k=3, comment="called from main") at MultiUseHeader.hpp:66
66  autoPtrExample(3);
(gdb) info args
k = 3
comment = "called from main"
(gdb) 

To see the local arguments of the selected frame use the info locals command to print the variables:


(gdb) info locals
filename = "./StateWisePopulationRecord.csv"
(gdb) 

[Windows] Refreshing Environment variable for windows command prompt or Powershell

Sometimes, after installing a package like chocolatey ,or npm packages, it asks you to restart the cmd.exe or powershell window to reload t...