Domanda

Ho un file di memoria mappata, da cui desidero analizzare il contenuto del buffer. La mmap () restituisce il successo, e posso stampare il contenuto del buffer in un file utilizzando fprintf successo. Tuttavia, quando provo ad accedere al buffer come un array nel mio programma direttamente, ottengo un errore di segmentazione. Perché sta succedendo? Ecco il codice:

  #define PTT_DUMP "/home/dhruv/somefile"     
  .
  .
  .

  int fd_ptt_dump = open(PTT_DUMP, O_RDONLY);
  struct stat struct_ptt_dump;
  fstat(fd_ptt_dump, &struct_ptt_dump);
  printf("\n\n\t\t\t --- The size of the dump is = %d -----\n\n",    struct_ptt_dump.st_size);
  char *membuffer;
  char pid_num[100];
  char cycles[100], instr[100], cpi[100] ;
  int pid_index =0;
  int cycles_index = 0;
  int instr_index = 0;
  int cpi_index = 0 ;
  int len = (int)struct_ptt_dump.st_size;
  int newline_count = 0;
  int n = 0;
  if( (membuffer = mmap(0, struct_ptt_dump.st_size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd_ptt_dump, 0)) == (caddr_t) -1)
   err_sys("mmap error");

  /* If the following is uncommented, it prints fine */

  /*
  for ( n =0; n < struct_ptt_dump.st_size ; n++)      
  fprintf(fp_logfile,"%c", membuffer[n]);
  */

  /* But, I really want to access the buffer as an array for speed if possible */
  /* Here is where everything goes haywire */

  while (newline_count != 5)
   if ( membuffer[n++] == '\n' )
    newline_count++ ;

  /* printf returns OK, and I am able to skip newlines */

  printf("\n\n newlines = %d\n\n 10 buffer characters", newline_count);

  int k =0;


  /* All code from here gives segmentation fault */

  while ( membuffer[n++] != ' ' )
pid_num[pid_index++] = membuffer[n] ;

  /* Even if I comment out everything from here on, the above assignment itself results in a segmentation fault */


  pid_num[pid_index] = '\0';

  printf("\n\n pid = %s", pid_num);



  while ( membuffer[len--] != ' ' )
if ( membuffer[len] != '\n' )
  cpi[cpi_index++] = membuffer[len];

  cpi[cpi_index] = '\0';

  for( ; membuffer[len] == ' ' ; len-- )
;

  for(n=0; membuffer[len-n] != ' '; n++)
instr[instr_index++] = membuffer[len-n] ;

  instr[instr_index] = '\0' ;
  n++;

  for( ; membuffer[len-n] != ' ' ; n++)
cycles[cycles_index++] = membuffer[len-n];

  cycles[cycles_index] = '\0';

  printf("\n\n\t\t\t\t ********** buffer values *************\n\n");
  printf("\t\t\t\tdominant pid = %s\t cycles = %s\t instructions = %s\t cpi = %s \n\n", pid_num, cycles, instr, cpi);



  fflush(STDOUT_FILENO);
È stato utile?

Soluzione

Quanto grande è pid_index sempre? Stai forse leggendo più di 100 caratteri senza trovare uno spazio?

A proposito, questo

while ( membuffer[n++] != ' ' )
    pid_num[pid_index++] = membuffer[n] ;

dovrebbe probabilmente essere questo

while ( membuffer[n] != ' ' )
    pid_num[pid_index++] = membuffer[n++] ;

No?

Altri suggerimenti

  • Non sta verificando che n soggiorni
  • Non sta verificando che pid_index rimane <100
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top