6.5. 如何使用回溯

回溯是一种文本,它会显示你的程序在错误发生之前执行到哪里。 这对于开发者在调试时非常有用。最好的方法是举个例子。你是否尝试过 第 6.4 节 末尾的 who 模块? 如果尝试过,请检查它是否正常工作。现在修改这行代码string command = QUERY(path2who)+" "+QUERY(options2who);改为string command = 0;。 这会产生一个错误,因为我们将一个整数放入了字符串中。 如果我们想这样做,我们必须进行类型转换(例如,使用 (string) 0)。 如果你还没有这样做,请点击更多选项按钮在 CIF. 中,并重新加载模块。 检查全局变量 -> show_internals选项设置为 yes,然后尝试你的模块。 你会看到一个类似这样的错误

Caudium version: Caudium (Caudium/1.2.0)
Requested URL: /who

Error: Sprintf: Wrong type for argument 2: expected string, got int.
../local/modules/who.pike:76: 
CaudiumModule(Who,My first virtual server)->find_file("",object)
base_server/configuration.pike (version 1.91):1587: 
Configuration(My first virtual server)->low_get_file(object,0)
base_server/configuration.pike (version 1.91):1779: 
Configuration(My first virtual server)->get_file(object,0)
base_server/configuration.pike (version 1.91):1760: 
Configuration(My first virtual server)->handle_request(object)
protocols/http.pike (version 1.71):1549: unknown function()
protocols/http.pike (version 1.71):1610: 
unknown function(0,"GET /who HTTP/1.1\r\nHost: localhost\r\nUser-Agent:
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.8) 
Gecko/20020214\r\nAccept: text/xml,application/xml,
application/xhtml+xml,text/html;q=0.9,text/plain"+[246])
/usr/local/pike/7.2.262/lib/modules/Stdio.pmod/module.pmod
(version 1.114):683:
Stdio.File("socket", "127.0.0.1 1260", 777 /* fd=-1 */)
->__stdio_read_callback()
      

这看起来很糟糕,但事实并非如此。 第一行是错误本身

"Error: Sprintf: Wrong type for argument 2: expected string, got int."
The next line "../local/modules/who.pike:76: 
CaudiumModule(Who,My first virtual server)->find_file("",object)"
is the program (../local/modules/who.pike at line 76)
      

错误发生的位置。 find_file 是发生错误的函数名,你还可以看到传递给它的参数。 如果你使用源代码,你会看到 `find_file(string path, object id)`。 所以这里 path="",id=object [1]。 下一行是调用了low_get_fileconfiguration.pike) 函数中调用的find_filewho.pike。 你还可以看到它的参数等等。 当错误不是直接来自你的代码,而是来自之前的其他代码时,这个回溯非常有用。

注意

[1]

Pike 无法显示对象的内容,但可以显示任何其他类型。