📄 unix编程应用问答scz.html
字号:
在所有的计算器处理程序中,都会对SIGFPE信号做相应处理,前些日子看yacc/lex的
时候又碰上过。正确的做法是,利用远跳转转移,让开引发异常的指令。
代码修改如下
--------------------------------------------------------------------------
/*
* gcc -Wall -pipe -O3 -o sigfpe_test_1 sigfpe_test_1.c
*
* 注意与下面的编译效果进行对比,去掉优化开关-O3
*
* gcc -Wall -pipe -o sigfpe_test_1 sigfpe_test_1.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <setjmp.h>
/*
* for signal handlers
*/
typedef void Sigfunc ( int );
Sigfunc * signal ( int signo, Sigfunc *func );
static Sigfunc * Signal ( int signo, Sigfunc *func );
static void on_fpe ( int signo );
static sigjmp_buf jmpbuf;
static volatile sig_atomic_t canjump = 0;
Sigfunc * signal ( int signo, Sigfunc *func )
{
struct sigaction act, oact;
act.sa_handler = func;
sigemptyset( &act.sa_mask );
act.sa_flags = 0;
if ( signo == SIGALRM )
{
#ifdef SA_INTERRUPT
act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */
#endif
}
else
{
#ifdef SA_RESTART
act.sa_flags |= SA_RESTART; /* SVR4, 44BSD */
#endif
}
if ( sigaction( signo, &act, &oact ) < 0 )
{
return( SIG_ERR );
}
return( oact.sa_handler );
} /* end of signal */
static Sigfunc * Signal ( int signo, Sigfunc *func )
{
Sigfunc *sigfunc;
if ( ( sigfunc = signal( signo, func ) ) == SIG_ERR )
{
perror( "signal" );
exit( EXIT_FAILURE );
}
return( sigfunc );
} /* end of Signal */
static void on_fpe ( int signo )
{
if ( canjump == 0 )
{
return; /* unexpected signal, ignore */
}
canjump = 0;
fprintf( stderr, "here is on_fpe\n" );
siglongjmp( jmpbuf, signo ); /* jump back to main, don't return */
return;
} /* end of on_fpe */
int main ( int argc, char * argv[] )
{
unsigned int i;
if ( sigsetjmp( jmpbuf, 1 ) != 0 )
{
fprintf( stderr, "c u later\n" );
return( EXIT_SUCCESS );
}
/*
* now sigsetjump() is OK
*/
canjump = 1;
Signal( SIGFPE, on_fpe );
i = 51211314 / 0;
/*
* 另外,增加这行后,再次对比有-O3和无-O3的效果
*
* fprintf( stderr, "i = %#X\n", i );
*/
return( EXIT_SUCCESS );
} /* end of main */
--------------------------------------------------------------------------
关于-O3的讨论,对gcc编译器熟悉的朋友请继续,呵,我对Linux下的这此东西,实
在缺乏兴趣。
3. -lelf、-lkvm、-lkstat相关问题
3.1 如何判断可执行文件是否携带了调试信息
Q: 某些时候需要知道编译可执行文件时是否携带了调试信息(比如是否指定了-g编译
选项)。检查可执行文件中是否包含".stab" elf section,".stab" section用于
保存相关调试信息。
A: Sun Microsystems 2000-05-15
下面这个脚本演示如何判断可执行文件是否携带调试信息
--------------------------------------------------------------------------
#! /bin/sh
#
# Script that test whether or not a given file has been built for
# debug (-g option specified in the compilation)
if [ $# -le 0 ]
then
echo "Usage: $1 filename"
exit 1
fi
if [ ! -f $1 ]
then
echo "File $1 does not exist"
exit 1
fi
/usr/ccs/bin/dump -hv $1 | /bin/egrep -s '.stab$'
if [ $? -eq 0 ]
then
echo "File '$1' has been built for debug"
exit 0
else
echo "File '$1' has not been built for debug"
exit 1
fi
--------------------------------------------------------------------------
如果对ELF文件格式不熟悉,理解上述代码可能有点困难,参看
http://www.digibel.org/~tompy/hacking/elf.txt,这是1.1版的ELF文件格式规范。
3.2 mprotect如何用
A: 小四 <cloudsky@263.net>
# truss prtconf 2>&1 | grep sysconf
sysconfig(_CONFIG_PAGESIZE) = 8192
sysconfig(_CONFIG_PHYS_PAGES) = 16384
#
由此可知当前系统页尺寸是8192字节。
--------------------------------------------------------------------------
/*
* gcc -Wall -g -ggdb -static -o mtest mtest.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>
int main ( int argc, char * argv[] )
{
char *buf;
char c;
/*
* 分配一块内存,拥有缺省的rw-保护
*/
buf = ( char * )malloc( 1024 + 8191 );
if ( !buf )
{
perror( "malloc" );
exit( errno );
}
/*
* Align to a multiple of PAGESIZE, assumed to be a power of two
*/
buf = ( char * )( ( ( unsigned int )buf + 8191 ) & ~8191 );
c = buf[77];
buf[77] = c;
printf( "ok\n" );
/*
* Mark the buffer read-only.
*
* 必须保证这里buf位于页边界上,否则mprotect()失败,报告无效参数
*/
if ( mprotect( buf, 1024, PROT_READ ) )
{
perror( "\nmprotect" );
exit( errno );
}
c = buf[77];
/*
* Write error, program dies on SIGSEGV
*/
buf[77] = c;
exit( 0 );
} /* end of main */
--------------------------------------------------------------------------
$ ./mtest
ok
段错误 (core dumped) <-- 内存保护起作用了
$
3.3 mmap如何用
A: 小四 <cloudsky@263.net>
下面写一个完成文件复制功能的小程序,利用mmap(2),而不是标准文件I/O接口。
--------------------------------------------------------------------------
/*
* gcc -Wall -O3 -o copy_mmap copy_mmap.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* for memcpy */
#include <strings.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define PERMS 0600
int main ( int argc, char * argv[] )
{
int src, dst;
void *sm, *dm;
struct stat statbuf;
if ( argc != 3 )
{
fprintf( stderr, " Usage: %s <source> <target>\n", argv[0] );
exit( EXIT_FAILURE );
}
if ( ( src = open( argv[1], O_RDONLY ) ) < 0 )
{
perror( "open source" );
exit( EXIT_FAILURE );
}
/* 为了完成复制,必须包含读打开,否则mmap()失败 */
if ( ( dst = open( argv[2], O_RDWR | O_CREAT | O_TRUNC, PERMS ) ) < 0 )
{
perror( "open target" );
exit( EXIT_FAILURE );
}
if ( fstat( src, &statbuf ) < 0 )
{
perror( "fstat source" );
exit( EXIT_FAILURE );
}
/*
* 参看前面man手册中的说明,mmap()不能用于扩展文件长度。所以这里必须事
* 先扩大目标文件长度,准备一个空架子等待复制。
*/
if ( lseek( dst, statbuf.st_size - 1, SEEK_SET ) < 0 )
{
perror( "lseek target" );
exit( EXIT_FAILURE );
}
if ( write( dst, &statbuf, 1 ) != 1 )
{
perror( "write target" );
exit( EXIT_FAILURE );
}
/* 读的时候指定 MAP_PRIVATE 即可 */
sm = mmap( 0, ( size_t )statbuf.st_size, PROT_READ,
MAP_PRIVATE | MAP_NORESERVE, src, 0 );
if ( MAP_FAILED == sm )
{
perror( "mmap source" );
exit( EXIT_FAILURE );
}
/* 这里必须指定 MAP_SHARED 才可能真正改变静态文件 */
dm = mmap( 0, ( size_t )statbuf.st_size, PROT_WRITE,
MAP_SHARED, dst, 0 );
if ( MAP_FAILED == dm )
{
perror( "mmap target" );
exit( EXIT_FAILURE );
}
memcpy( dm, sm, ( size_t )statbuf.st_size );
/*
* 可以不要这行代码
*
* msync( dm, ( size_t )statbuf.st_size, MS_SYNC );
*/
return( EXIT_SUCCESS );
} /* end of main */
--------------------------------------------------------------------------
mmap()好处是处理大文件时速度明显快于标准文件I/O,无论读写,都少了一次用户
空间与内核空间之间的复制过程。操作内存还便于设计、优化算法。
文件I/O操作/proc/self/mem不存在页边界对齐的问题。至少Linux的mmap()的最后一
个形参offset并未强制要求页边界对齐,如果提供的值未对齐,系统自动向上舍入到
页边界上。
malloc()分配得到的地址不见得对齐在页边界上
/proc/self/mem和/dev/kmem不同。root用户打开/dev/kmem就可以在用户空间访问到
内核空间的数据,包括偏移0处的数据,系统提供了这样的支持。
显然代码段经过/proc/self/mem可写映射后已经可写,无须mprotect()介入。
D: scz <scz@nsfocus.com>
Solaris 2.6下参看getpagesize(3C)手册页,关于如何获取页大小,一般是8192。
Linux下参看getpagesize(2)手册页,一般是4096。
3.4 getrusage如何用
A: 小四 <cloudsky@263.net>
在SPARC/Solaris 2.6/7下结论一致,只支持了ru_utime和ru_stime成员,其他成员
被设置成0。修改头文件后在FreeBSD 4.3-RELEASE上测试,则不只支持ru_utime和
ru_stime成员。从FreeBSD的getrusage(2)手册页可以看到,这个函数源自4.2 BSD。
如此来说,至少对于SPARC/Solaris 2.6/7,getrusage(3C)并无多大意义。
3.5 setitimer如何用
D: scz <scz@nsfocus.com>
为什么要学习使用setitimer(2),因为alarm(3)属于被淘汰的定时器技术。
A: 小四 <cloudsky@263.net>
下面是个x86/FreeBSD 4.3-RELEASE下的例子
--------------------------------------------------------------------------
/*
* File : timer_sample.c
* Author : Unknown (Don't ask me anything about this program)
* Complie : gcc -Wall -pipe -O3 -o timer_sample timer_sample.c
* Platform : x86/FreeBSD 4.3-RELEASE
* Date : 2001-09-18 15:18
*/
/************************************************************************
* *
* Head File *
* *
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <signal.h>
/************************************************************************
* *
* Macro *
* *
************************************************************************/
typedef void Sigfunc ( int ); /* for signal handlers */
/************************************************************************
* *
* Function Prototype *
* *
************************************************************************/
static void Atexit ( void ( *func ) ( void ) );
static void init_signal ( void );
static void init_timer ( void );
static void on_alarm ( int signo );
static void on_terminate ( int signo );
static int Setitimer ( int which, const struct itimerval *value,
struct itimerval *ovalue );
Sigfunc * signal ( int signo, Sigfunc *func );
static Sigfunc * Signal ( int signo, Sigfunc *func );
static void terminate ( void );
/************************************************************************
* *
* Static Global Var *
* *
************************************************************************/
/************************************************************************/
static void Atexit ( void ( *func ) ( void ) )
{
if ( atexit( func ) != 0 )
{
perror( "atexit" );
exit( EXIT_FAILURE );
}
return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -