文章列表
 
您正在查看 "程序设计" 分类下的文章

2008-02-22 9:44
原文地址: http://blog.chinaunix.net/u/19651/showart_344578.html
source Insight常用自定义命令和一些小技巧[z]

在Source Insight中添加自定义功能的步骤如下:
1.Source Insight中,Options->Custom Commands...->Add...,New Command name 随便写,我的是"Edit with Vim"
2.Run中写入: "C:\Program Files\Vim\vim63\gvim.exe" --remote-silent +%l %f
意思是在当前已经打开的gvim窗口里面打开当前的文件,并且跳转到指定行
%l为当前的行号,%f为文件名
使用 --remote-silent 的作用是,如果已经打开了对应文件,就不会打开第二次,而是在已经打开的文件里跳转到对应行
3.还是同一个对话框里面,选择Keys->Assign New Key...->按F12,如果你已经将F12设置给其他命令,选择其他的按键就行了

下面是一些常用自定义功能:( CUSTOM COMMANDS )

打开资源管理器并选中当前文件
ShellExecute open explorer /e,/select,%f
查看log
"C:\Program Files\TortoiseSVN\bin\TortoiseProc.exe" /command:log /path:%f /notempfile /closeonend
diff
"C:\Program Files\TortoiseSVN\bin\TortoiseProc.exe" /command:diff /path:%f /notempfile /closeonend
取得锁定(check out)
"C:\Program Files\TortoiseSVN\bin\TortoiseProc.exe" /command:lock /path:%f /notempfile /closeonend
提交(check in)
"C:\Program Files\TortoiseSVN\bin\TortoiseProc.exe" /command:commit /path:%f /notempfile /closeonend
更新(update)
"C:\Program Files\TortoiseSVN\bin\TortoiseProc.exe" /command:update /path:%f /notempfile /closeonend
更新整个目录(update all)
"C:\Program Files\TortoiseSVN\bin\TortoiseProc.exe" /command:update /path:*.* /notempfile /closeonend
取消锁定(undo check out)
"C:\Program Files\TortoiseSVN\bin\TortoiseProc.exe" /command:revert /path:%f /notempfile /closeonend
在ultriEdit中编辑
"C:\Program Files\UltraEdit-32/uedit32" %f
在vim中编辑并定位到当前行
"C:\Program Files\Vim\vim63\gvim.exe" --remote-silent +%l %f

汇总其他小技巧:

让{ 和 } 不缩进:

Options->Document Options->Auto Indent->Indent Open Brace/Indent Close Brace

hao space: SourceInsight 小技巧
1、按住"ctrl", 再用鼠标指向某个变量,点击一下,就能进入这个变量的定义。

2、今天把一个用sourceinsight排版整齐的C文件,偶然用VC打开一看,全乱了。研究了半天,发现SI对每个字符的宽度不太一致。
    请教同事发现选上"view --> draft view", 就可以让每个字符的宽度一致了。快捷键是 "Alt + F12"

3、"shift+F8" 标亮所有文本中光标所在位置的单词

4、跳到某一行:"ctrl + g"

Source Insight是阅读和编写代码的好东东,基本上也算得上是经典之作了,虽然还有一点点小bug,不过对于我们这些C程序员来说可是一旦拥有别无所求。下 列小技巧是在工作中同事整理总结的,对提高工作效率多少有点帮助,其中有些是对应于SVN的,没有使用SVN做版本管理的人就不要白费力气了。

ShellExecute open explorer /e,/select,%f
        /*作用是在资源管理器中打开当前编辑文件并选中*/
        /*可以设置快捷键如ctrl+e,这样能很方便的在资源管理器打开对应的文件,并进行tortoiseSVN的相关操作*/

X:\Progra~1\TortoiseSVN\bin\TortoiseProc.exe /command:log /path:% /notempfile /closeonend
        /*使用前注意更改对应的bin安装路径*/
        /*作用是直接查看当前文件的svn log*/
        /*可以设置快捷键如ctrl+l*/

X:\Progra~1\TortoiseSVN\bin\TortoiseProc.exe /command:diff /path:% /notempfile /closeonend
        /*使用前注意更改对应的bin安装路径*/
        /*作用是直接查看当前文件和基准版本的比较*/
        /*可以设置快捷键如ctrl+d*/

在Source Insight中快速添加注释
      将以下代码保存成Utils.em,详细使用说明看文章结尾

/* Utils.em - a small collection of useful editing macros */

/*-------------------------------------------------------------------------
    I N S E R T   H E A D E R

    Inserts a comment header block at the top of the current function.
    This actually works on any type of symbol, not just functions.

    To use this, define an environment variable "MYNAME" and set it
    to your email name. eg. set MYNAME=raygr
-------------------------------------------------------------------------*/
macro InsertHeader()
{
    // Get the owner's name from the environment variable: MYNAME.
    // If the variable doesn't exist, then the owner field is skipped.
    szMyName = getenv(MYNAME)
   
    // Get a handle to the current file buffer and the name
    // and location of the current symbol where the cursor is.
    hbuf = GetCurrentBuf()
    szFunc = GetCurSymbol()
    ln = GetSymbolLine(szFunc)

    // begin assembling the title string
    sz = "/*   "
   
    /* convert symbol name to T E X T   L I K E   T H I S */
    cch = strlen(szFunc)
    ich = 0
    while (ich < cch)
        {
        ch = szFunc[ich]
        if (ich > 0)
            if (isupper(ch))
                sz = cat(sz, "   ")
            else
                sz = cat(sz, " ")
        sz = Cat(sz, toupper(ch))
        ich = ich + 1
        }
   
    sz = Cat(sz, "   */")
    InsBufLine(hbuf, ln, sz)
    InsBufLine(hbuf, ln+1, "/*-------------------------------------------------------------------------")
   
    /* if owner variable exists, insert Owner: name */
    if (strlen(szMyName) > 0)
        {
        InsBufLine(hbuf, ln+2, "    Owner: @szMyName@")
        InsBufLine(hbuf, ln+3, " ")
        ln = ln + 4
        }
    else
        ln = ln + 2
   
    InsBufLine(hbuf, ln,   "    ") // provide an indent already
    InsBufLine(hbuf, ln+1, "-------------------------------------------------------------------------*/")
   
    // put the insertion point inside the header comment
    SetBufIns(hbuf, ln, 4)
}


/* InsertFileHeader:

   Inserts a comment header block at the top of the current function.
   This actually works on any type of symbol, not just functions.

   To use this, define an environment variable "MYNAME" and set it
   to your email name. eg. set MYNAME=raygr
*/

macro InsertFileHeader()
{
    szMyName = getenv(MYNAME)
   
    hbuf = GetCurrentBuf()

    InsBufLine(hbuf, 0, "/*-------------------------------------------------------------------------")
   
    /* if owner variable exists, insert Owner: name */
    InsBufLine(hbuf, 1, "    ")
    if (strlen(szMyName) > 0)
        {
        sz = "    Owner: @szMyName@"
        InsBufLine(hbuf, 2, " ")
        InsBufLine(hbuf, 3, sz)
        ln = 4
        }
    else
        ln = 2
   
    InsBufLine(hbuf, ln, "-------------------------------------------------------------------------*/")
}



// Inserts "Returns True .. or False..." at the current line
macro ReturnTrueOrFalse()
{
    hbuf = GetCurrentBuf()
    ln = GetBufLineCur(hbuf)

    InsBufLine(hbuf, ln, "    Returns True if successful or False if errors.")
}



/* Inserts ifdef REVIEW around the selection */
macro IfdefReview()
{
    IfdefSz("REVIEW");
}


/* Inserts ifdef BOGUS around the selection */
macro IfdefBogus()
{
    IfdefSz("BOGUS");
}


/* Inserts ifdef NEVER around the selection */
macro IfdefNever()
{
    IfdefSz("NEVER");
}


// Ask user for ifdef condition and wrap it around current
// selection.
macro InsertIfdef()
{
    sz = Ask("Enter ifdef condition:")
    if (sz != "")
        IfdefSz(sz);
}

macro InsertCPlusPlus()
{
    IfdefSz("__cplusplus");
}


// Wrap ifdef <sz> .. endif around the current selection
macro IfdefSz(sz)
{
    hwnd = GetCurrentWnd()
    lnFirst = GetWndSelLnFirst(hwnd)
    lnLast = GetWndSelLnLast(hwnd)
   
    hbuf = GetCurrentBuf()
    InsBufLine(hbuf, lnFirst, "#ifdef @sz@")
    InsBufLine(hbuf, lnLast+2, "#endif /* @sz@ */")
}


// Delete the current line and appends it to the clipboard buffer
macro KillLine()
{
    hbufCur = GetCurrentBuf();
    lnCur = GetBufLnCur(hbufCur)
    hbufClip = GetBufHandle("Clipboard")
    AppendBufLine(hbufClip, GetBufLine(hbufCur, lnCur))
    DelBufLine(hbufCur, lnCur)
}


// Paste lines killed with KillLine (clipboard is emptied)
macro PasteKillLine()
{
    Paste
    EmptyBuf(GetBufHandle("Clipboard"))
}



// delete all lines in the buffer
macro EmptyBuf(hbuf)
{
    lnMax = GetBufLineCount(hbuf)
    while (lnMax > 0)
        {
        DelBufLine(hbuf, 0)
        lnMax = lnMax - 1
        }
}


// Ask the user for a symbol name, then jump to its declaration
macro JumpAnywhere()
{
    symbol = Ask("What declaration would you like to see?")
    JumpToSymbolDef(symbol)
}

   
// list all siblings of a user specified symbol
// A sibling is any other symbol declared in the same file.
macro OutputSiblingSymbols()
{
    symbol = Ask("What symbol would you like to list siblings for?")
    hbuf = ListAllSiblings(symbol)
    SetCurrentBuf(hbuf)
}


// Given a symbol name, open the file its declared in and
// create a new output buffer listing all of the symbols declared
// in that file. Returns the new buffer handle.
macro ListAllSiblings(symbol)
{
    loc = GetSymbolLocation(symbol)
    if (loc == "")
        {
        msg ("@symbol@ not found.")
        stop
        }
   
    hbufOutput = NewBuf("Results")
   
    hbuf = OpenBuf(loc.file)
    if (hbuf == 0)
        {
        msg ("Can't open file.")
        stop
        }
       
    isymMax = GetBufSymCount(hbuf)
    isym = 0;
    while (isym < isymMax)
        {
        AppendBufLine(hbufOutput, GetBufSymName(hbuf, isym))
        isym = isym + 1
        }

    CloseBuf(hbuf)
   
    return hbufOutput

}

/*

    written by yubind

                                                            */

macro SingleLineComment()
{
szMyName = "chenjsa"
// Get a handle to the current file buffer and the name
// and location of the current symbol where the cursor is.
hbuf = GetCurrentBuf()
ln = GetBufLnCur(hbuf)

// Get current time
szTime = GetSysTime(1)
Hour = szTime.Hour
Minute = szTime.Minute
Second = szTime.Second
Day = szTime.Day
Month = szTime.Month
Year = szTime.Year
if (Day < 10)
szDay = "0@Day@"
else
szDay = Day
//szMonth = NumToName(Month)
if (Month < 10)
     szMonth = "0@Month@"
else
szMonth = Month

szDescription = Ask("请输入修改原因")
// begin assembling the title string
InsBufLine(hbuf, ln+1, "/*@szDescription@ @szMyName@.xmyanfa @Year@-@szMonth@-@szDay@*/")
}

macro MultiLineCommentHeader()
{
szMyName = "chenjsa"
// Get a handle to the current file buffer and the name
// and location of the current symbol where the cursor is.
hbuf = GetCurrentBuf()
ln = GetBufLnCur(hbuf)

// Get current time
szTime = GetSysTime(1)
Hour = szTime.Hour
Minute = szTime.Minute
Second = szTime.Second
Day = szTime.Day
Month = szTime.Month
Year = szTime.Year
if (Day < 10)
szDay = "0@Day@"
else
szDay = Day
//szMonth = NumToName(Month)
if (Month < 10)
     szMonth = "0@Month@"
else
szMonth = Month

szDescription = Ask("请输入修改原因:")
// begin assembling the title string
InsBufLine(hbuf, ln + 1, "/*@szDescription@ @szMyName@.xmyanfa @Year@-@szMonth@-@szDay@ begin*/")
}

macro MultiLineCommentEnd()
{
szMyName = "chenjsa"
// Get a handle to the current file buffer and the name
// and location of the current symbol where the cursor is.
hbuf = GetCurrentBuf()
ln = GetBufLnCur(hbuf)

// Get current time
szTime = GetSysTime(1)
Hour = szTime.Hour
Minute = szTime.Minute
Second = szTime.Second
Day = szTime.Day
Month = szTime.Month
Year = szTime.Year
if (Day < 10)
szDay = "0@Day@"
else
szDay = Day
//szMonth = NumToName(Month)
if (Month < 10)
     szMonth = "0@Month@"
else
szMonth = Month

InsBufLine(hbuf, ln + 1, "/*@szMyName@.xmyanfa @Year@-@szMonth@-@szDay@ end*/")
}

使用说明:可以实现在sourceinsight中快速添加修改注释。

    1. Project->Open Project... 打开Base工程(该工程一般在我的文档\\Source Insight\\Projects\\Base中);
    2. 搜索utils.em 里的字串"chenjsa" 改成自己的姓名
    3. Project->Add and Remove Project Files... 加入宏文件(即utils.em);
    4. Options->Menu Assignments 打开Menu Assignments窗口, 在Command中输入Macro, 选中要使用的宏(SingleLineComment ,MultiLineCommentHeader,MultiLineCommentEnd), 添加到合适的菜单中.

 
2007-04-05 16:17
首先,批处理文件是一个文本文件,这个文件的每一行都是一条DOS命令(大部分时候就好象我们在DOS提示符下执行的命令行一样),你可以使用DOS下的Edit或者Windows的记事本(notepad)等任何文本文件编辑工具创建和修改批处理文件。
其次,批处理文件是一种简单的程序,可以通过条件语句(if)和流程控制语句(goto)来控制命令运行的流程,在批处理中也可以使用循环语句(for)来循环执行一条命令。当然,批处理文件的编程能力与C语言等编程语句比起来是十分有限的,也是十分不规范的。批处理的程序语句就是一条条的DOS命令(包括内部命令和外部命令),而批处理的能力主要取决于你所使用的命令。

第三,每个编写好的批处理文件都相当于一个DOS的外部命令,你可以把它所在的目录放到你的DOS搜索路径(path)中来使得它可以在任意位置运行。一个良好的习惯是在硬盘上建立一个bat或者batch目录(例如C:\BATCH),然后将所有你编写的批处理文件放到该目录中,这样只要在path中设置上c:\batch,你就可以在任意位置运行所有你编写的批处理程序。

第四,在DOS和Win9x/Me系统下,C:盘根目录下的AUTOEXEC.BAT批处理文件是自动运行批处理文件,每次系统启动时会自动运行该文件,你可以将系统每次启动时都要运行的命令放入该文件中,例如设置搜索路径,调入鼠标驱动和磁盘缓存,设置系统环境变量等。下面是一个运行于Windows 98下的autoexec.bat的示例:
@ECHO OFF
PATH C:\WINDOWS;C:\WINDOWS\COMMAND;C:\UCDOS;C:\DOSTools;C:\SYSTOOLS;C:\WINTOOLS;C:\BATCH
LH SMARTDRV.EXE /X
LH DOSKEY.COM /INSERT
LH CTMOUSE.EXE
SET TEMP=D:\TEMP
SET TMP=D:\TEMP


批处理的作用
简单的说,批处理的作用就是自动的连续执行多条命令。

这里先讲一个最简单的应用:在启动wps软件时,每次都必须执行(>前面内容表示DOS提示符):
C:\>cd wps
C:\WPS>spdos
C:\WPS>py
C:\WPS>wbx
C:\WPS>wps
如果每次用WPS之前都这样执行一遍,您是不是觉得很麻烦呢?

好了,用批处理,就可以实现将这些麻烦的操作简单化,首先我们编写一个runwps.bat批处理文件,内容如下:
@echo off
c:
cd\wps
spdos
py
wbx
wps
cd\

以后,我们每次进入wps,只需要运行runwps这个批处理文件即可。

常用命令

echo、@、call、pause、rem(小技巧:用::代替rem)是批处理文件最常用的几个命令,我们就从他们开始学起。
echo 表示显示此命令后的字符
echo off 表示在此语句后所有运行的命令都不显示命令行本身
@与echo off相象,但它是加在每个命令行的最前面,表示运行时不显示这一行的命令行(只能影响当前行)。
call 调用另一个批处理文件(如果不用call而直接调用别的批处理文件,那么执行完那个批处理文件后将无法返回当前文件并执行当前文件的后续命令)。
pause 运行此句会暂停批处理的执行并在屏幕上显示Press any key to continue...的提示,等待用户按任意键后继续
rem 表示此命令后的字符为解释行(注释),不执行,只是给自己今后参考用的(相当于程序中的注释)。

例1:用edit编辑a.bat文件,输入下列内容后存盘为c:\a.bat,执行该批处理文件后可实现:将根目录中所有文件写入 a.txt中,启动UCDOS,进入WPS等功能。

  批处理文件的内容为:         命令注释:

    @echo off           不显示后续命令行及当前命令行
    dir c:\*.* >a.txt       将c盘文件列表写入a.txt
    call c:\ucdos\ucdos.bat    调用ucdos
    echo 你好            显示"你好"
    pause              暂停,等待按键继续
    rem 准备运行wps         注释:准备运行wps
    cd ucdos            进入ucdos目录
    wps               运行wps  

批处理文件的参数

批处理文件还可以像C语言的函数一样使用参数(相当于DOS命令的命令行参数),这需要用到一个参数表示符“%”。

%[1-9]表示参数,参数是指在运行批处理文件时在文件名后加的以空格(或者Tab)分隔的字符串。变量可以从%0到%9,%0表示批处理命令本身,其它参数字符串用%1到%9顺序表示。

例2:C:根目录下有一批处理文件名为f.bat,内容为:
@echo off
format %1

如果执行C:\>f a:
那么在执行f.bat时,%1就表示a:,这样format %1就相当于format a:,于是上面的命令运行时实际执行的是format a:

例3:C:根目录下一批处理文件名为t.bat,内容为:
@echo off
type %1
type %2

那么运行C:\>t a.txt b.txt
%1 : 表示a.txt
%2 : 表示b.txt
于是上面的命令将顺序地显示a.txt和b.txt文件的内容。


特殊命令

if goto choice for是批处理文件中比较高级的命令,如果这几个你用得很熟练,你就是批处理文件的专家啦。

一、if 是条件语句,用来判断是否符合规定的条件,从而决定执行不同的命令。 有三种格式:

1、if [not] "参数" == "字符串" 待执行的命令

参数如果等于(not表示不等,下同)指定的字符串,则条件成立,运行命令,否则运行下一句。

例:if "%1"=="a" format a:

2、if [not] exist [路径\]文件名 待执行的命令
如果有指定的文件,则条件成立,运行命令,否则运行下一句。

