日期:2014-05-16  浏览次数:20754 次

思考mysql内核之初级系列10---mysql内核调试方法(摘自老杨)

思考mysql内核之初级系列10---mysql内核调试方法(摘自老杨)
2010年10月27日
  在前面三篇,bingxi和alex聊了关于innodb的hash、list、以及动态数组的实现方法,这三个结构比较常用。讲完前9篇内容,本篇会描述在windows环境下debug mysql的方法,强烈建议通过debug的方式进行学习。在本篇里,bingxi和alex会聊到windows下常用的调试mysql代码的方法,仅供参考。 1)在windows和linux下调试的异同?
  Bingxi:"alex,咱们看myslq代码的方法,是通过windows看好呢,还是linux/unix下看呢,两者之间最大的差异是什么?"
  Alex:"在mysql 5.1的高版本开始,windows环境与linux环境使用同一套代码。我电脑里面正好有两个版本的代码,我们看下mysql-6.0.4-alpha目录下的INSTALL-WIN-SOURCE文件,其中有这么一段:
  To  build MySQL on Windows from source, you must satisfy the
  following system, compiler, and resource requirements:
  * Windows 2000, Windows XP, or newer version. Windows Vista is
  not supported until Microsoft certifies Visual Studio 2005 on
  Vista.
  * CMake, which can be downloaded from http://www.cmake.org.
  After  installing, modify your path to include the cmake
  binary.
  * Microsoft Visual C++ 2005 Express Edition, Visual Studio .Net
  2003 (7.1), or Visual Studio 2005 (8.0) compiler system.
  * If you are using Visual C++ 2005 Express Edition, you must
  also install an appropriate Platform SDK. More information and
  links to downloads for various Windows platforms is available
  from http://msdn.microsoft.com/platformsdk/.
  * If you are compiling from a BitKeeper tree or making changes
  to  the parser, you need bison for Windows, which can be
  downloaded from
  http://gnuwin32.sourceforge.net/packages/bison.htm .Download
  the package labeled "Complete package, excluding sources".
  After installing the package, modify your path to include the
  bison binary and ensure that this binary is accessible from
  Visual Studio.
  * Cygwin might be necessary if you want to run the test script
  or package the compiled binaries and support files into a Zip
  archive.  (Cygwin  is needed only to test or package the
  distribution, not to build it.) Cygwin is available from
  http://cygwin.com.
  * 3GB to 5GB of disk space.
  可以通过这样的方式来生成一份代码,然后用vs2005或者更高版本来调试。
  "
  Bingxi:"alex,你电脑里面的另外一个软件包是mysql-5.1.7的吧。"
  Alex:"嗯,这个版本是mysql5.1.7代码刚出来的时候进行下载的。这个版本的代码直接解压缩之后,可以直接用vs2003进行编译调试。对innodb而言,用这个版本的就可以了,innodb的变化不大,如果需要理解查询引擎,则需要使用更新的版本进行学习。"
  Bingxi:"mysql-5.1.7-beta-win-src.zip,这个软件包的内容,我们学了之后,会不会和linux下不一样,有人会有这样的疑问,毕竟在很多公司里面,mysql是运行在linux/unix环境的。我们知道windows与linux/unix的差异还是存在的,尤其是底层的系统函数。"
  Alex:"嗯,这个是很多人的疑问。其实mysql进行了代码的封装,比如在5.1.7的windows版本的代码中,也是可以看到系统函数的封装。比如event semaphore。看下对应的代码:
  /************************************************* ************
  Creates an event semaphore, i.e., a semaphore which may just have two
  states: signaled and nonsignaled. The created event is manual reset: it
  must be reset explicitly by calling sync_os_reset_event. */
  os_event_t
  os_event_create(
  /*============*/
  /* out: the event handle */
  const char*    name)     /* in: the name of the event, if NULL
  the event is created without a name */
  {
  #ifdef __WIN__
  os_event_t event;
  event = ut_malloc(sizeof(struct os_event_struct));
  event->handle = CreateEvent(NULL,/* No security attributes */
  TRUE,           /* Manual reset */
  FALSE,          /* Initial state nonsignaled */
  (LPCTSTR) name);
  if (!event->handle) {
  fprintf(stderr,
  "InnoDB: Could not create a Windows event semaphore; Windows error %lu\n",                 (ulong) GetLastError());        } #else /* Unix */