Tuesday, May 21, 2013

Passing arguments to program when using GDB command line

If you have a program that takes input parameters on start and you want to debug it using GDB in the command line, here is how to pass parameters to the program:
  1. Start GDB with given program
    gdb myProgram.a
  2. Set break points etc. 
  3.  Start debugging and give parameters you would usually give to program here
    run param1 param2 
Alternatively you can specify arguments directly when calling gdb with --args flag:
gdb --args myProgram.a param1 param2

Cheatsheet for GDB command line: http://www.yolinux.com/TUTORIALS/GDB-Commands.html

Source: http://bytes.com/topic/c/answers/833755-how-do-i-pass-argument-gdb

Wednesday, May 15, 2013

Remote Desktop Windows To Linux

There are probably a ton of ways to do this, but here is one.:)
Use X11VNC on Linux and a standalone TightVNC client to connect to the Linux machine from Windows.
  1.  Install X11VNC on Linux (Ubuntu)
    sudo apt-get install x11vnc
  2. Run the VNC server on Linux
    x11vnc
    or
    x11vnc -forever
    to keep listening for new connections after the first client has disconnected.
    WARNING: X11VNC runs without password as default, you should set a password if you intend to let the server run in the background or for longer periods.
  3. Download standalone (or installer if you have the rights to install) of TightVNC:
    Latest version, installer only:
    http://www.tightvnc.com/download.php
    Old version, standalone:
    http://www.tightvnc.com/download-old.php
    http://www.tightvnc.com/download/1.3.10/tightvnc-1.3.10_x86_viewer.zip
  4. Connect to your Linux machine (use ifconfig to get IP-address) using the TightVNC viewer.
Consider adding x11vnc-forever to your /etc/rc.local and make it executable to start a VNC server when your computer boots.

Wednesday, May 8, 2013

Google Test check for derived class pointer

I have a function that creates an object of a derived/child Derived class and passes this object as a pointer to a function accepting a pointer of the base/parent Base class.

Now I wanted to test this using the Google Test Framework in conjunction with Google Mock:

TEST_F(myClassToBeTestedTest, functionToBeTestedTest)
{
    EXPECT_CALL(myClassMock, myFunc(::testing::A<Derived *>()));
    myClassToBeTested.functionToBeTested();
}


This did not compile with the following error:

mytestclasstest.cpp:X:Y: error: no matching function for call to 'MyClassMock::gmock_myFunc(testing::Matcher<Derived*>)'
mytestclasstest.cpp:X:Y: note: candidate is:
myclassmock.h:X:Y: note: testing::internal::MockSpec<void(Base*)>& 'MyClassMock::gmock_myFunc(const testing::Matcher<Base*>&)

...

Googling a bit I found http://stackoverflow.com/questions/5056538/google-mock-using-testingan which explained that Google Test needed const and &. However I couldn't get this to make sense for my type argument. E.g. EXPECT_CALL(myClassMock, myFunc(const ::testing::A<Derived *>() &)); doesn't work.

After more searching (and annoyance at google for removing :: and <>) I ended up with creating my own custom matcher using "Using Predicates as Matchers" from http://code.google.com/p/googlemock/wiki/CookBook#Using_Predicates_as_Matchers together with http://stackoverflow.com/questions/500493/c-equivalent-of-instanceof to check if object is type of Derived:

int IsDerived(Base * base)
{
    Derived * derived = dynamic_cast<Derived *>(base);
    return derived != NULL ? 1 : 0;
}

TEST_F(myClassToBeTestedTest, functionToBeTestedTest)
{
    EXPECT_CALL(myClassMock, myFunc(::testing::Truly(IsDerived)));
    myClassToBeTested.functionToBeTested();
}