免费注册 查看新帖 |

Chinaunix

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

gdb调试程序时候,运行next出现问题! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-08 21:37 |只看该作者 |倒序浏览
2.6.11-1.1369_FC4
这是程序:

  1. #include <stdio.h>
  2. #include <string.h>
  3. void my_print(char *);
  4. void my_print2(char *);
  5. int main(int argc ,char *argv[])
  6. {
  7.   char my_string[]="hello there";
  8.   my_print(my_string);
  9.   my_print2(my_string);
  10.   return 0;
  11. }
  12. void my_print(char *string)
  13. {
  14.   printf("the string is :%s\n",string);
  15. }
  16. void my_print2(char *string)
  17. {
  18.   char *string2;
  19.   int size,i;
  20.   size=strlen(string);
  21.   string2=(char *)malloc((size+1)*sizeof(char *));
  22.   for(i=0;i<size;i++)
  23.     string2[size-i]=string[i];        /*backword string*/
  24.   string2[size+1]='\0';
  25.   printf("the string backword is :%s\n",string2);
  26. }
复制代码

然后编译:
  1. [root@localhost code]# gcc -o greeting -g greeting.c
  2. greeting.c: 在函数 ‘my_print2’ 中:
  3. greeting.c:21: 警告:内建函数 ‘malloc’ 不兼容的隐式声明
  4. [root@localhost code]# ./greeting
  5. the string is :hello there
  6. the string backword is :
  7. [root@localhost code]#
复制代码


然后GDB        

[code]
[root@localhost code]# gdb greeting
GNU gdb Red Hat Linux (6.3.0.0-1.21rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".

(gdb) list
1       #include <stdio.h>
2       #include <string.h>
3       void my_print(char *);
4       void my_print2(char *);
5       int main(int argc ,char *argv[])
6       {
7         char my_string[]="hello there";
8         my_print(my_string);
9         my_print2(my_string);
10        return 0;
(gdb) list
11      }
12      void my_print(char *string)
13      {
14        printf("the string is :%s\n",string);
15      }
16      void my_print2(char *string)
17      {
18        char *string2;
19        int size,i;
20        size=strlen(string);
(gdb) list
21        string2=(char *)malloc((size+1)*sizeof(char *));
22        for(i=0;i<size;i++)
23          string2[size-i]=string;        /*backword string*/
24        string2[size+1]='\0';
25        printf("the string backword is :%s\n",string2);
26      }
(gdb) break 23
Breakpoint 1 at 0x8048466: file greeting.c, line 23.
(gdb) run
Starting program: /root/code/greeting
Reading symbols from shared object read from target memory...done.
Loaded system supplied DSO at 0x336000
the string is :hello there

Breakpoint 1, my_print2 (string=0xbf9450d4 "hello there") at greeting.c:23
23          string2[size-i]=string;        /*backword string*/
(gdb) watch string2[size-i]
Hardware watchpoint 2: string2[size - i]
(gdb) next
warning: Could not remove hardware watchpoint 2.
Warning:
Could not insert hardware watchpoint 2.
Could not insert hardware breakpoints:
You may have requested too many hardware breakpoints/watchpoints.


(gdb)
红色部分就是出现的问题,无法继续下去

[ 本帖最后由 rainballdh 于 2005-11-8 21:50 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2005-11-08 21:53 |只看该作者
离题一下:

头文件加<stdlib.h> -----------> malloc是在这头文件中定义的

在my_print2中
1.string2=(char *)malloc((size+1)*sizeof(char *))  
 ----->string2=(char *)malloc((size+1)*sizeof(char))

2.怎么没有free(string2);

论坛徽章:
0
3 [报告]
发表于 2005-11-08 21:58 |只看该作者
谢谢,加上#include <stdlib.h>就没有warning,还有free()
程序本来有问题,就是通过gdb来分析

[ 本帖最后由 rainballdh 于 2005-11-8 22:03 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2005-11-09 11:30 |只看该作者
怎么没有几个人回复呀!

论坛徽章:
0
5 [报告]
发表于 2005-11-09 11:43 |只看该作者
[quote][quote]原帖由 rainballdh 于 2005-11-8 21:37 发表
2.6.11-1.1369_FC4
这是程序:

  1. #include <stdio.h>
  2. #include <string.h>
  3. void my_print(char *);
  4. void my_print2(char *);
  5. int main(int argc ,char *argv[])
  6. { ... [/quote][/quote]
  7. 该问题我遇见过,

  8. [code] (gdb) watch string2[size-i]
复制代码


问题是:你这里watch 的参数是一个不断变化的变量(因为i是不断变化的)。
最好指定一个具体的变量来监视。
对于数组,最好是其中一个具体的元素。例如
  1. (gdb) watch string2[1]
复制代码

至于说具体的原因,我再找找看。

[ 本帖最后由 kernelxu 于 2005-11-9 11:44 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2005-11-09 11:51 |只看该作者
我也遇到类似问题!顶一下!

论坛徽章:
0
7 [报告]
发表于 2005-11-09 11:52 |只看该作者
你逆向赋值肯定有问题,string2[0]没有值,i最大取到size-1,此时string2[size-(size-1)] = string2[1];也就是string2的最左值,所以string2[0]没有值;
应该是string2[size-i-1] = string[i];最后应该string2[size] = '';注意长度不是从0开始的。“abc"的len=3,但是[3] = '';
从你的警告信息来看,是不能下到断点,不能watch.可以不用watch,用print单步试试.

论坛徽章:
0
8 [报告]
发表于 2005-11-09 11:53 |只看该作者
怪了居然打不出来,''里面都是

论坛徽章:
0
9 [报告]
发表于 2005-11-09 12:14 |只看该作者
硬件断点的数目肯定是有限制的。
先说一下什么是硬件断点。

在intel的80386处理器里面实现了调试寄存器dr0-dr7,和测试寄存器tr.

其中dr0-dr3四个寄存器用于存储4个32位的线性断点地址,d4-dr5保留。指向指令或数据的断点地址不断与DR0~DR4中的地址进行比较,当两个地址符合时,80386就会产生调试中断。我记得tdebug就是利用这个来实现调试的。而DR6~DR7用于存储一些控制信息,具体的情况你可以查手册。其它处理器上、应当有同等性质的调试机制。调试器可能使用dr0~dr3中的某几个,不一定会全使用,印象里tdebug好像使用了2个。

所以,硬件断点的数目是有限的。

watch string2[size-i]这条调试指令应当是定义了过多的硬件断点,使得寄存器不够用,所以会产生上述信息。

评分

参与人数 1可用积分 +1 收起 理由
win_hate + 1 我很赞同

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP