Dumping the memory contents of a object - c++

In a game that I mod for they recently made some changes which broke a specific entity. After speaking with someone who figured out a fix for it, they only information they gave me was that they "patched it" and wouldn't share anymore.
I am basically trying to remember how to dump the memory contents of a class object at runtime. I vaguely remember doing something similar before, but it has been a very long time. Any help on remember how to go about this would be most appreciated.

template <class T>
void dumpobject(T const *t) {
unsigned char const *p = reinterpret_cast<unsigned char const *>(t);
for (size_t n = 0 ; n < sizeof(T) ; ++n)
printf("%02d ", p[n]);
printf("\n");
}

Well, you may reinterpret_cast your object instance as a char array and display that.
Foo foo; // Your object
// Here comes the ugly cast
const unsigned char* a = reinterpret_cast<const unsigned char*>(&foo);
for (size_t i = 0; i < sizeof(foo); ++i)
{
using namespace std;
cout << hex << setw(2) << static_cast<unsigned int>(a[i]) << " ";
}
This is ugly but should work.
Anyway, dealing with the internals of some implementation is usually not a good idea.

Related

How to calculate the length of a mpz_class in bytes?

I want to implement RSA with padding but first I have to find out the length in bytes of the message which is a mpz_class item. Which function would be useful in cpp to accomplish this?
const mpz_class m(argv[1])
What is the length of m in bytes?
Thank you!
#Shawn's comment is correct: The bytes occupied in memory by your class are not what you should be concerned about. Not only does the location of the bytes in memory depend on how your compiler decides to pack them, but their order can also depend on the hardware used.
So, instead of doing some awkward and very fragile memcopy'ish thing that are almost guaranteed to break at some point, you should construct the message yourself (google keyword: Serialization). This also has the advantage that your class can contain stuff that you don't want to add to the message (caches with temp results, or other implementation/optimization stuff).
To the best of my knowledge C++ (unlike f.ex. C#) does not come with build in serialization support, but there are likely to exist libraries that can do a lot of it for you. Otherwise you just have to write your "data member to byte array" functions yourself.
Super simple example:
#include <vector>
#include<iostream>
class myClass
{
int32_t a;
public:
myClass(int32_t val) : a(val) {}
// Deserializer
bool initFromBytes(std::vector<uint8_t> msg)
{
if (msg.size() < 4)
return false;
a = 0;
for (int i = 0; i < 4; ++i)
{
a += msg[i] << (i * 8);
}
return true;
}
// Serializer
std::vector<uint8_t> toBytes()
{
std::vector<uint8_t> res;
for (int i = 0; i < 4; ++i)
{
res.push_back(a >> (i*8));
}
return res;
}
void print() { std::cout << "myClass: " << a << std::endl; }
};
int main()
{
myClass myC(123456789);
myC.print();
std::vector<uint8_t> message = myC.toBytes();
myClass recreate(0);
if (recreate.initFromBytes(message))
recreate.print();
else
std::cout << "Error" << std::endl;
return 0;
}

Properly serializing/unserializing primitives with Endian in mind

For a project, I'm trying to serialize some primitives and send them over the wire to a locally hosted Java server. The Java part isn't so important right now, but what is important is that Java interprets its data as Big Endian, whereas my C++ program is Little Endian.
For a small example, I have a double:
_fields.degLatitude = 50.0;
I then want to serialize that data, but also swap the byte order (to conform to Little->Big Endian):
char buffer[sizeof(struct Fields)];
char* p = buffer;
writeReversedData<double>(&p, _fields.degLatitude);
writeReversedData's implementation is here:
template <typename T> void JsbSimWrapper::writeReversedData(char** bb, T& data)
{
const char* charBuffer = std::to_string(data).c_str();
for (int i = 0; i < sizeof(T); i++)
{
(*bb)[i] = charBuffer[sizeof(T) - i - 1];
}
*bb += sizeof(T);
}
Then just as a test on my side, I wanted to unserialize that data then swap it back to Little Endian:
std::cout << getReversedData<double>(&p) << std::endl;
And here's getReversedData's implementation:
template <typename T> T JsbSimWrapper::getReversedData(char** p)
{
union temp
{
char bytes[sizeof(T)];
T tt;
};
temp t;
for (int i = 0; i < sizeof(T); i++)
{
t.bytes[i] = (*p)[(sizeof(T) - 1) - i];
}
*p += sizeof(T);
return t.tt;
}
Unfortunately, cout just returns huge numbers that aren't even close to 50.0. I also tested the data received on the Java end, and it matches up with the cout in the C++, so it has to be something with my write call.
I know for sure that my getReversedData works, because I tested it with the Java side sending me serialized data, and the C++ side interprets it just fine.
This code stores a pointer to a temporary. It will be dangling as soon as the statement completes.
const char* charBuffer = std::to_string(data).c_str();
Also, in the for loop
for (int i = 0; i < sizeof(T); i++)
{
(*bb)[i] = charBuffer[sizeof(T) - i - 1];
}
there is no rule that the string representation is sizeof(T). For example, a 4 byte int might convert to 10 digits.
It would be much better to store the converted result in a std::string and then use the length() of that string.

Having trouble with pointers

I have some trouble with displaying data from a couple of pointer chars.
int main()
{
char *arr = new char();
for (int t = 0;t < 10;t++)
{
int size = Generator::GetInstance()->Generate(arr);
for (int generateChars = 0;generateChars < size;generateChars++)
{
std::cout << &arr[generateChars] << " ";
}
std::cout << std::endl;
}
return 0;
}
and the Generator class
int Generator::Generate(char *ar)
{
srand(time(NULL));
int size = (rand() % 10)+1;
arr = new char[size];
for (int t = 0;t < size;t++)
{
int path = rand() % 4;
switch (path)
{
case 0:
arr[t] = 'U';
break;
case 1:
arr[t] = 'R';
break;
case 2:
arr[t] = 'D';
break;
case 3:
arr[t] = 'L';
break;
}
}
arr[size] = '\0';
*ar = *arr;
return size;
}
What am I doing wrong this time and why is it displaying junk data each time I am trying something. Sorry if this is something that is answered before already.
If you try to print a char *, C++ assumes that it is a C-style null-terminated string.
It appears that you are allocating (and passing a pointer into) a char array without a terminating null -- so, the I/O library will follow that character with all the junk data that happens to be in memory, until it happens to encounter a null byte.
A simple solution is to pass either a char or a char &, instead of a char *, so that the I/O will correctly interpret it as a single character.
However, that will not fix your memory leaks, which are a separate problem. If you want to use C++ for anything beyond toy programs, you should make a habit of delete-ing anything allocated by new when you no longer need it.
Finally, as mentioned in comments, your code is very non-idiomatic for C++. It is actually terribly educational to play around with low-level code like this, but the standard library has std::string's and all sorts of containers that make it easier to manage your memory correctly, so make sure you learn how to use them, as well as understanding the lower-level features they are built on...
Change your "Generate" functions parameter from a pointer to a pointer reference:
int Generator::Generate(char *& ar)
And change the line:
*ar = *arr;
that is in your "Generate" function to:
ar = arr;
Also remember to clear any dynamic memory that "ar" may point to, when "Generate" is called.
I tested this code and it worked for me...
EDIT In the "for" loop that displays your array, change the line:
std::cout << &arr[generateChars] << " ";
to:
std::cout << arr[generateChars] << " ";

Printing one variable based on the contents of another variable

I have a function similar to the following that takes an integer as a parameter, and using this integer I want to to access a specific variable.
Of course, I can just casebash using the case/switch statement, or if/else, but this is far too dirty and I'm guessing there must be some form of elegant solution.
static unsigned char frame1[] = {...};
static unsigned char frame2[] = {...};
/*...*/
static unsigned char frame30[] = {...};
int select_frame(int frame_number){
/* How can I use frame_number to select the correct frame variable without case/switch, if at all? */
}
I feel as though there could potentially be a stringstream method (or similar), though I'm not sure.
Any suggestions? I'm happy to see solutions in either of C or C++.
EDIT: I feel I should note that most of these arrays will have over 500 individual elements in them, and thus it becomes a little impractical to combine them all into one giant array. If it's possible, I'm looking for a solution that avoids this method, though I'm currently seriously considering it.
If you want to access one of the frames based on an integer, then simply put the frames into an array:
static unsigned char* frames[30];
frames[0] = (unsigned char*)strdup("first frame's char");
frames[1] = (unsigned char*)strdup("second frame's char");
Then you can directly and efficiently index to frames[i].
In C++, a more flexible approach is to use a std::vector<std::string> (as long as your numbers are contiguous) or even a std::map<int, std::string> (good if you have numbers with gaps, or want to insert/erase single elements often during runtime.
EDIT: helper function to create string literal source code from existing unsigned char arrays (off-the-top-of-my-head, tweaks may be needed):
void f(std::ostream& os, unsigned char* p)
{
os << '"';
for ( ; *p; ++p)
{
if (*p < ' ' || *p >= 127)
os << '\\' << std::setw(3) << std::setfill('0') << std::oct << *p;
else if (*p == '\\')
os << "\\\\";
else
os << *p;
}
os << '"';
}
Then just call f(frame1); f(frame2); etc...
I don't think that's possible in C or C++. If you can, why not have a
static unsigned char frame[30][]
You can use 2D array and make it a single frames array variable. Or, if you are ok with using char array then you can use std::string.
static string frames[30] = { "frame1 string", "frame2 string", ..., "frame30 string" };
int select_frame (int frame_number)
{
string &check = frames[frame_number];
}
If your C compiler is C99 conforming, and the array is defined outside
functions, compound literal like the following might meet the purpose.
static unsigned char *frames[] = {
(unsigned char[]){ 1, 2, ... },
(unsigned char[]){ 3, 4, ... },
...
};
int select_frame(int frame_number){
... frames[ frame_number ] ...
}

How to visualize bytes with C/C++

I'm working my way through some C++ training. So far so good, but I need some help reinforcing some of the concepts I am learning. My question is how do I go about visualizing the byte patterns for objects I create. For example, how would I print out the byte pattern for structs, longs, ints etc?
I understand it in my head and can understand the diagrams in my study materials, I'd just like to be able to programaticially display byte patterns from within some of my study programs.
I realize this is pretty trivial but any answers would greatly help me hammer in these concepts.
Thanks.
Edit: I use mostly XCode for my other development projects, but have VMs for Windows7 and fedora core. At work I use XP with visual studio 2005.
( I can't comment as I am still a n00b here :D)
I used unwind's solution which is about what I am looking for. I am also thinking that maybe I could just use the dos DEBUG command as I'd also like to look at chunks for memory too. Again, this is just to help me reinforce what I am learning. Thanks again people!
You can use a function such as this, to print the bytes:
static void print_bytes(const void *object, size_t size)
{
#ifdef __cplusplus
const unsigned char * const bytes = static_cast<const unsigned char *>(object);
#else // __cplusplus
const unsigned char * const bytes = object;
#endif // __cplusplus
size_t i;
printf("[ ");
for(i = 0; i < size; i++)
{
printf("%02x ", bytes[i]);
}
printf("]\n");
}
Usage would look like this, for instance:
int x = 37;
float y = 3.14;
print_bytes(&x, sizeof x);
print_bytes(&y, sizeof y);
This shows the bytes just as raw numerical values, in hexadecimal which is commonly used for "memory dumps" like these.
On a random (might even be virtual, for all I know) Linux machine running a "Intel(R) Xeon(R)" CPU, this prints:
[ 25 00 00 00 ]
[ c3 f5 48 40 ]
This handily also demonstrates that the Intel family of CPU:s really are little endian.
If you are using gcc and X, you can use the DDD debugger to draw pretty pictures of your data structures for you.
Just for completeness, a C++ example:
#include <iostream>
template <typename T>
void print_bytes(const T& input, std::ostream& os = std::cout)
{
const unsigned char* p = reinterpret_cast<const unsigned char*>(&input);
os << std::hex << std::showbase;
os << "[";
for (unsigned int i=0; i<sizeof(T); ++i)
os << static_cast<int>(*(p++)) << " ";
os << "]" << std::endl;;
}
int main()
{
int i = 12345678;
print_bytes(i);
float x = 3.14f;
print_bytes(x);
}
Or if you have the boost lib and want to use lambda evaluations you can do it this way ...
template<class T>
void bytePattern( const T& object )
{
typedef unsigned char byte_type;
typedef const byte_type* iterator;
std::cout << "Object type:" << typeid( T ).name() << std::hex;
std::for_each(
reinterpret_cast<iterator>(&object),
reinterpret_cast<iterator>(&object) + sizeof(T),
std::cout << constant(' ') << ll_static_cast<int>(_1 )&&0xFF );
std::cout << "\n";
}
Most (visual) debuggers have a "View Memory' option. IIRC the one in Xcode is pretty basic, just showing bytes in HEX and ASCII, with a variable line length. Visual Studio (Debug->Windows->Memory in Vs2008) can format the hex portion as different integer lengths, or floating point, change the endianess, and display ANSI or UNICODE text. You can also set just about any number for the width of the window (I think xcode only lets you go to 64 bytes wide) The other IDE I have here at work has a lot of options, though not quite as many as VS.
A little bit by bit console program i whipped up, hope it helps somebody
#include <iostream>
#include <inttypes.h>
#include <vector>
using namespace std;
typedef vector<uint8_t> ByteVector;
///////////////////////////////////////////////////////////////
uint8_t Flags[8] = { 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
void print_bytes(ByteVector Bv){
for (unsigned i = 0; i < Bv.size(); i++){
printf("Byte %d [ ",i);
for (int j = 0;j < 8;++j){
Bv[i] & Flags[j] ? printf("1") : printf("0");
}
printf("]\n");
}
}
int main(){
ByteVector Bv;
for (int i = 0; i < 4; ++i) { Bv.push_back(i); }
print_bytes(Bv);
}
try this:
MyClass* myObj = new MyClass();
int size=sizeof(*myObj);
int i;
char* ptr = obj; // closest approximation to byte
for( i=0; i<size; i++ )
std::cout << *ptr << endl;
Cheers,
jrh.