用shellshell 函数调用 参数word时,被告知实时错误是为什么?

问:vb怎么打开选择的窗体请教下vb怎么打开选择的窗体有 码就最好啦!想要个好像任务管理器里新...答:在 path.txt里写入一行 D:downloadMP3(使用者可以改为需要的路径)把这个和你的程序放在同一个夹里。在VB程序:Private Sub mand1_Click()...
问:VB程序里shell函数问题VB程序里的shell能打开DOS并执行DOS命令,可以吧命令执行结果写到一个里去...答:Shell会返回执行程序的进程的标识号Pid,通过定时pid就能判断程序是否还在运行以下函数返回指定的pid是否存在,调用方法:MsgBox PidIsTrue(Shell("notepad...
问:vb中的shell我想用VB写个程序:用shell运行另一个会自动结束的程序,并且测出它被运行的时间....答:'有多种方法可以实现这个需求,下面提供一种方法,供参考:Option Explicit Private Const WAIT_INFINITE=-1&Private Const SYNCHRONIZE=H1 Private Declare...
问:vb如何用shell打开或夹vb窗体上有text1和listview1两个控件当在text1里输入F:test时,listview1控件...答:Private Declare Function ShellExecute Lib"shell32.dll"Alias"ShellExecuteA"(ByVal hwnd As Long,ByVal lpOperation As String,ByVal lpFile As String,ByVal...
问:VB打开问题.在窗体上有一个mand1名称为"打开"按钮,双击写上以下 码:Shell"explorer D:...答:在 path.txt里写入一行 D:downloadMP3(使用者可以改为需要的路径)把这个和你的程序放在同一个夹里。在VB程序:Private Sub mand1_Click()...
问:如何使用VB中的Shell函数?有什么功能?答:语法Shell(pathname[, style])Shell函数的语法含有下面这些命名参数:部分描述pathname必要参数。Variant(String),要执行的程序名,以及任何必需的参数...
问:如何使用VB6.Shell函数转跳我想变一个程序程序里有一个文本框(1)和一个按钮(A)我想变一个删除帖子程序...答:你的程序本身没有错误是你获取的数据有问题Shell"explorer.exe ja:_doPostBack('lbDel_1','')"应该是这句出了问题了.
问:VB关于Shell函数的返回值Shell函数的返回值为所打开程序的进程ID我的系统是Win64位的,用这个函数打开...答:explorer.exe是不允 重复运行的,你pid=Shell("explorer.exe")调用实际上会出错,出错的时候返回值不是PID,需要查找现有explorer.exe进程的PID需要使用其它...
问:shell这是什么啊?有什么作用?vb中的,给详细解释一下! ...答:Shell是VB程序在运行中现在执行状态,去执行一个外部程序或DOS下的批处理命令等使用的VB语句,执行后一般返回原程序,例:1 Private Sub mand1_Click()Dim...
问:VB Shell 打开的一些问题我现在用Shell打开一个指定,怎样可以让它打开是的时候是以直接打开窗口方式...答:要想让它打开是的时候是以直接打开窗口方式打开(也就是正常焦点)Shell"D:Program FilesF-softTest-toolseTools.exe",vbNormalFocus注意:Shell只能打开应用...
问:用VB中的shell命令怎样打开gpedit.msc用VB中的 shell命令怎样打开gpedit.msc用"shell%windir%system32gpedit.msc...答:可以使用如下方法:Shell"mmc.exe C: ssystem32gpedit.msc"其中mmc.exe是Microsoft Management Console(MMC、微软管理控制台),是显示管理 件的控制...
问:VB如何用shell调用word程序的打开word我的语句是:Shell("C:Program FilesMicrosoft OfficeOFFICE11WORD.EXE"&...答:两条语句的顺序换换 dim file as string=“C:1.doc”a=Shell("C:Program FilesMicrosoft OfficeOFFICE11WORD.EXE"&file,vbNormalFocus)
问:VB shell函数打开夹 Dim a As String a="E:电影"用变量a取 路径,用sehll函数打开夹的...答:Shell"explorer.exe"&a explorer.exe后要有一个空格不然就会出错"a"没""吧
问:vb用shell打开程序。输入路径有的能打开 的路径:("C:Program Files(x6)Tencent ...答:图片“C:UsersadminPictures212-12-1"应该有扩展名的,不如jpg,bmp,明显你这个路径不对,应该包含图片名你贴的只是夹,没有名...
09-0509-1408-1809-16
10-2310-0712-2703-22
◇本站云标签左飞 的BLOG
用户名:左飞
文章数:85
评论数:183
访问量:124120
注册日期:
阅读量:24883
阅读量:267094
阅读量:1001701
阅读量:150648
51CTO推荐博文
要以引用返回函数值,则函数定义时必须遵循以下格式:
类型标识符 &函数名 (形参列表及类型说明)
可见,以引用返回函数值,定义函数时需要在函数名前加&。引用作为函数的返回值时,函数的返回值可以理解为函数返回了一个变量(事实上,函数返回引用时,它返回的是一个指向返回值的隐式指针),因此,值为引用的函数可以用作赋值运算符的左操作数。另外,用引用返回一个函数值的最大好处是,在内存中不产生被返回值的副本。
#include&iostream&
int &func()
static int num = 0;
&&&&&&& return ++
void main()
&&&&&&& for(i=0; i&5; i++)
&&&&&&&&&&& cout&&func()&&'\t';
&&&&&&& cout&&
&&& func()=10;
&&&&&&& for(i=0; i&5; i++)
&&&&&&&&&&& cout&&func()&&'\t';
&&&&&&& cout&&
编译并运行上述程序,其输出结果如图13.1所示,可见函数func被当作赋值运算符的左操作数来使用了。
图13.1 程序输出结果
下面再给出一个使用返回引用的函数作为左值的例子,它更有力地揭示了返回引用的函数的本质。
#include &iostream&
double array[5] = {100.1, 100.2, 100.3, 100.4, 100.5};
double &change(int i)
&&&&&&& return array[i];
int main()
&&& cout&&"原始值如下: ";
&&&&&&& for(i = 0; i & 5; i++)
&&&&&&&&&&& cout && array[i] &&" ";
&&&&&&& cout&&
&&&&&&& change(2) = 3.14;
&&&&&&& change(3) = -99.99;
&&&&&&& cout&&"修改后如下: ";
&&&&&&& for(i = 0; i & 5; i++)
&&&&&&&&&&& cout&&array[i]&&" ";
&&& cout&&
&&& return 0;
编译并运行上述程序,其输出结果请读者自己实验吧。函数change的返回值为double类型的引用,而该值又是一个由其参数i指定的数组array中元素的引用。因此,在主函数中,当语句“change(2) = 3.14;”被执行时,change函数返回的其实是对数组元素array[2]的引用。通过这个引用,array[2]被赋值3.14。随后的语句“change(3) = -99.99;”同此原理。由于change返回的是数组中特定元素的引用,所以该函数可以放在赋值语句的左边用来对这个数组元素进行赋值。注意前面讲过没有数组的引用,但是指向数组元素的引用时存在的。
在把引用作为返回值时有些一些地方需要提醒大家注意。
首先,不能返回局部变量或临时变量的引用,但可以返回全局变量的引用,也就是说要注意被引用的对象不能超出作用域。主要原因是局部变量会在函数返回后被销毁,因此被返回的引用就成为了“无所指”的引用,这是不被允许的。例如下面这段代码就是错误的:
int &func()
&&& int i = 10;
在Visual C++ 6.0中,上述代码在编译时会抛出警告“warning C4172: returning address of local variable or temporary”,尽管编译器没有报错,但这仍然意味着一种不安定。而且在Visual C++ 6.0中实验上述func()函数作为左值来使用的情况,结果表明赋值并未成功,这同样告诫我们这种用法是绝对应当被禁止的。毕竟,当函数func()返回时,局部变量i就超出了作用域。于是由func()返回的对i的引用就是未定义的引用。而且,某些对标准C++支持更强的编译器中会对上述func()作左值的情况报错!另外,这类问题也可能会间接产生,这时的错误显得更加隐蔽而不容易被发现,所以当返回对一个对象的引用时,务必要仔细检查这个对象是否会超出作用域。
其次,不能返回函数内部动态分配的内存的引用。虽然不存在局部变量的被动销毁的问题,但是在此种情况下,仍然存在一些问题。例如,被函数返回的引用只是作为一个临时变量出现,而没有被赋予一个实际的变量,那么这个引用所指向的由new分配的空间就无法被释放,从而造成内存泄漏问题。
最后,可以返回类成员的引用,但最好是const常量。这是因为当对象的属性是与某种业务规则相关联的时候,其赋值常常与某些其它属性或者对象的状态有关,于是有必要将赋值操作封装在一个业务规则当中。如果其它对象可以获得该属性的非常量引用,那么对该属性的单纯赋值就会破坏业务规则的完整性。
--------------------------------------------------------------------本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)应用的系统调用跟踪
strace 命令
  用途:打印 STREAMS 跟踪消息。
  语法:strace [ mid sid level ] ...
 描述:没有参数的 strace 命令将所有的驱动程序和模块中的所有 STREAMS 事件跟踪消息写到它的标准输出中。 这些消息是从STREAMS日志驱动程序中获取的。如果提供参数,它们必须是在三元组中。每个三元组表明跟踪消息要从给定的模块或驱动程序、子标识(通常表明次要设备)以及优先级别等于或小于给定级别的模块或驱动程序中接。all 标记可由任何成员使用,以表明对该属性没有限制。
  参数:mid---指定 STREAMS 模块的标识号&&sid---指定子标识号&&level----指定跟踪优先级别
  输出格式:每个跟踪消息输出的格式是:
  & &跟踪序列号
  & &消息时间(格式为 hh:mm:ss)
  & &系统启动后,以机器滴答信号表示消息的时间
  & &跟踪优先级别
  & &有以下值之一:
   消息也在错误日志中
   表示一个致命错误
   邮件已发送给系统管理员
  & &&&源的模块标识号
  & &&&源的子标识号
  & &&&跟踪消息的格式化文本
  在多处理器系统上, 由两部分组成:消息所有者发送处的处理器号码,格式化文本本身。
  一旦启动,strace 命令将继续执行,直到用户终止该命令。
  注:由于性能的考虑,所以一次只允许一个 strace 命令来打开 STREAMS 日志驱动程序。
 日志驱动程序有一个三元组的列表(该列表在命令调用中指定),并且程序会根据该列表比较每个潜在的跟踪消息,以决定是否要格式化和发送这个信息到
strace 进程中。因此,长的三元组列表会对 STREAMS 的总体性能的影响更大。 运行 strace命令对于某些模块和驱动程序(生成要发送给 strace 进程的跟踪消息的模块和驱动程序)的定时的影响最大。 如果跟踪消息生成过快,以至strace 进程无法处理,那么就会丢失一些消息。 最后的情况可以通过检查跟踪消息输出上的序列号来确定。
  要输出模块标识为 41 的模块或驱动程序中的所有跟踪消息,请输入:
  strace 41 all all
  要输出模块标识为 41,子标识为 0、1 或 2 的模块或驱动程序中的跟踪消息:
  strace 41 0 1 41 1 1 41 2 0
   子标识为 0 和 1 的模块或驱动程序中的消息必须具有小于或等于 1 的跟踪级别。子标识为 2 的模块或驱动程序中的消息必须具有跟踪级别 0。
  strace: option requires an argument -- e
  usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]
  & && && && &&&[-p pid] ... [-s strsize] [-u username] [-E var=val] ...
  & && && && &&&[command [arg ...]]
  & &or: strace -c [-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
  -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
  strace - 跟踪系统调用和信号
  usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] [-o file]
  [-p pid] [-s strsize] [-u username] [command [arg]]
  strace -c [-e expr] [-O overhead] [-S sortby] [command [arg]]
  -a column
   指定显示返回值的列位置,默认是40(从0开始计数),就是说&=&出现在40列的位
  -c 产生类似下面的统计信息
   strace -c -p 14653 (Ctrl-C)
   % time seconds usecs/call calls errors syscall
   ------ ----------- ----------- --------- --------- ----------------
   53.99 0.7 4 2 wait4
   42.16 0.8 5 read
   1.78 0. 7 write
   0.76 0. 18 ioctl
   0.50 0. 52 rt_sigprocmask
   0.48 0. 2 fork
   0.18 0. 18 rt_sigaction
   0.06 0. 1 1 stat
   0.03 0. 2 sigreturn
   0.02 0. 3 time
   0.02 0. 2 1 setpgid
   ------ ----------- ----------- --------- --------- ----------------
   100.00 0. 4 total
   -d 输出一些strace自身的调试信息到标准输出
   strace -c -p 14653 -d (Ctrl-C)
   [wait(0x137f) = 14653]
   pid 14653 stopped, [SIGSTOP]
   [wait(0x57f) = 14653]
   pid 14653 stopped, [SIGTRAP]
   cleanup: looking at pid 14653
   % time seconds usecs/call calls errors syscall
   ------ ----------- ----------- --------- --------- ----------------
   ------ ----------- ----------- --------- --------- ----------------
   100.00 0. total
   -e expr
   A qualifying expression which modifies which events to trace or how to trace
   them. The format of the expression is:
   [qualifier=][!]value1[,value2]...
   这里qualifier可以是trace、abbrev、verbose、raw、signal、read或者write。
   value是qualifier相关的符号或数值。缺省qualifier是trace。!表示取反。
   -eopen等价于-e trace=open,表示只跟踪open系统调用。-etrace=!open意思是
   跟踪除open系统调用之外的其他所有系统调用。此外value还可以取值all和none。
   某些shell用!表示重复历史指令,此时可能需要引号、转义符号(\)的帮助。
   -e trace=set
   只跟踪指定的系统调用列表。决定跟踪哪些系统调用时,-c选项很有用。
   trace=open,close,read,write意即只跟踪这四种系统调用,缺省是trace=all
   -e trace=file
   跟踪以指定文件名做参数的所有系统调用。
   -e trace=process
   Trace all system calls which involve process management. This is
   useful for watching the fork, wait, and exec steps of a process.
   -e trace=network
   跟踪所有和网络相关的系统调用
   -e trace=signal
   Trace all signal related system calls.
   -e trace=ipc
   Trace all IPC related system calls.
   -e abbrev=set
   Abbreviate the output from printing each member of large structures.
   缺省是abbrev=all,-v选项等价于abbrev=none
   -e verbose=set
   Dereference structures for the specified set of system calls.
   The default is verbose=all.
   -e raw=set
   Print raw, undecoded arguments for the specifed set of system calls.
   This option has the effect of causing all arguments to be printed in
   hexadecimal. This is mostly useful if you don&t trust the decoding or
   you need to know the actual numeric value of an argument.
   -e signal=set
   只跟踪指定的信号列表,缺省是signal=all。signal=!SIGIO (or signal=!io)
   导致 SIGIO 信号不被跟踪
   -e read=set
   Perform a full hexadecimal and ASCII dump of all the data read from
   file descriptors listed in the specified set. For example, to see all
   input activity on file descriptors 3 and 5 use -e read=3,5. Note that
   this is independent from the normal tracing of the read(2) system call
   which is controlled by the option -e trace=read.
   -e write=set
   Perform a full hexadecimal and ASCII dump of all the data written to
   file descriptors listed in the specified set. For example, to see all
   output activity on file descriptors 3 and 5 use -e write=3,5. Note
   that this is independent from the normal tracing of the write(2)
   system call which is controlled by the option -e trace=write.
   follow forks,跟随子进程?
   Trace child processes as they are created by currently traced
   processes as a result of the fork(2) system call. The new process
   is attached to as soon as its pid is known (through the return value
   of fork(2) in the parent process). This means that such children may
   run uncontrolled for a while (especially in the case of a vfork(2)),
   until the parent is scheduled again to complete its (v)fork(2)
   call. If the parent process decides to wait(2) for a child that is
   currently being traced, it is suspended until an appropriate child
   process either terminates or incurs a signal that would cause it to
   terminate (as determined from the child&s current signal disposition).
   意思应该是说跟踪某个进程时,如果发生fork()调用,则选择跟踪子进程
   可以参考gdb的set follow-fork-mode设置
   attempt to follow vforks
   (On SunOS 4.x, this is accomplished with some dynamic linking trickery.
   On Linux, it requires some kernel functionality not yet in the
   standard kernel.) Otherwise, vforks will not be followed even if -f
   has been given.
   类似-f选项
   如果-o file选项有效指定,则跟踪过程中新产生的其他相关进程的信息分别写
   入file.pid,这里pid是各个进程号。
   显示帮助信息
   显示发生系统调用时的IP寄存器值
   strace -p 14653 -i
   -o filename
   指定保存strace输出信息的文件,默认使用标准错误输出stderr
   Use filename.pid if -ff is used. If the argument begins with `|& or
   with `!& then the rest of the argument is treated as a command and all
   output is piped to it. This is convenient for piping the debugging
   output to a program without affecting the redirections of executed
   programs.
   -O overhead
   Set the overhead for tracing system calls to overhead microseconds.
   This is useful for overriding the default heuristic for guessing how
   much time is spent in mere measuring when timing system calls using
   the -c option. The acuracy of the heuristic can be gauged by timing
   a given program run without tracing (using time(1)) and comparing
   the accumulated system call time to the total produced using -c.
   好象是用于确定哪些系统调用耗时多
   -p pid
   指定待跟踪的进程号,可以用Ctrl-C终止这种跟踪而被跟踪进程继续运行。可以
   指定多达32个-p参数同时进行跟踪。
   比如 strace -ff -o output -p 14653 -p 14117
   Suppress messages about attaching, detaching etc. This happens
   automatically when output is redirected to a file and the command is
   run directly instead of attaching.
   Print a relative timestamp upon entry to each system call. This
   records the time difference between the beginning of successive
   system calls.
   strace -p 14653 -i -r
   -s strsize
   指定字符串最大显示长度,默认32。但文件名总是显示完整。
   -S sortby
   Sort the output of the histogram printed by the -c option by the
   specified critereon. Legal values are time, calls, name, and nothing
   (default time).
   与-r选项类似,只不过-r采用相对时间戳,-t采用绝对时间戳(当前时钟)
   与-t类似,绝对时间戳中包含微秒
   If given thrice, the time printed will include the microseconds and
   the leading portion will be printed as the number of seconds since
   the epoch.
   这个选项显示单个系统调用耗时
   -u username
   用指定用户的UID、GID以及辅助组身份运行待跟踪程序
   冗余显示模式
   Print unabbreviated versions of environment, stat, termios, etc. calls.
   These structures are very common in calls and so the default behavior
   displays a reasonable subset of structure members. Use this option to
   get all of the gory details.
   显示strace版本信息
   -x 以16进制字符串格式显示非ascii码,比如&\x08&,默认采用8进制,比如&\10&
   -xx 以16进制字符串格式显示所有字节
strace&命令是一种强大的工具,它能够显示所有由用户空间程序发出的系统调用。
  strace&显示这些调用的参数并返回符号形式的值。strace&从内核接收信息,而且不需要以任何特殊的方式来构建内核。
  下面记录几个常用 option .
  1 -f -F选项告诉strace同时跟踪fork和vfork出来的进程
  2 -o xxx.txt 输出到某个文件。
  3 -e execve 只记录 execve 这类系统调用
  ---------------------------------------------------
  进程无法启动,软件运行速度突然变慢,程序的&SegmentFault&等等都是让每个Unix系统用户头痛的问题,
  本文通过三个实际案例演示如何使用truss、strace和ltrace这三个常用的调试工具来快速诊断软件的&疑难杂症&。
  truss和strace用来跟踪一个进程的系统调用或信号产生的情况,而 ltrace用来跟踪进程调用库函数的情况。truss是早期为System
V R4开发的调试程序,包括Aix、FreeBSD在内的大部分Unix系统都自带了这个工具;
  而strace最初是为SunOS系统编写的,ltrace最早出现在GNU/DebianLinux中。
  这两个工具现在也已被移植到了大部分Unix系统中,大多数Linux发行版都自带了strace和ltrace,而FreeBSD也可通过Ports安装它们。
  你不仅可以从命令行调试一个新开始的程序,也可以把truss、strace或ltrace绑定到一个已有的PID上来调试一个正在运行的程序。三个调试工具的基本使用方法大体相同,下面仅介绍三者共有,而且是最常用的三个命令行参数:
  -f :除了跟踪当前进程外,还跟踪其子进程。
  -o file :将输出信息写到文件file中,而不是显示到标准错误输出(stderr)。
  -p pid :绑定到一个由pid对应的正在运行的进程。此参数常用来调试后台进程。
   使用上述三个参数基本上就可以完成大多数调试任务了,下面举几个命令行例子:
  truss -o ls.truss ls -al: 跟踪ls -al的运行,将输出信息写到文件/tmp/ls.truss中。
  strace&-f -o vim.strace&vim:
跟踪vim及其子进程的运行,将输出信息写到文件vim.strace。
  ltrace -p 234: 跟踪一个pid为234的已经在运行的进程。
   三个调试工具的输出结果格式也很相似,以strace为例:
  brk(0) = 0x8062aa8
  brk(0x8063000) = 0x8063000
  mmap2(NULL, 4096, PROT_READ, MAP_PRIVATE, 3, 0x92f) = 0x
  每一行都是一条系统调用,等号左边是系统调用的函数名及其参数,右边是该调用的返回值。 truss、strace和ltrace的工作原理大同小异,都是使用ptrace系统调用跟踪调试运行中的进程,详细原理不在本文讨论范围内,有兴趣可以参考它们的源代码。
  举两个实例演示如何利用这三个调试工具诊断软件的&疑难杂症&:
  案例一:运行clint出现Segment Fault错误
  操作系统:FreeBSD-5.2.1-release
  clint是一个C++静态源代码分析工具,通过Ports安装好之后,运行:
  # clint foo.cpp
  Segmentation fault (core dumped)
   在Unix系统中遇见&Segmentation Fault&就像在MS Windows中弹出&非法操作&对话框一样令人讨厌。OK,我们用truss给clint&把把脉&:
  # truss -f -o clint.truss clint
  Segmentation fault (core dumped)
  # tail clint.truss
   739: read(0x6,0x806f000,0x1000) = 0)
   739: fstat(6,0xbfbfe4d0) = 0 (0x0)
   739: fcntl(0x6,0x3,0x0) = 4 (0x4)
   739: fcntl(0x6,0x4,0x0) = 0 (0x0)
   739: close(6) = 0 (0x0)
   739: stat(&/root/.clint/plugins&,0xbfbfe680) ERR#2 'No such file or directory'
  SIGNAL 11
  SIGNAL 11
  Process stopped because of: 16
  process exit, rval = 139
  我们用truss跟踪clint的系统调用执行情况,并把结果输出到文件clint.truss,然后用tail查看最后几行。
  注意看clint执行的最后一条系统调用(倒数第五行):stat(&/root/.clint/plugins&,0xbfbfe680) ERR#2 'No such file or directory',问题就出在这里:clint找不到目录&/root/.clint/plugins&,从而引发了段错误。怎样解决?很简单: mkdir -p /root/.clint/plugins,不过这次运行clint还是会&Segmentation
Fault&9。继续用truss跟踪,发现clint还需要这个目录&/root/.clint/plugins/python&,建好这个目录后 clint终于能够正常运行了。
  案例二:vim启动速度明显变慢
  操作系统:FreeBSD-5.2.1-release
  vim版本为6.2.154,从命令行运行vim后,要等待近半分钟才能进入编辑界面,而且没有任何错误输出。仔细检查了.vimrc和所有的vim脚本都没有错误配置,在网上也找不到类似问题的解决办法,难不成要hacking source code?没有必要,用truss就能找到问题所在:
  # truss -f -D -o vim.truss vim
  这里-D参数的作用是:在每行输出前加上相对时间戳,即每执行一条系统调用所耗费的时间。我们只要关注哪些系统调用耗费的时间比较长就可以了,用less仔细查看输出文件vim.truss,很快就找到了疑点:
  735: 0. socket(0x2,0x1,0x0) = 4 (0x4)
  735: 0. setsockopt(0x4,0x6,0x1,0xbfbfe3c8,0x4) = 0 (0x0)
  735: 0. setsockopt(0x4,0xffff,0x8,0xbfbfe2ec,0x4) = 0 (0x0)
  735: 0. connect(0x4,{ AF_INET 10.57.18.27:6000 },16) ERR#61 'Connection refused'
  735: 0. close(4) = 0 (0x0)
  735: 1. nanosleep(0xbfbfe468,0xbfbfe460) = 0 (0x0)
  735: 0. socket(0x2,0x1,0x0) = 4 (0x4)
  735: 0. setsockopt(0x4,0x6,0x1,0xbfbfe3c8,0x4) = 0 (0x0)
  735: 0. setsockopt(0x4,0xffff,0x8,0xbfbfe2ec,0x4) = 0 (0x0)
  735: 0. connect(0x4,{ AF_INET 10.57.18.27:6000 },16) ERR#61 'Connection refused'
  735: 0. close(4) = 0 (0x0)
  735: 1. nanosleep(0xbfbfe468,0xbfbfe460) = 0 (0x0)
  vim试图连接10.57.18.27这台主机的6000端口(第四行的connect()),连接失败后,睡眠一秒钟继续重试(第6行的 nanosleep())。以上片断循环出现了十几次,每次都要耗费一秒多钟的时间,这就是vim明显变慢的原因。可是,你肯定会纳闷:&vim怎么会无缘无故连接其它计算机的6000端口呢?&。问得好,那么请你回想一下6000是什么服务的端口?没错,就是X Server。看来vim是要把输出定向到一个远程X
Server,那么Shell中肯定定义了DISPLAY变量,查看.cshrc,果然有这么一行:setenv DISPLAY ${REMOTEHOST}:0,把它注释掉,再重新登录,问题就解决了。
  案例三:用调试工具掌握软件的工作原理
  操作系统:Red Hat Linux 9.0
  用调试工具实时跟踪软件的运行情况不仅是诊断软件&疑难杂症&的有效的手段,也可帮助我们理清软件的&脉络&,即快速掌握软件的运行流程和工作原理,不失为一种学习源代码的辅助方法。下面这个案例展现了如何使用strace通过跟踪别的软件来&触发灵感&,从而解决软件开发中的难题的。
  大家都知道,在进程内打开一个文件,都有唯一一个文件描述符(fd:file descriptor)与这个文件对应。而本人在开发一个软件过程中遇到这样一个问题:
  已知一个fd,如何获取这个fd所对应文件的完整路径?不管是Linux、FreeBSD或是其它Unix系统都没有提供这样的API,怎么办呢?我们换个角度思考:Unix下有没有什么软件可以获取进程打开了哪些文件?如果你经验足够丰富,很容易想到lsof,使用它既可以知道进程打开了哪些文件,也可以了解一个文件被哪个进程打开。好,我们用一个小程序来试验一下lsof,看它是如何获取进程打开了哪些文件。lsof: 显示进程打开的文件。
  /* testlsof.c */
  #include #include #include #include #include
  int main(void)
   open(&/tmp/foo&, O_CREAT|O_RDONLY); /* 打开文件/tmp/foo */
   sleep(1200); /* 睡眠1200秒,以便进行后续操作 */
   return 0;
  将testlsof放入后台运行,其pid为3125。命令lsof -p 3125查看进程3125打开了哪些文件,我们用strace跟踪lsof的运行,输出结果保存在lsof.strace中:
  # gcc testlsof.c -o testlsof
  # ./testlsof &
  [1] 3125
  #&strace&-o lsof.strace&lsof
  我们以&/tmp/foo&为关键字搜索输出文件lsof.strace,结果只有一条:
  # grep '/tmp/foo' lsof.strace
  readlink(&/proc/3125/fd/3&, &/tmp/foo&, 4096) = 8
  原来lsof巧妙的利用了/proc/nnnn/fd/目录(nnnn为pid):Linux内核会为每一个进程在/proc/建立一个以其pid为名的目录用来保存进程的相关信息,而其子目录fd保存的是该进程打开的所有文件的fd。目标离我们很近了。好,我们到/proc/3125/fd/看个究竟:
  # cd /proc/3125/fd/
  # ls -l
  total 0
  lrwx------ 1 root root 64 Nov 5 09:50 0 -& /dev/pts/0
  lrwx------ 1 root root 64 Nov 5 09:50 1 -& /dev/pts/0
  lrwx------ 1 root root 64 Nov 5 09:50 2 -& /dev/pts/0
  lr-x------ 1 root root 64 Nov 5 09:50 3 -& /tmp/foo
  # readlink /proc/3125/fd/3
  /tmp/foo
  答案已经很明显了:/proc/nnnn/fd/目录下的每一个fd文件都是符号链接,而此链接就指向被该进程打开的一个文件。我们只要用readlink()系统调用就可以获取某个fd对应的文件了,代码如下:
  #include #include #include #include #include #include
  int get_pathname_from_fd(int fd, char pathname[], int n)
   char buf[1024];
   bzero(buf, 1024);
   pid = getpid();
   snprintf(buf, 1024, &/proc/%i/fd/%i&, pid, fd);
   return readlink(buf, pathname, n);
  int main(void)
   char pathname[4096];
   bzero(pathname, 4096);
   fd = open(&/tmp/foo&, O_CREAT|O_RDONLY);
   get_pathname_from_fd(fd, pathname, 4096);
   printf(&fd=%d; pathname=%sn&, fd, pathname);
   return 0;
  出于安全方面的考虑,在FreeBSD 5 之后系统默认已经不再自动装载proc文件系统,因此,要想使用truss或strace跟踪程序,你必须手工装载proc文件系统:mount
-t procfs proc /proc;或者在/etc/fstab中加上一行:
  proc /proc procfs rw 0 0&
其中第一行显示程序试图创建/tmp/.ICE-unix目录,权限为0777,这个操作因为目录已经存在而失败了。第二个系统调用(lstat64)检查 了目录状态,并显示这个目录的权限是0755,这里出现了第一个程序运行错误的线索:程序试图创建属性为0777的目录,但是已经存在了一个属性为 0755的目录。第三个系统调用(unlink)试图删除一个文件,但是这个文件并不存在。这并不奇怪,因为这个操作只是试图删掉可能存在的老文件。
但是,第四行确认了错误所在。他试图绑定到/tmp/.ICE-unix/dcop4596,但是出现了拒绝访问错误。. ICE_unix目录的用户和组都是root,并且只有所有者具有写权限。一个非root用户无法在这个目录下面建立文件,如果把目录属性改成0777, 则前面的操作有可能可以执行,而这正是第一步错误出现时进行过的操作。
所以我运行了chmod 0777 /tmp/.ICE-unix之后KDE就可以正常启动了,问题解决了,用strace进行跟踪调试只需要花很短的几分钟时间跟踪程序运行,然后检查并分 析输出文件。
说明:运行chmod 0777只是一个测试,一般不要把一个目录设置成所有用户可读写,同时不设置粘滞位(sticky bit)。给目录设置粘滞位可以阻止一个用户随意删除可写目录下面其他人的文件。一般你会发现/tmp目录因为这个原因设置了粘滞位。KDE可以正常启动 之后,运行chmod +t /tmp/.ICE-unix给.ICE_unix设置粘滞位。
& 解决库依赖问题
starce 的另一个用处是解决和动态库相关的问题。当对一个可执行文件运行ldd时,它会告诉你程序使用的动态库和找到动态库的位置。但是如果你正在使用一个比较老 的glibc版本(2.2或更早),你可能会有一个有bug的ldd程序,它可能会报告在一个目录下发现一个动态库,但是真正运行程序时动态连接程序 (/lib/ld-linux.so.2)却可能到另外一个目录去找动态连接库。这通常因为/etc/ld.so.conf和 /etc/ld.so.cache文件不一致,或者/etc/ld.so.cache被破坏。在glibc
2.3.2版本上这个错误不会出现,可能ld-linux的这个bug已经被解决了。
尽管这样,ldd并不能把所有程序依赖的动态库列出 来,系统调用dlopen可以在需要的时候自动调入需要的动态库,而这些库可能不会被ldd列出来。作为glibc的一部分的NSS(Name Server Switch)库就是一个典型的例子,NSS的一个作用就是告诉应用程序到哪里去寻找系统帐号数据库。应用程序不会直接连接到NSS库,glibc则会通 过dlopen自动调入NSS库。如果这样的库偶然丢失,你不会被告知存在库依赖问题,但这样的程序就无法通过用户名解析得到用户ID了。让我们看一个例子:
whoami程序会给出你自己的用户名,这个程序在一些需要知道运行程序的真正用户的脚本程序里面非常有用,whoami的一个示例 输出如下:
假设因为某种原因在升 级glibc的过程中负责用户名和用户ID转换的库NSS丢失,我们可以通过把nss库改名来模拟这个环境:
# mv /lib/libnss_files.so.2 /lib/libnss_files.so.2.backup
whoami: cannot find username for UID 0
这里你可以看到,运行whoami时出现了错误,ldd程序的输出不会提供有用的帮助:
# ldd /usr/bin/whoami
libc.so.6 =& /lib/libc.so.6 (0x)
/lib/ld-linux.so.2 =& /lib/ld-linux.so.2 (0x)
你只会看到whoami依赖Libc.so.6和ld-linux.so.2,它没有给出运行whoami所必须的其他库。这里时用strace跟踪 whoami时的输出:
strace -o whoami-strace.txt whoami
open(&/lib/libnss_files.so.2&, O_RDONLY) = -1 ENOENT (No such file or directory)
open(&/lib/i686/mmx/libnss_files.so.2&, O_RDONLY) = -1 ENOENT (No such file or directory)
stat64(&/lib/i686/mmx&, 0xbffff190) = -1 ENOENT (No such file or directory)
open(&/lib/i686/libnss_files.so.2&, O_RDONLY) = -1 ENOENT (No such file or directory)
stat64(&/lib/i686&, 0xbffff190) = -1 ENOENT (No such file or directory)
open(&/lib/mmx/libnss_files.so.2&, O_RDONLY) = -1 ENOENT (No such file or directory)
stat64(&/lib/mmx&, 0xbffff190) = -1 ENOENT (No such file or directory)
open(&/lib/libnss_files.so.2&, O_RDONLY) = -1 ENOENT (No such file or directory)
stat64(&/lib&, {st_mode=S_IFDIR|0755, st_size=2352, ...}) = 0
open(&/usr/lib/i686/mmx/libnss_files.so.2&, O_RDONLY) = -1 ENOENT (No such file or directory)
stat64(&/usr/lib/i686/mmx&, 0xbffff190) = -1 ENOENT (No such file or directory)
open(&/usr/lib/i686/libnss_files.so.2&, O_RDONLY) = -1 ENOENT (No such file or directory)
你可以发现在不同目录下面查找libnss.so.2的尝试,但是都失败了。如果没有strace这样的工具,很难发现这个错误是由于缺少动态库造成的。现 在只需要找到libnss.so.2并把它放回到正确的位置就可以了。 
& 限制strace只跟踪特定的系统调用
如果你已经知道你要找什么,你可以让strace只跟踪一些类型的系统调用。例如,你需要看看在configure脚本里面执行的程序,你需要监视的系统调 用就是execve。让strace只记录execve的调用用这个命令:
strace -f -o configure-strace.txt -e execve ./configure
> 本站内容系网友提交或本网编辑转载,其目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请及时与本网联系,我们将在第一时间删除内容!
strace 命令
用途:打印 STREAMS 跟踪消息. 语法:strace [ mid sid level ] ... 描述:没有参数的 strace 命令将所有的驱动程序和模块中的所有 STREAMS 事件跟踪消息写到它的标准输出中. 这些消息是从STREAMS日志驱动程序中获取的.如果提供参数,它们必须是在三元组中.每个三元组表明跟踪消息要从给 ...
strace 命令是一种强大的工具,它能够显示所有由用户空间程序发出的系统调用 strace 显示这些调用的参数并返回符号形式的值.strace 从内核接收信息,而且不需要以任何特殊的方式来构建内核. 下面记录几个常用 option
1 -f -F选项告诉strace同时跟踪fork和vfork出来的进程
2 -o xxx.txt 输出到某个文件.
引言 &Oops,系统挂死了...& &Oops,程序崩溃了...& &Oops,命令执行报错...& 对于维护人员来说,这样的悲剧每天都在上演.理想情况下,系统或应用程序的错误日志提供了足够全面的信息,通过查看相关日志,维护人员就能很快地定位出问题发生的原因.但现实情况,许多错误日志打印模凌两可, ...
原文出处:http://linuxgazette.net/148/saha.html原文作者:Amit Kumar Saha and Sayantini Ghosh翻译时间:日译者:王旭 &gnawux (at) &
译者按:翻译篇文章换换心情,strace 是个有用的工具,LinuxGazette 是个不错的 ...
AIX,solaris
可用自带的trussLinux:可用自带的strace或ltraceHP-UX:可用tusc 需要下载tusc进行安装.1. 从http://hpux.connect.org.uk/hppd/hpux/Sysadmin/tusc-7.10/下载tusc2. 安装
swinstall -s /slview/tusc-7.10- ...
系统调用是linux用户态和内核态之间的一个桥梁,通过系统调用可以访问linux的众多核心功能.在使用上系统调用和库函数之间虽然并没有太大的区别,但是必须明确的是调用系统调用时,程序会进入内核态运行,而库函数始终是运行在用户态的.由于需要在用户态和内核态之间切换,系统调用的开销相对要大一些.
strace这个工具可以在运行时跟踪程序使 ...
转自:/blog/835853 ps aux | grep apache 查看占用cpu时间长的进程id strace -f -F -T -p 1729 调用: strace [ -dffhiqrtttTvxx ] [ -acolumn ] [ -eexpr ] ... [ -ofile ] [ -ppi ...
/article-3935-1.html译者: guodongxiaren
在调试的时候,strace能帮助你追踪到一个程序所执行的系统调用.当你想知道程序和操作系统如何交互的时候,这是极其方便的,比如你想知道执行了哪些系统调用,并且以何种顺序执行. 这个简单而又强大的工具几乎在所有的Linu ...}

我要回帖

更多关于 linux shell 函数调用 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信