如: if exist c:\config.sys type c:\config.sys
表示如果存在c:\config.sys文件,则显示它的内容。

3、if errorlevel <数字> 待执行的命令

很多DOS程序在运行结束后会返回一个数字值用来表示程序运行的结果(或者状态),通过if errorlevel命令可以判断程序的返回值,根据不同的返回值来决定执行不同的命令(返回值必须按照从大到小的顺序排列)。如果返回值等于指定的数字,则条件成立,运行命令,否则运行下一句。

如if errorlevel 2 goto x2

二、goto 批处理文件运行到这里将跳到goto所指定的标号(标号即label,标号用:后跟标准字符串来定义)处,goto语句一般与if配合使用,根据不同的条件来执行不同的命令组。

如:

goto end

:end
echo this is the end

标号用“:字符串”来定义,标号所在行不被执行。

三、choice 使用此命令可以让用户输入一个字符(用于选择),从而根据用户的选择返回不同的errorlevel,然后于if errorlevel配合,根据用户的选择运行不同的命令。

注意:choice命令为DOS或者Windows系统提供的外部命令,不同版本的choice命令语法会稍有不同,请用choice /?查看用法。

choice的命令语法(该语法为Windows 2003中choice命令的语法,其它版本的choice的命令语法与此大同小异):

CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]

描述:
    该工具允许用户从选择列表选择一个项目并返回所选项目的索引。

参数列表:
   /C     choices        指定要创建的选项列表。默认列表是 "YN"。

   /N                   在提示符中隐藏选项列表。提示前面的消息得到显示,
                       选项依旧处于启用状态。

   /CS                  允许选择分大小写的选项。在默认情况下,这个工具
                       是不分大小写的。

   /T     timeout        做出默认选择之前,暂停的秒数。可接受的值是从 0
                       到 9999。如果指定了 0,就不会有暂停,默认选项
                       会得到选择。

   /D     choice         在 nnnn 秒之后指定默认选项。字符必须在用 /C 选
                       项指定的一组选择中; 同时,必须用 /T 指定 nnnn。

   /M     text           指定提示之前要显示的消息。如果没有指定,工具只
                       显示提示。

   /?                   显示帮助消息。

   注意:
   ERRORLEVEL 环境变量被设置为从选择集选择的键索引。列出的第一个选
   择返回 1,第二个选择返回 2,等等。如果用户按的键不是有效的选择,
   该工具会发出警告响声。如果该工具检测到错误状态,它会返回 255 的
   ERRORLEVEL 值。如果用户按 Ctrl+Break 或 Ctrl+C 键,该工具会返回 0
   的 ERRORLEVEL 值。在一个批程序中使用 ERRORLEVEL 参数时,将参数降
   序排列。

示例:
   CHOICE /?
   CHOICE /C YNC /M "确认请按 Y,否请按 N,或者取消请按 C。"
   CHOICE /T 10 /C ync /CS /D y
   CHOICE /C ab /M "选项 1 请选择 a,选项 2 请选择 b。"
   CHOICE /C ab /N /M "选项 1 请选择 a,选项 2 请选择 b。"
  
如果我运行命令:CHOICE /C YNC /M "确认请按 Y,否请按 N,或者取消请按 C。"
屏幕上会显示:
确认请按 Y,否请按 N,或者取消请按 C。 [Y,N,C]?
  
  
例:test.bat的内容如下(注意,用if errorlevel判断返回值时,要按返回值从高到低排列):
@echo off
choice /C dme /M "defrag,mem,end"
if errorlevel 3 goto end
if errorlevel 2 goto mem
if errotlevel 1 goto defrag

:defrag
c:\dos\defrag
goto end

:mem
mem
goto end

:end
echo good bye

此批处理运行后,将显示“defrag,mem,end[D,M,E]?” ,用户可选择d m e ,然后if语句根据用户的选择作出判断,d表示执行标号为defrag的程序段,m表示执行标号为mem的程序段,e表示执行标号为end的程序段,每个程序段最后都以goto end将程序跳到end标号处,然后程序将显示good bye,批处理运行结束。

四、for 循环命令,只要条件符合,它将多次执行同一命令。

语法:
对一组文件中的每一个文件执行某个特定命令。

FOR %%variable IN (set) DO command [command-parameters]

%%variable   指定一个单一字母可替换的参数。
(set)       指定一个或一组文件。可以使用通配符。
command     指定对每个文件执行的命令。
command-parameters
             为特定命令指定参数或命令行开关。

例如一个批处理文件中有一行:
for %%c in (*.bat *.txt) do type %%c

则该命令行会显示当前目录下所有以bat和txt为扩展名的文件的内容。


批处理示例

1. IF-EXIST

1)

首先用记事本在C:\建立一个test1.bat批处理文件,文件内容如下:
@echo off
IF EXIST \AUTOEXEC.BAT TYPE \AUTOEXEC.BAT
IF NOT EXIST \AUTOEXEC.BAT ECHO \AUTOEXEC.BAT does not exist

然后运行它:
C:\>TEST1.BAT

如果C:\存在AUTOEXEC.BAT文件,那么它的内容就会被显示出来,如果不存在,批处理就会提示你该文件不存在。

2)

接着再建立一个test2.bat文件,内容如下:
@ECHO OFF
IF EXIST \%1 TYPE \%1
IF NOT EXIST \%1 ECHO \%1 does not exist

执行:
C:\>TEST2 AUTOEXEC.BAT
该命令运行结果同上。

说明:
(1) IF EXIST 是用来测试文件是否存在的,格式为
IF EXIST [路径+文件名] 命令
(2) test2.bat文件中的%1是参数,DOS允许传递9个批参数信息给批处理文件,分别为%1~%9(%0表示test2命令本身) ,这有点象编程中的实参和形参的关系,%1是形参,AUTOEXEC.BAT是实参。

3) 更进一步的,建立一个名为TEST3.BAT的文件,内容如下:
@echo off
IF "%1" == "A" ECHO XIAO
IF "%2" == "B" ECHO TIAN
IF "%3" == "C" ECHO XIN

如果运行:
C:\>TEST3 A B C
屏幕上会显示:
XIAO
TIAN
XIN

如果运行:
C:\>TEST3 A B
屏幕上会显示
XIAO
TIAN

在这个命令执行过程中,DOS会将一个空字符串指定给参数%3。

2、IF-ERRORLEVEL

建立TEST4.BAT,内容如下:
@ECHO OFF
XCOPY C:\AUTOEXEC.BAT D:IF ERRORLEVEL 1 ECHO 文件拷贝失败
IF ERRORLEVEL 0 ECHO 成功拷贝文件

然后执行文件:
C:\>TEST4

如果文件拷贝成功,屏幕就会显示“成功拷贝文件”,否则就会显示“文件拷贝失败”。

IF ERRORLEVEL 是用来测试它的上一个DOS命令的返回值的,注意只是上一个命令的返回值,而且返回值必须依照从大到小次序顺序判断。
因此下面的批处理文件是错误的:
@ECHO OFF
XCOPY C:\AUTOEXEC.BAT D:\
IF ERRORLEVEL 0 ECHO 成功拷贝文件
IF ERRORLEVEL 1 ECHO 未找到拷贝文件
IF ERRORLEVEL 2 ECHO 用户通过ctrl-c中止拷贝操作
IF ERRORLEVEL 3 ECHO 预置错误阻止文件拷贝操作
IF ERRORLEVEL 4 ECHO 拷贝过程中写盘错误

无论拷贝是否成功,后面的:

未找到拷贝文件
用户通过ctrl-c中止拷贝操作
预置错误阻止文件拷贝操作
拷贝过程中写盘错误

都将显示出来。

以下就是几个常用命令的返回值及其代表的意义:
backup
0 备份成功
1 未找到备份文件
2 文件共享冲突阻止备份完成
3 用户用ctrl-c中止备份
4 由于致命的错误使备份操作中止

diskcomp
0 盘比较相同
1 盘比较不同
2 用户通过ctrl-c中止比较操作
3 由于致命的错误使比较操作中止
4 预置错误中止比较

diskcopy
0 盘拷贝操作成功
1 非致命盘读/写错
2 用户通过ctrl-c结束拷贝操作
3 因致命的处理错误使盘拷贝中止
4 预置错误阻止拷贝操作

format
0 格式化成功
3 用户通过ctrl-c中止格式化处理
4 因致命的处理错误使格式化中止
5 在提示“proceed with format(y/n)?”下用户键入n结束

xcopy
0 成功拷贝文件
1 未找到拷贝文件
2 用户通过ctrl-c中止拷贝操作
4 预置错误阻止文件拷贝操作
5 拷贝过程中写盘错误

3、IF STRING1 == STRING2

建立TEST5.BAT,文件内容如下:
@echo off
IF "%1" == "A" formAT A:

执行:
C:\>TEST5 A
屏幕上就出现是否将A:盘格式化的内容。

注意:为了防止参数为空的情况,一般会将字符串用双引号(或者其它符号,注意不能使用保留符号)括起来。
如:if [%1]==[A] 或者 if %1*==A*

5、GOTO

建立TEST6.BAT,文件内容如下:
@ECHO OFF
IF EXIST C:\AUTOEXEC.BAT GOTO _COPY
GOTO _DONE
:_COPY
COPY C:\AUTOEXEC.BAT D:\
:_DONE

注意:
(1) 标号前是ASCII字符的冒号":",冒号与标号之间不能有空格。
(2) 标号的命名规则与文件名的命名规则相同。
(3) DOS支持最长八位字符的标号,当无法区别两个标号时,将跳转至最近的一个标号。

6、FOR

建立C:\TEST7.BAT,文件内容如下:
@ECHO OFF
FOR %%C IN (*.BAT *.TXT *.SYS) DO TYPE %%C

运行:
C:>TEST7

执行以后,屏幕上会将C:盘根目录下所有以BAT、TXT、SYS为扩展名的文件内容显示出来(不包括隐藏文件)。
 
2007-02-27 14:51
在使用gdb调试程序的时候,有时候需要设定多个断点,重复执行某些操作,而这些操作写起来比较麻烦,这个时候就应该想起来用gdb命令脚本了,它能够很好的完成这些工作。
以设置多个断点为例,我写的命令脚本为
---------------------------------------------------
#filename: .gdbinit
#gdb will read it when starting
file test_gdbscript
set args hello
b main
b foo
r
---------------------------------------------------
有两种方式来使用这个脚本:
1)启动gdb时候
gdb在启动的时候,会在当前目录下查找".gdbinit"这个文件,并把它的内容作为gdb命令进行解释,所以如果我把脚本命名为".gdbinit",这样在启动的时候就会处理这些命令。
2)gdb运行期间
可以使用 source script-file 来解释gdb命令脚本script-file
 
2007-02-06 11:09
相信有不少的同志调试过包含共享库代码的程序,这个时候最为头疼的就是不能进行单步跟踪(当然是在你不知道如何解决的情况下^_^),本文根据一个实例来讲述如何来解决这个问题。首先来看我们的程序,包含两个文件:dyn.c, main.c,其中dyn.c被编译成一个共享库libdyn.so,在链接的时候要用到它。有一点必须声明,就是你的共享库代码必须是带有调试信息的(比如使用 -g选项)。
    $cat dyn.c
dyn ()
{
  puts ("Hello.");
}
    

    $cat main.c    
main ()
{
  puts ("before");
  dyn ();
  puts ("after");
}


    $cat Makefile
main:
        gcc -g -save-temps -c main.c -o main.o
        gcc -g -save-temps -c -fpic -ffunction-sections dyn.c
        gcc -g -save-temps -shared dyn.o -o libdyn.so
        gcc -g -save-temps main.o libdyn.so -o main
 
clean:
        rm -rf *.o *.so main


下面我们就来编译和调试程序:
    $make main 这之后就会在当前目录下生成我们所要的libdyn.so 和 main程序
如果我们向往常一样直接输入 $./main 来执行程序,这是不行的,它会给我们这样的错误提示:
before
./main: relocation error: ./main: undefined symbol: dyn
为什么?原来是我们使用了共享库libdyn.so却没有告诉动态链接程序到哪里去找他!好,这回我们告诉它:
    $LD_LIBRARY_PATH=`pwd` ./main
    before
    Hello.
    after
怎么样,出现我们想要的结果了吧。
    以上这些是一些小儿科啦,牛人们不要笑话啊,下面才是这次要讲的。通常情况下我们使用gdb进行调试的时候:
    $ gdb -q main
(gdb) b main
Breakpoint 1 at 0x8048478: file main.c, line 3.
(gdb) r
Starting program: /home/lirui/Test/main 
 
Breakpoint 1, main () at main.c:3
3         puts ("before");
(gdb) next
before
4         dyn ();
(gdb) 
/home/lirui/Test/main: relocation error: /home/lirui/Test/main: undefined symbol: dyn
 
Program exited with code 0177.
(gdb)
按照我们的本意,我们是想跟踪到dyn()函数内部去看一些咚咚的,可是它却找不到对应的符号信息,怎么办?
方法一:设定gdb环境变量 LD_PRELOAD,在执行程序前先把共享库代码load进来不就能找到了吗
    $gdb -q main
(gdb) set environment LD_PRELOAD ./libdyn.so
(gdb) break dyn 
Breakpoint 1 at 0x80483a8
(gdb) run
Starting program: /home/lirui/Test/main 
Breakpoint 1 at 0x400176ff: file dyn.c, line 3.
before
 
Breakpoint 1, dyn () at dyn.c:3
3         puts ("Hello.");
(gdb) list
1       dyn ()
2       {
3         puts ("Hello.");
4       }
(gdb)
这下不就找到dyn()函数了吗,这样你就可以很舒心的调试喽!

方法二:如果你使用的gdb版本中对”pending breakpoint"提供支持(V6.3当中就有支持),那么恭喜你,你可以先设定一个pending breakpoint,然后有gdb来决定到什么时候这个断点起作用。这里面有一点必须注意,你必须指定你的链接库的位置,可以通过设定环境变量LD_LIBRARY_PATH来实现。在执行gdb之前,我们可以这样做: $ export LD_LIBARY_PATH=`pwd`,告诉gdb在当前目录下查找共享库文件,然后向往常一样调试程序就可以了:
    $ export LD_LIBRARY_PATH=`pwd` 
    $ gdb -q main
Using host libthread_db library "/lib/libthread_db.so.1".
(gdb) b dyn
Function "dyn" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (dyn) pending.
(gdb) r
Starting program: /home/lirui/Test/tmp/main 
Reading symbols from shared object read from target memory...done.
Loaded system supplied DSO at 0xffffe000
Breakpoint 2 at 0xb7f4853a: file dyn.c, line 3.
Pending breakpoint "dyn" resolved
before
 
Breakpoint 2, dyn () at dyn.c:3
3         puts ("Hello.");
(gdb) l
1       dyn ()
2       {
3         puts ("Hello.");
4       }
(gdb) 

这样是不是也可以啊,^_^
方法三:这种情况只针对你要调试的程序整个就是一个动态链接的可执行程序,它在load到内存之后,入口地址都是动态变化的,如果你使用gdb进行调试,最初的时候你用 b function_name的话,它把断点设在了以0x0为基址的offset上,而程序load到内存之后,这个基址已经变了,所以总是不能设置成功断点。(我在调试qemu的时候就遇到这种情况),怎么办呢?最简单的方法就是不把这个程序编译成可重载的,像普通程序一样去编译它,不要为gcc 添加 -wl,-shared等参数就行了,这时候编译出来的就很容易调试了。
    针对这种情况,我觉得还应该有一个办法,但是我没有试验成功,就是在使用gdb的时候,最初不要把符号表load进来,如果已经load进来的话,就使用 symbol-file命令(后面不加参数)把已经载入的符号表丢弃掉,这时候再使用add_symbol_file filename start_address命令把符号表从filename中载入到以start_address开始的地址当中,就可以调试了。我以这种方法试验过不适用共享库的程序,是可以的,因为我们知道一般程序的开始地址是0x8048000,但是对于共享对象,我们不知道它的开始地址是多少,所以就不好办了,如果有一种办法能够告诉我们它的.text的起始地址,我们就很容易以这种方法来做了。

应该还有其他的方法吧,如果你知道,请告诉我 colin719@126.com,多谢!

另外一种Makefile的写法是:

.PHONY:all clean

all:main 

main:
        gcc -g -c main.c -o main.o
        gcc -g -c -fPIC -ffunction-sections dyn.c
        gcc -g -shared dyn.o -o libdyn.so
        gcc -g -Wl,-rpath ./ main.o ./libdyn.so -o main

clean:
        rm -rf *.o *.so main


 
2007-01-13 20:54

位段以位为单位定义结构体(或共用体)中成员所占存储空间的长度。
 含有位段的结构体类型称为位段结构。

位段结构也是一种结构体类型,只不过其中含有以位为单位定义存储长度的整数类型位段成员。采用位段结构既节省存储空间,又可方便操作。

位段结构中位段的定义格式为:
         unsigned <成员名>:<二进制位数>
例如:
struct bytedata
{unsigned a:2;   /*位段a,占2位*/
 unsigned:6;  /*无名位段,占6位,但不能访问*/
 unsigned:0;     /*无名位段,占0位,表下一位段从下一字边界开始*/
 unsigned b:10;  /*位段b,占10位*/
 int i;          /*成员i,从下一字边界开始*/
}data;

位段数据的引用:
同结构体成员中的数据引用一样,但应注意位段的最大取值范围不要超出二进制位数定的范围,否则超出部分会丢弃。
例如:data.a=2;   但  data.a=10;就超出范围(a占2位,最大3)

关于位段数据,注意以下几点:

(1)一个位段必须存储在同一存储单元(即字)之中,不能跨两个单元。如果其单元空间不够,则剩余空间不用,从下一个单元起存放该位段。
(2)可以通过定义长度为0的位段的方式使下一位段从下一存储单元开始。
(3)可以定义无名位段。
(4)位段的长度不能大于存储单元的长度。
(5)位段无地址,不能对位段进行取地址运算。
(6)位段可以以%d,%o,%x格式输出。
(7)位段若出现在表达式中,将被系统自动转换成整数。

 
2007-01-12 2:59
1,防止一个头文件被重复包含

#ifndef COMDEF_H

#define COMDEF_H

  //头文件内容

#endif

2,重新定义一些类型,防止由于各种平台和编译器的不同,而产生的类型字节数差异,方便移植。

typedef  unsigned char      boolean;     /* Boolean value type. */

typedef  unsigned long int  uint32;      /* Unsigned 32 bit value */
typedef  unsigned short     uint16;      /* Unsigned 16 bit value */
typedef  unsigned char      uint8;       /* Unsigned 8  bit value */
typedef  signed long int    int32;       /* Signed 32 bit value */
typedef  signed short       int16;       /* Signed 16 bit value */
typedef  signed char        int8;        /* Signed 8  bit value */

//下面的不建议使用

typedef  unsigned char     byte;         /* Unsigned 8  bit value type. */
typedef  unsigned short    word;         /* Unsinged 16 bit value type. */
typedef  unsigned long     dword;        /* Unsigned 32 bit value type. */
typedef  unsigned char     uint1;        /* Unsigned 8  bit value type. */
typedef  unsigned short    uint2;        /* Unsigned 16 bit value type. */
typedef  unsigned long     uint4;        /* Unsigned 32 bit value type. */

typedef  signed char       int1;         /* Signed 8  bit value type. */
typedef  signed short      int2;         /* Signed 16 bit value type. */
typedef  long int          int4;         /* Signed 32 bit value type. */
typedef  signed long       sint31;       /* Signed 32 bit value */
typedef  signed short      sint15;       /* Signed 16 bit value */
typedef  signed char       sint7;        /* Signed 8  bit value */
如果使用Linux平台编程,可以包含"stdint.h"或者“inttypes.h"头文件,其中已经定义了这些类型。

3,得到指定地址上的一个字节或字

#define  MEM_B( x )  ( *( (byte *) (x) ) )
#define  MEM_W( x )  ( *( (word *) (x) ) )

4,求最大值和最小值

#define  MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define  MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )

5,得到一个field在结构体(struct)中的偏移量

#define FPOS( type, field ) \
/*lint -e545 */ ( (dword) &(( type *) 0)-> field ) /*lint +e545 */

6,得到一个结构体中field所占用的字节数

#define FSIZ( type, field ) sizeof( ((type *) 0)->field )

7,按照LSB格式把两个字节转化为一个Word

#define  FLIPW( ray ) ( (((word) (ray)[0]) * 256) + (ray)[1] )

8,按照LSB格式把一个Word转化为两个字节

#define  FLOPW( ray, val ) \
  (ray)[0] = ((val) / 256); \
  (ray)[1] = ((val) & 0xFF)

9,得到一个变量的地址(word宽度)

#define  B_PTR( var )  ( (byte *) (void *) &(var) )
#define  W_PTR( var )  ( (word *) (void *) &(var) )

10,得到一个字的高位和低位字节

#define  WORD_LO(xxx)  ((byte) ((word)(xxx) & 255))
#define  WORD_HI(xxx)  ((byte) ((word)(xxx) >> 8))

11,返回一个比X大的最接近的8的倍数

#define RND8( x )       ((((x) + 7) / 8 ) * 8 )

12,将一个字母转换为大写

#define  UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )

13,判断字符是不是10进值的数字

#define  DECCHK( c ) ((c) >= '0' && (c) <= '9')

14,判断字符是不是16进值的数字

#define  HEXCHK( c ) ( ((c) >= '0' && (c) <= '9') ||\
                       ((c) >= 'A' && (c) <= 'F') ||\
                       ((c) >= 'a' && (c) <= 'f') )

15,防止溢出的一个方法

#define  INC_SAT( val )  (val = ((val)+1 > (val)) ? (val)+1 : (val))

16,返回数组元素的个数

#define  ARR_SIZE( a )  ( sizeof( (a) ) / sizeof( (a[0]) ) )

17,返回一个无符号数n尾的值MOD_BY_POWER_OF_TWO(X,n)=X%(2^n)

#define MOD_BY_POWER_OF_TWO( val, mod_by ) \
           ( (dword)(val) & (dword)((mod_by)-1) )

18,对于IO空间映射在存储空间的结构,输入输出处理

#define inp(port)         (*((volatile byte *) (port)))
#define inpw(port)        (*((volatile word *) (port)))
#define inpdw(port)       (*((volatile dword *)(port)))
#define outp(port, val)   (*((volatile byte *) (port)) = ((byte) (val)))
#define outpw(port, val)  (*((volatile word *) (port)) = ((word) (val)))
#define outpdw(port, val) (*((volatile dword *) (port)) = ((dword) (val)))

19,使用一些宏跟踪调试

A N S I标准说明了五个预定义的宏名。它们是:
_ L I N E _
_ F I L E _
_ D A T E _
_ T I M E _
_ S T D C _
如果编译不是标准的,则可能仅支持以上宏名中的几个,或根本不支持。记住编译程序
也许还提供其它预定义的宏名。
_ L I N E _及_ F I L E _宏指令在有关# l i n e的部分中已讨论,这里讨论其余的宏名。
_ D AT E _宏指令含有形式为月/日/年的串,表示源文件被翻译到代码时的日期。
源代码翻译到目标代码的时间作为串包含在_ T I M E _中。串形式为时:分:秒。
如果实现是标准的,则宏_ S T D C _含有十进制常量1。如果它含有任何其它数,则实现是
非标准的。

可以定义宏,例如:
当定义了_DEBUG,输出数据信息和所在文件所在行
#ifdef _DEBUG
      #define DEBUGMSG(msg,date) printf(msg);printf(“%d%d%d”,date,_LINE_,_FILE_)
#else
      #define DEBUGMSG(msg,date) 
#endif

20,宏定义防止使用是错误
用小括号包含。
例如:#define ADD(a,b) (a+b)

用do{}while(0)语句包含多语句防止错误
例如:#difne DO(a,b) a+b;\
                  a++;
应用时:if(….)
          DO(a,b); //产生错误
        else
解决方法: #difne DO(a,b) do{a+b;\
                  a++;}while(0) 

宏中"#"和"##"的用法
一、一般用法
我们使用#把宏参数变为一个字符串,用##把两个宏参数贴合在一起.
用法:
#include<cstdio>
#include<climits>
using namespace std;

#define STR(s)     #s
#define CONS(a,b)  int(a##e##b)

int main()
{
    printf(STR(vck));           // 输出字符串"vck"
    printf("%d\n", CONS(2,3));  // 2e3 输出:2000
    return 0;
}

二、当宏参数是另一个宏的时候
需要注意的是凡宏定义里有用'#'或'##'的地方宏参数是不会再展开.

1, 非'#'和'##'的情况
#define TOW      (2)
#define MUL(a,b) (a*b)

printf("%d*%d=%d\n", TOW, TOW, MUL(TOW,TOW));
这行的宏会被展开为:
printf("%d*%d=%d\n", (2), (2), ((2)*(2)));
MUL里的参数TOW会被展开为(2).

2, 当有'#'或'##'的时候
#define A          (2)
#define STR(s)     #s
#define CONS(a,b)  int(a##e##b)

printf("int max: %s\n",  STR(INT_MAX));    // INT_MAX #i nclude<climits>
这行会被展开为:
printf("int max: %s\n", "INT_MAX");

printf("%s\n", CONS(A, A));               // compile error 
这一行则是:
printf("%s\n", int(AeA));

INT_MAX和A都不会再被展开, 然而解决这个问题的方法很简单. 加多一层中间转换宏.
加这层宏的用意是把所有宏的参数在这层里全部展开, 那么在转换宏里的那一个宏(_STR)就能得到正确的宏参数.

#define A           (2)
#define _STR(s)     #s
#define STR(s)      _STR(s)          // 转换宏
#define _CONS(a,b)  int(a##e##b)
#define CONS(a,b)   _CONS(a,b)       // 转换宏

printf("int max: %s\n", STR(INT_MAX));          // INT_MAX,int型的最大值,为一个变量 #i nclude<climits>
输出为: int max: 0x7fffffff
STR(INT_MAX) -->  _STR(0x7fffffff) 然后再转换成字符串;

printf("%d\n", CONS(A, A));
输出为:200
CONS(A, A)  -->  _CONS((2), (2))  --> int((2)e(2))

三、'#'和'##'的一些应用特例
1、合并匿名变量名
#define  ___ANONYMOUS1(type, var, line)  type  var##line
#define  __ANONYMOUS0(type, line)  ___ANONYMOUS1(type, _anonymous, line)
#define  ANONYMOUS(type)  __ANONYMOUS0(type, __LINE__)
例:ANONYMOUS(static int);  即: static int _anonymous70;  70表示该行行号;
第一层:ANONYMOUS(static int);  -->  __ANONYMOUS0(static int, __LINE__);
第二层:                        -->  ___ANONYMOUS1(static int, _anonymous, 70);
第三层:                        -->  static int  _anonymous70;
即每次只能解开当前层的宏,所以__LINE__在第二层才能被解开;

2、填充结构
#define  FILL(a)   {a, #a}

enum IDD{OPEN, CLOSE};
typedef struct MSG{
  IDD id;
  const char * msg;
}MSG;

MSG _msg[] = {FILL(OPEN), FILL(CLOSE)};
相当于:
MSG _msg[] = {{OPEN, "OPEN"},
              {CLOSE, "CLOSE"}};

3、记录文件名
#define  _GET_FILE_NAME(f)   #f
#define  GET_FILE_NAME(f)    _GET_FILE_NAME(f)
static char  FILE_NAME[] = GET_FILE_NAME(__FILE__);

4、得到一个数值类型所对应的字符串缓冲大小
#define  _TYPE_BUF_SIZE(type)  sizeof #type
#define  TYPE_BUF_SIZE(type)   _TYPE_BUF_SIZE(type)
char  buf[TYPE_BUF_SIZE(INT_MAX)];
     -->  char  buf[_TYPE_BUF_SIZE(0x7fffffff)];
     -->  char  buf[sizeof "0x7fffffff"];
这里相当于:
char  buf[11];
 
2007-01-12 0:52
在性能优化方面永远注意80-20原则,即20%的程序消耗了80%的运行时间,因而我们要改进效率,最主要是考虑改进那20%的代码。不要优化程序中开销不大的那80%,这是劳而无功的。

第一招:以空间换时间

计算机程序中最大的矛盾是空间和时间的矛盾,那么,从这个角度出发逆向思维来考虑程序的效率问题,我们就有了解决问题的第1招--以空间换时间。比如说字符串的赋值:

方法A:通常的办法

#define LEN 32
char string1 [LEN];
memset (string1,0,LEN);
strcpy (string1,"This is a example!!");

方法B:

const char string2[LEN] ="This is a example!";
char * cp;
cp = string2 ;

使用的时候可以直接用指针来操作。

从 上面的例子可以看出,A和B的效率是不能比的。在同样的存储空间下,B直接使用指针就可以操作了,而A需要调用两个字符函数才能完成。B的缺点在于灵活性 没有A好。在需要频繁更改一个字符串内容的时候,A具有更好的灵活性;如果采用方法B,则需要预存许多字符串,虽然占用了大量的内存,但是获得了程序执行 的高效率。

如果系统的实时性要求很高,内存还有一些,那我推荐你使用该招数。

第二招: 使用宏而不是函数。

    这也是第一招的变招。函数和宏的区别就在于,宏占用了大量的空间,而函数占用了时间。大家要知道的是,函数调用是要使用系统的栈来保存数据的,如果编译器 里有栈检查选 项,一般在函数的头会嵌入一些汇编语句对当前栈进行检查;同时,CPU也要在函数调用时保存和恢复当前的现场,进行压栈和弹栈操作,所以,函数调用需要一 些CPU时间。 而宏不存在这个问题。宏仅仅作为预先写好的代码嵌入到当前程序,不会产生函数调用,所以仅仅是占用了空间,在频繁调用同一个宏的时候,该现象尤其突出。

举例如下:

方法C:

