免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3902 | 回复: 6
打印 上一主题 下一主题

[C] 程序编译后执行的结果,跟用gdb单步调试的结果不一样,求解?? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-01 16:05 |只看该作者 |倒序浏览
用C代码做测试脚本,gdb单步是pass,然而执行却是fail,可能是什么原因引起的?等待高手

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:45
2 [报告]
发表于 2011-11-01 16:07 |只看该作者
恭喜lz,俺之前也遇到过这样的情况{:3_192:}

论坛徽章:
0
3 [报告]
发表于 2011-11-01 16:38 |只看该作者
多进/线程?

论坛徽章:
0
4 [报告]
发表于 2011-11-01 16:54 |只看该作者
回复 3# 鸡丝拌面


    用到fork函数   child=fork(); if(child==0){.........}
执行时能进入if,但gdb调试时不能进入

论坛徽章:
11
未羊
日期:2013-12-16 12:45:4615-16赛季CBA联赛之青岛
日期:2016-04-11 19:17:4715-16赛季CBA联赛之广夏
日期:2016-04-06 16:34:012015亚冠之卡尔希纳萨夫
日期:2015-11-10 10:04:522015亚冠之大阪钢巴
日期:2015-07-30 18:29:402015亚冠之城南
日期:2015-06-15 17:56:392015亚冠之卡尔希纳萨夫
日期:2015-05-15 15:19:272015亚冠之山东鲁能
日期:2015-05-14 12:38:13金牛座
日期:2014-12-04 15:34:06子鼠
日期:2014-10-16 13:40:4715-16赛季CBA联赛之八一
日期:2016-07-22 09:41:40
5 [报告]
发表于 2011-11-01 16:58 |只看该作者
晕, 查查那个 fork_follow_mode(或者 follow_fork_mode)???

论坛徽章:
0
6 [报告]
发表于 2011-11-01 17:21 |只看该作者
By default, when a program forks, gdb will continue to debug the parent process and the child process will run unimpeded.

If you want to follow the child process instead of the parent process, use the command set follow-fork-mode.

set follow-fork-mode mode
Set the debugger response to a program call of fork or vfork. A call to fork or vfork creates a new process. The mode argument can be:
parent
The original process is debugged after a fork. The child process runs unimpeded. This is the default.
child
The new process is debugged after a fork. The parent process runs unimpeded.

http://sourceware.org/gdb/onlinedocs/gdb/Forks.html

论坛徽章:
0
7 [报告]
发表于 2011-11-01 17:53 |只看该作者
#define _XOPEN_SOURCE 600

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include "posixtest.h"

#define TNAME "mmap/11-4.c"

int main()
{
  char tmpfname[256];
  long  page_size;
  long total_size;

  void *pa = NULL, *pa_2 = NULL;
  void *addr = NULL;
  size_t len;
  int flag;
  int fd, fd_2;
  off_t off = 0;
  int prot;

  pid_t child;
  char *ch = NULL, *ch_2 = NULL;
int exit_val;

  page_size = sysconf(_SC_PAGE_SIZE);

  /* Size of the file to be mapped */
  total_size = page_size / 2;

  /* mmap will create a partial page */
  len = page_size / 2;

  snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_11_5_%d",
             getpid());
  child = fork();
  if (child == 0)
  {
    /* Create shared object */
    unlink(tmpfname);
    fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL,
              S_IRUSR | S_IWUSR);
    if (fd == -1)
    {
      printf(TNAME " Error at open(): %s\n",
             strerror(errno));
      exit(PTS_UNRESOLVED);
    }
    if(ftruncate(fd, total_size) == -1) {
      printf(TNAME " Error at ftruncate(): %s\n", strerror(errno));
      return PTS_UNRESOLVED;
    }

    prot = PROT_READ | PROT_WRITE;
    flag = MAP_SHARED;
off = 0;
    pa = mmap(addr, len, prot, flag, fd, off);
    if (pa == MAP_FAILED)
    {
      printf("Test FAIL: " TNAME " Error at mmap(): %s\n",
             strerror(errno));
      return PTS_FAIL;
    }
    printf("pa: %p\n", pa);
    /* Check the patial page is ZERO filled */
    ch = pa + len + 1;
    if (*ch != 0)
    {
      printf("Test Fail: " TNAME " The partial page at the end of an object "
              "is not zero-filled\n");
      return PTS_FAIL;
    }

    /* Write the partial page */
    *ch = 'b';
    munmap (pa, len);
    close (fd);
    return PTS_PASS;

  }
  wait(&exit_val);
  if (!(WIFEXITED(exit_val) && (WEXITSTATUS(exit_val) == PTS_PASS)))
{
    unlink(tmpfname);
    return PTS_FAIL;
  }

  fd_2 = open(tmpfname, O_RDWR, 0);
  unlink(tmpfname);

  prot = PROT_READ | PROT_WRITE;
  flag = MAP_SHARED;
  off = 0;
  pa = mmap(addr, len, prot, flag, fd_2, off);
  pa_2 = mmap(addr, len, prot, flag, fd_2, off);
  if (pa_2 == MAP_FAILED)
  {
    printf("Test FAIL: " TNAME " Error at 2nd mmap(): %s\n",
            strerror(errno));
    exit(PTS_FAIL);
  }

  printf("pa_2: %p\n", pa_2);
  ch_2 = pa_2 + len + 1;
  if (*ch_2 == 'b')
  {
        printf("Test Fail: " TNAME " Modification of the partial page "
           "at the end of an object is written out\n");
        exit(PTS_FAIL);
  }
  close (fd_2);
  munmap (pa_2, len);
  printf("Test Passed\n");
  return PTS_PASS;
}

输出结果为:pa: 0xb77c4000
pa_2: 0xb77c3000
Test Fail: mmap/11-4.c Modification of the partial page at the end of an object is written out
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP