适用GDB调试NS2程序

 非常详细的一篇文章,特转载收藏

gdb调试工具类似于vc中的debug工具。目前有命令行的和窗口界面的。窗口界面是DDD。我只用了命令行的。

用gdb调试ns2,要注意几点:
1,安装gdb,下载gdb包,安装了就是了。
2,改写Makefile,使之称为debug版本,具体做法:
在代码里面添加调试信息:修改Makefile(没有任何后缀的)将里面
CCOPT =     #如果是ns-allinone-2.28,这里是CCOPT = -O2
STATIC = 
LDFLAGS = $(STATIC)
LDOUT = -o $(BLANK)
改变为:
CCOPT = -g   #如果是ns-allinone-2.28,这里修改为 CCOPT = -O2 -g
STATIC = 
LDFLAGS = $(STATIC)
LDOUT = -o $(BLANK)
修改的东西是粉红色字体所示
然后重新编译 
注意:以下步骤一个都不能少
Make clean 
Make depend
Make

3,调试:
建议把我们的测试脚本写在ns-allinone-2.**/ns-2.XX/mytclscript/下面
当调试时,首先进入ns-2.XX文件夹   [tengda@localhost ~]$ cd ns-allinone-2.31/ns-2.31/
然后 [tengda@localhost ns-2.31]$ gdb ns
之后会出现
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".
 
(gdb) 
然后在该平台上运行我们的脚本,可以用cd 命令进入我们脚本所在文件夹。运行我们的脚本:
(gdb) run mbs.tcl

这样就可以进行调试了。具体调试过程与命令,我转载如下:

============================================

GDB(GNU 项目调试器)可以让您了解程序在执行时“内部” 究竟在干些什么,以及在程序发生崩溃的瞬间正在做什么。

GDB 做以下 4 件主要的事情来帮助您捕获程序中的 bug:

  • 在程序启动之前指定一些可以影响程序行为的变量或条件
  • 在某个指定的地方或条件下暂停程序
  • 在程序停止时检查已经发生了什么
  • 在程序执行过程中修改程序中的变量或条件,这样就可以体验修复一个 bug 的成果,并继续了解其他 bug

 

要调试的程序可以是使用 C、C++、Pascal、Objective-C 以及其他很多语言编写的。GDB 的二进制文件名是 gdb。

gdb 中有很多命令。使用 help 命令可以列出所有的命令,以及关于如何使用这些命令的介绍。下表给出了最常用的 GDB 命令。


表 1. gdb 中最常用的命令

命令 说明 例子
help 显示命令类别 help - 显示命令类别
help breakpoints - 显示属于 breakpoints 类别的命令
help break - 显示 break 命令的解释
run 启动所调试的程序 ?
kill 终止正在调试的程序的执行 通常这会在要执行的代码行已经超过了您想要调试的代码时使用。执行 kill 会重置断点,并从头再次运行这个程序
     
cont 所调试的程序运行到一个断点、异常或单步之后,继续执行 ?
info break 显示当前的断点或观察点 ?
break 在指定的行或函数处设置断点 break 93 if i=8 - 当变量 i 等于 8 时,在第 93 行停止程序执行
Step 单步执行程序,直到它到达一个不同的源代码行。您可以使用 s 来代表 step 命令 ?
Next 与 step 命令类似,只是它不会“单步跟踪到”子例程中 ?
print 打印一个变量或表达式的值 print pointer - 打印变量指针的内容
print *pointer - 打印指针所指向的数据结构的内容
delete 删除某些断点或自动显示表达式 delete 1 - 删除断点 1。断点可以通过 info break 来显示
watch 为一个表达式设置一个观察点。当表达式的值发生变化时,这个观察点就会暂停程序的执行 ?
where 打印所有堆栈帧的栈信息 where - 不使用参数,输出当前线程的堆栈信息
where all - 输出当前线程组中所有线程的堆栈信息
where threadindex - 输出指定线程的堆栈信息
attach 开始查看一个已经运行的进程 attach <process_id> - 附加到进程 process_id 上。process_id 可以使用 ps 命令找到
info thread 显示当前正在运行的线程 ?
thread apply threadno command 对一个线程运行 gdb 命令 thread apply 3 where - 对线程 3 运行 where 命令
Thread threadno 选择一个线程作为当前线程 ?

 

如果一个程序崩溃了,并生成了一个 core 文件,您可以查看 core 文件来判断进程结束时的状态。使用下面的命令启动 gdb:

 

# gdb programname corefilename 

 

要调试一个 core 文件,您需要可执行程序、源代码文件以及 core 文件。要对一个 core 文件启动 gdb,请使用 -c 选项:

 

# gdb -c core programname 

 

gdb 会显示是哪行代码导致这个程序产生了核心转储。

默认情况下,核心转储在 Novell 的 SUSE LINUX Enterprise Server 9(SLES 9)和 Red Hat? Enterprise Linux Advanced Server(RHEL AS 4)上都是禁用的。要启用核心转储,请以 root 用户的身份在命令行中执行 ulimit –c unlimited

