Re: run time memory map of an application




David Schwartz wrote:
benmidgley@xxxxxxxxxxxxxx wrote:

Can someone tell me if what I am attempting is feasible (my embedded
background says this should be simple but then I dont normally have
pages and virtual memory).

You throw out a bunch of random ideas, but you never tell us what it is
you are actually trying to do. So I'm not sure how anyone could tell
you whether or not what you are attempting is feasible. What is your
goal?

DS

Okay here is my effort to date. Simply I have an application which has
been built as a shared object, in this example libsimple.so. When I
call the main of this application it runs a simple counter
1,2,3,4,5.... with a small delay in a continuous loop. From nm I can
tell you that the data value in that simple shared object is located at
offset 0x2c78 and is an integer.

I start another thread which can use some more interfaces to halt the
loop, print the value and restart the loop within the shared object. I
am trying to backup the data held in the shared object and restore it
in my thread.

All comments welcome. This is an example for illustration.

#include <stdlib.h>
#include <dlfcn.h>
#include <stdio.h>
#include <link.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <fstream.h>
#include <iostream.h>

#define NUL (void *)0
#define N_READ_BUFFERS 6

extern unsigned char * __bss_start;
extern unsigned char * __data_start;
extern unsigned char * _end;

typedef struct {
void * SOHandle;
void (*initHandle)();
void (*mainHandle)();
void (*stopHandle)();
void (*startHandle)();
void (*printHandle)();
char * error;
pid_t pid;
void * p_base_addr;
void * p_data_start;
void * p_data_end;
char SOName[80];
} Sim_Control_Block;

static Sim_Control_Block Sim;

static void dltest(void);
static void Sim_Control_Initialise(void);
static void Get_Shared_Object_Base_Address( void );
static void * Sim_Control_Test( void * threadid );
static void Restore_Snapshot( char * filename );
static void Take_Snapshot( char * filename );
static void Examine_Binary( char * filename , unsigned long offset );

int main(void)
{
if (( Sim.SOHandle = dlopen("lib/libsimple.so",RTLD_LAZY)) != NULL
) {

/* Map the Init function pointer */
*(void **)&Sim.initHandle = dlsym(Sim.SOHandle, "simpleinit");
dltest();

/* Map the Main function pointer */
*(void **)&Sim.mainHandle = dlsym(Sim.SOHandle, "scheduler");
dltest();

/* Map our control functions */
*(void **)&Sim.startHandle = dlsym(Sim.SOHandle,
"start_scheduler");
dltest();
*(void **)&Sim.stopHandle = dlsym(Sim.SOHandle,
"stop_scheduler");
dltest();
*(void **)&Sim.printHandle = dlsym(Sim.SOHandle,
"print_value");
dltest();

/* Gather Sim Info */
Get_Shared_Object_Base_Address();
Sim_Control_Initialise();

/* Initialise Ada Runtime */
Sim.initHandle();

{
pthread_t test_thread;

/* The test routine for the application */
int rc =
pthread_create(&test_thread,NULL,Sim_Control_Test,NULL);

/* Error handler */
if (rc) {
printf("ERROR: return code from pthread_create is
%d\n",rc);
exit(-1);
}
}

/* Run the Application */
Sim.mainHandle();

} else {
/* There was a failure loading the shared library */
printf("%s\n",dlerror());
}

return (0);
}


static void * Sim_Control_Test( void * threadid ) {

printf("Test Thread Started\n");

sleep(5);

Sim.stopHandle();

Sim.printHandle();

Take_Snapshot("snapshot.sdat");

Sim.startHandle();

sleep(5);

Sim.stopHandle();

Sim.printHandle();

Restore_Snapshot("snapshot.sdat");

Sim.printHandle();

Sim.startHandle();

}

static void Examine_Binary( char * filename , unsigned long offset ){
int data_word;
ifstream fp(filename,ios::in | ios::binary);
fp.seekg(offset);
fp.read((char *)&data_word,sizeof(data_word));
printf("The Data Word at offset 0x%x is 0x%x\n",offset,data_word);
fp.close();
}

static void Take_Snapshot( char * filename ) {

printf("\n\n\t Take Snaphot\n");

printf("Stopping Simulation\n");
Sim.stopHandle();

printf("Printing Simulation Data");
Sim.printHandle();

printf("Storing Simulation Data\n");

{
ofstream fp(filename,ios::out | ios::binary);
if ( !fp.write((const char *)Sim.p_data_start,(int)((int
*)Sim.p_data_end - (int *)Sim.p_data_start)) ) {
printf("Failed to write to binary file\n");
exit(-1);
}
fp.close();
}

Examine_Binary(filename,0x2c78);

printf("Restarting Simulation\n\n");
Sim.startHandle();
}


static void Restore_Snapshot( char * filename ) {

printf("\n\n\t Restore Snaphot\n");

printf("Stopping Simulation\n");
Sim.stopHandle();

printf("Printing Simulation Data");
Sim.printHandle();

printf("Restoring Simulation Data\n");

{
ifstream fp(filename,ios::in | ios::binary);
if ( !fp.read((char *)Sim.p_data_start,(int)((int
*)Sim.p_data_end - (int *)Sim.p_data_start)) ) {
printf("Failed to read binary data from file");
}
fp.close();
}

printf("Printing Simulation Data\n");
Sim.printHandle();

printf("Restarting Simulation\n\n");
Sim.startHandle();
Sim.stopHandle();
}

static void dltest(void) {
/* Test For Error */
if (( Sim.error = dlerror()) != NULL ) {
/* Report Error */
printf("%s\n",Sim.error);
/* Exit if found*/
exit(1);
}
}

static void Sim_Control_Initialise(void) {

char mem_file[50];

Sim.pid = getpid();

/* Construct the name of the memory map file for our process */

if ( sprintf(mem_file,"/proc/%d/maps",Sim.pid) > 0 ) {

/* Open the memory map file for this process */

FILE * fp = fopen(mem_file,"r");

if ( fp != NULL ) {

char buffer[N_READ_BUFFERS][80];
int i = 0;

printf("Opened map file:: %s \n",mem_file);

/* Search for the string in the map file relating to our
sim library and
buffer previous 5 reads */

while (
(fscanf(fp,"%s",buffer[i]) != EOF) &&
(strstr(buffer[i],Sim.SOName) == NULL)
) {
i = ++i % N_READ_BUFFERS;
}

/* What we have found is the line describing the .text
segment of our
library so read the next five entries and we have our
description
for the .data segment
*/

for ( i = 0 ; i < N_READ_BUFFERS ; i++ ) {
fscanf(fp,"%s",buffer[i]);
}

printf("%s %s %s %s %s
%s\n",buffer[0],buffer[1],buffer[2],buffer[3],buffer[4],buffer[5]);

/* Now extract the base and end address from the string */
{
char * pDataStart , * pDataEnd ;

printf("Buffer[0]= %s \n ",buffer[0]);

pDataStart = strtok(buffer[0],"-");
pDataEnd = strtok(NULL,"-");

printf("The Data is in the range %s to
%s\n",pDataStart,pDataEnd);

Sim.p_data_start = (void *)strtoul(pDataStart,NULL,16);

Sim.p_data_end = (void *)strtoul(pDataEnd,NULL,16);

}

fclose(fp);

} else {
/* Failed to open the memory map file for this process */
printf("Failed to open the memory map file for this
process, %s\n",mem_file);
exit(1);
}
} else {
printf("Failed to create process memory map file string\n");
exit(1);
}
}

static void Get_Shared_Object_Base_Address( void )
{
link_map * LinkMap;

dlinfo( Sim.SOHandle , // handle
RTLD_DI_LINKMAP ,// request
&LinkMap // void * p
);

dltest();

Sim.p_base_addr = (void *)LinkMap->l_addr;

strcpy(Sim.SOName,LinkMap->l_name);
}

/*
printf("The offset into the dso is 0x2b90\n");
int * pValue = (int *)( ( int ) LinkMap->l_addr + 0x2b90 );
*pValue = 10;
*/

.



Relevant Pages

  • Re: Display class
    ... +D: Author: ks0108 LCD Controller driver ... * GNU General Public License for more details. ... +void cfag12864b_set(unsigned char x, unsigned char y) ... +static void cfag12864b_setbit ...
    (Linux-Kernel)
  • [PATCH 2.6.19-rc1 full] drivers: add LCD support
    ... Adds support for the ks0108 LCD Controller as a device driver. ... * GNU General Public License for more details. ... +static void cfag12864b_setbit(unsigned char state, ...
    (Linux-Kernel)
  • Re: [PATCH V2] display: Driver ks0108 and cfag12864b
    ... "Display" is very generic, people will think it is for video stuff too. ... +- Divide your driver into the controller driver, like ks0108, ... +static void cfag12864b_e(unsigned char state) ...
    (Linux-Kernel)
  • [PATCH] Fixed warnings from -Wmissing-prototypes in HOSTCFLAGS
    ... +static int get_family_id ... +static void print_cgroupstats ... -void cfag12864b_set(unsigned char x, unsigned char y) ... +static void slab_validate(struct slabinfo *s) ...
    (Linux-Kernel)
  • [PATCH V2 unwrapped] display: Driver ks0108 and cfag12864b
    ... +The cfag12864b LCD Display Series don't have a official wiring. ... NEW DISPLAY DRIVERS ... +static void cfag12864b_e ... +static void cfag12864b_cs1(unsigned char state) ...
    (Linux-Kernel)