c - Passing struct to main via char* pointer -


what i'm ask bit hacky (in it's poor method of attacking problem) - understand , not plan on using long-term solution, proof-of-concept.

that said, i'm working on project uses qemu foundation migrate processes (eventually threads) 1 machine another. i'm starting process on native machine, pausing using ptrace, , copying cpu's registers , stack , pushing values newly-created instance of qemu (with same underlying architecture, i.e. x86-64 --> x86_64, arm64 --> arm64). (eventually) resume execution in qemu.

i've reached stage need pass registers + stack qemu, i've hit bit of wall; ideally, break apart qemu , compile entire program part of own program, proof-of-concept comes play. qemu massive program, , breaking apart/reconstructing makefiles i'm not terribly keen on approaching right now. so...

is possible me fill struct registers + stack in program, create pointer struct, cast pointer char*, , pass char* pointer execlp, recast in (a modified version of) qemu? goal access values within qemu. example:

struct regs_and_stack my_struct = {...}; struct regs_and_stack *my_struct_ptr = &my_struct; execlp("qemu", "qemu", "test", "100000", (char*)my_struct_ptr); 

i can post rest of code better sense of big picture, if requested. always, thank help!!!

edit

i've identified point in qemu main function (linux-users/main.c) can pop final pointer argv before reaches point parses through options; i'll use information later in program's execution. question how struct main function in first place.

here if understood want serialize structs , deserialize them on other end. following code fragment form answers of question serialization of struct gonna solve problem , have edit own serialize , deserialize functions because depend strut's structuer. skip q variable enough open place structs variable fit in text sequence.

#include <iostream> #include <cstring>  #define bufsize 512 #define packetsize sizeof(msg)  using namespace std;  typedef struct msg {     int type;     int priority;     int sender;     char message[bufsize]; }msg;  void serialize(msg* msgpacket, char *data); void deserialize(char *data, msg* msgpacket); void printmsg(msg* msgpacket);  int main() {     msg* newmsg = new msg;     newmsg->type = 1;     newmsg->priority = 9;     newmsg->sender = 2;     strcpy(newmsg->message, "hello server\0");     printmsg(newmsg);      char data[packetsize];      serialize(newmsg, data);      msg* temp = new msg;     deserialize(data, temp);     printmsg(temp);      return 0; }      void     serialize(msg* msgpacket, char *data) {     int *q = (int*)data;         *q = msgpacket->type;       q++;         *q = msgpacket->priority;   q++;         *q = msgpacket->sender;     q++;      char *p = (char*)q;     int     = 0;     while (i < bufsize)     {         *p = msgpacket->message[i];         p++;         i++;     } }  void deserialize(char *data, msg* msgpacket) {     int *q = (int*)data;         msgpacket->type = *q;       q++;         msgpacket->priority = *q;   q++;         msgpacket->sender = *q;     q++;      char *p = (char*)q;     int = 0;     while (i < bufsize)     {         msgpacket->message[i] = *p;         p++;         i++;     } }  void printmsg(msg* msgpacket) {     cout << msgpacket->type << endl;     cout << msgpacket->priority << endl;     cout << msgpacket->sender << endl;     cout << msgpacket->message << endl; } 

this link useful too: stackoverflow.com/questions/1653681/serialization-deserialization-of-a-struct-to-a-char-in-c


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -