The stack error is hard to debug, but we can debug it assisted by the tool provided by GCC. As we known, there are three options to debug the stack in gcc.

  • -fstack-protector
  • -fstack-protector-strong
  • -fstack-protector-all To ALL functions

Here, we only check the –fstack-protector-all, see code example below.

#include <stdio.h>
#include <string.h> void bar(char* str)
{
char buf[4];
strcpy(buf, str);
} void foo()
{
printf("It survived!");
} int main(void)
{
bar("Longer than 4.");
foo();
return 0;
}

  

The code is wrong implementation obviously, it assigns more data to 4 byte memory in bar(). Compile it by gcc -ggdb -fstack-protector-all stack.c -o stack.

Debug it, you will find out the stack error.

gdb stack

GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.3) 7.7.1

Copyright (C) 2014 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.  Type "show copying"

and "show warranty" for details.

This GDB was configured as "x86_64-linux-gnu".

Type "show configuration" for configuration details.

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/>.

Find the GDB manual and other documentation resources online at:

<http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".

Type "apropos word" to search for commands related to "word"...

Reading symbols from stack...done.

(gdb) run

Starting program: /home/zjb/Documents/test/stack

*** stack smashing detected ***: /home/zjb/Documents/test/stack terminated

Program received signal SIGABRT, Aborted.

0x00007ffff7a47c37 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56

56            ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.

(gdb) bt full

#0  0x00007ffff7a47c37 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56

resultvar = 0

pid = 25535

selftid = 25535

#1  0x00007ffff7a4b028 in __GI_abort () at abort.c:89

save_stage = 2

act = {__sigaction_handler = {sa_handler = 0x7ffff7a21ca8, sa_sigaction = 0x7ffff7a21ca8}, sa_mask = {__val = {140737353977856, 140737488346240, 140737351925564,

140733193388032, 524302793, 1, 3, 140737353979144, 140737351927990, 0, 140737488345920, 140737347931440, 140737488346192, 140737347942264, 8192231,

140737488346176}}, sa_flags = -136470528, sa_restorer = 0x0}

sigs = {__val = {32, 0 <repeats 15 times>}}

#2  0x00007ffff7a842a4 in __libc_message (do_abort=do_abort@entry=1, fmt=fmt@entry=0x7ffff7b93db0 "*** %s ***: %s terminated\n") at ../sysdeps/posix/libc_fatal.c:175

ap = {{gp_offset = 32, fp_offset = 0, overflow_arg_area = 0x7fffffffdd50, reg_save_area = 0x7fffffffdce0}}

fd = 3

on_2 = <optimized out>

list = <optimized out>

nlist = <optimized out>

cp = <optimized out>

written = <optimized out>

#3  0x00007ffff7b1f87c in __GI___fortify_fail (msg=<optimized out>, msg@entry=0x7ffff7b93d98 "stack smashing detected") at fortify_fail.c:38

do_abort = 1

#4  0x00007ffff7b1f820 in __stack_chk_fail () at stack_chk_fail.c:28

No locals.

#5  0x000000000040062f in bar (str=0x400751 "Longer than 4.") at stack.c:8

buf = "Long"

#6  0x000000000040068e in main () at stack.c:17

No locals.

(gdb) help bt

Print backtrace of all stack frames, or innermost COUNT frames.

With a negative argument, print outermost -COUNT frames.

Use of the 'full' qualifier also prints the values of the local variables.

Use of the 'no-filters' qualifier prohibits frame filters from executing

on this backtrace.

Using gcc stack debug skill的更多相关文章

  1. Stack Overflow Skill IQ Testing All In One

    Stack Overflow Skill IQ Testing All In One Pluralsight IQ | Stack Overflow https://www.pluralsight.c ...

  2. GCC: compilation process..

    gcc -Iproj/src myfile.c -o myfile gcc -c myfile.c "compile without linking gcc -D DEBUG myfile. ...

  3. golang debug调试

    1. debug by gdb: office doc download the runtime-gdb file. $ wget -q -O - https://golang.org/src/run ...

  4. link options and how g++ is invoked gcc g++

    yum install gcc yum install gcc-c++ yum reinstall gcc gcc-c++ Downloading packages:(1/2): gcc-c++-4. ...

  5. gcc栈溢出保护机制:stack-protector

    关键词:stack-protector.stack-protector-strong.stack-protector-all等等. 1. gcc栈保护机制stack-protector简介 gcc提供 ...

  6. 结合python版本安装python-devel gcc和g++的区别 安装前做yum搜索

    [test@ecs autocloudservices]# yum install python-develLoaded plugins: fastestmirrorLoading mirror sp ...

  7. 清华大学OS操作系统实验lab1练习知识点汇总

    lab1知识点汇总 还是有很多问题,但是我觉得我需要在查看更多资料后回来再理解,学这个也学了一周了,看了大量的资料...还是它们自己的80386手册和lab的指导手册觉得最准确,现在我就把这部分知识做 ...

  8. C++成员函数指针错误用法警示(成员函数指针与高性能的C++委托,三篇),附好多评论

    今天做一个成绩管理系统的并发引擎,用Qt做的,仿照QtConcurrent搞了个模板基类.这里为了隐藏细节,隔离变化,把并发的东西全部包含在模板基类中.子类只需注册需要并发执行的入口函数即可在单独线程 ...

  9. linux c 链接详解1-多目标文件链接

    1. 多目标文件的链接 摘自:linux c编程一站式学习 http://learn.akae.cn/media/index.html 可以学会在linux下将多个c语言文件一起编译. 现在我们把例  ...

随机推荐

  1. Android Touch事件之二:dispatchTouchEvent()和onTouchEvent()篇

    2015-12-01 15:06:14 Android Touch事件第一篇:Touch事件在父ViewGroup和子View之间的传递简单分析了事件的传递流程,这次深入了解下dispatchTouc ...

  2. HTML语义化简介思维导图

  3. 5.5 C++重载赋值操作符

    参考:http://www.weixueyuan.net/view/6383.html 总结: 重载赋值操作符同重载类的是拷贝构造函数的原因是一样,将一个对象拷贝给另一个对象,同时当类中存在指针类型的 ...

  4. 4.5 C++重载、覆盖和遮蔽

    参考:http://www.weixueyuan.net/view/6375.html 总结: 函数签名包括函数名和函数参数的个数.顺序以及参数数据类型. 需要注意的是函数签名并不包含函数返回值部分, ...

  5. L313 珊瑚裸鼠灭绝

    This week the Australian government declared the extinction of a tiny rodent called Bramble Cay melo ...

  6. 关于 数据库 my_slq的 安装及其卸载

    安装的时候 注意事项 自定后 根据电脑的系统版本 选择32  或者64  然后选择→方向键 密码默认是123456  或者  123123 查看装的数据库是否安装好了 如何完全卸载 mysql 数据库 ...

  7. linux之ls、ll

    ls == list,根据不同的选项,列举指定目录或文件的相关信息,是Unix/Linux下最常用的命令之一,cd到某一目录下后执行的第一个命令. ls命令格式:ls [OPTION]... [FIL ...

  8. LeetCode—66、88、118、119、121 Array(Easy)

    66. Plus One Given a non-negative integer represented as a non-empty array of digits, plus one to th ...

  9. 【Python】unittest-2-断言

    Unittest中的断言 1.  python unintest单元测试框架提供了一整套内置的断言方法. (1)如果断言失败,则抛出一个AssertionError,并标识该测试为失败状态 (2)如果 ...

  10. Python根据路径名称获取文件的名称以及所在的路径

    大神一看题目就知道用python中的string.split('\'),记得之前处理大量的文件的时候,有时候有几十万的文本文件,经常会读取获取名称,并且保存为名字一样的另外一种格式的文件 其实pyth ...