﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-战魂小筑-随笔分类-程序调试技术</title><link>http://www.cppblog.com/sunicdavy/category/11294.html</link><description>讨论群:309800774 知乎关注:http://zhihu.com/people/sunicdavy 开源项目:https://github.com/davyxu</description><language>zh-cn</language><lastBuildDate>Thu, 20 Apr 2017 12:24:21 GMT</lastBuildDate><pubDate>Thu, 20 Apr 2017 12:24:21 GMT</pubDate><ttl>60</ttl><item><title>使用Visual Studio Code调试Golang工程</title><link>http://www.cppblog.com/sunicdavy/archive/2017/04/20/214874.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Thu, 20 Apr 2017 04:52:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2017/04/20/214874.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/214874.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2017/04/20/214874.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/214874.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/214874.html</trackback:ping><description><![CDATA[<h1 id="使用visual-studio-code调试golang工程" style="box-sizing: border-box; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 2.25em/1.2 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; margin: 0px 0px 16px; widows: 1; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="1">使用Visual Studio Code调试Golang工程</h1>
<h2 id="关键字" style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 1.75em/1.225 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 1em; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="3"><a class="markdownIt-Anchor" style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="http://www.cppblog.com/sunicdavy/admin/EditPosts.aspx#关键字"></a>关键字</h2>
<ul style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; padding-left: 2em; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="4"><li style="box-sizing: border-box">最简单的调试攻略</li><li style="box-sizing: border-box">多项目调试, 适用个人开发和项目开发</li><li style="box-sizing: border-box">无需修改系统环境变量</li></ul>
<h2 id="准备vscode" style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 1.75em/1.225 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 1em; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="8"><a class="markdownIt-Anchor" style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="http://www.cppblog.com/sunicdavy/admin/EditPosts.aspx#准备vscode"></a>准备VSCode</h2>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="10">在官网下载最新版的VSCode:</p>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="12"><a style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="https://code.visualstudio.com/">https://code.visualstudio.com/</a></p>
<h2 id="安装golang插件" style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 1.75em/1.225 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 1em; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="14"><a class="markdownIt-Anchor" style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="http://www.cppblog.com/sunicdavy/admin/EditPosts.aspx#安装golang插件"></a>安装Golang插件</h2>
<ul style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; padding-left: 2em; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="15"><li style="box-sizing: border-box">
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">打开扩展面板</p>
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">VSCode-&gt;查看-&gt;扩展</p></li><li style="box-sizing: border-box">
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">找到Go插件 在搜索框里输入Go, 找到第二行写有 Rich Go language support for Visual Studio Code的插件, 点击安装</p>
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">注意不是排名最高的</p></li><li style="box-sizing: border-box">
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">重启编辑器</p></li></ul>
<h2 id="配置启动项" style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 1.75em/1.225 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 1em; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="26"><a class="markdownIt-Anchor" style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="http://www.cppblog.com/sunicdavy/admin/EditPosts.aspx#配置启动项"></a>配置启动项</h2>
<ul style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; padding-left: 2em; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="27"><li style="box-sizing: border-box">
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">打开调试面板</p>
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">VSCode-&gt;查看-&gt;调试</p></li><li style="box-sizing: border-box">
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">添加调试目标</p>
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">在"没有调试"的下拉框中点击"添加配置.."</p></li><li style="box-sizing: border-box">
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">添加目标调试配置</p>
<p style="box-sizing: border-box; margin-bottom: 16px; margin-top: 16px">例子:</p><pre style="box-sizing: border-box; overflow: auto; word-wrap: normal; margin-bottom: 16px; padding-bottom: 16px; padding-top: 16px; font: 11px/1.45 Consolas, 'Liberation Mono', Menlo, Courier, monospace; padding-left: 16px; margin-top: 0px; padding-right: 16px; background-color: rgb(247,247,247); font-stretch: normal; border-radius: 3px" data-source-line="38"><code class="hljs" style="box-sizing: border-box; word-wrap: normal; font-size: 11px; border-top: 0px; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; border-right: 0px;background: 0px 0px; white-space: pre; border-bottom: 0px; word-break: normal; color: rgb(51,51,51); padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px; border-radius: 3px">{
    "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">version</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"0.2.0"</span></span>,
    "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">configurations</span>": <span class="hljs-value" style="box-sizing: border-box">[
        {
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">name</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"Launch"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">type</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"go"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">request</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"launch"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">mode</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"debug"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">remotePath</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">""</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">port</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-number" style="box-sizing: border-box; color: teal">2345</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">host</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"127.0.0.1"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">program</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"${fileDirname}"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">env</span>": <span class="hljs-value" style="box-sizing: border-box">{
                "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">GOPATH</span>":<span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"D:/Develop/vscodegolang"</span>
            </span>}</span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">args</span>": <span class="hljs-value" style="box-sizing: border-box">[]</span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">showLog</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-literal" style="box-sizing: border-box">true</span>
        </span>}
    ]
</span>}</code></pre></li></ul>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="61">其中: "port", "host"都是go插件自动生成的</p>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="63">"env"为设置环境变量, 设置为你的工程目录就可以(包含bin, src的文件夹)</p>
<h2 id="准备调试插件" style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 1.75em/1.225 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 1em; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="66"><a class="markdownIt-Anchor" style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="http://www.cppblog.com/sunicdavy/admin/EditPosts.aspx#准备调试插件"></a>准备调试插件</h2>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="70">此时找到main.go按F5, 会报错提示:</p><pre style="box-sizing: border-box; overflow: auto; word-wrap: normal; margin-bottom: 16px; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); padding-bottom: 16px; padding-top: 16px; font: 11px/1.45 Consolas, 'Liberation Mono', Menlo, Courier, monospace; padding-left: 16px; widows: 1; margin-top: 0px; letter-spacing: normal; padding-right: 16px; background-color: rgb(247,247,247); text-indent: 0px; -webkit-text-stroke-width: 0px; font-stretch: normal; border-radius: 3px" data-source-line="71"><code class="hljs" style="box-sizing: border-box; word-wrap: normal; font-size: 11px; border-top: 0px; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; border-right: 0px;background: 0px 0px; white-space: pre; border-bottom: 0px; word-break: normal; color: rgb(51,51,51); padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px; border-radius: 3px">Failded to continue:"Cannot find Delve debugger. <span class="hljs-operator" style="box-sizing: border-box"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: 700; color: rgb(51,51,51)">Install</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: 700; color: rgb(51,51,51)">from</span> https://github.com/derekparker/delve &amp; ensure it <span class="hljs-keyword" style="box-sizing: border-box; font-weight: 700; color: rgb(51,51,51)">is</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: 700; color: rgb(51,51,51)">in</span> your <span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"GOPATH/bin"</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: 700; color: rgb(51,51,51)">or</span> <span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"PATH"</span></span></code></pre>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="75">我们使用go命令行编译调试器</p><pre style="box-sizing: border-box; overflow: auto; word-wrap: normal; margin-bottom: 16px; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); padding-bottom: 16px; padding-top: 16px; font: 11px/1.45 Consolas, 'Liberation Mono', Menlo, Courier, monospace; padding-left: 16px; widows: 1; margin-top: 0px; letter-spacing: normal; padding-right: 16px; background-color: rgb(247,247,247); text-indent: 0px; -webkit-text-stroke-width: 0px; font-stretch: normal; border-radius: 3px" data-source-line="76"><code class="hljs" style="box-sizing: border-box; word-wrap: normal; font-size: 11px; border-top: 0px; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; border-right: 0px;background: 0px 0px; white-space: pre; border-bottom: 0px; word-break: normal; color: rgb(51,51,51); padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px; border-radius: 3px"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: 700; color: rgb(51,51,51)">go</span> <span class="hljs-built_in" style="box-sizing: border-box; color: rgb(0,134,179)">get</span> github.<span class="hljs-keyword" style="box-sizing: border-box; font-weight: 700; color: rgb(51,51,51)">com</span>/derekparker/delve/cmd/dlv</code></pre>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="80">将dlv调试器放在GOPATH(工程目录)的bin目录下</p>
<h2 id="开始调试" style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 1.75em/1.225 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 1em; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="82"><a class="markdownIt-Anchor" style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="http://www.cppblog.com/sunicdavy/admin/EditPosts.aspx#开始调试"></a>开始调试</h2>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="84">选中要调试的main.go, 点击F5, 既可以开始调试</p>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="86">调试快捷键和Visual Studio系一致</p>
<ul style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; padding-left: 2em; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="87"><li style="box-sizing: border-box">F9 切换断点</li><li style="box-sizing: border-box">F10 Step over</li><li style="box-sizing: border-box">F11 Step in</li><li style="box-sizing: border-box">Shift+F11 Step out</li></ul>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="92">注意点</p>
<ul style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; padding-left: 2em; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="93"><li style="box-sizing: border-box">某些结构体成员无法直接显示时, 可以直接选中变量名, 添加到监视, 或者右键点击: "调试:求值"</li></ul>
<h2 id="多项目调试" style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 1.75em/1.225 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 1em; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="96"><a class="markdownIt-Anchor" style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="http://www.cppblog.com/sunicdavy/admin/EditPosts.aspx#多项目调试"></a>多项目调试</h2>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="98">在launch.json中可以添加多组调试入口, 通过调试面板中选中对应的配置开启不同目标的调试</p><pre style="box-sizing: border-box; overflow: auto; word-wrap: normal; margin-bottom: 16px; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); padding-bottom: 16px; padding-top: 16px; font: 11px/1.45 Consolas, 'Liberation Mono', Menlo, Courier, monospace; padding-left: 16px; widows: 1; margin-top: 0px; letter-spacing: normal; padding-right: 16px; background-color: rgb(247,247,247); text-indent: 0px; -webkit-text-stroke-width: 0px; font-stretch: normal; border-radius: 3px" data-source-line="100"><code class="hljs" style="box-sizing: border-box; word-wrap: normal; font-size: 11px; border-top: 0px; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; border-right: 0px;background: 0px 0px; white-space: pre; border-bottom: 0px; word-break: normal; color: rgb(51,51,51); padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px; border-radius: 3px">{
    "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">version</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"0.2.0"</span></span>,
    "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">configurations</span>": <span class="hljs-value" style="box-sizing: border-box">[
        {
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">name</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"client"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">type</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"go"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">request</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"launch"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">mode</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"debug"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">remotePath</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">""</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">port</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-number" style="box-sizing: border-box; color: teal">2345</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">host</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"127.0.0.1"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">program</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"${fileDirname}"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">env</span>": <span class="hljs-value" style="box-sizing: border-box">{
                "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">GOPATH</span>":<span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"D:/Develop/vscodegolang"</span>
            </span>}</span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">args</span>": <span class="hljs-value" style="box-sizing: border-box">[]</span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">showLog</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-literal" style="box-sizing: border-box">true</span>
        </span>},

        {
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">name</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"server"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">type</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"go"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">request</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"launch"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">mode</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"debug"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">remotePath</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">""</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">port</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-number" style="box-sizing: border-box; color: teal">2345</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">host</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"127.0.0.1"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">program</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"${workspaceRoot}/src/server"</span></span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">env</span>": <span class="hljs-value" style="box-sizing: border-box">{
                "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">GOPATH</span>":<span class="hljs-value" style="box-sizing: border-box"><span class="hljs-string" style="box-sizing: border-box; color: rgb(221,17,68)">"D:/Develop/vscodegolang"</span>
            </span>}</span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">args</span>": <span class="hljs-value" style="box-sizing: border-box">[]</span>,
            "<span class="hljs-attribute" style="box-sizing: border-box; color: teal">showLog</span>": <span class="hljs-value" style="box-sizing: border-box"><span class="hljs-literal" style="box-sizing: border-box">true</span>
        </span>}
    ]
