码迷,mamicode.com
首页 > 系统相关 > 详细

我的openwrt学习笔记(十九):linux便捷开发命令之strace

时间:2015-08-30 11:22:27      阅读:1007      评论:0      收藏:0      [点我收藏+]

标签:openwrt   智能终端   linux   物联网   

                        我的openwrt学习笔记(十九):linux便捷开发命令之strace

         学习linux命令的是LINUX 的基础,相信很多人N多年前就知道这个命令,学习额时候也用过它,但是久而久之这个命令就淡出了我们的视野!在实际的工作中有时候会遗忘它,终其主要原因是字实际应用中运用的不够多!

   今天介绍个特别有用的命令,strace,可以跟踪程序的执行调用过程,它可以给你提供程序执行错误的相关信息!

1.

linux@ubuntu:~$ which strace

/usr/bin/strace

linux@ubuntu:~$ strace -V

strace -- version 4.5.20

linux@ubuntu:~$ strace -v

usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]

              [-p pid] ... [-s strsize] [-u username] [-E var=val] ...

              [command [arg ...]]

   or: strace -c -D [-e expr] ... [-O overhead] [-S sortby] [-E var=val] ...

              [command [arg ...]]

-c -- count time, calls, and errors for each syscall and report summary

-f -- follow forks, -ff -- with output into separate files

-F -- attempt to follow vforks, -h -- print help message

-i -- print instruction pointer at time of syscall

-q -- suppress messages about attaching, detaching, etc.

-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs

-T -- print time spent in each syscall, -V -- print version

-v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args

-x -- print non-ascii strings in hex, -xx -- print all strings in hex

-a column -- alignment COLUMN for printing syscall results (default 40)

-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...

   options: trace, abbrev, verbose, raw, signal, read, or write

-o file -- send trace output to FILE instead of stderr

-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs

-p pid -- trace process with process id PID, may be repeated

-D -- run tracer process as a detached grandchild, not as parent

-s strsize -- limit length of print strings to STRSIZE chars (default 32)

-S sortby -- sort syscall counts by: time, calls, name, nothing (default time)

-u username -- run command as username handling setuid and/or setgid

-E var=val -- put var=val in the environment for command

-E var -- remove var from the environment for command

 

 

2.

Test.c代码

#include <stdio.h>

 

 

int main(void)

{

        printf("hi,strace test 20150814\n");

        return 0;

}

linux@ubuntu:~/linux_c$ touch test.c

linux@ubuntu:~/linux_c$ vi test.c

linux@ubuntu:~/linux_c$ gcc -g test.c

linux@ubuntu:~/linux_c$ ./a.out

hi,strace test 20150814

linux@ubuntu:~/linux_c$ strace a.out

strace: a.out: command not found

linux@ubuntu:~/linux_c$ strace ./a.out

execve("./a.out", ["./a.out"], [/* 37 vars */]) = 0

brk(0)                                  = 0x9a21000

access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)

mmap2(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7782000

access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)

open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3

fstat64(3, {st_mode=S_IFREG|0644, st_size=74329, ...}) = 0

mmap2(NULL, 74329, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb776f000

close(3)                                = 0

access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)

open("/lib/i386-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3

read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0000\226\1\0004\0\0\0"..., 512) = 512

fstat64(3, {st_mode=S_IFREG|0755, st_size=1713640, ...}) = 0

mmap2(NULL, 1723100, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb75ca000

mmap2(0xb7769000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19f) = 0xb7769000

mmap2(0xb776c000, 10972, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb776c000

close(3)                                = 0

mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb75c9000

set_thread_area({entry_number:-1 -> 6, base_addr:0xb75c9900, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0

mprotect(0xb7769000, 8192, PROT_READ)   = 0

mprotect(0x8049000, 4096, PROT_READ)    = 0

mprotect(0xb77a5000, 4096, PROT_READ)   = 0

munmap(0xb776f000, 74329)               = 0

fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0

mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7781000

write(1, "hi,strace test 20150814\n", 24hi,strace test 20150814

) = 24

exit_group(0)                           = ?

linux@ubuntu:~/linux_c$

 

 

3.

strace案例

用strace调试程序例子

http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316692.html

http://www.cnblogs.com/bangerlee/archive/2012/02/20/2356818.html

http://jingyan.baidu.com/article/a3761b2bb92d2d1576f9aa05.html

4.

编译strace  ,最新版strace-4.10

 

http://sourceforge.net/projects/strace/

 

编译

授人以鱼不如授人以渔

先看readme 提示编译安装查看 install文件

 

linux@ubuntu:~/linux_c/strace-4.10$ ./configure --help

`configure‘ configures strace 4.10 to adapt to many kinds of systems.

 

Usage: ./configure [OPTION]... [VAR=VALUE]...

linux@ubuntu:~/linux_c/strace-4.10$ ./configure -V

strace configure 4.10

generated by GNU Autoconf 2.69

 

Copyright (C) 2012 Free Software Foundation, Inc.

This configure script is free software; the Free Software Foundation

gives unlimited permission to copy, distribute and modify it.

 

generated by GNU Autoconf 2.69

别小看这句话,这句话告诉你configure是按照Autoconf库创建出来的,有标准通用参数

如以下:

linux@ubuntu:~/linux_c/strace-4.10$ ./configure CC=/usr/bin/gcc

linux@ubuntu:~/linux_c/strace-4.10$ make

.o strace-utimes.o strace-v4l2.o strace-vsprintf.o strace-wait.o strace-xattr.o  

make[2]: Leaving directory `/home/linux/linux_c/strace-4.10‘

make[1]: Leaving directory `/home/linux/linux_c/strace-4.10‘

linux@ubuntu:~/linux_c/strace-4.10$ make install

make  install-recursive

make[1]: Entering directory `/home/linux/linux_c/strace-4.10‘

Making install in tests

make[2]: Entering directory `/home/linux/linux_c/strace-4.10/tests‘

make[3]: Entering directory `/home/linux/linux_c/strace-4.10/tests‘

make[3]: Nothing to be done for `install-exec-am‘.

make[3]: Nothing to be done for `install-data-am‘.

make[3]: Leaving directory `/home/linux/linux_c/strace-4.10/tests‘

make[2]: Leaving directory `/home/linux/linux_c/strace-4.10/tests‘

make[2]: Entering directory `/home/linux/linux_c/strace-4.10‘

make[3]: Entering directory `/home/linux/linux_c/strace-4.10‘

 /bin/mkdir -p ‘/usr/local/bin‘

  /usr/bin/install -c strace ‘/usr/local/bin‘

/usr/bin/install: cannot create regular file `/usr/local/bin/strace‘: Permission denied

make[3]: *** [install-binPROGRAMS] Error 1

make[3]: Leaving directory `/home/linux/linux_c/strace-4.10‘

make[2]: *** [install-am] Error 2

make[2]: Leaving directory `/home/linux/linux_c/strace-4.10‘

make[1]: *** [install-recursive] Error 1

make[1]: Leaving directory `/home/linux/linux_c/strace-4.10‘

make: *** [install] Error 2

linux@ubuntu:~/linux_c/strace-4.10$ file strace

strace: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x06f3c3b4e130a4374122afc287fb62d41ce3f644, not stripped

 

 

 Openwrt编译mips –el 编译器

 

 

linux@ubuntu:~/mt7688/openwrt-3.10.14/bin/ramips$ ls -l

total 49040

-rw-r--r-- 1 linux linux      360 Aug 14 16:41 md5sums

-rw-r--r-- 1 linux linux  3407876 Aug 14 16:41 openwrt-ramips-mt7628-mt7628-squashfs-sysupgrade.bin

-rw-r--r-- 1 linux linux  1966080 Aug 14 16:41 openwrt-ramips-mt7628-root.squashfs

-rw-r--r-- 1 linux linux  1267526 Aug 14 16:41 openwrt-ramips-mt7628-uImage.bin

-rwxr-xr-x 1 linux linux  3750544 Aug 14 16:41 openwrt-ramips-mt7628-vmlinux.bin

-rwxr-xr-x 1 linux linux  3816984 Aug 14 16:41 openwrt-ramips-mt7628-vmlinux.elf

-rw-r--r-- 1 linux linux 36051998 Aug 14 16:42 OpenWrt-Toolchain-ramips-for-mipsel_24kec+dsp-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2

drwxr-xr-x 5 linux linux     4096 Aug  7 16:39 packages

linux@ubuntu:~/mt7688/openwrt-3.10.14/bin/ramips$ pwd

/home/linux/mt7688/openwrt-3.10.14/bin/ramips

所以可见编译好后  工具链存放于/home/linux/mt7688/openwrt-3.10.14/bin/ramips

 

Mips工具链安装

解压工具链~/app

linux@ubuntu:~/app/OpenWrt-Toolchain-ramips-for-mipsel_24kec+dsp-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin$

 

 

 

linux@ubuntu:~$ cat /etc/environment

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

linux@ubuntu:~$ vi /etc/environment

linux@ubuntu:~$ sudo chmod 777 /etc/environment

[sudo] password for linux:

linux@ubuntu:~$ vi /etc/environment

linux@ubuntu:~$ echo $PATH

/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

linux@ubuntu:~$ source /etc/environment

linux@ubuntu:~$ echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:~/app/OpenWrt-Toolchain-ramips-for-mipsel_24kec+dsp-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin

 

linux@ubuntu:~/linux_c$ mipsel-openwrt-linux-gcc -g test.c

mipsel-openwrt-linux-uclibc-gcc.bin: warning: environment variable ‘STAGING_DIR‘ not defined

mipsel-openwrt-linux-uclibc-gcc.bin: warning: environment variable ‘STAGING_DIR‘ not defined

mipsel-openwrt-linux-uclibc-gcc.bin: warning: environment variable ‘STAGING_DIR‘ not defined

linux@ubuntu:~/linux_c$ ls -l

total 612

-rwxrwxr-x  1 linux linux   6938 Aug 14 16:51 a.out

drwxr-xr-x 11 linux linux  12288 Aug 14 16:32 strace-4.10

-rw-rw-r--  1 linux linux 601856 Aug 14 16:18 strace-4.10.tar.xz

-rw-rw-r--  1 linux linux     89 Aug 14 16:01 test.c

linux@ubuntu:~/linux_c$ file a.out

a.       out: ELF 32-bit LSB executable, MIPS, MIPS32 rel2 version 1, dynamically linked (uses shared libs), with unknown capability 0xf41 = 0x756e6700, with unknown capability 0x70100 = 0x3040000, not stripped

 

http://blog.csdn.net/flymachine/article/details/7055923 

如何交叉编译strace工具

strace mips编译器编译

linux@ubuntu:~/linux_c/strace-4.10$ ./configure --host=/home/linux/app/OpenWrt-Toolchain-ramips-for-mipsel_24kec+dsp-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mipsel-openwrt-linux-gcc

checking for a BSD-compatible install... /usr/bin/install -c

checking whether build environment is sane... yes

checking for /home/linux/app/OpenWrt-Toolchain-ramips-for-mipsel_24kec+dsp-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mipsel-openwrt-linux-gcc-strip... no

checking for strip... strip

checking for a thread-safe mkdir -p... /bin/mkdir -p

checking for gawk... gawk

checking whether make sets $(MAKE)... yes

checking whether make supports nested variables... yes

checking whether to enable maintainer-specific portions of Makefiles... no

checking build system type... i686-pc-linux-gnu

checking host system type... Invalid configuration `/home/linux/app/OpenWrt-Toolchain-ramips-for-mipsel_24kec+dsp-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mipsel-openwrt-linux-gcc‘: machine `/home/linux/app/OpenWrt-Toolchain-ramips-for-mipsel_24kec+dsp-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mipsel-openwrt-linux‘ not recognized

configure: error: /bin/bash ./config.sub /home/linux/app/OpenWrt-Toolchain-ramips-for-mipsel_24kec+dsp-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mipsel-openwrt-linux-gcc failed

、//-----------------------

linux@ubuntu:~/linux_c/strace-4.10$ ./configure CC=/home/linux/app/OpenWrt-Toolchain-ramips-for-mipsel_24kec+dsp-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mipsel-openwrt-linux-gcc

checking for a BSD-compatible install... /usr/bin/install -c

checking whether build environment is sane... yes

checking for a thread-safe mkdir -p... /bin/mkdir -p

checking for gawk... gawk

checking whether make sets $(MAKE)... yes

checking whether make supports nested variables... yes

checking whether to enable maintainer-specific portions of Makefiles... no

checking build system type... i686-pc-linux-uclibc

checking host system type... i686-pc-linux-uclibc

checking for gcc... /home/linux/app/OpenWrt-Toolchain-ramips-for-mipsel_24kec+dsp-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mipsel-openwrt-linux-gcc

checking whether the C compiler works... yes

checking for C compiler default output file name... a.out

checking for suffix of executables...

checking whether we are cross compiling... configure: error: in `/home/linux/linux_c/strace-4.10‘:

configure: error: cannot run C compiled programs.

If you meant to cross compile, use `--host‘.

See `config.log‘ for more details

 

 

恩??为毛不对

linux@ubuntu:~/linux_c/strace-4.10$ ./configure --host=mipsel-openwrt-linux CC=mipsel-openwrt-linux-gcc LD=mipsel-openwrt-linux-ld

 

checking for libunwind-ptrace.h... no

checking whether to enable stack tracing support using libunwind... no

Generated MIPS syscallent stubs

checking that generated files are newer than configure... done

configure: creating ./config.status

config.status: creating Makefile

config.status: creating tests/Makefile

config.status: creating tests-m32/Makefile

config.status: creating tests-mx32/Makefile

config.status: creating config.h

config.status: executing depfiles commands

 

linux@ubuntu:~/linux_c/strace-4.10$ make

e-sysmips.o strace-term.o strace-time.o strace-truncate.o strace-uid.o strace-uid16.o strace-umask.o strace-umount.o strace-uname.o strace-util.o strace-utime.o strace-utimes.o strace-v4l2.o strace-vsprintf.o strace-wait.o strace-xattr.o  

mipsel-openwrt-linux-uclibc-gcc.bin: warning: environment variable ‘STAGING_DIR‘ not defined

mipsel-openwrt-linux-uclibc-gcc.bin: warning: environment variable ‘STAGING_DIR‘ not defined

make[2]: Leaving directory `/home/linux/linux_c/strace-4.10‘

make[1]: Leaving directory `/home/linux/linux_c/strace-4.10‘

linux@ubuntu:~/linux_c/strace-4.10$ file strace

strace: ELF 32-bit LSB executable, MIPS, MIPS32 rel2 version 1, dynamically linked (uses shared libs), with unknown capability 0xf41 = 0x756e6700, with unknown capability 0x70100 = 0x3040000, not stripped

 

默认make生成的是动态的

tu:~/linux_c/strace-4.10$ make CFLAGS+=-static

linux@ubuntu:~/linux_c/strace-4.10$ file strace

strace: ELF 32-bit LSB executable, MIPS, MIPS32 rel2 version 1, statically linked, with unknown capability 0xf41 = 0x756e6700, with unknown capability 0x70100 = 0x3040000, not stripped

 

 

linux@ubuntu:~/linux_c/strace-4.10$ cd ../

linux@ubuntu:~/linux_c$ ls -lh

total 3.0M

-rwxrwxr-x  1 linux linux 6.8K Aug 14 16:51 a.out

-rwxrwxr-x  1 linux linux  928 Aug 14 16:57 cident.sh

-rwxrwxr-x  1 linux linux  474 Aug 14 16:57 makefile

-rwxrwxr-x  1 linux linux 1.3M Aug 14 17:10 strace

drwxr-xr-x 11 linux linux  12K Aug 14 17:08 strace-4.10

-rw-rw-r--  1 linux linux 588K Aug 14 16:18 strace-4.10.tar.xz

-rwxrwxr-x  1 linux linux 1.1M Aug 14 17:08 strace.dyn

-rw-rw-r--  1 linux linux   89 Aug 14 16:01 test.c

 

生成strace静态可执行文件,1.3MB

 

 

 

Mafile 无法识别到mipsel-openwrt-linux-gcc目录,

修改PATH,从/etc/envimont

改到 ~/.bashrc

 

 

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/linux/app/OpenWrt-Toolchain-ramips-for-mipsel_24kec+dsp-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin

linux@ubuntu:~/linux_c$which mipsel-openwrt-linux-gcc

/home/linux/app/OpenWrt-Toolchain-ramips-for-mipsel_24kec+dsp-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mipsel-openwrt-linux-gcc

 

:~/linux_c$ make

mipsel-openwrt-linux-gcc  -o app.elf test.c -g -lpthread -ldl -Wall -Wimplicit-function-declaration

mipsel-openwrt-linux-uclibc-gcc.bin:warning: environment variable ‘STAGING_DIR‘ not defined

mipsel-openwrt-linux-uclibc-gcc.bin:warning: environment variable ‘STAGING_DIR‘ not defined

mipsel-openwrt-linux-uclibc-gcc.bin:warning: environment variable ‘STAGING_DIR‘ not defined

linux@ubuntu:~/linux_c$file app.elf

app.elf: ELF 32-bitLSB executable, MIPS, MIPS32 rel2 version 1, dynamically linked (uses sharedlibs), with unknown capability 0xf41 = 0x756e6700, with unknown capability0x70100 = 0x3040000, not stripped

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

我的openwrt学习笔记(十九):linux便捷开发命令之strace

标签:openwrt   智能终端   linux   物联网   

原文地址:http://blog.csdn.net/xushx_bigbear/article/details/48102851

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!