清单 8 中的例子阐述了如何使用 gdb 来定位程序中的 bug。清单 8 是一段包含 bug 的 C++ 代码。

清单 8 中的 C++ 程序试图构建 10 个链接在一起的数字框(number box),例如:


图 1. 一个包含 10 个链接在一起的数字框的列表

然后试图从这个列表中逐个删除数字框。

编译并运行这个程序,如下所示:


清单 9. 编译并运行这个程序

                
# g++ -g -o gdbtest1 gdbtest1.cpp
# ./gdbtest1
Number Box "0" created
Number Box "1" created
Number Box "2" created
Number Box "3" created
Number Box "4" created
Number Box "5" created
Number Box "6" created
Number Box "7" created
Number Box "8" created
Number Box "9" created
list created
Number Box "9" deleted
Segmentation fault

 

正如您可以看到的一样,这个程序会导致段错误。调用 gdb 来看一下这个问题,如下所示:


清单 10. 调用 gdb

                
# gdb ./gdbtest1
GNU gdb 6.2.1
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you 
are welcome to change it and/or distribute copies of it under certain 
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for 
details.
This GDB was configured as "ppc-suse-linux"...Using host libthread_db 
library "/lib/tls/libthread_db.so.1".
(gdb)

 

您知道段错误是在数字框 "9" 被删除之后发生的。执行 run 和 where 命令来精确定位段错误发生在程序中的什么位置。


清单 11. 执行 run 和 where 命令

                
(gdb) run
Starting program: /root/test/gdbtest1 
Number Box "0" created
Number Box "1" created
Number Box "2" created
Number Box "3" created
Number Box "4" created
Number Box "5" created
Number Box "6" created
Number Box "7" created
Number Box "8" created
Number Box "9" created
list created
Number Box "9" deleted
Program received signal SIGSEGV, Segmentation fault.
0x10000f74 in NumBox<int>::GetNext (this=0x0) at gdbtest1.cpp:14
14              NumBox<T>*GetNext() const { return Next; }
(gdb) where
#0  0x10000f74 in NumBox<int>::GetNext (this=0x0) at gdbtest1.cpp:14
#1  0x10000d10 in NumChain<int>::RemoveBox (this=0x10012008, 

item_to_remove=@0xffffe200) at gdbtest1.cpp:63 #2 0x10000978 in main (argc=1, argv=0xffffe554) at gdbtest1.cpp:94 (gdb)

 

跟踪信息显示这个程序在第 14 行 NumBox<int>::GetNext (this=0x0) 接收到一个段错误。这个数字框上 Next 指针的地址是 0x0,这对于一个数字框来说是一个无效的地址。从上面的跟踪信息可以看出,GetNext 函数是由 63 行调用的。看一下在 gdbtest1.cpp 的 63 行附近发生了什么:


清单 12. gdbtest1.cpp

                
     54                       } else {
     55                               temp->SetNext (current->GetNext());
     56                               delete temp;
     57                               temp = 0;
     58                               return 0;
     59                       }
     60               }
     61               current = 0;
     62               temp = current;
     63               current = current->GetNext();
     64       }
     65 
     66       return -1;

 

第 61 行 current=0 将这个指针设置为一个无效的地址,这正是产生段错误的根源。注释掉第 61 行,将其保存为 gdbtest2.cpp,然后编译并重新运行。


清单 13. 再次运行程序(gdbtest2.cpp)

                
# g++ -g -o gdbtest2 gdbtest2.cpp
# ./gdbtest2
Number Box "0" created
Number Box "1" created
Number Box "2" created
Number Box "3" created
Number Box "4" created
Number Box "5" created
Number Box "6" created
Number Box "7" created
Number Box "8" created
Number Box "9" created
list created
Number Box "9" deleted
Number Box "0" deleted

 

这个程序现在可以成功完成而不会出现段错误了。然而,结果并不像我们预期的一样:程序在删除 Number Box "9"之后删除了 Number Box "0",而不像我们期望的一样删除 Number Box "8,"。使用 gdb 再次来看一下。


清单 14. 再次使用 gdb 进行查看

                
# gdb ./gdbtest2
GNU gdb 6.2.1
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you 
are welcome to change it and/or distribute copies of it under certain 
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for 
details.
This GDB was configured as "ppc-suse-linux"...Using host libthread_db 
library "/lib/tls/libthread_db.so.1".
(gdb) break 94 if i==8
Breakpoint 1 at 0x10000968: file gdbtest2.cpp, line 94.
(gdb) run
Starting program: /root/test/gdbtest2 
Number Box "0" created
Number Box "1" created
Number Box "2" created
Number Box "3" created
Number Box "4" created
Number Box "5" created
Number Box "6" created
Number Box "7" created
Number Box "8" created
Number Box "9" created
list created
Number Box "9" deleted
Breakpoint 1, main (argc=1, argv=0xffffe554) at gdbtest2.cpp:94
94                      list ->RemoveBox(i);

 

