How to visualize bytes with C/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.

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;
}

Programmatically get debug information

Using
#include <execinfo.h>
one can access methods for unwinding the stack, at least on a most linux configurations. However, this allows one to fetch a char * to some NTBS (null terminated byte string) which shows some information, but not all of it, especially not:
file names
line numbers
function names
I have written a bash-script which can deduce a line number and a file using objdump and the text address of the instruction, however its use is tedious as I have to copy paste the address of multiple stack frames to it manually.
Given that g++ allows one to include debugging symbols with the -g command line option, how can I parse and access them in c++ programmatically? Debuggers like gdb and valgrind are also capable of accessing this information at runtime somehow: I'm assuming they use some library for that or if they implement it themselves export the functionality as an API. For example, valgrind defines some interesting function declarations in include/pub_tool_debuginfo.h. Unfortunately, I couldn't find anything else. I created this as a starting point:
#include <execinfo.h>
namespace stck {
class stacktrace_t {};
stacktrace_t stacktrace;
std::ostream &operator<<(std::ostream &out, stacktrace_t) {
out << "stacktrace:\n";
size_t max = 256;
void **stackframes = new void *[max];
size_t numel;
while ((numel = backtrace(stackframes, max)) >= max) {
max *= 2;
delete[] stackframes;
stackframes = new void *[max];
}
char **symbols = backtrace_symbols(stackframes, numel);
for(size_t i = 0; i < numel; ++i)
out << symbols[i] << '\n';
delete[] stackframes;
return out;
}
}
source: http://ideone.com/RWoADT
Are there any suggestions to append this code to also output human readable debugging information?
As a note, I'm implementing this for usage with mex, a matlab compiler which uses g[++|cc]. Whenever I'm using the functionality, the program is in a 'good' state, that is an error is detected but noting really has has happened; like a segmentation fault.
For example, one could check if the argument of sqrt is non-negative, and if not use the stck::stacktrace to show where this happened.
update
I think the information is not directly available at runtime (although I'm not sure), but is only available in the executable file, not in the executable text in memory. (Please correct me if I'm wrong.
Hence I think there is no way around parsing the executable file, for example via addre2line:
namespace stck {
std::string getstackframe(char *frame) {
std::string fr(frame);
size_t loc0 = fr.find("(");
size_t loc1 = fr.find(")");
std::stringstream ss;
ss << "addr2line -e " << fr.substr(0, loc0) << " -pfC " << fr.substr(loc0 + 2, loc1 - loc0 - 2);
FILE* pipe = popen(ss.str().c_str(), "r");
char buffer[128];
std::stringstream result;
while(!feof(pipe))
if (fgets(buffer, 128, pipe) != NULL)
result << buffer;
pclose(pipe);
return result.str();
}
class stacktrace_t {};
stacktrace_t stacktrace;
std::ostream &operator<<(std::ostream &out, stacktrace_t) {
out << "stacktrace:\n";
size_t max = 256;
void **stackframes = new void *[max];
size_t numel;
while ((numel = backtrace(stackframes, max)) >= max) {
max *= 2;
delete[] stackframes;
stackframes = new void *[max];
}
char **symbols = backtrace_symbols(stackframes, numel);
for(size_t i = 0; i < numel; ++i)
out << getstackframe(symbols[i]);
out << '\n';
delete[] stackframes;
return out;
}
}

Assigning value to passed references using variable argument list (error in VS2010)

The following code compiles and runs in Code::Blocks, but issues and error in VS2010:
"Undhandled exception at 0x770815de in test2.exe: 0xC0000005: Access violation writing to location 0x00000002."
I realise the code is sort of dangerous, it's basically prototyping an idea I have for another project. What I want to be able to do is pass a reference to any given number of ints followed by a value. Then put this value into the referenced ints and bob's your uncle. And it works, which is nice. But not in VS2010 which bothers me. I'm not the most experience with pointers, so I don't know if I'm doing something wrong or it's just this kind of operation is not something that VS2010 is fond of. Which is a problem because the project I'm testing this for is all in VS2010! So I need this to work for that!
EDIT: I'm sorry, I'm new to the Code:Blocks thing. I guess I should specify which compiler I use in Code::Blocks? :D I use the miniGW (or something) implementation of the GNU GCC Compiler (or something like that). I hope it makes sense to you experience Code::Blocks users!
#include <iostream>
#include <stdarg.h>
using namespace std;
void getMonkey(int Count, ... )
{
int test;
va_list Monkeys;
va_start(Monkeys, Count );
for(int i = 0; i < (Count / 2); i++ )
{
*va_arg(Monkeys, int*) = va_arg(Monkeys, int);
}
va_end(Monkeys);
}
int main()
{
int monkey1 = 0;
int monkey2 = 0;
int monkey3 = 0;
getMonkey(6, &monkey1, 2, &monkey2, 4, &monkey3, 5);
cout << monkey1 << " " << monkey2 << " " << monkey3;
return 0;
}
Turns out lvalues and rvalues are NOT evaluated in the order I assumed! TY stackoverflow!
Updated getMonkey method:
void getMonkey(int Count, ... )
{
int test;
va_list Monkeys;
va_start(Monkeys, Count );
for(int i = 0; i < (Count / 2); i++ )
{
int* tempMonkeyPtr = va_arg(Monkeys, int*); //herp
*tempMonkeyPtr = va_arg(Monkeys, int); //derp
}
va_end(Monkeys);
}
Yey! I'm getting a hang of this pointer business me thinks!

array of 2s power using template in c++

Can we create the following array or something similar using template in c++ at compile time.
int powerOf2[] = {1,2,4,8,16,32,64,128,256}
This is the closest I got.
template <int Y> struct PowerArray{enum { value=2* PowerArray<Y-1>::value };};
but then to use I need something like PowerArray <i> which compiler gives error as i is dynamic variable.
You can use BOOST_PP_ENUM for this:
#include <iostream>
#include <cmath>
#include <boost/preprocessor/repetition/enum.hpp>
#define ORDER(z, n, text) std::pow(z,n)
int main() {
int const a[] = { BOOST_PP_ENUM(10, ORDER, ~) };
std::size_t const n = sizeof(a)/sizeof(int);
for(std::size_t i = 0 ; i != n ; ++i )
std::cout << a[i] << "\n";
return 0;
}
Output ideone:
1
2
4
8
16
32
64
128
256
512
This example is a modified version (to suit your need) of this:
Trick : filling array values using macros (code generation)
Nothing against using BOOST_PP_ENUM but I think your going for more of the kind of code I will show you.
What I would do, is I would make a constructor of your type class which just sets the array to the stuff you need. That way it does it as soon as the program builds and it stays nice and neat. AKA the proper way.
class Power
{
public:
Power();
void Output();
// Insert other functions here
private:
int powArray[10];
};
Then the implementation would be with a basic "for loop" to load them into the array you just created.
Power::Power()
{
for(int i=0;i<10;i++){
powArray[i] = pow(2,i);
}
}
void Power::Output()
{
for(int i=0;i<10;i++){
cout<<powArray[i]<<endl;
}
}
Hopes this helps...

Dumping the memory contents of a object

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.