Search This Blog

Tuesday, February 9, 2010

How to modify C++ MyString class object using visual studio debugger?

Scenario:
You are in between some serious and lengthy debugging. For doing some test you need to change string object of type MyString which contains char* and len. New string is of length shorter than original string.

Mostly the string class (Let's call it MyString) contains these data members -
1. char* m_buf => character pointer
2. int m_len => length of string

i.e. its declaration looks something like
class MyString {
public:
   // Public members
private:
    // Private data

   char* m_buf;
   int m_len;
};
After googling for few seconds I got one such sample myString class

Data:
Suppose name of string variable is "string_obj".

Required modification:
Suppose m_buf originally points to buffer "bacada", you want to to change its value to "aada"
Change the buffer, by accessing the contents of buffer using array member method -

How to do it?

First get the char* object in watch window
(string_obj).m_buf   // This will show value of pointer m_buf

To modify its contents in watch window do this (I do not find it worth to put image here)-
(string_obj).m_buf[0] = 'a'
(string_obj).m_buf[1] = 'a'
(string_obj).m_buf[2] = 'd'
(string_obj).m_buf[3] = 'a
(string_obj).m_buf[4] = 0  // Do not forget to make it null terminated string

Also do not forget to change the length variable
(string_obj).m_len = 4;

One known limitation of this hack -
You can not change the buffer of size 6 to buffer of size 8 - this can lead to crash.

Please post comment if you know better method.

Wednesday, February 3, 2010

Tools for exploring executable/ library files

For Linux -
objdump
"$ man objdump" will be best guide than any other link.


For Windows -
dumpbin

I do not want to unnecessarily waste the web space by rewriting the information already available on internet.

Few things which you with these utilities -
1. Get assembly language code from executable/ DLL.
2. Get the header information.
Explore these tools and enjoy.

I will also mention here tool called hex editor which can be used to edit a binary file :). I do not know how to use it - hope I will learn it someday.