您可能希望找出为什么这个程序删除的是 Number Box 0,而不是 Number Box 8,因此需要在您认为程序会删除 Number Box 8 的地方停止程序。设置这个断点:break 94 if i==8,可以在 i 等于 8 时在第 94 行处停止程序。然后单步跟踪到 RemoveBox() 函数中。


清单 15. 单步跟踪到 RemoveBox() 函数中

                
(gdb) s
38                      NumBox<T> *temp = 0;  
(gdb) s
40                      while (current != 0) {
(gdb) print pointer
$1 = (NumBox<int> *) 0x100120a8
 (gdb) print *pointer
$2 = {Num = 0, Next = 0x0}
(gdb)

 

指针早已指向了 Number Box "0",因此这个 bug 可能就存在于程序删除 Number Box "9" 的地方。要在 gdb 中重新启动这个程序,请使用 kill 删除原来的断点,然后添加一个 i 等于 9 时的新断点,然后再次运行这个程序。


清单 16. 在 gdb 中重新启动程序

                
(gdb) kill
Kill the program being debugged? (y or n) y
(gdb) info break
Num Type           Disp Enb Address    What
1   breakpoint     keep y   0x10000968 in main at gdbtest2.cpp:94
        stop only if i == 8
        breakpoint already hit 1 time
(gdb) delete 1
(gdb) break 94 if i==9
Breakpoint 2 at 0x10000968: file gdbtest2.cpp, line 94.
(gdb) run
Starting program: /root/test/gdbtest2 
Number Box "0" created
Number Box "1" created
Number Box "2" created
Number Box "3" created
Number Box "4" created
Number Box "5" created
Number Box "6" created
Number Box "7" created
Number Box "8" created
Number Box "9" created
list created
Breakpoint 2, main (argc=1, argv=0xffffe554) at gdbtest2.cpp:94
94                      list ->RemoveBox(i);
(gdb)

 

当这一次单步跟踪 RemoveBox() 函数时,要特别注意 list->pointer 正在指向哪一个数字框,因为 bug 可能就在于 list->pointer 开始指向 Number Box "0" 的地方。请使用 display *pointer命令来查看,这会自动显示这个函数。


清单 17. 使用 display *pointer 命令进行监视

                
Breakpoint 2, main (argc=1, argv=0xffffe554) at gdbtest2.cpp:94
94            list ->RemoveBox(i);
(gdb) s
NumChain<int>::RemoveBox (this=0x10012008, item_to_remove=@0xffffe200) 
at gdbtest2.cpp:37 37 NumBox<T> *current = pointer; (gdb) display *pointer 1: *this->pointer = {Num = 9, Next = 0x10012098} (gdb) s 38 NumBox<T> *temp = 0; 1: *this->pointer = {Num = 9, Next = 0x10012098} (gdb) s 40 while (current != 0) { 1: *this->pointer = {Num = 9, Next = 0x10012098} (gdb) s 41 if (current->GetValue() == item_to_remove) { 1: *this->pointer = {Num = 9, Next = 0x10012098} (gdb) s NumBox<int>::GetValue (this=0x100120a8) at gdbtest2.cpp:16 16 const T& GetValue () const { return Num; } (gdb) s NumChain<int>::RemoveBox (this=0x10012008, item_to_remove=@0xffffe200)
at gdbtest2.cpp:42 42 if (temp == 0) { 1: *this->pointer = {Num = 9, Next = 0x10012098} (gdb) s 44 if (current->GetNext() == 0) { 1: *this->pointer = {Num = 9, Next = 0x10012098} (gdb) s NumBox<int>::GetNext (this=0x100120a8) at gdbtest2.cpp:14 14 NumBox<T>*GetNext() const { return Next; } (gdb) s NumChain<int>::RemoveBox (this=0x10012008, item_to_remove=@0xffffe200)
at gdbtest2.cpp:50 50 delete current; 1: *this->pointer = {Num = 9, Next = 0x10012098} (gdb) s ~NumBox (this=0x100120a8) at gdbtest2.cpp:10 10 std::cout << "Number Box " <<"\"" << GetValue()
<<"\"" <<" deleted" << std::endl; (gdb) s NumBox<int>::GetValue (this=0x100120a8) at gdbtest2.cpp:16 16 const T& GetValue () const { return Num; } (gdb) s Number Box "9" deleted ~NumBox (this=0x100120a8) at gdbtest2.cpp:11 11 Next = 0; (gdb) s NumChain<int>::RemoveBox (this=0x10012008, item_to_remove=@0xffffe200)
at gdbtest2.cpp:51 51 current = 0; 1: *this->pointer = {Num = 0, Next = 0x0} (gdb) s 53 return 0; 1: *this->pointer = {Num = 0, Next = 0x0} (gdb) s 0x10000d1c 66 return -1; 1: *this->pointer = {Num = 0, Next = 0x0}

 

从上面的跟踪过程中,您可以看到 list->pointer 在删除 Number Box "9" 之后指向了 Number Box "0"。这个逻辑并不正确,因为在删除 Number Box "9" 之后,list->pointer 应该指向的是 Number Box "8"。现在非常显然我们应该在第 50 行之前添加一条语句 pointer = pointer->GetNext();,如下所示:


清单 18. 在第 50 行之前添加一条 pointer = pointer->GetNext(); 语句

                
     49                     } else {
     50                             pointer = pointer->GetNext();
     51                             delete current;
     52                             current = 0;
     53                      }
     54                      return 0;

 

将新修改之后的程序保存为 gdbtest3.cpp,然后编译并再次运行。


清单 19. 再次运行程序(gdbtest3.cpp)

                
# g++ -g -o gdbtest3 gdbtest3.cpp
# ./gdbtest3
Number Box "0" created
Number Box "1" created
Number Box "2" created
Number Box "3" created
Number Box "4" created
Number Box "5" created
Number Box "6" created
Number Box "7" created
Number Box "8" created
Number Box "9" created
list created
Number Box "9" deleted
Number Box "8" deleted
Number Box "7" deleted
Number Box "6" deleted
Number Box "5" deleted
Number Box "4" deleted
Number Box "3" deleted
Number Box "2" deleted
Number Box "1" deleted
Number Box "0" deleted

 

这才是我们期望的结果。

多线程环境

在 GDB 中有一些特殊的命令可以用于多线程应用程序的调试。下面这个例子给出了一个死锁情况,并介绍了如何使用这些命令来检查多线程应用程序中的问题:


清单 20. 多线程的例子

                
#include <stdio.h>
#include "pthread.h>
pthread_mutex_t AccountA_mutex;
pthread_mutex_t AccountB_mutex;
struct BankAccount {
     char account_name[1];
     int balance;
};
struct BankAccount  accountA = {"A", 10000 };
struct BankAccount  accountB = {"B", 20000 };
void * transferAB (void* amount_ptr) {
     int amount = *((int*)amount_ptr);
     pthread_mutex_lock(&AccountA_mutex);
     if (accountA.balance < amount)   {
             printf("There is not enough memory in Account A!\n");
             pthread_mutex_unlock(&AccountA_mutex);
             pthread_exit((void *)1);
     }
     accountA.balance -=amount;
     sleep(1);
     pthread_mutex_lock(&AccountB_mutex);
     accountB.balance +=amount;
     pthread_mutex_unlock(&AccountA_mutex); 
     pthread_mutex_unlock(&AccountB_mutex);
}
void * transferBA (void* amount_ptr) {
     int amount = *((int*)amount_ptr);
     pthread_mutex_lock(&AccountB_mutex);
     if (accountB.balance < amount)   {
             printf("There is not enough memory in Account B!\n");
             pthread_mutex_unlock(&AccountB_mutex);
             pthread_exit((void *)1);
     }
     accountB.balance -=amount;
     sleep(1);
     pthread_mutex_lock(&AccountA_mutex);
     accountA.balance +=amount;
     pthread_mutex_unlock(&AccountB_mutex);
     pthread_mutex_unlock(&AccountA_mutex);
}
int main(int argc, char* argv[]) {
     int             threadid[4];
     pthread_t       pthread[4];
     int             transfer_amount[4] = {100, 200, 300, 400};
     int             final_balanceA, final_balanceB;
     final_balanceA=accountA.balance-transfer_amount[0]-
transfer_amount[1]+transfer_amount[2]+transfer_amount[3]; final_balanceB=accountB.balance+transfer_amount[0]
+transfer_amount[1]-transfer_amount[2]-transfer_amount[3]; if (threadid[0] = pthread_create(&pthread[0], NULL, transferAB,
(void*)&transfer_amount[0]) " 0) { perror("Thread #0 creation failed."); exit (1); } if (threadid[1] = pthread_create(&pthread[1], NULL, transferAB,
(void*)&transfer_amount[1]) " 0) { perror("Thread #1 creation failed."); exit (1); } if (threadid[2] = pthread_create(&pthread[2], NULL, transferBA,
(void*)&transfer_amount[2]) < 0) { perror("Thread #2 creation failed."); exit (1); } if (threadid[3] = pthread_create(&pthread[3], NULL, transferBA,
(void*)&transfer_amount[3]) < 0) { perror("Thread #3 creation failed."); exit (1); } printf("Transitions are in progress.."); while ((accountA.balance != final_balanceA) && (accountB.balance
!= final_balanceB)) { printf(".."); } printf("\nAll the money is transferred !!\n"); }

 

使用 gcc 来编译这个程序,如下所示:

# gcc -g -o gdbtest2 gdbtest2.c -L/lib/tls -lpthread

 

程序 gdbtest2 会挂起,不会返回一条 All the money is transferred !! 消息。

将 gdb 附加到正在运行的进程上,从而了解这个进程内部正在发生什么。


清单 21. 将 gdb 附加到正在运行的进程上

                
# ps -ef |grep gdbtest2
root      9510  8065  1 06:30 pts/1    00:00:00 ./gdbtest2
root      9516  9400  0 06:30 pts/4    00:00:00 grep gdbtest2
# gdb -pid 9510
GNU gdb 6.2.1
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you 
are welcome to change it and/or distribute copies of it under certain 
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for 
details.
This GDB was configured as "ppc-suse-linux".
Attaching to process 9510
Reading symbols from /root/test/gdbtest2...done.
Using host libthread_db library "/lib/tls/libthread_db.so.1".
Reading symbols from /lib/tls/libpthread.so.0...done.
[Thread debugging using libthread_db enabled]
[New Thread 1073991712 (LWP 9510)]
[New Thread 1090771744 (LWP 9514)]
[New Thread 1086577440 (LWP 9513)]
[New Thread 1082383136 (LWP 9512)]
[New Thread 1078188832 (LWP 9511)]
Loaded symbols for /lib/tls/libpthread.so.0
Reading symbols from /lib/tls/libc.so.6...done.
Loaded symbols for /lib/tls/libc.so.6
Reading symbols from /lib/ld.so.1...done.
Loaded symbols for /lib/ld.so.1
0x0ff4ac40 in __write_nocancel () from /lib/tls/libc.so.6
(gdb) info thread
  5 Thread 1078188832 (LWP 9511)  0x0ffe94ec in __lll_lock_wait () 
from /lib/tls/libpthread.so.0 4 Thread 1082383136 (LWP 9512) 0x0ffe94ec in __lll_lock_wait ()
from /lib/tls/libpthread.so.0 3 Thread 1086577440 (LWP 9513) 0x0ffe94ec in __lll_lock_wait ()
from /lib/tls/libpthread.so.0 2 Thread 1090771744 (LWP 9514) 0x0ffe94ec in __lll_lock_wait ()
from /lib/tls/libpthread.so.0 1 Thread 1073991712 (LWP 9510) 0x0ff4ac40 in __write_nocancel ()
from /lib/tls/libc.so.6 (gdb)

 

从 info thread 命令中,我们可以了解到除了主线程(thread #1)之外的所有线程都在等待函数 __lll_lock_wait () 完成。

使用 thread apply threadno where 命令来查看每个线程到底运行到了什么地方:


清单 22. 查看每个线程运行到了什么地方

                
(gdb) thread apply 1 where
Thread 1 (Thread 1073991712 (LWP 9510)):
#0  0x0ff4ac40 in __write_nocancel () from /lib/tls/libc.so.6
#1  0x0ff4ac28 in __write_nocancel () from /lib/tls/libc.so.6
Previous frame identical to this frame (corrupt stack?)
#0  0x0ff4ac40 in __write_nocancel () from /lib/tls/libc.so.6
(gdb) thread apply 2 where
Thread 2 (Thread 1090771744 (LWP 9514)):
#0  0x0ffe94ec in __lll_lock_wait () from /lib/tls/libpthread.so.0
#1  0x0ffe466c in pthread_mutex_lock () from /lib/tls/libpthread.so.0
#2  0x0ffe466c in pthread_mutex_lock () from /lib/tls/libpthread.so.0
#3  0x0ffe466c in pthread_mutex_lock () from /lib/tls/libpthread.so.0
#4  0x0ffe466c in pthread_mutex_lock () from /lib/tls/libpthread.so.0
Previous frame inner to this frame (corrupt stack?)
#0  0x0ff4ac40 in __write_nocancel () from /lib/tls/libc.so.6
(gdb) thread apply 3 where
Thread 3 (Thread 1086577440 (LWP 9513)):
#0  0x0ffe94ec in __lll_lock_wait () from /lib/tls/libpthread.so.0
#1  0x0ffe466c in pthread_mutex_lock () from /lib/tls/libpthread.so.0
#2  0x0ffe466c in pthread_mutex_lock () from /lib/tls/libpthread.so.0
#3  0x0ffe466c in pthread_mutex_lock () from /lib/tls/libpthread.so.0
#4  0x0ffe466c in pthread_mutex_lock () from /lib/tls/libpthread.so.0
Previous frame inner to this frame (corrupt stack?)
#0  0x0ff4ac40 in __write_nocancel () from /lib/tls/libc.so.6
(gdb) thread apply 4 where
Thread 4 (Thread 1082383136 (LWP 9512)):
#0  0x0ffe94ec in __lll_lock_wait () from /lib/tls/libpthread.so.0
#1  0x0ffe466c in pthread_mutex_lock () from /lib/tls/libpthread.so.0
#2  0x0ffe466c in pthread_mutex_lock () from /lib/tls/libpthread.so.0
#3  0x0ffe466c in pthread_mutex_lock () from /lib/tls/libpthread.so.0
#4  0x0ffe466c in pthread_mutex_lock () from /lib/tls/libpthread.so.0
Previous frame inner to this frame (corrupt stack?)
#0  0x0ff4ac40 in __write_nocancel () from /lib/tls/libc.so.6
 (gdb) thread apply 5 where
Thread 5 (Thread 1078188832 (LWP 9511)):
#0  0x0ffe94ec in __lll_lock_wait () from /lib/tls/libpthread.so.0
#1  0x0ffe466c in pthread_mutex_lock () from /lib/tls/libpthread.so.0
#2  0x0ffe466c in pthread_mutex_lock () from /lib/tls/libpthread.so.0
#3  0x0ffe466c in pthread_mutex_lock () from /lib/tls/libpthread.so.0
#4  0x0ffe466c in pthread_mutex_lock () from /lib/tls/libpthread.so.0
Previous frame inner to this frame (corrupt stack?)
#0  0x0ff4ac40 in __write_nocancel () from /lib/tls/libc.so.6

 

每个线程都试图对一个互斥体进行加锁,但是这个互斥体却是不可用的,可能是因为有另外一个线程已经对其进行加锁了。从上面的证据我们可以判断程序中一定存在死锁。您还可以看到哪个线程现在拥有这个互斥体。


清单 23. 查看哪个线程拥有互斥体

                
(gdb) print AccountA_mutex
$1 = {__m_reserved = 2, __m_count = 0, __m_owner = 0x2527, 
__m_kind = 0, __m_lock = {__status = 1, __spinlock = 0}} (gdb) print 0x2527 $2 = 9511 (gdb) print AccountB_mutex $3 = {__m_reserved = 2, __m_count = 0, __m_owner = 0x2529,
__m_kind = 0, __m_lock = {__status = 1, __spinlock = 0}} (gdb) print 0x2529 $4 = 9513 (gdb)

 

从上面的命令中,我们可以看出 AccontA_mutex 是被线程 5(LWP 9511)加锁(拥有)的,而 AccontB_mutex 是被线程 3(LWP 9513)加锁(拥有)的。

为了解决上面的死锁情况,可以按照相同的顺序对互斥体进行加锁,如下所示: 
清单 24. 按照相同的顺序对互斥体进行加锁

                    
.
.
void * transferAB (void* amount_ptr) {
        int amount = *((int*)amount_ptr);
        pthread_mutex_lock(&AccountA_mutex);
        pthread_mutex_lock(&AccountB_mutex);
        if (accountA.balance < amount)   {
                printf("There is not enough memory in Account A!\n");
                pthread_mutex_unlock(&AccountA_mutex);
                pthread_exit((void *)1);
        }
        accountA.balance -=amount;
        sleep(1);
        accountB.balance +=amount;
        pthread_mutex_unlock(&AccountA_mutex);
        pthread_mutex_unlock(&AccountB_mutex);
}
void * transferBA (void* amount_ptr) {
        int amount = *((int*)amount_ptr);
        pthread_mutex_lock(&AccountA_mutex);
        pthread_mutex_lock(&AccountB_mutex);
        if (accountB.balance < amount)   {
                printf("There is not enough memory in Account B!\n");
                pthread_mutex_unlock(&AccountB_mutex);
                pthread_exit((void *)1);
        }
        accountB.balance -=amount;
        sleep(1);
        accountA.balance +=amount;
        pthread_mutex_unlock(&AccountA_mutex);
        pthread_mutex_unlock(&AccountB_mutex);
}
.
.

 

或者对每个帐号单独进行加锁,如下所示: 
清单 25. 对每个帐号单独进行加锁

                    
.
.
void * transferAB (void* amount_ptr) {
        int amount = *((int*)amount_ptr);
        pthread_mutex_lock(&AccountA_mutex);
        if (accountA.balance < amount)   {
                printf("There is not enough memory in Account A!\n");
                pthread_mutex_unlock(&AccountA_mutex);
                pthread_exit((void *)1);
        }
        accountA.balance -=amount;
        sleep(1);
        pthread_mutex_unlock(&AccountA_mutex);
        pthread_mutex_lock(&AccountB_mutex); 
        accountB.balance +=amount;
        pthread_mutex_unlock(&AccountB_mutex);
}
void * transferBA (void* amount_ptr) {
        int amount = *((int*)amount_ptr);
        pthread_mutex_lock(&AccountB_mutex);
        if (accountB.balance < amount)   {
                printf("There is not enough memory in Account B!\n");
                pthread_mutex_unlock(&AccountB_mutex);
                pthread_exit((void *)1);
        }
        accountB.balance -=amount;
        sleep(1);
        pthread_mutex_unlock(&AccountB_mutex);
        pthread_mutex_lock(&AccountA_mutex); 
        accountA.balance +=amount;
        pthread_mutex_unlock(&AccountA_mutex);
}.

 

===========================================

Tags:

zz浅谈:国内软件公司为何无法做大做强?

纵览,国内比较大的软件公司(以下统一简称"国软"),清一色都是做政府项目的(他们能做大的原因我就不用说了吧),真正能做大的国软又有几家呢?更何况 开源企业,这是为什么呢?
今天风吹就给大家简单分析下:
1."作坊"式管理
"作坊"往往是效率最高的, 国软几乎都是从作坊走过来的,但把作坊式的管理模式套用到一个不断壮大的公司中显然是不行的.组织架构到达一定程度后就必然要进行分工的细化,依靠作坊式的"暴力开发"是行不通的.
2."法制社会"
上班必须打卡,迟到要扣钱,还一次比一次多,加班没有加班费,反正算下来就是,只有扣钱的项目,没有加钱的项目.比起外企,人家上班不打卡,迟到不扣钱,加班有加班费,这样宽松点的环境不好吗?
3. 自身自灭
国软一般没有师徒制,有的话也只是形式上的,公司基本没人管你,你也不用去管别人,新进的员工,不管会不会,先丢个东西给你做,自己研究,不懂的google去.这也是为什么国软喜欢招有经验的人,因为没经验的人熬不住,跑了几个以后,国软就不招了.
4. 销售-开发-维护脱节
这点是非常严重的,会直接导致项目流产的.国软的典型的做法是,销售为了业绩,在没有调研的情况下就签了合同 (这里主要是指项目型的,产品型的一般可以控制),而且合同的范围也非常模糊,可大可小,接下来就是调研人员上场,调研后发现,10w块钱的合同,调研出来了100w的需求,接下来就是和客户扯皮,最后直接导致项目流产,甚至打上官司.项目或产品上线后,维护人员对系统不了解(一方面是没有文档,另外一方面维护人员一般没有参与到开发中),接下来往往就会发生两种情况:
a.维护人员在不了解系统的情况下擅自修改,结果导致系统越改问题 越多.
b.维护人员一不做二不休,所有客户反馈的问题全部打回给开发人员,于是开发人员就生不如死,在做别的项目的同时还要维护以前的项目,结果就是导致几个项目都失败.
5.缺乏规划
今天要用这个,明天想用那个(笔者就经历了公司在半年的时间内对框架进行了两次大的变动,导致开发人员都必须重新学习框架)产品也接,项目也接,大的也接,小的也接.今天领导说往左走,明天说往右走,也不能怪领导,他也没经验,我们就是他的DEMO.公司没有一个明确的目标,要做成什么样,只是一味的提出做大做强,但是没有规划出如何做大做强.(和我的标题一样哦)
6.三无-无需求,无设计,无测试.
a.没有文档是国软的通病,曾几何时,产品经理丢过来的那一句话:" 喂,**,给我做一个**模块来",然后开发人员就开始埋头苦写了.
b.当然如果你天资聪慧,可以轻易理解出产品经理的意思,那有没有设计都无所谓了,但是,当有一天别人要维护你的程序的时候问题就出现了,没有文档,代码又那么天马行空,怎么维护?改了这个地方,又影响了那个地方...
c.其实程序员都懂得测试的意义,可以工时安排的那么紧,哪来的时间测试?测试又没有算工时.所以几乎所有的程序员的做法就是,直接丢给用户测试
这时候有人肯定要问:那项目经理呢?他不是可以测试吗?请记住这是国软,刚才写代码的那个人就是项目经理,还是售前,还是设计人员,还是维护,还是...归结还是成本问题,在外资软件公司中,做文档的工时是比做开发的工时更多的,国软为了节省成本,这块当然要 CUT掉了.不必去追去文档有多么详细多么美观,需要做的就是找到一个平衡点,一份适合自己的文档.
7.员工都是"十项全能"
在国软里面的员工各个都是十项全能(笔者就是一个鲜明的例子,从系统调研分析设计,到进度管理,开发,测试,验收,实施,维护,甚至拉给客户拉网线都需要我去.)直接导致的结果就是这些员工每过多久就直接出来自己开公司了...嘿嘿又一家作坊诞生了...这样做对员工个人其实是有好处的,但是对于企业本上来说是没有好处的,并不是说员工成立了作坊,成为了你的竞争对手,而是让员工各个都是十项全能的结果就是
a.员工都是"十项全不 能".
b.员工一旦离职,他手头的项目必定流产.
c.对公司的发展是不利的(细化分工).
8.莫不关心
老板并不知道员工在做什么,员工也不知道老板在做什么.上级很少去关心下级的工作,更别说去关心下级的生活,一个东西丢给你,一个月后交差,中间不管你任何事情,交不了差就唯你是问.下级也不知道能为上级分担什么,只有等着上级分配任务.甚至还有些老板都不不知道员工的名字,在这样的国软的,每个人都是孤立的,又怎么能做大做强呢?
9.企业文化
所谓十年树木,百年树人.国软的企业文化表面功夫算是做的很好的了,什么"为客户创造价值","做最好的行业解决方案","软件公司的最大资源就是人才"等等,要多华丽有多华丽,重复体现了"口号文化".真正做到企业文化又有多少呢?有多少仅仅是为了做给客户看的呢?
10.盲目跟风
很多国软看到人家外企软件公司最近在搞什么推进活动,就跟风,效仿外企做,可是无法领悟精髓,纯粹只是在模仿.(外企集体笑:"一直被模仿,从未被超越")做完了也不知道这么做的意义,劳民伤财.
11.缺乏"执行力"
国软的通病,就是"执行力",国软的学习劲头很足,今天提出要完善测试标准,明天提出要每周写工作报告,可是又有哪些东西能真正的去执行呢?
导致这个问题的主要原因有两个:
a.提出来的东西到底有没必要做,还是只是应付领导走个过场.
b.谁来跟踪这些东西?员工写了工作报告,领导没有去查看,去反馈,员 工觉得写的也没意义,自然不会继续执行下去.
12.管理混乱
没有划分清楚员工的归属组织,员工并不明确他的上级领导是谁,导致有的员工处于游离状态,有的是员工又是多个领导,不懂要听谁的,有些人忙的要死,有些人又闲的要命,最后搞的最痛苦的就是员工,导致员工离职.
13.缺乏团队精神
为什么会缺乏团队精神呢?并不是国软没有这方面的概念,国软也很希望培养员工的团队观念 和精神,
无奈因为国软,一般都是一个人负责一个或者多个项目,连团队都没有,何来的团队精神?
14.无法做到补 足
一个项目一旦中途有人辞职,这个项目就会流产.一个员工一旦辞职,会有N个项目没人维护.A组的员工无法胜任B组的工作,归根结底就是组织上根本没有考虑过组织变动对项目的影响,没有提前培养人员.
15.一成不变和随心所欲
有两类人一种是把前辈的东西COPY过来,不作任何修改,因为他深信,前辈的一定是对的,还有一类是不管前人怎么做的,一律不要,全凭自己的"经验",随心所欲,天马行空的进行自主研发,造成的结果就是错的还是错的,乱的更乱了.
16.人才育成
成本,还是成本,培养一个人要多少成 本?这就是国软做不大的原因,永远只能停留在"作坊"的原因.
17.向心力.
老板做的是事业,员工做的是事情, 这是国软员工的一致观点.
18.恶性循环
a.人员力量不足 -> 接不了项目 -> 收入少 -> 人员流失.
b.人员力量不足 -> 强行接项目 -> 亏本 -> 破产.
最后 我想说一句的是:成也国软,败也国软.
说的不对的地方请大家指出,或者补充下没说到的地方.

来源:风吹的博客

Tags:

CCTV的水墨宣传片

Tags:

彪悍的有道手机字典:摄像头取词

还没公开,据说视频是有道内部会议时拍的

Tags:

老外:中国人别以为我们听不懂中文

   1、一天在超市里买东西,看见一个黑人,我就对旁边的朋友说:“看,老黑,真黑。”结果他看了看我说:“就你白!”

 

  2、两个女人和一个外国男子一起坐电梯,一个女的见到那个老外的胸毛很长就对另外一个说,你看老外的胸毛好****啊。谁知那老外突然回答说:谢谢!

 

  3、偶和偶女朋友在旱冰场玩,女朋友屡屡摔倒,我就说:“你看那边的外国美女,比你高多了,滑得多好。”结果她滑过来,“谢谢帅哥夸奖。”晕,我急忙用英语说了一句:“not at all。”

 

  4、有次俺爹爬长城,走着走着看见一高大白人坐在台阶上。俺爹跟周围人说:“看那个老外没劲爬啦。”那白人说:“我歇会不行么?”

 

  5、曾经在广州地铁,看见一家外国人,妈妈带着个女儿,那小女孩长得好漂亮啊,跟个洋娃娃似的。然后旁边有个大叔用标准English问:where are you from?小女孩很淡漠地、不耐烦地回了一句:美国。

 

  6、一个周末去北京想拉上同学作伴,她犯懒不肯跟我去,只好一个人上路。在城铁上,旁边坐一颇有“姿色”外国GG,于是打同学电话气她说:“你就不跟我来吧,叫你抱憾终身!告诉你,坐我旁边的外国GG侧面酷似布拉德皮特,养眼啊~~~”电话放下发现那GG冲我乐,心里咯噔一下,脸上发烧。外国GG还就真的冲我来了一句:小姐,你的侧面也很漂亮!……对面坐着的一对情侣当场笑倒,我恨不得跳车!

 

  7、我是济南的,一次我爸爸的同事几个去美国出差,到超市买东西,用济南话讨论,这是旁边有个美国人过来用标准的济南话说:“你们是济南的?!”同事们暴汗,聊了会,原来这老外是以前战争时期留在中国的美国人后代,同事问他:“那你英文怎么样啊?”老外一拍大腿道:“英语太****难学了!”

Tags:

泡妞就像挂QQ

标  题: 泡妞就像挂QQ

发信站: 瀚海星云 (2009年09月25日18:10:51 星期五), 站内信件 WWWPOST
 
追求一名舞蹈教练,每天发短信打电话,却一直没有进展。于是,向朋友问计:
友人甲:泡妞就像做销售,需要想法设法持续影响你的目标客户,取得其信任,时间长
了,你就可以成功了;
友人乙:追MM就像挂QQ一样,每天至少两小时,达到一定的天数后就可以太阳了……
嗯嗯,加油!
 
感觉这帖子比较有意思,转载之

Tags:

熊猫NONO的超级变变变

Tags:

来看看国外的跑酷牛人

 如果不知道什么是跑酷,请自己去搜索一下,同时推荐暴力街区的2部电影。下面这个视频是现实版牛人的跑酷,合肥也有人玩这个,不过不知道有没有传说中的神人存在。有一次去步行街,看到几个年轻人在那里跳护栏。

Tags: