gdb学习6:给函数名设置断点

版权声明:欢迎转发,转发请标明出处 https://blog.csdn.net/weixin_39465823/article/details/88922213

gdb真的是调试代码的神器,不但可以给代码行设置断点,也可以给函数名设置断点。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *MyStrCopy1(const char *s1)
{
	if(NULL == s1)
	{
		return "string is NULL";
	}
	else
	{

		char *s2 = (char *)malloc(strlen(s1)+1);
		if(NULL == s2)
		{
			return "malloc error";
		}
		char *p = s2;
		while((*s2++ = *s1++));
		return p;
	}	
}

char *MyStrCopy2(const char *s1,char *s2)
{
	char *p = s2;
	while(*s2++ == *s1++);
	//while(*s2++ = *s1++);
	return p;
}

int main(int argv,char **argc)
{
	const char *str1 = NULL;
	const char *str2 = "string copying";
	char str3[strlen(str2)+1];
	//char str3[20];
	printf("%s\n",MyStrCopy1(str1));
	printf("%s\n",MyStrCopy1(str2));
	char *res = MyStrCopy2(str2,str3);
	printf("%s\n",res);
	return 0;
}

调试信息:

(gdb) b 41
Breakpoint 1 at 0x8a9: file strcopy.c, line 41.
(gdb) b MyStrCopy2
Breakpoint 2 at 0x7c7: file strcopy.c, line 27.
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000000008a9 in main at strcopy.c:41
2       breakpoint     keep y   0x00000000000007c7 in MyStrCopy2 
                                                   at strcopy.c:27
(gdb) r
Starting program: /home/gyz/mc/strcopy 
string is NULL
string copying

Breakpoint 1, main (argv=1, argc=0x7fffffffe228) at strcopy.c:41
41		char *res = MyStrCopy2(str2,str3);
(gdb) c
Continuing.

Breakpoint 2, MyStrCopy2 (s1=0x555555554990 "string copying", 
    s2=0x7fffffffe0c0 "\001") at strcopy.c:27
27		char *p = s2;
(gdb) 

上面的例子中给MyStrCopy2()函数设置了断点。

猜你喜欢

转载自blog.csdn.net/weixin_39465823/article/details/88922213