</span>}</code></pre>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="138">"program"中的"${fileDirname}"是以当前选中文件作为启动点</p>
<p style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="140"><strong style="box-sizing: border-box; font-weight: bolder">更建议使用"program"的"${workspaceRoot}", 以包名作为启动点的方式进行配置</strong></p>
<h2 id="参考链接" style="box-sizing: border-box; margin-bottom: 16px; white-space: normal; word-spacing: 0px; border-bottom: rgb(238,238,238) 1px solid; text-transform: none; color: rgb(51,51,51); padding-bottom: 0.3em; font: 700 1.75em/1.225 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 1em; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="144"><a class="markdownIt-Anchor" style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="http://www.cppblog.com/sunicdavy/admin/EditPosts.aspx#参考链接"></a>参考链接</h2>
<p style="box-sizing: border-box; margin-bottom: 0px !important; white-space: normal; word-spacing: 0px; text-transform: none; color: rgb(51,51,51); font: 14px/22px 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; widows: 1; margin-top: 0px; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-stroke-width: 0px" data-source-line="146"><a style="box-sizing: border-box; text-decoration: none; color: rgb(64,120,192); background-color: transparent" href="https://code.visualstudio.com/Docs/editor/debugging">https://code.visualstudio.com/Docs/editor/debugging</a></p><img src ="http://www.cppblog.com/sunicdavy/aggbug/214874.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2017-04-20 12:52 <a href="http://www.cppblog.com/sunicdavy/archive/2017/04/20/214874.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用delve调试Golang程序技巧</title><link>http://www.cppblog.com/sunicdavy/archive/2016/09/03/214254.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Sat, 03 Sep 2016 10:12:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2016/09/03/214254.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/214254.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2016/09/03/214254.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/214254.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/214254.html</trackback:ping><description><![CDATA[<p style="margin-right: 0px; margin-bottom: 10px; margin-left: 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1; margin-top: 0px !important;"></p><p style="margin-right: 0px; margin-bottom: 10px; margin-left: 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1; margin-top: 0px !important;">Google官方为Golang的调试例子默认使用了gdb</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">然而, 使用gdb调试go程序会遇到goroutine的各类问题, 因为gdb不懂go</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">因此, 这里使用delve黑科技来进行Golang的程序调试</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">纯命令行调试方法在网上很容易搜索到, 本文主要以LiteIDE来进行程序调试</p><h1>关闭编译器优化</h1><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">正常go build/install出的go程序是完全优化过的, 强行使用调试器挂接调试时, 某些local变量/lamda表达式捕获的变量会直接进入寄存器, 无法使用调试器查看</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">删掉所有的pkg, 为build或install参数加入关闭编译器优化的参数 -gcflags "-N -l"</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">例如:</p><pre linenums=""  prettyprinted"="" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid #e1e1e8; font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; box-shadow: #fbfbfc 40px 0px 0px inset, #ececf0 41px 0px 0px inset; widows: 1; background-color: #f7f7f9;"><ol style="margin: 0px; padding-left: 30px !important; color: #1e347b;"><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">   go install </span><span style="color: #93a1a1;">-</span><span style="color: #48484c;">gcflags </span><span style="color: #dd1144;">"-N -l"</span><span style="color: #48484c;"> svc\gamesvc</span></code></li></ol></pre><h2>delve调试器安装方法</h2><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">LiteIDE自带了gdb, 但是没有delve调试器, 需要自行安装, 命令如下</p><pre linenums=""  prettyprinted"="" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid #e1e1e8; font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; box-shadow: #fbfbfc 40px 0px 0px inset, #ececf0 41px 0px 0px inset; widows: 1; background-color: #f7f7f9;"><ol style="margin: 0px; padding-left: 30px !important; color: #1e347b;"><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">go </span><span style="color: #1e347b;">get</span><span style="color: #48484c;"> github</span><span style="color: #93a1a1;">.</span><span style="color: #48484c;">com</span><span style="color: #93a1a1;">/</span><span style="color: #48484c;">derekparker</span><span style="color: #93a1a1;">/</span><span style="color: #48484c;">delve</span><span style="color: #93a1a1;">/</span><span style="color: #48484c;">cmd</span><span style="color: #93a1a1;">/</span><span style="color: #48484c;">dlv</span></code></li></ol></pre><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">delve调试器会被放到你的GOPATH/bin下</p><h1>LiteIDE中的delve调试器配置</h1><h2>选择调试器</h2><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">在LiteIDE菜单中选择 调试-&gt;debugger/delve</p><h2>delve环境变量设置</h2><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">这个时候, LiteIDE依然找不到delve, 因为它不在环境变量PATH中, 这里无需修改环境变量, 只需要LiteIDE的环境配置</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">在LiteIDE菜单中选择 查看-&gt;编辑当前环境, 在弹出的文档中修改</p><pre linenums=""  prettyprinted"="" style="margin-top: 10px; margin-bottom: 10px; border: 1px solid #e1e1e8; font-size: 13px; line-height: 19px; overflow: auto; padding: 10px; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; box-shadow: #fbfbfc 40px 0px 0px inset, #ececf0 41px 0px 0px inset; widows: 1; background-color: #f7f7f9;"><ol style="margin: 0px; padding-left: 30px !important; color: #1e347b;"><li style="margin: 0px; color: #bebec5; line-height: 18px; padding-left: 12px !important; list-style-type: decimal !important;"><code style="margin: 0px; padding: 0px; border: none; border-radius: 3px; word-wrap: break-word; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; max-width: 100%; background: transparent;"><span style="color: #48484c; margin-top: 0px;">PATH</span><span style="color: #93a1a1;">=</span><span style="color: #48484c;">c</span><span style="color: #93a1a1;">:</span><span style="color: #48484c;">\mingw32\b</span><span style="color: #1e347b;">in</span><span style="color: #93a1a1;">;%</span><span style="color: #48484c;">GOROOT</span><span style="color: #93a1a1;">%</span><span style="color: #48484c;">\b</span><span style="color: #1e347b;">in</span><span style="color: #93a1a1;">;%</span><span style="color: #48484c;">PATH</span><span style="color: #93a1a1;">%;</span><span style="color: #48484c;">c</span><span style="color: #93a1a1;">:</span><span style="color: #48484c;">\y</span><span style="color: #1e347b;">our</span><span style="color: #48484c;">\path\to\delve</span></code></li></ol></pre><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">去掉PATH前的注释#, 在%PATH%添加分号, 然后和你到delve调试器的路径</p><h2>开始调试</h2><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">选择你的工程, 点击F5, 进入调试模式</p><h2>调试器显示变量值</h2><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;"><img src="file:///D:/Documents/My%20Knowledge/temp/6f9eec62-fe6c-4f28-89ad-acbade688a96/4/index_files/2be001ea-878e-4e38-9209-76ef93d427f0.png" style="border: 0px; max-width: 100%; margin: 2px 0px; height: auto !important;"  alt="" /></p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">LiteIDE使用delve调试时, 无法在 变量 监视等窗口中正确捕捉delve调试返回数据(因为格式太复杂了&#8230;)</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">没关系, 我们使用命令行配合显示即可</p><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">LiteIDE控制台或调试输出窗口在delve调试时, 实际上是一个标准命令行&nbsp;<br />命令如下</p><ul style="margin: 10px 0px; padding-left: 32px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; font-size: 15px; line-height: 24px; widows: 1;"><li style="margin: 0px 0px 5px;"><p style="margin: 0px;">p 变量名可以查看变量值</p></li><li style="margin: 5px 0px;"><p style="margin: 0px;">locals查看局部变量</p></li><li style="margin: 5px 0px;"><p style="margin: 0px;">ls可查看当前文件</p></li><li style="margin: 5px 0px;"><p style="margin: 0px;">stack查看栈</p></li><li style="margin: 5px 0px;"><p style="margin: 0px;">help可以查看各种帮助</p></li></ul><h2>调试外部程序</h2><p style="margin: 10px 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1;">如果你的程序是外部程序, 或者使用go install安装到GOPATH/bin目录的程序, 那么使用delve调试器启动程序时, 可能会碰到启动路径错误的问题</p><p style="margin-top: 10px; margin-right: 0px; margin-left: 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1; margin-bottom: 0px !important;">使用LiteIDE菜单 调试-&gt;调试其他应用程序&#8230; 填入你要调试程序的路径以及工作目录, 可以解决这个问题</p><p style="margin-top: 10px; margin-right: 0px; margin-left: 0px; font-size: 15px; font-family: Helvetica, &quot;Hiragino Sans GB&quot;, 微软雅黑, &quot;Microsoft YaHei UI&quot;, SimSun, SimHei, arial, sans-serif; line-height: 24px; widows: 1; margin-bottom: 0px !important;"></p><img src ="http://www.cppblog.com/sunicdavy/aggbug/214254.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2016-09-03 18:12 <a href="http://www.cppblog.com/sunicdavy/archive/2016/09/03/214254.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>lua调试的工具选择</title><link>http://www.cppblog.com/sunicdavy/archive/2014/09/28/208441.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Sun, 28 Sep 2014 07:19:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2014/09/28/208441.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/208441.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2014/09/28/208441.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/208441.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/208441.html</trackback:ping><description><![CDATA[<p>最近看到一个关于vs的lua调试插件, 装了vs2012试了下, 忍不住发此文总结下lua各种调试工具</p> <p><strong><a href="http://unknownworlds.com/decoda/">Decoda</a></strong></p> <p>&nbsp;&nbsp;&nbsp; 这是现今地球上调试lua5.1最方便的工具, 没有之一. 强大的注入式调试, 性能极高.支持 挂接进程, 变量展开, 断点等各种日常所需.</p> <p>早期的Decoda是收费工具, 因此质量非常高. </p> <p>&nbsp;&nbsp;&nbsp; Decoda现在已经停止开发并开源了, 调试lua5.2会crash. 源代码可以作为一种技术参考, 很多dll注入修改技术, 灰常牛X</p> <p><a href="http://www.cppblog.com/images/cppblog_com/sunicdavy/Windows-Live-Writer/lua_D2A5/image_7.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://www.cppblog.com/images/cppblog_com/sunicdavy/Windows-Live-Writer/lua_D2A5/image_thumb_2.png" width="623" height="437"></a></p> <p><a href="http://www.luastudio.net/"><strong>LuaStudio</strong></a></p> <p>&nbsp;&nbsp; 比较优秀的调试工具(因为收费), 可以调试lua5.1/5.2, 界面属于vs2008类型, 土豪可以考虑买几套试试</p> <p><img src="http://www.luastudio.net/wp-content/plugins/download-monitor/page-addon/screenshots/4.png" width="621" height="347"></p> <p>&nbsp;</p> <p><a href="http://studio.zerobrane.com/"><strong>ZeroBrane Studio</strong></a></p> <p>对lua5.1支持较好, 5.2也能调但偶尔还是会crash, 基于远程调试方式, 所以性能略低.</p> <p><strong><a href="http://www.keplerproject.org/remdebug/example.html">RemDebug</a></strong></p> <p>没有IDE, 纯命令行方式调试器, 但因为简单, 所以可以参考后写一个自己的程序内建调试器</p> <p><a href="https://babelua.codeplex.com/"><strong>Babe Lua</strong></a></p> <p>把这货放在最后是有原因的, 还记得那句老话: 老外一开源, 我们就有自主研发了, 对的, 这货一定是参考了Decoda的代码后搞出个vs的插件来, 虽然不收费, 但是不提下参考对象的行为还是值得批斗的. 这货在中文博客上说, 不支持挂接到进程(Decoda支持), 不支持64位调试(LuaStudio支持), 调试30~50次偶尔挂1到2次. 哎, 毕竟只是代码搬运工, 不生产代码.</p> <p>这货装上, 能用, 调5.2是不行的, 5.1比Decoda方便点, 毕竟vs支持悬浮显示变量. </p> <p>&nbsp;</p> <p>说了那么多, 其实对于lua5.2版本的调试, 还是没有免费的比较合适的方案, 如果实在想调试, 还是可以参考下RemDebug的原理及lua官方调试文档, 自己通过c api调用写一套适合自己的远程调试工具. 其实没有多复杂, 但总比不调试的好<img class="wlEmoticon wlEmoticon-smile" style="border-top-style: none; border-bottom-style: none; border-right-style: none; border-left-style: none" alt="微笑" src="http://www.cppblog.com/images/cppblog_com/sunicdavy/Windows-Live-Writer/lua_D2A5/wlEmoticon-smile_2.png"></p><img src ="http://www.cppblog.com/sunicdavy/aggbug/208441.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2014-09-28 15:19 <a href="http://www.cppblog.com/sunicdavy/archive/2014/09/28/208441.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>程序异常VC调试器无法定位的处理</title><link>http://www.cppblog.com/sunicdavy/archive/2013/06/27/201339.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Thu, 27 Jun 2013 03:05:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2013/06/27/201339.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/201339.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2013/06/27/201339.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/201339.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/201339.html</trackback:ping><description><![CDATA[<p>最近调试程序发现空指针老无法定位, 使用一个最简单的main函数做空指针检查也一样, 具体症状见图</p> <p><a href="http://www.cppblog.com/images/cppblog_com/sunicdavy/Windows-Live-Writer/Visual-Studio_9AF3/image_2.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.cppblog.com/images/cppblog_com/sunicdavy/Windows-Live-Writer/Visual-Studio_9AF3/image_thumb.png" width="749" height="439"></a></p> <p>pdb加载是正常的, 但是堆栈没显示如此简单的空指针访问位置. Google了以下</p> <p><a href="http://blogs.msdn.com/b/debugger/archive/2010/05/12/visual-studio-debugger-fails-to-catch-unhandled-exception-for-a-windows-form-or-wpf-application.aspx">http://blogs.msdn.com/b/debugger/archive/2010/05/12/visual-studio-debugger-fails-to-catch-unhandled-exception-for-a-windows-form-or-wpf-application.aspx</a></p> <p>解决方法: Debug-&gt;Exception中将所有异常勾选即可</p> <p>&nbsp;</p> <p>不知道哪里的设置导致如此诡异的问题, 害的我重装一次..</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/201339.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2013-06-27 11:05 <a href="http://www.cppblog.com/sunicdavy/archive/2013/06/27/201339.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>导致闪退的根源cocos2dx的平台实现大坑</title><link>http://www.cppblog.com/sunicdavy/archive/2013/06/14/201007.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Fri, 14 Jun 2013 09:15:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2013/06/14/201007.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/201007.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2013/06/14/201007.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/201007.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/201007.html</trackback:ping><description><![CDATA[<p>最近将cocos2dx的程序移植到Android上, 某阶段突然发现开始闪退. 日志方式跟了很久, 发现有内存被修改. 因为不能像VC那样有内存断点, 只有靠日志继续跟踪, 绕了很久, 终于发现一个问题</p> <p>CCApplication::sharedApplication()-&gt;run(); 在Windows下除了初始化回调外, 还有Windows平台特殊的消息循环. 既然是循环, 这个run函数会一直阻塞到程序退出, 因此我自然的在run后添加自己的资源卸载</p> <p>换到Android上呢CCApplication::sharedApplication()-&gt;run();的实现变了. 由于Android上没有Windows的消息循环, 所有事件都是通过java方式的事件通知. 因此CCApplication::sharedApplication()-&gt;run();变成了非阻塞, 可是我在run后添加了资源卸载. 结果导致系统刚初始化就卸载了资源, 内存直接不可用, 导致后面cocos2dx的API部分使用无问题, 而自己引擎的API调用闪退.</p> <p>cocos2dx的这个run函数命名有严重问题.. 被坑了好久, 还一直怀疑NDK内存管理是否有特殊之处, 殊不知..</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/201007.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2013-06-14 17:15 <a href="http://www.cppblog.com/sunicdavy/archive/2013/06/14/201007.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>最便捷的Android环境部署cocos2dx程序</title><link>http://www.cppblog.com/sunicdavy/archive/2013/06/09/200904.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Sun, 09 Jun 2013 09:55:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2013/06/09/200904.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/200904.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2013/06/09/200904.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/200904.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/200904.html</trackback:ping><description><![CDATA[<p>本文整个部署过程无需下载及安装使用Cygwin环境， 以下部署过程需要用到的程序及版本  </p><p>请注意下载对应你系统的版本, 64位系统请保证后文全系使用64位程序, 以免遇到不必要的麻烦  </p><p>1.JDK&amp;JRE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JAVA运行时及开发包  </p><p>2.ADT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 是Eclipse的一个插件，这一步是为了管理安卓开发库  </p><p><a href="http://developer.android.com/sdk/index.html">http://developer.android.com/sdk/index.html</a>  </p><p>作为新手, 请下载ADT Bundle For Windows, 这个版本已经包含  </p><p>ADK（安卓开发包）, CDT（Eclipse的C/C++开发插件）及对应的Eclipse, 可以避免第一次部署出现的各种烦心!  </p><p>3.NDK&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 只有ADT已经可以运行普通的Andriod程序，但是如果需要编译C/C++程序， 还需要NDK  </p><p><a href="http://developer.android.com/tools/sdk/ndk/index.html">http://developer.android.com/tools/sdk/ndk/index.html</a>  </p><p>4. cocos2dx 2.0.4版本  </p><p>&nbsp; </p><p><strong>准备SDK API</strong>  </p><p align="left">下载好ADT后解压， 有如下目录</p> <p>eclipse\&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;- 开发环境</p> <p>sdk\&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;- Andriod SDK</p> <p>SDK Manager.exe&nbsp;&nbsp;&nbsp;&nbsp; &lt;-- Android开发包管理器, 由于Andriod版本较多, 所以此管理器可以方便开发者选择部署目标机器</p> <p>打开SDK Manager在Android 2.2(API 8)里的 SDK Platform, Google APIs前打勾, 点击右下角的Instal packages</p> <p>如果感觉下载速度慢, 可以移步这里<a href="http://my.oschina.net/heguangdong/blog/17443">http://my.oschina.net/heguangdong/blog/17443</a>, 选择Andriod离线下载</p> <p>这里是下载链接</p> <p><a href="http://dl-ssl.google.com/android/repository/google_apis-8_r02.zip">http://dl-ssl.google.com/android/repository/google_apis-8_r02.zip</a></p> <p><a href="http://dl-ssl.google.com/android/repository/android-2.2_r02-windows.zip">http://dl-ssl.google.com/android/repository/android-2.2_r02-windows.zip</a></p> <p><a href="https://dl-ssl.google.com/android/repository/usb_driver_r04-windows.zip">https://dl-ssl.google.com/android/repository/usb_driver_r04-windows.zip</a></p> <p>把android开头的文件解压到platforms目录下</p> <p>把goole_apis开头的文件解压到add-ons目录下</p> <p align="left">把usb_driver_r03-windows.zip解压到usb_driver目录下。</p> <p><strong>Eclipse导入工程</strong>  </p><p>打开Eclipse</p> <p>导入Cocos2dx例子工程:</p> <p>Eclipse中File-&gt;New-&gt;Other...选择Andriod Project from Existing Code</p> <p>在Import Projects的Root Directory中导入D:\Develop\RevWar\sdk\cocos2d-2.0-x-2.0.4\samples\HelloCpp\proj.android\</p> <p><strong>注意, 不要选中 Copy project into workspace, 否则路径编乱很难编译成功</strong></p> <p align="left">&nbsp;</p> <p><strong>导入cocos2dx的java框架</strong>  </p><p>在src目录中new package, 输入org.cocos2dx.lib, 在org.cocos2dx.lib的package中点Import-&gt; FileSystem </p> <p>选中目录D:\Develop\RevWar\sdk\cocos2d-2.0-x-2.0.4\cocos2dx\platform\android\java\src\org\cocos2dx\lib\, 点选所有java文件</p> <p>工程Properties-&gt;Builder-&gt;New-&gt;Program</p> <p><strong>在Main标签中填写</strong></p> <p>填写NDK编译命令行 D:\Develop\android-ndk-r8e\ndk-build.cmd</p> <p>点击Browser Workspace选中当前工程,出现${workspace_loc:/HelloCpp}</p> <p><strong>切换到Environment标签中填写</strong></p> <p>新建NDK_MODULE_PATH 填写D:\Develop\RevWar\sdk\cocos2d-2.0-x-2.0.4\;D:\Develop\RevWar\sdk\cocos2d-2.0-x-2.0.4\cocos2dx\platform\third_party\android\prebuilt\</p> <p><strong>修改cocos2dx的Android.mk, diff如下</strong></p> <p>@@ -153,6 +153,7 @@</p> <p>LOCAL_WHOLE_STATIC_LIBRARIES += cocos_jpeg_static</p> <p>LOCAL_WHOLE_STATIC_LIBRARIES += cocos_libxml2_static</p> <p>LOCAL_WHOLE_STATIC_LIBRARIES += cocos_libtiff_static</p> <p>+LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static</p> <p># define the macro to compile through support/zip_support/ioapi.c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p> <p>LOCAL_CFLAGS := -DUSE_FILE32API</p> <p>@@ -164,3 +165,4 @@</p> <p>$(call import-module,libpng)</p> <p>$(call import-module,libxml2)</p> <p>$(call import-module,libtiff)</p> <p>+$(call import-module,CocosDenshion/android)</p> <blockquote></blockquote> <p><strong>F&amp;Q</strong></p> <p><strong>andriod-8问题</strong></p> <p>修改D:\Develop\RevWar\sdk\cocos2d-2.0-x-2.0.4cocos2dx\platform\android\java\project.properties中的target=android-8改成你需要的版本</p> <p><strong>resources.ap_ does not exist </strong></p> <p>assert目录中有资源出问题, 排查即可</p> <p>例如: cocos2d-2.0-x-2.0.4\samples\TestCpp\proj.android\assets\Images\*.pvr.gz</p> <p><strong>启动Android模拟器时的Failed to allocate memory: 8问题</strong></p> <p>调整内存值,请求内存太大导致</p> <p><strong>api版本过低导致JAVA Symbol未定义问题</strong></p> <p>setEGLContextClientVersion undefined</p> <p>api8(andriod 2.2)后的版本, 才支持openGL es 2.0</p> <p><strong>自己做工程遇到的问题D:\Develop\RevWar\sdk\cocos2d-2.0-x-2.0.4\/cocos2dx/platform/android/jni/JniHelper.h:28:18: fatal error: string: No such file or directory</strong></p> <p>将cocos2dx例子中的Application.mk拷过来, 修改下内部名称即可</p> <p><strong>调试请尽量使用真机, 模拟器速度很慢</strong></p><strong> <p><strong></strong><strong>小米2默认只能管理文件, 无法用adb 连接, 因此需要安装驱动, USB驱动直接在插入电脑后的虚拟盘里找.. 这个太坑了..</strong></p></strong><strong> <p><strong></strong><strong>保证每次都能部署最新的程序</strong></p></strong> <p>请执行每次Clean, Build project, Debug.&nbsp; 真机上在需要时, 会弹出安装...</p> <p><strong>Android启动日志</strong></p> <p>带有ADT的Eclipse中有一个logcat窗口, 里面有系统及程序本身的日志, 可以做过滤,方便检查问题. 如需自己打日志, 可以使用cocos2dx中的LOGD宏来做, 原型是__android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)</p> <p>&nbsp;</p> <p><strong>Remark</strong>  </p><p>添加assert后, F5刷新后再编译<br />NDK build时,默认从工程的jni目录开始  </p><p>Andriod.mk的import 原则$(call import-module,模块名) 这里的模块名必须与目录名, 模块make file中的名称报纸一致  </p><p><strong>参考文章</strong>  </p><p><a href="http://www.cnblogs.com/ybgame/archive/2012/06/07/2540693.html">http://www.cnblogs.com/ybgame/archive/2012/06/07/2540693.html</a>  </p><p>发文时, Andriod Studio已经发布了一段时间, 虽然是测试版, 但将代表未来更方便的Andriod发布工具</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/200904.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2013-06-09 17:55 <a href="http://www.cppblog.com/sunicdavy/archive/2013/06/09/200904.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>捕获Linux段错误(Segment fault)并且打印错误堆栈</title><link>http://www.cppblog.com/sunicdavy/archive/2012/12/29/196809.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Sat, 29 Dec 2012 09:53:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2012/12/29/196809.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/196809.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2012/12/29/196809.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/196809.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/196809.html</trackback:ping><description><![CDATA[<p>Linux上跑服务器如果遇到程序崩溃是一件很苦恼的事情, 再碰到重现很难的BUG, 估计只能通过传统的排查方法进行.</p> <p>在编写本文前, 笔者使用过诸如libunwind等库进行错误时堆栈打印, 但是其本身由于需要引用第三方库, 使用还是稍微麻烦.</p> <p>经过Google后, 居然找到一篇<a href="http://blog.csdn.net/kakaka2011/article/details/6597857">好文</a>, 其通过捕获SIGSEGV信号, 并迫使程序进入gdb调试阶段, 利用gdb强大的调试功能可以进行各种错误跟踪, 此法已与Windows下程序崩溃后弹出VC调试几乎接近. </p> <p>我在此文基础上, 扩展了其通用性及便利性</p> <p>1. 使用gdb的 -ex参数, 在挂接程序后, 执行bt指令打出程序堆栈</p> <p>2. 将信息重定向到自定义的文件,在多进程都需要进行后台输出时带来更大的灵活性, 同时也解决了gdb只能在前台调试的问题</p> <p>代码如下</p><pre class="csharpcode">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;signal.h&gt;
#include &lt;<span class="kwrd">string</span>.h&gt;

<span class="kwrd">void</span> dump(<span class="kwrd">int</span> signo)
{
        <span class="kwrd">char</span> buf[1024];
        <span class="kwrd">char</span> cmd[1024];
        FILE *fh;

        snprintf(buf, <span class="kwrd">sizeof</span>(buf), <span class="str">"/proc/%d/cmdline"</span>, getpid());
        <span class="kwrd">if</span>(!(fh = fopen(buf, <span class="str">"r"</span>)))
                exit(0);
        <span class="kwrd">if</span>(!fgets(buf, <span class="kwrd">sizeof</span>(buf), fh))
                exit(0);
        fclose(fh);
        <span class="kwrd">if</span>(buf[strlen(buf) - 1] == <span class="str">'/n'</span>)
                buf[strlen(buf) - 1] = <span class="str">'/0'</span>;
        snprintf(cmd, <span class="kwrd">sizeof</span>(cmd), <span class="str">"gdb %s %d -ex=bt &gt; ./a.txt"</span>, buf, getpid());
        system(cmd);

        exit(0);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>在服务器开启时,添加 signal(SIGSEGV, &amp;dump ); 进行信号处理挂接即可</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>引用: <a title="http://blog.csdn.net/kakaka2011/article/details/6597857" href="http://blog.csdn.net/kakaka2011/article/details/6597857">http://blog.csdn.net/kakaka2011/article/details/6597857</a>&nbsp; 作者: kakaka2011</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/196809.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2012-12-29 17:53 <a href="http://www.cppblog.com/sunicdavy/archive/2012/12/29/196809.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>让VC调试器正确显示UTF-8字符串</title><link>http://www.cppblog.com/sunicdavy/archive/2012/02/14/165566.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Tue, 14 Feb 2012 07:21:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2012/02/14/165566.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/165566.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2012/02/14/165566.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/165566.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/165566.html</trackback:ping><description><![CDATA[<p>默认的, VC调试器只能正常显示ANSI字符串及UNICODE字符串, 而UTF-8字符串及其他格式则无法显示</p> <p>这里无需编写插件及修改配置文件,只需要将要显示的字符串拉到Watch中,并在变量后面添加,s8即可显示</p> <p><a href="http://www.cppblog.com/images/cppblog_com/sunicdavy/Windows-Live-Writer/165611ed03f8_D62F/image_4.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.cppblog.com/images/cppblog_com/sunicdavy/Windows-Live-Writer/165611ed03f8_D62F/image_thumb_1.png" width="233" height="42"></a> --&gt; <a href="http://www.cppblog.com/images/cppblog_com/sunicdavy/Windows-Live-Writer/165611ed03f8_D62F/image_2.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.cppblog.com/images/cppblog_com/sunicdavy/Windows-Live-Writer/165611ed03f8_D62F/image_thumb.png" width="173" height="51"></a></p> <p>同样类型的功能也应该很熟悉</p> <p>,数字&nbsp; 将变量拆分为数组显示, 数字是要显示多少位, 此法对const char*这类原始字符串非常有用</p> <p>,x 16进制查看</p> <p>,hr&nbsp; 查看Windows HRESULT解释</p> <p>,wm Windows消息,例如0x0010, wm 显示 WM_CLOSE</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/165566.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2012-02-14 15:21 <a href="http://www.cppblog.com/sunicdavy/archive/2012/02/14/165566.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VC中使用SetThreadName引起的线程死锁</title><link>http://www.cppblog.com/sunicdavy/archive/2011/10/12/158109.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Wed, 12 Oct 2011 01:50:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2011/10/12/158109.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/158109.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2011/10/12/158109.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/158109.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/158109.html</trackback:ping><description><![CDATA[<p>我在工程里使用以下函数给线程设置一个方便调试的名称</p> <div class="csharpcode"><pre class="alt">    typedef <span class="kwrd">struct</span> tagTHREADNAME_INFO</pre><pre>    {</pre><pre class="alt">        DWORD dwType;        <span class="rem">// must be 0x1000</span></pre><pre>        LPCSTR szName;        <span class="rem">// pointer to name (in user addr space)</span></pre><pre class="alt">        DWORD dwThreadID;    <span class="rem">// thread ID (-1=caller thread)</span></pre><pre>        DWORD dwFlags;        <span class="rem">// reserved for future use, must be zero</span></pre><pre class="alt">    } THREADNAME_INFO;</pre><pre>&nbsp;</pre><pre class="alt">    <span class="kwrd">void</span> SetThreadName( DWORD dwThreadID, LPCSTR szThreadName )</pre><pre>    {</pre><pre class="alt">        THREADNAME_INFO info;</pre><pre>        info.dwType = 0x1000;</pre><pre class="alt">        info.szName = szThreadName;</pre><pre>        info.dwThreadID = dwThreadID;</pre><pre class="alt">        info.dwFlags = 0;</pre><pre>&nbsp;</pre><pre class="alt">          <span class="rem">// Visit http://www.cppblog.com/sunicdavy for original article</span></pre><pre>        __try</pre><pre class="alt">        {</pre><pre>            RaiseException( 0x406D1388, 0, <span class="kwrd">sizeof</span>(info)/<span class="kwrd">sizeof</span>(DWORD), (DWORD*)&amp;info );</pre><pre class="alt">        }</pre><pre>        __except(EXCEPTION_CONTINUE_EXECUTION)</pre><pre class="alt">        {</pre><pre>            <span class="kwrd">int</span> a =1;</pre><pre class="alt">        }</pre><pre>    }</pre></div>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>使用期间一直正常. 随着工程需求变化,我需要<strong>频繁的</strong>将一个被命名的线程创建工作一段时间后<strong>马上结束释放资源并重新创建</strong>,出现了死锁问题</p>
<p>死锁总是发生在主线程的Sleep或者是SetEvent两个WindowsAPI函数. 而且仅仅在Visual Studio 2008里调试时会发生死锁,脱离调试环境一切正常.</p>
<p>我开始打日志调试, 结果发现死锁前能够正常创建线程,但是线程函数根本没有调用进入.之后就在主线程Sleep处死锁了.</p>
<p>由于SetThreadName没有使用正规的API接口,而是使用抛异常方式, 而且线程名称应该只在Visual Studio IDE中才能显示, MSDN对RaiseException的描述里也有说明可以使用<b>WaitForDebugEvent</b>接收应用程序抛得异常.因此可以推断是SetThreadName造成的问题. 屏蔽SetThreadName,测试通过</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/158109.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2011-10-12 09:50 <a href="http://www.cppblog.com/sunicdavy/archive/2011/10/12/158109.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[原创]怎样自动启动VC调试器调试被另外一个程序启动的程序</title><link>http://www.cppblog.com/sunicdavy/archive/2010/11/16/133823.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Tue, 16 Nov 2010 13:22:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2010/11/16/133823.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/133823.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2010/11/16/133823.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/133823.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/133823.html</trackback:ping><description><![CDATA[<p>调试中总会遇到这样的情况，比如，需要调试一个被另外一个进程启动的程序。最简单的方法莫过于直接在IDE中Attach到进程，但是这样的方法做一次还行。要是一直需要调试，编译后需要再调试，就会让你抓狂。</p> <p>平时安装有VC IDE的机器一定会碰到这样的情况：如果程序有crash，会弹出一个JIT的调试对话框，询问使用何种调试器进行调试。这种情况同样会出现在使用dump文件时。这个对话框也是我们今天需要用到的。</p> <p>如果需要调试一个被另外进程启动的进程，那么需要让这个被启动的线程能启动调试器，方法如下：</p> <p>假定我们需要调试的应用程序名称为myapp.exe</p> <p>&nbsp;</p> <p>1. 打开注册表</p> <p>2. 在HKEY_LOCAL_MACHINE\Software\Microsoftware\Windows NT\CurrentVersion\Image File Execution Options中寻找myapp.exe文件夹</p> <p>3. 如果这个文件夹不存在，那么就新建一个，例如：myapp.exe</p> <p>4. 在文件夹里新建一个字符串键，名称为debugger，值为vsjitdebugger.exe</p> <p>5. 如果使用的不是VC调试器，那么需要在环境变量中添加这个exe的启动路径</p> <p>&nbsp;</p> <p>运行你需要被调试的程序试试吧，爽了吧！</p> <p>&nbsp;</p> <p>转载请注明此文来自 战魂小筑博客 <a href="http://www.cppblog.com/sunicdavy">http://www.cppblog.com/sunicdavy</a></p><img src ="http://www.cppblog.com/sunicdavy/aggbug/133823.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2010-11-16 21:22 <a href="http://www.cppblog.com/sunicdavy/archive/2010/11/16/133823.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>修改线程名称,并在VC调试器中显示</title><link>http://www.cppblog.com/sunicdavy/archive/2010/05/04/114342.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Tue, 04 May 2010 07:28:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2010/05/04/114342.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/114342.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2010/05/04/114342.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/114342.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/114342.html</trackback:ping><description><![CDATA[<div class="csharpcode"><pre class="alt"><span class="preproc">#define</span> MS_VC_EXCEPTION 0x406d1388</pre><pre>&nbsp;</pre><pre class="alt">typedef <span class="kwrd">struct</span> tagTHREADNAME_INFO</pre><pre>{</pre><pre class="alt">    DWORD dwType;        <span class="rem">// must be 0x1000</span></pre><pre>    LPCSTR szName;       <span class="rem">// pointer to name (in same addr space)</span></pre><pre class="alt">    DWORD dwThreadID;    <span class="rem">// thread ID (-1 caller thread)</span></pre><pre>    DWORD dwFlags;       <span class="rem">// reserved for future use, most be zero</span></pre><pre class="alt">} THREADNAME_INFO;</pre><pre>&nbsp;</pre><pre class="alt"><span class="kwrd">void</span> SetThreadName(DWORD dwThreadID, LPCTSTR szThreadName)</pre><pre>{</pre><pre class="alt">    THREADNAME_INFO info;</pre><pre>    info.dwType = 0x1000;</pre><pre class="alt">    info.szName = szThreadName;</pre><pre>    info.dwThreadID = dwThreadID;</pre><pre class="alt">    info.dwFlags = 0;</pre><pre>&nbsp;</pre><pre class="alt">    __try</pre><pre>    {</pre><pre class="alt">        RaiseException(MS_VC_EXCEPTION, 0, <span class="kwrd">sizeof</span>(info) / <span class="kwrd">sizeof</span>(DWORD),</pre><pre>(DWORD *)&amp;info);</pre><pre class="alt">    }</pre><pre>    except (EXCEPTION_CONTINUE_EXECUTION)</pre><pre class="alt">    {</pre><pre>    }</pre><pre class="alt">}</pre></div>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>&nbsp;</p>
<p>线程名称限制在9个ANSI字符内,调用这个函数后,将触发一个调试器异常,VC调试器将会把字符串设置为线程名称,并在调试器中显示</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/114342.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2010-05-04 15:28 <a href="http://www.cppblog.com/sunicdavy/archive/2010/05/04/114342.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>CRT未构造全局对象问题</title><link>http://www.cppblog.com/sunicdavy/archive/2010/04/23/113358.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Fri, 23 Apr 2010 09:47:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2010/04/23/113358.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/113358.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2010/04/23/113358.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/113358.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/113358.html</trackback:ping><description><![CDATA[<p>引擎中的每个工程均以静态库方式链接到最后的exe. 代码中包含有静态类成员,第一次运行正常,后面不知什么原因, 这些静态类成员不会被初始化, 在 crtexec.c 的CRT 初始化全局类成员函数</p> <p>static void __cdecl _initterm (<br>#endif&nbsp; /* CRTDLL */<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _PVFV * pfbegin,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _PVFV * pfend<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * walk the table of function pointers from the bottom up, until<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * the end is encountered.&nbsp; Do not skip the first entry.&nbsp; The initial<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * value of pfbegin points to the first valid entry.&nbsp; Do not try to<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * execute what pfend points to.&nbsp; Only entries before pfend are valid.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ( pfbegin &lt; pfend )<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * if current table entry is non-NULL, call thru it.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( *pfbegin != NULL )<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (**pfbegin)();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ++pfbegin;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>}  <p>&nbsp;</p> <p>&nbsp;</p> <p>这个函数会初始化所有链接到exe中的全局函数构造, 跟踪这个地方, 发现,只有1个lib中的能被正确调用,但是出问题的那个lib中所有构造都不能被调用. 尝试调整编译顺序无果</p> <p>唯一的方法只有修改架构为在winmain内进行构造</p> <p>如果有这方面调试经验的同学可以回帖,谢谢</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/113358.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2010-04-23 17:47 <a href="http://www.cppblog.com/sunicdavy/archive/2010/04/23/113358.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++ 语句{__asm int 3;} 能在VC下编译的过么？</title><link>http://www.cppblog.com/sunicdavy/archive/2010/02/24/108353.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Wed, 24 Feb 2010 09:11:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2010/02/24/108353.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/108353.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2010/02/24/108353.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/108353.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/108353.html</trackback:ping><description><![CDATA[<p>今天在写一个宏时始终报</p> <p>fatal error C1075: end of file found before the left brace '{' at 'd:\xxx.cpp(49)' was matched</p> <p>检查排除代码，去除了宏，留下了一句</p> <p>{__asm int 3;} </p> <p>将大括号去掉，能编译成功。去掉;也能编译成功</p> <p>我就奇了怪了，查了MSDN文档，里面是这么写的：</p> <p>__asm <i>assembly-instruction</i> [ ; ] <p>__asm { <i>assembly-instruction-list</i> } [ ; ] <dl> <dt><i>asm-statement</i>:  <dd> <p><b>__asm </b><i>assembly-instruction </i><b>;</b><sub>opt</sub> <p><b>__asm { </b><i>assembly-instruction-list </i><b>};</b><sub>opt</sub></p></dd></dl> <dl> <dt><i>assembly-instruction-list</i>:  <dd> <p><i>assembly-instruction</i><b>;</b><sub>opt</sub> <p><i>assembly-instruction</i><b>;</b><i>assembly-instruction-list</i><b>;</b><sub>opt</sub></p></dd></dl> <p>也就是说 分号是可选的，这个类似于lua</p> <p>不加分号也可以这样写</p><pre>__asm mov al, 2   __asm mov dx, 0xD007   __asm out dx, al</pre><pre>&nbsp;</pre><pre>于是乎，我想这成了一个VC的bug</pre><pre>&nbsp;</pre><pre>编译环境vs2008 c/c++</pre><img src ="http://www.cppblog.com/sunicdavy/aggbug/108353.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2010-02-24 17:11 <a href="http://www.cppblog.com/sunicdavy/archive/2010/02/24/108353.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Lua脚本浮点转整形bug分析</title><link>http://www.cppblog.com/sunicdavy/archive/2010/02/04/107161.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Thu, 04 Feb 2010 04:49:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2010/02/04/107161.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/107161.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2010/02/04/107161.html#Feedback</comments><slash:comments>9</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/107161.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/107161.html</trackback:ping><description><![CDATA[<p>前面一片<a href="http://www.cppblog.com/sunicdavy/archive/2010/02/04/107155.html">文章</a>中lua出现的bug，其实是lua本身结构问题导致的：</p> <p>lua中，数值使用double来存储，包含整形和double。而解析出来的整形也是被强转为double进行存储，这样就会出问题。</p> <p>举一个简单的例子：</p> <p>double f = (double)0xffffffff;<br>int a = int(f);</p>a肯定不是-1 <p><a href="http://www.cnblogs.com/wodehuajianrui/archive/2009/03/18/1415173.html">这里</a>的文章说明这个类型转换问题的缘由。</p> <p>&nbsp;</p> <p>在<a href="http://squirrel-lang.org/">Squirrel</a>脚本中就不会有这个问题</p> <p>local a = 0xffffffff</p> <p>print( a )</p> <p>结果为-1</p> <p>查看其源代码：</p> <p>typedef union tagSQObjectValue<br>{<br>&nbsp;&nbsp;&nbsp; struct SQTable *pTable;<br>&nbsp;&nbsp;&nbsp; struct SQArray *pArray;<br>&nbsp;&nbsp;&nbsp; struct SQClosure *pClosure;<br>&nbsp;&nbsp;&nbsp; struct SQGenerator *pGenerator;<br>&nbsp;&nbsp;&nbsp; struct SQNativeClosure *pNativeClosure;<br>&nbsp;&nbsp;&nbsp; struct SQString *pString;<br>&nbsp;&nbsp;&nbsp; struct SQUserData *pUserData;<br>&nbsp;&nbsp;&nbsp; SQInteger nInteger;<br>&nbsp;&nbsp;&nbsp; SQFloat fFloat;<br>&nbsp;&nbsp;&nbsp; SQUserPointer pUserPointer;<br>&nbsp;&nbsp;&nbsp; struct SQFunctionProto *pFunctionProto;<br>&nbsp;&nbsp;&nbsp; struct SQRefCounted *pRefCounted;<br>&nbsp;&nbsp;&nbsp; struct SQDelegable *pDelegable;<br>&nbsp;&nbsp;&nbsp; struct SQVM *pThread;<br>&nbsp;&nbsp;&nbsp; struct SQClass *pClass;<br>&nbsp;&nbsp;&nbsp; struct SQInstance *pInstance;<br>&nbsp;&nbsp;&nbsp; struct SQWeakRef *pWeakRef;<br>&nbsp;&nbsp;&nbsp; SQRawObjectVal raw;<br>}SQObjectValue; <p>&nbsp;</p> <p>可以看到</p> <p>SQInteger nInteger;<br>SQFloat fFloat; <p>是分开存储的，因此就不会有这个问题</p> <p>&nbsp;</p> <p>lua解决方法：</p> <p>1. 将十六进制换为10进制存储</p> <p>2. 等待大侠或者官方修改代码，做出patch</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/107161.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2010-02-04 12:49 <a href="http://www.cppblog.com/sunicdavy/archive/2010/02/04/107161.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Lua在Release版本下的一个bug</title><link>http://www.cppblog.com/sunicdavy/archive/2010/02/04/107155.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Thu, 04 Feb 2010 02:59:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2010/02/04/107155.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/107155.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2010/02/04/107155.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/107155.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/107155.html</trackback:ping><description><![CDATA[<p>最近将自己的UI工程在Release版下编译，发现有部分控件的颜色居然偏黄，想必液晶也不至于老化的那么厉害，随机开始将工程加入调试信息进行跟踪，排除UI工程，及自己写的lua封装问题，将问题缩小为：</p> <p>lua代码：</p> <p>t = {} </p> <p>t.FillColor = 0xFFFFFFFF</p> <p>foo( t )</p> <p>在C++中注册一个foo函数，然后获取table t中的FillColor成员</p> <p>发现取得的值居然为0x80000000</p> <p>&nbsp;</p> <p>使用lua c api测试代码</p> <p>lua_newtable( L );<br>lua_setglobal( L, "t"); <p>lua_getglobal( L, "t");<br>lua_pushstring(L,"FillColor");<br>lua_pushinteger( L, 0xffffffff );<br>lua_pushinteger( L, -1 );<br>lua_settable( L, -3 );  <p>lua_getglobal( L, "t");<br>lua_pushstring( L, "FillColor");<br>lua_gettable( L, -2 );<br>int t = lua_tointeger( L, -1 ); <p>结果t也是0x80000000 <p>&nbsp; <p>然后将十六进制的0xFFFFFFFF换成十进制的4294967295，测试结果正确显示-1 <p>再使用几个样本测试 <p>0xFF5F5F5F = 4284440415 会被改成0xFF5F5F00 <p>&nbsp; <p>这个bug倒是很好解决，将代码中的十六进制数全换为10进制即可。但是为什么只在Release版本发生呢？ <p>本人使用的是lua 5.1.4 原生</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/107155.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2010-02-04 10:59 <a href="http://www.cppblog.com/sunicdavy/archive/2010/02/04/107155.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个C++标准库导致的错误</title><link>http://www.cppblog.com/sunicdavy/archive/2009/11/11/100712.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Wed, 11 Nov 2009 08:49:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2009/11/11/100712.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/100712.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2009/11/11/100712.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/100712.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/100712.html</trackback:ping><description><![CDATA[
<p>&nbsp;&nbsp;&nbsp; 最近将代码调整为release版时，碰到这样一个奇怪的错误：直接运行程序，还没有进入main入口时就宕掉。这个exe工程链接过一个公用的静态库和一个底层功能的静态库。在debug版完全没有问题。</p> <p>&nbsp;&nbsp;&nbsp; 随即开始隔离代码分析，当隔离到一个继承公用静态库时，隔离后不会宕机。这表示静态库与exe之间的链接出了一些问题。再次观察挂掉的地方显示streambuf一类的东西，这是包含在iostream里的东西，继续点击堆栈向上跟踪。结果在wcout.cpp里发现了</p> <p>__PURE_APPDOMAIN_GLOBAL static wfilebuf wfout(_cpp_stdout);</p> <p>所有问题迎刃而解，应该是恶心的iostream的静态变量，在多重静态库链接时的错误，导致exe启动，CRT为其分配内存时的宕机。马上屏蔽所有使用过wcout及iostream的所有代码。release版正常运行，不再宕机</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/100712.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2009-11-11 16:49 <a href="http://www.cppblog.com/sunicdavy/archive/2009/11/11/100712.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>经典的C++内存分配导致crash的案例</title><link>http://www.cppblog.com/sunicdavy/archive/2009/09/03/95218.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Thu, 03 Sep 2009 08:10:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2009/09/03/95218.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/95218.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2009/09/03/95218.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/95218.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/95218.html</trackback:ping><description><![CDATA[
<p>今天qq某大侠问我，为啥他的代码会crash</p><pre class="csharpcode"><span class="kwrd">char</span>* pdata = <span class="kwrd">new</span> <span class="kwrd">char</span>(14245);
::memset( pdata, 0, 14245 );
</pre>
<p>我马上运行代码，结果果真是Access Violent。</p>
<p>之后试过malloc都没有问题</p>
<p>随机我把这段代码删除，过了几分钟后，我按照记忆，重新写下代码</p><pre class="csharpcode"><span class="kwrd">char</span>* pdata = <span class="kwrd">new</span> <span class="kwrd">char</span>[14245];
::memset( pdata, 0, 14245 );

结果不crash了。细细一看</pre><pre class="csharpcode">。</pre><pre class="csharpcode">。</pre><pre class="csharpcode">。</pre><pre class="csharpcode">&nbsp;</pre><pre class="csharpcode">&nbsp;</pre><pre class="csharpcode">&nbsp;</pre><pre class="csharpcode">&nbsp;</pre><pre class="csharpcode">&nbsp;</pre><pre class="csharpcode">&nbsp;</pre><pre class="csharpcode">为啥要调用char的构造函数传入14245的参数呢？这样只能new出来1个字节，但是memset又写了14245个字节，不挂才有鬼了</pre><pre class="csharpcode">之后问过很多人，基本都没看出来，可见这个写法极其骗人</pre><img src ="http://www.cppblog.com/sunicdavy/aggbug/95218.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2009-09-03 16:10 <a href="http://www.cppblog.com/sunicdavy/archive/2009/09/03/95218.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>对C++ delete[] 一直忽略的问题</title><link>http://www.cppblog.com/sunicdavy/archive/2009/08/20/93934.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Thu, 20 Aug 2009 10:22:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2009/08/20/93934.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/93934.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2009/08/20/93934.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/93934.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/93934.html</trackback:ping><description><![CDATA[
<p>&nbsp;&nbsp;&nbsp; C++中使用delete进行单个对象的析构及内存删除，使用delete[]进行对象数组的析构及内存删除</p> <p>&nbsp;&nbsp;&nbsp; 记得某次友人层说过，delete[]在Windows平台上都是用HeapFree来实现的，因此delete[]和delete来删除对象数组时都是一样的。之后我也参考过很多的内存管理器及new/delete重载实现,基本都是这个道理。</p> <p>&nbsp;&nbsp;&nbsp; 最近突然发现，如果是对象数组的话，使用delete确实能删除内存，但是对象数组中的每个对象的析构就不能被调用了。幸好平时一直都还是保持良好的C++习惯，一直使用delete[]，并且使用new[]大部分是char类型，不需要析构</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/93934.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2009-08-20 18:22 <a href="http://www.cppblog.com/sunicdavy/archive/2009/08/20/93934.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>lua库使用经验一则</title><link>http://www.cppblog.com/sunicdavy/archive/2009/07/20/90632.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Mon, 20 Jul 2009 08:20:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2009/07/20/90632.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/90632.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2009/07/20/90632.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/90632.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/90632.html</trackback:ping><description><![CDATA[
<p>今天将部分工程从静态链接库改为动态链接库，并使用接口方式创建时，原来lua调用的地方居然挂掉了，挂的地方是table在清除时，crash在newkey</p> <p>google了一下，找到了一篇颇有参考价值的<a href="http://lua-users.org/lists/lua-l/2008-01/msg00671.html">文章</a></p> <p>&nbsp;</p><pre>A common error is to link C extension modules (shared libraries)
with the static library. The linker command line for extension
modules must not ever contain -llua or anything similar!</pre><pre>I.e. check your build process. If you are unsure where the two
copies of the Lua core come from, grep the binaries for some
characteristic error message, like "table index is nil".</pre><pre>我的lua封装库是与lua源码一起封装在一个静态链接库提供给所有程序链接使用。有些工程只有1个dll或者exe单独链接，不会出现问题。问题很大程度上就发生于将一个lua源码构成的库链接给多个dll或者exe，或者混杂链接，这样极易出现莫名其妙的table访问到dummy_node的错误情况</pre><pre>因此，马上调整，尝试将这个库改为dll。但是在一些模板函数偏特化上出现了链接问题。最后发现，动态链接库完全就是模板的噩耗，虽然模板函数是内联，对于全局的模板函数，将其放在头文件中就会出现多重引用问题。因此放弃，将lua5.1的源码放在一个dll中，将封装库放在静态库中，这样就绝对没有任何问题</pre><img src ="http://www.cppblog.com/sunicdavy/aggbug/90632.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2009-07-20 16:20 <a href="http://www.cppblog.com/sunicdavy/archive/2009/07/20/90632.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>诡异的Visual Studio 的调试bug</title><link>http://www.cppblog.com/sunicdavy/archive/2009/07/17/90351.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Fri, 17 Jul 2009 08:06:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2009/07/17/90351.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/90351.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2009/07/17/90351.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/90351.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/90351.html</trackback:ping><description><![CDATA[
<p>&nbsp;</p> <h4>工程结构</h4> <p>包含有静态库</p> <p>motion.lib 一个UI逻辑的库</p> <p>HGErender.lib&nbsp; 使用一个motion的接口实现的HGE渲染器，与motion.lib没有链接和依赖关系</p> <p>一个exe静态链接motion.lib和HGErender.lib</p> <p>&nbsp;</p> <p>motion中加入了一些flash支持，将flash的包含放到了预编译头文件，这部分头文件exe是没有引用的</p> <h4>表现症状</h4> <p>exe可以正常运行，但是在motion中新添加的flash类实例化后无法看到成员</p> <p>重编，重新link，清空重编无用</p> <p>换用vs2005，依然无用，但是报了一个Error: error in OMF type information 错误</p> <h4>解决方法</h4> <p>尝试将flash包含到预编译头的文件放到对外包含中，让exe也能看到这一部分</p> <p>问题解决</p> <h4>分析</h4> <p>VS的调试系统并不是完全开发，因此我们只能从一些表象来分析一些调试器原理及行为</p> <p>这个bug的原因就在 作为调试入口的exe并没有获得完全的motion里调试的信息。如果将motion作为dll加载，理论分析不会出现这种问题。dll将被作为一个单独的调试入口，需要单独加载独立的pdb。</p> <p>本例中，静态库中的调试信息并没有链接到exe，因此看不到成员的任何信息</p> <h4>扩展</h4> <p>大多数的游戏或应用程序都是使用静态库来做链接，这样无论是编写还是代码安全都有所兼顾。但对于工程模块之间的互相隔离，C/C++静态库本身的毛病（CRT内存分配及静态，全局变量问题）就变得尤为严重。</p> <p>而使用动态链接库的接口方式也是有一部分程序这样使用的，如果为了安全或者加密，可以将动态链接库放到自己的包内，使用从内存读取dll的技术，这样安全，工程架构又清晰</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/90351.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2009-07-17 16:06 <a href="http://www.cppblog.com/sunicdavy/archive/2009/07/17/90351.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>lua退出时内存删除的诡异异常及处理</title><link>http://www.cppblog.com/sunicdavy/archive/2009/06/20/88152.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Sat, 20 Jun 2009 02:47:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2009/06/20/88152.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/88152.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2009/06/20/88152.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/88152.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/88152.html</trackback:ping><description><![CDATA[
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 最近在使用自己封住的一套lua库使用中遇到这样一个问题，多注册一个类成员函数后，即便一个lua文件都没加载，都会在lua_close中的freestack函数中crash</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 我的lua封装非常简单，支持C++类成员函数调用上也是在luaplus基础上改造而来的：</p><pre class="csharpcode">    <span class="kwrd">void</span> register_helper( lua_State* L, <span class="kwrd">int</span> tableindex, <span class="kwrd">const</span> <span class="kwrd">char</span>* funcname, lua_CFunction dispatcher, <span class="kwrd">void</span>* func, <span class="kwrd">int</span> sizeoffunc, <span class="kwrd">const</span> <span class="kwrd">void</span>* userdata, <span class="kwrd">int</span> sizeofud )
    {
        <span class="rem">// function | userdata = lightuserdata</span>
        unsigned <span class="kwrd">char</span>* buffer = (unsigned <span class="kwrd">char</span>*)lua_newuserdata( L, sizeoffunc + sizeofud );
        memcpy( buffer, (<span class="kwrd">void</span>*)func, sizeoffunc );
        memcpy( buffer + sizeoffunc, &amp;userdata, sizeofud );

        lua_pushstring( L, funcname );
        lua_pushlightuserdata( L, (<span class="kwrd">void</span>*)buffer);  
        lua_pushcclosure(L, dispatcher, 1);
        lua_settable( L, tableindex );
    }</pre><pre class="csharpcode">    </pre><pre class="csharpcode"> </pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>检查发现，貌似是lua_newuserdata的问题，将其换为malloc，问题解决了。</p>
<p>虽然支持google了一下类似的问题，并且也在gc库内打过断点，new出来的内存绝非简单的被gc回收，而有可能是lua某些机制造成的，毕竟这样给C++做绑定是很常见的</p>
<p>马上做了一个list保存指针，并在lua_close之前清除这些指针，所有问题迎刃而解</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/88152.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2009-06-20 10:47 <a href="http://www.cppblog.com/sunicdavy/archive/2009/06/20/88152.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>断点无效到C++ 全局类构造规律研究</title><link>http://www.cppblog.com/sunicdavy/archive/2009/06/14/87619.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Sun, 14 Jun 2009 02:51:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2009/06/14/87619.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/87619.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2009/06/14/87619.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/87619.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/87619.html</trackback:ping><description><![CDATA[
<p>最近遇到一个vs2008 C++中断点无法工作的问题，具体表现为：  </p><p>1. 某个修改后，断点无法工作，之前版本依然正常  </p><p>2. 只是某个文件中断点无法工作  </p><p>3. 断点打在 引用另外DLL中的类对象A  </p><p>文件结构：  </p><p>Core.dll  </p><p>A.cpp </p><pre class="csharpcode"><span class="kwrd">class</span> A 
{ 
<span class="kwrd">public</span>: 
    A( )
    { 
        printf(<span class="str">"constructor of a"</span>);
    } 

} 
</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode">&nbsp;</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>UI.dll 
</p><p>Manager.cpp </p><pre class="csharpcode"><span class="kwrd">class</span> Manager
{ 
<span class="kwrd">public</span>: 
    Manager( )
    { 
        printf(<span class="str">"constructor of Manager"</span>);
    } 

} 
</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>&nbsp; </p><p>Button.cpp 
</p><p>A ins; // 断点打在这里 
</p><p>Run.exe </p><pre class="csharpcode"><span class="kwrd">void</span> main() 
{ 
    Manager ins;  <span class="rem">// 实例化 </span>
} 
</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>这里发现<strong>A根本不会被C runtime在执行main之前构造</strong>，因此断点无法打到 
</p><p>已经排除特殊字符影响的原因，按照网上的方法是，将文件保存为UNICODE，问题依旧 
</p><p>开始用排除隔离法确定问题位置，最后发现这样一个规律： 
</p><p><strong>只要在Manager.cpp里构造一个Button </strong>
</p><p><strong>Button insB; </strong>
</p><p><strong>这样做了以后，断定必定能起作用。 </strong>
</p><p>因此推断CRT层的全局构造是需要一个链表来推动的 
</p><p>因为Manager需要构造，因此CRT肯定会扫描这个CPP对应OBJ链接到exe里的段，让这个段里的全局构造函数都初始化，但是Button因为没有使用，因此也不会构造。这样局部扫描应该是出于效率.
</p><p>解决这个问题还有一个方法就是全部使用静态链接方式，这样构造代码都放到最后的exe中，就不会出现这样的问题
</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/87619.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2009-06-14 10:51 <a href="http://www.cppblog.com/sunicdavy/archive/2009/06/14/87619.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>破解VC的断点保存位置</title><link>http://www.cppblog.com/sunicdavy/archive/2009/06/10/87346.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Wed, 10 Jun 2009 09:36:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2009/06/10/87346.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/87346.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2009/06/10/87346.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/87346.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/87346.html</trackback:ping><description><![CDATA[
<p>一直不是很清除VC的断点保存于何处，让我们看几个证据：
</p><ol style="margin-left: 90pt"><li>多人开发中，经常更新别人的代码后，突然停在某个自己没打过的断点上，我们组称之为附送断点。
</li><li>并且无论VSS还是SVN等代码管理工具都会出这样的问题。但是你提交的只有代码，没有其他单独的配置文件
</li><li>VC2005中，即便打过SP1，也会碰到某个文件突然无法打断点的BUG（排除没有重编，打错文件断点等低级错误），解决办法是：打开记事本，将这个文件的代码全部粘贴到记事本，再帖回来
</li><li>VB中的代码文件，使用记事本打开时，可以看到比IDE里看到更多的配置型代码
</li></ol><p>综上所述，基本可以断定VC的断点被保存于代码的尾部的某个位置，看不见的一些字符</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/87346.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2009-06-10 17:36 <a href="http://www.cppblog.com/sunicdavy/archive/2009/06/10/87346.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>韩文操作系统中输入中文的问题</title><link>http://www.cppblog.com/sunicdavy/archive/2009/05/20/83451.html</link><dc:creator>战魂小筑</dc:creator><author>战魂小筑</author><pubDate>Wed, 20 May 2009 03:32:00 GMT</pubDate><guid>http://www.cppblog.com/sunicdavy/archive/2009/05/20/83451.html</guid><wfw:comment>http://www.cppblog.com/sunicdavy/comments/83451.html</wfw:comment><comments>http://www.cppblog.com/sunicdavy/archive/2009/05/20/83451.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/sunicdavy/comments/commentRss/83451.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunicdavy/services/trackbacks/83451.html</trackback:ping><description><![CDATA[
<p>&nbsp;&nbsp;&nbsp;&nbsp;最近聊天系统测出一个问题：测试人员使用的是韩文操作系统，在上面安装了搜狗中文输入法。他们互相之间发送韩文聊天是没有乱码的，但是我们和他们互相以中文发送时，会出现乱码情况。随即进行测试发现以下问题：
</p><p>&nbsp;&nbsp;&nbsp;&nbsp;对于  "德国"
</p><p>&nbsp;&nbsp;&nbsp;&nbsp;他们发送的编码数据为 d3 ec 3f 
</p><p>&nbsp;&nbsp;&nbsp;&nbsp;而我们的中文系统为 b5 c2 b9 fa
</p><p>&nbsp;&nbsp;&nbsp;&nbsp;因为聊天系统采用ANSI的架构，但是为什么同是东方语言系，一个会是UTF8,一个是ANSI双字节，因此将聊天系统整体发送换成纯UNICODE进行测试</p><img src ="http://www.cppblog.com/sunicdavy/aggbug/83451.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunicdavy/" target="_blank">战魂小筑</a> 2009-05-20 11:32 <a href="http://www.cppblog.com/sunicdavy/archive/2009/05/20/83451.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>