#define bwMCDR2_ADDRESS 4
#define bsMCDR2_ADDRESS 17
int BIT_MASK(int __bf)
{
return ((1U << (bw ## __bf)) - 1)<< (bs ## __bf);
}
void SET_BITS(int __dst,
int __bf, int __val)
{
__dst = ((__dst) & ~(BIT_MASK(__bf))) |
\
(((__val) << (bs ## __bf))
& (BIT_MASK(__bf))))
}
SET_BITS(MCDR2, MCDR2_ADDRESS,ReGISterNumber);

方法D:

#define bwMCDR2_ADDRESS 4
#define bsMCDR2_ADDRESS 17
#define bmMCDR2_ADDRESS BIT_MASK(MCDR2_ADDRESS)
#define BIT_MASK(__bf)
(((1U << (bw ## __bf)) - 1)
<< (bs ## __bf))
#define SET_BITS(__dst, __bf, __val)
\
((__dst) = ((__dst) & ~(BIT_MASK(__bf)))
| \
(((__val) << (bs ## __bf))
& (BIT_MASK(__bf))))
SET_BITS(MCDR2, MCDR2_ADDRESS,
RegisterNumber);

D方法是我看到的最好的置位操作函数,是ARM公司源码的一部分,在短短的三行内实现了很多功能,几乎涵盖了所有的位操作功能。C方法是其变体,其中滋味还需大家仔细体会。

第三招:数学方法解决问题

现在我们演绎高效C语言编写的第二招--采用数学方法来解决问题。数学是计算机之母,没有数学的依据和基础,就没有计算机的发展,所以在编写程序的时候,采用一些数学方法会对程序的执行效率有数量级的提高。举例如下,求 1~100的和。

方法E:

int I , j;
for (I = 1 ;I<=100; I ++)
{
j += I;
}

方法F

int I;
I = (100 * (1+100)) / 2

这 个例子是我印象最深的一个数学用例,是我的计算机启蒙老师考我的。当时我只有小学三年级,可惜我当时不知道用公式 N×(N+1)/ 2 来解决这个问题。方法E循环了100次才解决问题,也就是说最少用了100个赋值,100个判断,200个加法(I和j);而方法F仅仅用了1个加法,1 次乘法,1次除法。效果自然不言而喻。所以,现在我在编程序的时候,更多的是动脑筋找规律,最大限度地发挥数学的威力来提高程序运行的效率。

第四招:使用位操作

    使用位操作。减少除法和取模的运算。在计算机程序中数据的位是可以操作的最小数据单位,理论上可以用"位运算"来完成所有的运算和操作。一般的位操作是用来控制硬件的,或者做数据变换使用,但是,灵活的位操作可以有效地提高程序运行的效率。举例如下:

方法G

int I,J;
I = 257 /8;
J = 456 % 32;

方法H

int I,J;
I = 257 >>3;
J = 456 - (456 >> 4 << 4);

在 字面上好像H比G麻烦了好多,但是,仔细查看产生的汇编代码就会明白,方法G调用了基本的取模函数和除法函数,既有函数调用,还有很多汇编代码和寄存器参 与运算;而方法H则仅仅是几句相关的汇编,代码更简洁,效率更高。当然,由于编译器的不同,可能效率的差距不大,但是,以我目前遇到的MS C ,ARM C 来看,效率的差距还是不小。

        对于以2的指数次方为"*"、"/"或"%"因子的数学运算,转化为移位运算"<< >>"通常可以提高算法效率。因为乘除运算指令周期通常比移位运算大。

C语言位运算除了可以提高运算效率外,在嵌入式系统的编程中,它的另一个最典型的应用,而且十分广泛地正在被使用着的是位间的与(&)、或 (|)、非(~)操作,这跟嵌入式系统的编程特点有很大关系。我们通常要对硬件寄存器进行位设置,譬如,我们通过将AM186ER型80186处理器的中 断屏蔽控制寄存器的第低6位设置为0(开中断2),最通用的做法是:


#define INT_I2_MASK 0x0040
wTemp = inword(INT_MASK);
outword(INT_MASK, wTemp &~INT_I2_MASK);   

而将该位设置为1的做法是:


#define INT_I2_MASK 0x0040
wTemp = inword(INT_MASK);
outword(INT_MASK, wTemp | INT_I2_MASK);   

判断该位是否为1的做法是:


#define INT_I2_MASK 0x0040
wTemp = inword(INT_MASK);
if(wTemp & INT_I2_MASK)
{
… /* 该位为1 */
}   

运用这招需要注意的是,因为CPU的不同而产生的问题。比如说,在PC上用这招编写的程序,并在PC上调试通过,在移植到一个16位机平台上的时候,可能会产生代码隐患。所以只有在一定技术进阶的基础下才可以使用这招。


第五招:汇编嵌入

        在熟悉汇编语言的人眼里,C语言编写的程序都是垃圾"。这种说法虽然偏激了一些,但是却有它的道理。汇编语言是效率最高的计算机语言,但是,不可能靠着它 来写一个操作系统吧?所以,为了获得程序的高效率,我们只好采用变通的方法--嵌入汇编,混合编程。嵌入式C程序中主要使用在线汇编,即在C程序中直接插 入_asm{ }内嵌汇编语句。
举例如下,将数组一赋值给数组二,要求每一字节都相符。
char string1[1024],string2[1024];

方法I

int I;
for (I =0 ;I<1024;I++)
*(string2 + I) = *(string1 + I)

方法J

#ifdef _PC_
int I;
for (I =0 ;I<1024;I++)
*(string2 + I) = *(string1 + I);
#else
#ifdef _ARM_
__asm
{
MOV R0,string1
MOV R1,string2
MOV R2,#0
loop:
LDMIA R0!, [R3-R11]
STMIA R1!, [R3-R11]
ADD R2,R2,#8
CMP R2, #400
BNE loop
}
#endif

再举个例子:

/* 把两个输入参数的值相加,结果存放到另外一个全局变量中 */
int result;
void Add(long a, long *b)
{
_asm
{
MOV AX, a
MOV BX, b
ADD AX, [BX]
MOV result, AX
}
}

方 法I是最常见的方法,使用了1024次循环;方法J则根据平台不同做了区分,在ARM平台下,用嵌入汇编仅用128次循环就完成了同样的操作。这里有朋友 会说,为什么不用标准的内存拷贝函数呢?这是因为在源数据里可能含有数据为0的字节,这样的话,标准库函数会提前结束而不会完成我们要求的操作。这个例程 典型应用于LCD数据的拷贝过程。根据不同的CPU,熟练使用相应的嵌入汇编,可以大大提高程序执行的效率。

虽然是必杀技,但是如果轻易使用会付出惨重的代价。这是因为,使用了嵌入汇编,便限制了程序的可移植性,使程序在不同平台移植的过程中,卧虎藏龙,险象环生!同时该招数也与现代软件工程的思想相违背,只有在迫不得已的情况下才可以采用。

第六招, 使用寄存器变量
    当对一个变量频繁被读写时,需要反复访问内存,从而花费大量的存取时间。为此,C语言提供了一种变量,即寄存器变量。这种变量存放在CPU的寄存器中,使 用时,不需要访问内存,而直接从寄存器中读写,从而提高效率。寄存器变量的说明符是register。对于循环次数较多的循环控制变量及循环体内反复使用 的变量均可定义为寄存器变量,而循环计数是应用寄存器变量的最好候选者。

(1) 只有局部自动变量和形参才可以定义为寄存器变量。因为寄存器变量属于动态存储方式,凡需要采用静态存储方式的量都不能定义为寄存器变量,包括:模块间全局变量、模块内全局变量、局部static变量;

(2) register是一个"建议"型关键字,意指程序建议该变量放在寄存器中,但最终该变量可能因为条件不满足并未成为寄存器变量,而是被放在了存储器中,但编译器中并不报错(在C++语言中有另一个"建议"型关键字:inline)。

下面是一个采用寄存器变量的例子:

/* 求1+2+3+….+n的值 */
WORD Addition(BYTE n)
{
register i,s=0;
for(i=1;i<=n;i++)
{
s=s+i;
}
return s;
}   

本程序循环n次,i和s都被频繁使用,因此可定义为寄存器变量。

第七招: 利用硬件特性

首先要明白CPU对各种存储器的访问速度,基本上是:

CPU内部RAM > 外部同步RAM > 外部异步RAM > FLASH/ROM

对于程序代码,已经被烧录在FLASH或ROM中,我们可以让CPU直接从其中读取代码执行,但通常这不是一个好办法,我们最好在系统启动后将FLASH或ROM中的目标代码拷贝入RAM中后再执行以提高取指令速度;

对于UART等设备,其内部有一定容量的接收BUFFER,我们应尽量在BUFFER被占满后再向CPU提出中断。例如计算机终端在向目标机通过RS-232传递数据时,不宜设置UART只接收到一个BYTE就向CPU提中断,从而无谓浪费中断处理时间;

如果对某设备能采取DMA方式读取,就采用DMA读取,DMA读取方式在读取目标中包含的存储信息较大时效率较高,其数据传输的基本单位是块,而所传输 的数据是从设备直接送入内存的(或者相反)。DMA方式较之中断驱动方式,减少了CPU 对外设的干预,进一步提高了CPU与外设的并行操作程度。

参考文献:
  http://purec.binghua.com/viewthread.php?tid=1318&extra=page%3D1
  http://www.51c51.net/article/show.asp?id=560
 
2007-01-11 17:47
C语言变量类型

auto
static
external
static external
register

auto 局部变量

     auto 变量是用堆栈(stack)方式占用储存器空间,因此,当执行此区段是,系统会立即为这个变量分配存储器空间,而程序执行完后,这个堆栈立即被系统收回.在大括号{}内声明.
     自动变量就是指在函数内部定义使用的变量。他只是允许在定义他的函数内部使用它。在函数外的其他任何地方都不能使用的变量。自动变量是 局部变量,即它的区域性是在定义他的函数内部有效。当然这说明自动变量也没有链接性,因为它也不允许其他的文件访问他。由于自动变量在定义他的函数的外面 的任何地方都是不可见的,所以允许我们在这个函数外的其他地方或者是其他的函数内部定义同名的变量,他们之间不会发生冲突的。因为他们都有自己的区域性, 而且它没有链接性(即:不允许其他的文件访问他的)。来看看自动量的持续性。计算机在执行这个函数的时候,创建并为它分配内存,当函数执行完毕返回后,自 动变量就会被销毁。这个过程是通过一个堆栈的机制来实现的。为自动变量分配内存就压栈,而函数返回时就退栈。

static 静态变量

    static 变量是C程序编译器以固定地址存放的变量,只要程序不结束,内存不被释放.
    静态变量与自动变量的本质区别是,静态变量并不像自动变量那样使用堆栈机制来使用内存。而是为静态变量分配固定的内存,在程序运行的整个过程中,它都会被 保持,而不会不销毁。这就是说静态变量的持续性是程序运行的整个周期。这有利于我们共享一些数据。如果静态变量在函数内部定义,则它的作用域就是在这个函 数内部,仅在这个函数内部使用它才有效,但是它不同于自动变量的,自动变量离开函数后就会别销毁,而静态变量不会被销毁。他在函数的整个运行周期内都会存 在。在函数外面定义的变量为全局变量,工程内的所有文件都可以访问他,但是它在整个工程内只能定义一次,不能有重复的定义,不然就会发生错误,而其他的文 件要想使用这个变量,必须用extern来声明这个变量,这个声明叫做引用声明。这一点很重要,如过你没有用extern 来声明在其他文件中已经定义的全局变量,就来使用它,就会发生错误如果你只是想在定义他的文件中使用它,而不允许在其他的文件中使用它,那么就用关键字 static来在函数外面声明变量。这样这个变量在其他文件中将不可见,即它的连接性而内部链接。有一点是我们只得注意的像:如果你在函数外这样声明一个 变量,const int a ; 变量a的连接性为内部链接,只能在定义他的文件内使用。还有如果你在定义静态变量的时候并没有给变量初始化,则静态变量将被自s动初始化为0;

external 变量

外部变量 定义在程序外部,所有的函数很程序段都可以使用.

外部变量可能会在某一程序段被重新定义,以段内变量为参考值.

static external 变量

静态外部变量和外部变量差别在于,外部变量生命可以同时给多个文件使用,而静态外部变量则只能给声明此变量的文件使用.

register 变量


寄存器变量,是由寄存器分配空间,访问速度比访问内存快,加快执行速度.寄存器大小有限.

在c语言当中可以使用寄存器变量来优化程序的性能,最常见的是在一个函数体当中,将一个常用的变量声明为寄存器变量: register int ra; 如果可能的话,编译器就会为它分配一个单独的寄存器,在整个函数执行期间对这个变量的操作全都是对这个寄存器进行操作,这时候就不用频繁地去访存了,自然就提高了性能。但是寄存器变量不是强制性的,也就是说,即使你使用register关键字去声明一个变量为寄存器变量,编译器还是有可能把它作为一个普通的变量而不是寄存器变量来使用的。

需要注意的是,目前C编译器还不允许全局寄存器变量,也就是说寄存器变量只能是局部变量或者函数形参变量,而且最好是int,char或者指针类型变量。在声明寄存器变量的时候,可以制定使用哪个寄存器,在X86平台上常用的有”ebp, ebx, esi, edi“。可以这样声明: register int local_var __asm__("ebp"); 这时候需要在编译程序的时候给出特别的选项,因为有些寄存器原本是另有用途的,比如ebp寄存器,原来是做frame-pointer用途的,在调试程序的时候可以用它来跟踪程序的调用关系。这时候我们使用 -fomit-frame-pointer 选项来指明这一点。
 
2007-01-08 9:55

This article continues our discussion on debugging software crashes. Here we focus on memory corruption crash symptoms. We will also look at the special considerations in debugging C++ code crashes. Finally we will look at techniques to simplify crash debugging. 

Debugging Memory Corruption

Programs store data in any of the following ways:

Global All variables of objects declared as global in a C/C++ program fall into this category. This also includes static variable declarations.
Heap Memory allocated using new or malloc is allocated on the heap. In many systems, stack and heap are allocated from opposite sides of a memory block. (See the figure below)
Stack All local variables and function parameters are passed on the stack. Stack is also used for storing the return address of the calling functions. Stack also keeps the register contents and return address when an interrupt service routine is called.

 

Stack and Heap Memory Allocation

Memory corruption in the global area, stack or the heap can have confusing symptoms. These symptoms are explored here.

Global Memory Corruption

If a global data location is found to be corrupted, there is good chance that this is caused by array index overflow from the previous global data declarations. Also the corruption might have been caused by an array index underflow (array accessed with a negative index) from the next variable declarations. The following rules should be helpful in debugging this condition:

  • If you have a debugging system which allows you to put breakpoints on data write to a certain location, use that feature to find the offending program corrupting the memory. If you don't have the luxury of such a tool, the following steps might help.
  • If the variable is a part of structure, check if overflow/underflow of previous or next variables in the structure could have caused this corruption.
  • If other structure member access seems harmless, use the linker generated symbol map to locate other global variables declared in the vicinity of the corrupted structure. Examine the data structures to determine if they could have caused the corruption.
  • Sometimes looking at the corrupted memory locations can also give a good idea of the cause of corruption. You might be able to recognize a string or data pattern identifying the culprit. This might be your only hope if the corruption is caused by an un-initialized pointer.
  • Extent of corruption might also give a clue of the cause of corruption. Try to determine the starting and ending points of a corruption (only possible if the corrupting program is writing in an identifiable pattern).

Heap Memory Corruption

Corruption on the heap can be very hard to detect. A heap corruption could lead to a crash in heap management primitives that are invoked by memory management functions like malloc and free. It might be very hard to detect the original source of corruption as the buffer that lead to corruption of adjacent buffers might have long been freed. Guidelines for debugging crashes in heap area are:

  • If a crash is observed in memory management primitives of the operating system, heap corruption is a possibility. It has been observed that memory buffer corruption sometimes leads to corruption of OS buffer linked list, causing crashes on OS code.
  • If a memory corruption is observed in an allocated buffer, check the buffers in the vicinity of this buffer to look for source of corruption.
  • Corruption of buffers close to heap boundary might be due to stack overflow or stack overwrite leading to heap corruption (see the above figure)
  • Conversely, stack corruption might take place if a write into the heap overflows and corrupts the stack area.

Stack Memory Corruption

Stack corruption by far produces the most varied symptoms. Modern programming languages use the stack for a large number of operations like maintaining local variables, function parameter passing, function return address management. See the article on c to assembly translation for details.

 Here are the rules for debugging stack corruption:

  • If a crash is observed when a function returns, this might be due to stack corruption. The return address on the stack might have been corrupted by stack operations of called functions.
  • Crash after an interrupt service routine returns might also be caused by stack corruption.
  • Stack corruption can also be suspected when a passed parameter seems to have a value different from the one passed by the calling function. 
  • When a stack corruption is detected, one should look at the local variables in the called and calling functions to look for possible sources of memory corruption. Check array and pointer declarations for sources of errors.
  • Sometimes stray corruption of a processors registers might also be due to a stack corruption. If a register gets corrupted due to no reason, one possibility is that an offending thread or program corrupted the register context on the stack. When the register is restored as a part of a context switch, the task crashes.
  • Corruption in heap can trickle down to the stack.
  • Stack overflow takes place when a programs function nesting exceeds the stack allocated to the program. This can cause a stack area or heap area corruption. (Depends upon who attempts to access the corrupted memory first, a heap operation or stack operation).

Crash Debugging in C++

We have been discussing crash debugging techniques that apply equally well to C as well as C++. This section covers crash debugging techniques that are specific to C++.

Invalid Object Pointer

Many C++ developers get confused by crashes that involve method invocation on a corrupted pointer. Developers need to realize that invoking a method for an illegal object pointer is equivalent to passing an illegal pointer to a function. A crash would result when any member variable is accessed in the called method. 

In the example given below, when HandleMsg() is invoked for a NULL pX, the crash will result only when an access is attempted to member variables of X. There will be no problem in calling PrepareForMessage() or HandleYMsg() for Y pointer. (For more details on this refer to C and C++   article.

Corrupted Object Pointer Access
class X
{
int m_x;
public:

void HandleMsg(Y *pY, Msg *pMsg)
{
pY->PrepareForMessage();
pY->HandleYMsg(pMsg);
m_x = pMsg->GetX(); // Crash takes place here
}
};
main()
{
X *pX = NULL;
Y y;
. . .
// pX is still NULL
pX->HandleMsg(&y, pMsg);
}

V-Table Pointer Corruption

Inheriting Classes
class A
{
int m_a;
int m_array[MAX_ARRAY];
public:
void SetA(int a);
int GetA() const;
virtual void SendCommand() = 0;
};

class B : public A
{
int m_b;
public:
void SetB(int b);
int GetB() const;
void SendCommand(); // Override method
};

All classes with virtual functions have a pointer to the V-table corresponding to overrides for that class. The V-table pointer is generally stored just after the elements of the base class. Corruption of the v-table pointer can baffle developers as the real problem often gets hidden by the symptoms of the crash.

The figure above shows the declaration of class A and B. The figure below shows the memory layout for an object of class B. If m_array array is indexed with an index exceeding its size, the first variable to be corrupted will be the v-table pointer. This problem will manifest as a crash on invoking method SendCommand. The reason this happens is that SendCommand is a virtual function, so the real access will be using a virtual table. If the virtual table pointer is corrupted, calling this function will take you to never-never land.

int m_a
int m_array [MAX_ARRAY]
VTable *vptr
int m_b

For more details on v-table organization refer to C and C++ Comparison II article.

Dynamic Memory Allocation

Many C++ programs involve a lot of dynamic memory allocation by new. Many C++ crashes can be attributed to not checking for memory allocation failure. In C++ this can be achieved in two ways:

  • Handle out of memory exception
  • Check for new returning a NULL pointer.

Simplifying Crash Debugging

Here are a few simple techniques for simplifying crash debugging:

Obtaining Stack Dump

Make sure that every embedded processor in the system supports dumping of the stack at the time of crash. The crash dump should be saved in non volatile memory so that it can be retrieved by tools on processor reboot. In fact attempt should be made to save as much as possible of processor state and key data structures at the time of crash.

Using assert

An ounce of prevention is better than a pound of cure. Detecting crash causing conditions by using assert macro can be a very useful tool in detecting problems much before they lead to a crash. Basically assert macros check for a condition which the function assumes to be true. For example, the code below shows an assertion which checks that the message to be processed is non NULL. During initial debugging of the system this assert condition might help you detect the condition, before it leads to a crash. 

Note that asserts do not have any overhead in the shipped system as in the release builds asserts are defined to a NULL macro, effectively removing all the assert conditions.

assert usage
void HandleOrigination(const OriginationMsg *pMsg)
{
assert(pMsg);
assert(pMsg->numberOfDigits != 0);
. . .
}

Defensive Checks and Tracing

Similar to asserts, use of defensive checks can many times save the system from a crash even when an invalid condition is detected. The main difference here is that unlike asserts, defensive checks remain in the shipped system.

Tracing and maintaining event history can also be very useful in debugging crashes in the early phase of development. However tracing of limited use in debugging systems when the system has been shipped.
 
2007-01-08 9:53

Debugging software crashes is one of the most difficult parts of real-time and embedded software development. Software crashes when an application performs an illegal operation and the operating system is forced to abort the execution of the application. Here we will discuss several causes of crash in typical embedded application. A good understanding of C to assembly would be helpful in understanding the content described here.

The following software problems lead to crashes:

Invalid Array Indexing

Invalid array indexing is one of the biggest source of crashes in C and C++ programs. Both the languages do not support array bound checking, thus invalid array indexing usually goes undetected during testing. Out of bound array indexing will corrupt data structures that allocated memory after the array. Another point often missed in analyzing  array indexing problems is the fact that invalid array indexing can corrupt data structures declared before the array. This happens when the array is indexed with a very large unsigned number that represents a negative number in signed arithmetic. Consider an array b which is accidentally indexed with the number 0xFFFFFFFF, Since array index is considered to be a signed integer, this access will be treated as an access to -1 index. Thus this access will corrupt variables declared before the array, i.e. memory allocated to a. If the array is indexed with an index greater that 99, it will corrupt c

Array Declaration
Data1 a;     // Corrupted when b is indexed with 0xFFFFFFFF (-1)
int b[100]; // Declaration of b. Keep in mind that array indexing is a signed operation
Data2 c; // Corrupted when index into b is greater than 99

Un-initialized Pointer Operations

Un-initialized pointer operations are also a big reason for crashes in C and C++ programs. This problem is so acute that languages like Java and C# do not permit pointer operations. If a pointer is not initialized before access, this can result in corrupting pretty much any area of the memory. Sometimes this can result in hard to detect crashes as the pointer causing memory corruption might be located in completely unrelated area of the code. Also, un-initialized pointers can lead to unexpected behavior when the memory map of the application is modified. This happens if an un-initialized pointer operation was corrupting a unused memory block. Shifting the memory map or resizing of data structures might cause the corrupting pointer access to modify used memory. This type of problems should be suspected when a developer has just changed the size of some data structure and a stable application starts crashing.

A special case of this problem is invalid access resulting with an attempt to read or write using a NULL pointer. Here the detection of the problem is very much hardware dependent. On some platforms, accessing memory for read or write using in NULL pointer will result in an exception. On other platforms, read using a NULL pointer might go undetected but a write operation results in a crash. In yet other architectures, read and write accesses using NULL pointers might go undetected.

Another special condition is described below. If UpdateTerminalInfo is called with an un-initialized pointer, there is a possibility that the program does not crash when status is updated in the structure but it crashes in UpdateAdditionalInfo when the info variable is updated. This can happen if the beginning of the structure maps to a valid address but following elements map to illegal addresses.  

Un-initialized Pointer Crash
typedef struct 
{
int status;
. . .

int info;
}TerminalInfo;

void UpdateTerminalInfo(TerminalInfo *pTermInfo)
{
pTermInfo->status = INSERVICE;
UpdateAdditionalInfo(pTermInfo);
}

void UpdateAdditionalInfo(TerminalInfo *pTermInfo)
{
pTermInfo->info = TERMINAL_INFO;
}

Unauthorized Buffer Operations

Many times applications free an area of memory but continue to use a pointer to the memory. This can result in hard to detect crashes as the buffer might have been reallocated to some other application. This might lead to unexpected behavior in a different application. Sometimes this might also cause a crash in the memory management subsystem of the operating system as unauthorized buffer access might corrupt the heap management data structures.

A special case of unauthorized buffer operations is covered below. Here the buffer is freed up in the function and an access is attempted to the buffer after freeing it. This type of problem might go undetected and might even be harmless on some systems. However in a multithreaded design, the buffer might have already been allocated to a different thread!

Unauthorized Buffer Operation
void foo(Data1 *buf)
{
// buf is freed in this line
free(buf);

// An access is attempted to buf even after it has been freed up.
// This might cause a problem if the thread got descheduled between
// the free statement and unauthorized buffer operation. The buffer
// might have already been allocated to a different thread!
buf->x = NULL;
}

Illegal Stack Operations

Illegal stack operations can lead to hard to detect crashes. This typically takes place when a program passes a pointer of the wrong type to a function. The example given below shows a case of a function expecting an integer pointer and the caller passes a pointer to a character.

char pointer/int pointer mixup
main()
{
char count;
// The routine expects a int pointer but a char pointer has been passed
// Older compilers and non ANSI C compilers do not catch this error
GetCount(&count);
// The called function was expecting an int (say 4 byte) variable. It was
// however passed a char pointer with one byte space. GetCount will still
// write four bytes, thus corrupting local variables or parameters on the
// stack
}

bool GetCount(int *pCount)
{
. . .
*pCount = returnValue;
return true;
}

Invalid Processor Operations

Processors detect various exception conditions and abort program execution when they detect an error condition. A few of these conditions are:

  • Divide by zero attempted by application
  • Program running in user mode attempted to execute an instruction that can only be executed in supervisor (kernel) mode.
  • Program attempted access to an illegal address. The address might be out of range or the program might not have the privilege to perform the access. For example, a program attempting to write to read only segment will result in an exception.
  • Misaligned access to memory also results in an exception. Most modern processors restrict long word reads to addresses divisible by 4. An exception will be raised if a long word operation is attempted at an address that is not divisible by 4. (See the byte alignment and ordering article for details)

Infinite Loop

When a program enters an infinite loop, it might crash due to invalid array indexing when the loop index exceeds the array bounds and corrupts memory. In other scenarios, the program continues to loop until a watchdog kicks in and aborts the program. If watchdog functionality is not supported, the system will "hang" and never recover from the error. Thus all embedded systems must be designed to support watchdog reset functionality.

See the article on fault handling techniques for more details about watchdog handling. 

 
   
 
 
文章分类
 
   
 
文章存档
 
     
 
最新文章评论
  

debug hacks(中文版)里166页有一段脚本就是用来设置一些调试操作的。 脚本写在debu
 

tags.sh: line 22: cscope: command not found 这个是什么情况?
 

回复chy:我记得reverse.put.as 的作者写了个.gdbinit的脚本 很强大 你可以参考一
 

谢谢!写的不错,很受益。
 

大哥,应该加工已下呀,你这东西连主函数也没有,怎么看呀
   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu