Linux mmap() 错误

发布于 2024-07-29 12:45:32 字数 2300 浏览 3 评论 0原文

我有一个内存映射文件,我希望从中解析缓冲区的内容。 mmap() 返回成功,我可以使用 fprintf 成功地将缓冲区内容打印到文件中。 但是,当我尝试直接在程序中将缓冲区作为数组访问时,出现分段错误。 为什么会发生这种情况? 这是代码:

  #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);

I have a memory mapped file, from which I wish to parse the contents of the buffer. The mmap() returns success, and I can print out the buffer contents to a file using fprintf successfully. However, when I try to access the buffer as an array in my program directly, I get a segmentation fault. Why is this happening?
Here is the code:

  #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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人骨 2024-08-05 12:45:32

pid_index 有多大? 您是否可能读了 100 多个字符却找不到空格?

对了,这个

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

应该是这个

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

吧?

How big is pid_index getting? Are you perhaps reading more than 100 characters without finding a space?

By the way, this

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

should probably be this

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

No?

风吹短裙飘 2024-08-05 12:45:32
  • 您没有检查 n 是否保持 < struct_ptt_dump.st_size
  • 您没有检查 pid_index 是否保持 < 100
  • You aren't checking that n stays < struct_ptt_dump.st_size
  • You aren't checking that pid_index stays < 100
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文