2010-02-19 43 views
2

我得到一些C代碼段錯誤,我無法弄清楚如何看這個,所以我可以找出問題..排除分段錯誤

沒有人有任何的技術,可以幫助我嗎?有什麼東西會跳出來嗎?

下面是GDB輸出:

 
GNU gdb 6.8 for GNAT Pro 6.2.1 (20090115) [rev:143235] 
Copyright (C) 2008 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it. 
See your support agreement for details of warranty and support. 
If you do not have a current support agreement, then there is absolutely 
no warranty for this version of GDB. Type "show copying" 
and "show warranty" for details. 
This GDB was configured as "sparc-sun-solaris2.8"... 
Reading symbols from /usr/lib/libsocket.so.1...done. 
Loaded symbols for /usr/lib/libsocket.so.1 
Reading symbols from /usr/lib/libnsl.so.1...done. 
Loaded symbols for /usr/lib/libnsl.so.1 
Reading symbols from /usr/lib/libgen.so.1...done. 
Loaded symbols for /usr/lib/libgen.so.1 
Reading symbols from /usr/lib/libintl.so.1... 
warning: Lowest section in /usr/lib/libintl.so.1 is .dynamic at 00000074 
done. 
Loaded symbols for /usr/lib/libintl.so.1 
Reading symbols from /usr/lib/libw.so.1... 
warning: Lowest section in /usr/lib/libw.so.1 is .dynamic at 00000074 
done. 
Loaded symbols for /usr/lib/libw.so.1 
Reading symbols from /usr/lib/libm.so.1...done. 
Loaded symbols for /usr/lib/libm.so.1 
Reading symbols from /temp/gnat/Omnyx/Services/ABPUimLegacy/buildSunOS/lib/libabpuimlegacyC.so...done. 
Loaded symbols for /temp/gnat/Omnyx/Services/ABPUimLegacy/buildSunOS/lib/libabpuimlegacyC.so 
Reading symbols from /usr/openwin/lib/libXext.so.0...done. 
Loaded symbols for /usr/openwin/lib/libXext.so.0 
Reading symbols from /usr/openwin/lib/libX11.so.4...done. 
Loaded symbols for /usr/openwin/lib/libX11.so.4 
Reading symbols from /usr/openwin/lib/libXmu.so.4...done. 
Loaded symbols for /usr/openwin/lib/libXmu.so.4 
Reading symbols from /usr/openwin/lib/libXt.so.4...done. 
Loaded symbols for /usr/openwin/lib/libXt.so.4 
Reading symbols from /usr/dt/lib/libXm.so.3...done. 
Loaded symbols for /usr/dt/lib/libXm.so.3 
Reading symbols from /usr/lib/libc.so.1...done. 
Loaded symbols for /usr/lib/libc.so.1 
Reading symbols from /temp/gnat/Omnyx/Services/ABPCommonLegacy/buildSunOS/lib/libabpcommonlegacy.so...done. 
Loaded symbols for /temp/gnat/Omnyx/Services/ABPCommonLegacy/buildSunOS/lib/libabpcommonlegacy.so 
Reading symbols from /temp/gnat/Omnyx/Services/ABPCommonLegacy/buildSunOS/lib/libabpcommonlegacyC.so...done. 
Loaded symbols for /temp/gnat/Omnyx/Services/ABPCommonLegacy/buildSunOS/lib/libabpcommonlegacyC.so 
Reading symbols from /app/gnatpro6.2.1/lib/gcc/sparc-sun-solaris2.8/4.3.3/rts-native/adalib/libgnarl-6.2.so...done. 
Loaded symbols for /adapts/ad/irad-c2/tools/SunOS/gnatpro6.2.1/lib/gcc/sparc-sun-solaris2.8/4.3.3/adalib/libgnarl-6.2.so 
Reading symbols from /app/gnatpro6.2.1/lib/gcc/sparc-sun-solaris2.8/4.3.3/rts-native/adalib/libgnat-6.2.so...done. 
Loaded symbols for /adapts/ad/irad-c2/tools/SunOS/gnatpro6.2.1/lib/gcc/sparc-sun-solaris2.8/4.3.3/adalib/libgnat-6.2.so 
Reading symbols from /usr/lib/libpthread.so.1... 
warning: Lowest section in /usr/lib/libpthread.so.1 is .dynamic at 00000074 
done. 
Loaded symbols for /usr/lib/libpthread.so.1 
Reading symbols from /usr/lib/librt.so.1...done. 
Loaded symbols for /usr/lib/librt.so.1 
Reading symbols from /app/gnatpro6.2.1/lib/libstdc++.so.6...done. 
Loaded symbols for /adapts/ad/irad-c2/tools/SunOS/gnatpro6.2.1/lib/libstdc++.so.6 
Reading symbols from /app/gnatpro6.2.1/lib/libgcc_s.so.1...done. 
Loaded symbols for /adapts/ad/irad-c2/tools/SunOS/gnatpro6.2.1/lib/libgcc_s.so.1 
Reading symbols from /usr/lib/libthread.so.1... 
warning: Lowest section in /usr/lib/libthread.so.1 is .dynamic at 00000074 
done. 
Loaded symbols for /usr/lib/libthread.so.1 
Reading symbols from /usr/lib/libaio.so.1...done. 
Loaded symbols for /usr/lib/libaio.so.1 
Reading symbols from /usr/lib/libmd.so.1...done. 
Loaded symbols for /usr/lib/libmd.so.1 
Reading symbols from /usr/lib/libm.so.2...done. 
Loaded symbols for /usr/lib/libm.so.2 
Reading symbols from /platform/sun4v/lib/libc_psr.so.1...done. 
Loaded symbols for /platform/SUNW,Sun-Fire-T200/lib/libc_psr.so.1 
Reading symbols from /lib/ld.so.1...done. 
Loaded symbols for /lib/ld.so.1 
Core was generated by `./buildSunOS/apsui -aps_instance 1006 -aps_ato 0 -reject_menu_tearoff -aps_ipc_'. 
Program terminated with signal 11, Segmentation fault. 
[New process 75224 ] 
#0 0x7f1e4d00 in _XmGetFocusData() from /usr/dt/lib/libXm.so.3 

下面是GDB回溯充分顯示出:

 
(gdb) bt full 
#0 0x7f1e4d00 in _XmGetFocusData() from /usr/dt/lib/libXm.so.3 
No symbol table info available. 
#1 0x7f1e2768 in _XmNavigInitialize() from /usr/dt/lib/libXm.so.3 
No symbol table info available. 
#2 0x7f1e8adc in Initialize() from /usr/dt/lib/libXm.so.3 
No symbol table info available. 
#3 0x7f357760 in CallInitialize() from /usr/openwin/lib/libXt.so.4 
No symbol table info available. 
#4 0x7f3576b4 in CallInitialize() from /usr/openwin/lib/libXt.so.4 
No symbol table info available. 
#5 0x7f3576b4 in CallInitialize() from /usr/openwin/lib/libXt.so.4 
No symbol table info available. 
#6 0x7f353804 in xtCreate() from /usr/openwin/lib/libXt.so.4 
No symbol table info available. 
#7 0x7f35bf0c in _XtCreateWidget() from /usr/openwin/lib/libXt.so.4 
No symbol table info available. 
#8 0x7f35bc8c in XtCreateWidget() from /usr/openwin/lib/libXt.so.4 
No symbol table info available. 
#9 0x7f6026f8 in uicr_xmcreate_window (win=0xd3bc0) 
    at /temp/gnat/Omnyx/Services/ABPUimLegacy/src/uim/create/uicr_xmcreate_window.c:180 
    n_args = 0 
    args = {{name = 0x0, value = 0} } 
    err_msg = '\0' , "ÿ¿Ïä", '\0' , "REVIEW PRIME MANAGEMENT MERGE IMPORT SETUP PLANNING PLAN_SETUP PLAN_PARAMS FROB ATO_APPROVAL ATO_TRICKLE AIRLIFTmain_window_menu\000T_CMD MARITIME_TGT_CMD TEST \000K1\177}al\000\000\000\004\000\000\000\001\000\000\000\000"... 
    shell = (Widget) 0xd50b8 
    object_width = 0 
    object_height = 1660944384 
    window_name = "TBM_PrimaryWin\000ä\000\000\000\000\177ÿÿø\177ÿü\000\177\023\222¤\000\000 \000\000\f\215ð\000\f\217 \177\023\222¬\177ÿü\000\000\000\000\000ÿ¿Î\200\177\005f,\000\000\000\000\177\023Vx\000\fO \000\aÀ\020\000\fO \1772*\000ui_ipc_xref." 
    icon_pixmap = 8332422 
    obj = (gen_obj_list_t *) 0x6b0 
    vis = (vis_list_t *) 0x7f832bb8 
    toolbar = (toolbar_t *) 0x0 
#10 0x7f5f524c in uicr_create_window (in_buff=0x19750 "main_window_menu") 
    at /temp/gnat/Omnyx/Services/ABPUimLegacy/src/uim/create/uicr_create_window.c:431 
    func_name = "uicr_create_window" 
    str = "\000\000\000\000\000\000\000\000¿¿Ä6'yC \000\000\000\000\000\000\000\000\177\235p0\000\000\000\000\000\003eÈ\000\003e¸\000\000\000\004ÿÿ\000\000\000\000\000\000ÿÿÿß\000\000\000\017\000\000\000\017\000\003^\200ÿ¿Ô\000\000\000\000\000\000\000\000\001\000\000\000\004\000\000\000\017ÿ¿Ó \177\2133, "\001\000\000\000\234\000\000\000\000\000\000\000\036\000\000\000\000\000\rP\000ÿ¿Ô \177\213\022\020\000"... 
    ptr = 0x19760 "" 
    keyword = 0x8a130 "" 
    window_name = 0x8a130 "" 
---Type to continue, or q to quit--- 
    title_name = "\000¿ÒØ\177\211îÄ\000\000\000\000\000\000\000\004", '\0' , "\003e¸\000\000\000\004ÿÿ\000\000\000\000\000\000ÿÿÿß\000\000\000\017\000\000\000 \000\001\023T\177¿sÈ\177·\fà\005øص\000\000\[email protected]\177·\020Xÿ¿Ó8" 
    full_title_name = 0x2b870 "TAP" 
    object_width = 2141025804 
    object_height = 1 
    window_width = 0 
    window_height = 0 
    first_object = 1 
    other_obj = 0 
    centered_max_width = 18866798 

下面是dbx的輸出:

For information about new features see `help changes' 
To remove this message, put `dbxenv suppress_startup_message 7.7' in your .dbxrc 
Reading apsui 
core file header read successfully 
Reading ld.so.1 
Reading libsocket.so.1 
Reading libnsl.so.1 
Reading libgen.so.1 
Reading libintl.so.1 
Reading libw.so.1 
Reading libm.so.1 
Reading librt.so.1 
Reading libabpuimlegacyC.so 
Reading libXext.so.0 
Reading libX11.so.4 
Reading libXmu.so.4 
Reading libXt.so.4 
Reading libXm.so.3 
Reading libabpcommonlegacy.so 
Reading libabpcommonlegacyC.so 
Reading libgnarl-6.2.so 
Reading libgnat-6.2.so 
Reading libpthread.so.1 
Reading libc.so.1 
Reading libaio.so.1 
Reading libmd.so.1 
Reading libstdc++.so.6.0.10 
Reading libgcc_s.so.1 
Reading libthread.so.1 
Reading libm.so.2 
Reading libc_psr.so.1 
[email protected] ([email protected]) program terminated by signal SEGV (no mapping at the fault address) 
0x7f264d00: _XmGetFocusData+0x0098: ld  [%o0], %o1 
Current function is uicr_xmcreate_window 
    180  win->main_window = XmCreateMainWindow(shell, win->name, args, n_args); 
>check -all 
access checking - ON 
memuse checking - ON 
Running: apsui 
(process id 18052) 
Reading rtcapihook.so 
Reading libdl.so.1 
Reading rtcaudit.so 
Reading libmapmalloc.so.1 
Reading rtcboot.so 
Reading librtc.so 
RTC: Enabling Error Checking... 
RTC: Using UltraSparc trap mechanism 
RTC: See `help rtc showmap' and `help rtc limitations' for details. 
RTC: Running program... 
abpcommonlegacydummy.adb elaborated 
User Interface version TOOLKIT : 22 MAR 2010 UIMPID=18052 
Read from unallocated (rua) on thread 1: 
Attempting to read 4 bytes through NULL pointer 
[email protected] ([email protected]) stopped in _XmGetFocusData at 0x5d164d00 
0x5d164d00: _XmGetFocusData+0x0098: ld  [%o0], %o1 
Current function is uicr_xmcreate_window 
    180  win->main_window = XmCreateMainWindow(shell, win->name, args, n_args); 

下面是在過去的函數調用我可以修改的堆棧跟蹤(不在外部庫 - 在uicr_xmcreate_window中)..此類的完整代碼可以是在這裏看到:http://utilitybase.com/paste/26607

void uicr_xmcreate_window(window_t *win) 
{ 
    Cardinal n_args; 
    Arg  args[MAX_ARGS]; 

    Widget shell = NULL; 

//MORE STUFF HERE 

    memset(&(args), 0, sizeof(Arg)*MAX_ARGS); n_args = 0; 
    if (win->attributes != PRIMARY_WINDOW) { 
    XtSetArg(args[n_args], XmNtopAttachment, XmATTACH_FORM); n_args++; 
    XtSetArg(args[n_args], XmNbottomAttachment, XmATTACH_FORM); n_args++; 
    XtSetArg(args[n_args], XmNleftAttachment, XmATTACH_FORM); n_args++; 
    XtSetArg(args[n_args], XmNrightAttachment, XmATTACH_FORM); n_args++; 
    XtSetArg(args[n_args], XmNtopOffset, 0); n_args++; 
    XtSetArg(args[n_args], XmNbottomOffset, 0); n_args++; 
    XtSetArg(args[n_args], XmNleftOffset, 0); n_args++; 
    XtSetArg(args[n_args], XmNrightOffset, 0); n_args++; 
    } 
    win->main_window = XmCreateMainWindow(shell, win->name, args, n_args); 

編輯

我加在gdb斷點的函數調用之前的權利,並打印出一些值(不知道這將幫助 - 我是一個的n00b):

Breakpoint 1, uicr_xmcreate_window (win=0x9b378) 
    at /temp/gnat/Omnyx/Services/ABPUimLegacy/src/uim/create/uicr_xmcreate_window.c:179 
179 printf("%d",n_args); 
(gdb) p *win 
$1 = {struct_type = 1045, next = 0x0, hash_name = 971, 
    name = 0x994e8 "error_log", widget = 0x0, main_window = 0x0, workarea = 0x0, 
    menu_bar = 0x0, message_window = 0x0, window_RC = 0x0, working_box = 0, 
    working_identifier = 0x0, has_message_area = 0, pos = {x = 0, y = 0}, 
    illegal_char_set = 0x0, height = 0, width = 0, configured = 0, 
    actions = 0x0, title = 0x99d18, help_text = 0x0, groups = 0x0, 
    scroll_bars = 0x0, write_protect = 0, attributes = 0, initial_focus = { 
    obj_type = 0, obj_name = 0x0, area = 0x0}, text_edit = { 
    text_selected_widget = 0x0, text_focus_widget = 0x0, updated_widget = 0x0, 
    start = 0, end = 0, updated_text = 0x0, last_operation = 0}, 
    close_rqt = 0x0, kill_application_action_list_name = 0x0, parent = 0x0, 
    gen_objs = 0x99da0, panes = 0x0, table_list = 0x0, selected_table = 0x0, 
    dialogs = 0x0, has_been_loaded = 0 '\0', source_file_name = 0x0} 
(gdb) p *shell 
$2 = {core = {self = 0x9db50, widget_class = 0xfec7897c, parent = 0x0, 
    xrm_name = 466, being_destroyed = 0 '\0', destroy_callbacks = 0x9ae58, 
    constraints = 0x0, x = 0, y = 0, width = 0, height = 0, border_width = 1, 
    managed = 0 '\0', sensitive = 1 '\001', ancestor_sensitive = 1 '\001', 
    event_table = 0x9ad98, tm = {translations = 0x0, proc_table = 0x0, 
     current_state = 0x0, lastEventTime = 0}, accelerators = 0x0, 
    border_pixel = 0, border_pixmap = 2, popup_list = 0x0, num_popups = 0, 
    name = 0x7776e "TBM_Dialog_Fixed", screen = 0x7a7e0, colormap = 32, 
    window = 0, depth = 24, background_pixel = 12825262, 
    background_pixmap = 2, visible = 1 '\001', mapped_when_managed = 1 '\001'}} 
(gdb) p *args 
$3 = {name = 0x0, value = 0} 
(gdb) p *n_args 
Cannot access memory at address 0x0 
(gdb) p n_args 
$4 = 0 
(gdb) p args 
$5 = {{name = 0x0, value = 0} <repeats 20 times>} 
+3

那麼是什麼'n_args'初始化爲? – GManNickG 2010-02-19 04:48:53

+2

'shell'獲得的值不是'NULL'嗎? – 2010-02-19 04:49:42

+0

對不起,錯過了GDB的一部分。你能從GDB內打印回溯嗎?這應該給你變量和它們的值。在地址中尋找零(null)或其他奇怪的東西。 – Anycorn 2010-02-19 05:02:36

回答

5

假設dbx表示「試圖通過NULL指針讀取4個字節」,並且錯誤報告在函數調用的行中,而不是在XmCreateMainWindow函數中,我認爲這個問題與聲明有關win->name。這是C的這一行中唯一可以在函數實際調用之前從指針讀取的位置(它將讀取存儲在win->name中的數據的副本並將副本傳遞給該函數)。

嘗試插入上立即撥打以上線以下,以XmCreateMainWindow

assert(win != NULL); 

您需要#include <assert.h>,如果它是不是已經。這應該在調用函數時驗證win不是NULL指針。爲了徹底起見,您也可以爲shell添加類似的行。

1

我馬上就可以看到從看你的輸出是:

一)GDB的版本,你是使用已經過時了一年。

b)您提供的輸出都不是非常有用;你需要發佈一個回溯。

現在,回溯中的最後一項或程序實際停止的地方(本例中爲libXm)幾乎從未成爲問題的實際原因。假設libXm沒有錯,你真的需要一個適當的回溯來看看你寫的代碼的最後一個執行點;這是問題的更可能的來源。

還有一件事;學習使用GDB。如果你要編寫一個比幾行更長的程序,這是必要的。

+4

我從來沒有意識到GDB是易腐爛的。 – bk1e 2010-02-19 06:57:26

+0

感謝您的提示......僅供參考,這不是我寫的代碼,它的代碼從Forte c編譯器移到了GNAT ..我是整個GNAT世界的新手......試圖找出如何回溯現在.. – systemoutprintln 2010-02-19 13:48:00

+0

過時了一年?我們需要使用'GNU gdb 6.3.50-20050815(Apple版本gdb-1461)'! – kennytm 2010-02-20 06:48:14

1

你的問題在你的memset中。

Arg  args[MAX_ARGS]; 
... 
memset(&(args), 0, sizeof(Arg)*MAX_ARGS); n_args = 0; 

在這裏,你正在使用&(args),你應該使用args&args[0]args是一個指向數組開始的指針,而&(args)是一個指針指針。當您嘗試memset &(args)時,您正試圖在您不應寫入的內存範圍內寫入(可能)大量的零。在某個時候,該命令嘗試寫入它不應該觸及的內存範圍,並且系統將其殺死。

+0

我試過去改變它許多不同的方式,沒有運氣..我嘗試了args和&args [0] ...任何其他想法?謝謝您的幫助。 – systemoutprintln 2010-02-20 03:44:23

+2

args位於堆棧上,&args和&args [0]是相同的指針。這個構造有點奇怪(通常你會memset(args,sizeof(args)),但功能正常 - 它不會覆蓋任何東西。 – 2010-02-20 06:47:00

+1

@bta:不正確。&args'不是指針 - 指針,'&args'是'Arg(*)[MAX_ARGS]'類型的指針,即它是一個指向整個'args'數組的指針,在數值上,這個指針與'&args [0]'完全相同,即它會爲'memset'的第一個參數產生完全相同的'void *'值,它絕對沒有區別,使用哪種形式,在風格上,我更喜歡'memset(args,0,sizeof args)'或' memset(&args,0,sizeof args)',但上面寫的memset沒有問題 – AnT 2010-02-25 07:33:41

4

你的崩潰發生在libXm裏面。或者你的堆事先被損壞了,或者你傳遞了不好的數據(或者,更不可能的是,libXm和/或其他系統庫存在bug)。 args的東西看起來好像沒問題(雖然你的memset有點不標準)。

要測試堆損壞使用Valgrind,但我不確定它是否在Solaris上可用。你可以在Linux上建立你的軟件包,看看你是否在那裏得到同樣的崩潰。

(如果切換到Linux上,你可以很容易地安裝libXm調試符號+源,以找出其中的數據可能是想錯了。)

對於第三(不太可能)的可能性,檢查你在最新的補丁級別。

+0

Solaris上的Sun Studio dbx具有內置內存檢查功能 - 不像valgrind那樣多,但總比沒有好。查看http://stackoverflow.com/questions/1881343/locate-bad-memory-access-on-solaris – alanc 2010-02-20 06:57:20

+0

我添加了dbx輸出 – systemoutprintln 2010-02-21 16:51:17

+0

@Grasper,你enabl編輯「check -all」*後* segv。如果你正在尋找堆腐敗,你必須打破主要,並在* segv之前啓用*。也就是說,dbx並不像Valgrind一樣全面,所以它可能仍然不能檢測到真正的問題。 – 2010-02-22 17:32:10

1

將memset替換爲手動設置不同args的函數。 memset只能用於字節數組,因爲任何其他類型的0可能並不意味着你的想法。

1

通過gdb輸出可能很困難,所以無論是使用IDE還是嘗試自己查找錯誤。

段錯誤通常是由於:

  1. 當試圖取消引用NULL指針
  2. 修改字符串中,對於例如

     
    
        char *s = "string"; 
        *(s+1) = 's'; 
     
    
     
        Correct way would be to allocate memory to the pointer or use Character Array or use: 
    
        char *s = strdup("string"); 
        *(s+1) = 's'; 
     
    

  3. 試圖訪問已釋放/解除分配的內存。
  4. 試圖改變/訪問未分配給程序的內存。

+0

恕我直言,「gdb輸出」對任何值得他的鹽的程序員來說都是絕對可讀的。 – ggiroux 2010-02-26 01:08:58

+0

我特地寫了'可能很難':)。是的,它的重要性。 – 2010-02-26 01:39:56

1

你傳遞NULL作爲第一個參數XmCreateMainWindow(parent, ...)而在谷歌發現大部分樣本似乎傳遞一個非NULL Widget從XtVaAppInitialize未來()那裏,你確定,你不應該這樣做太?

做不到這一點,在gdb,將斷點上線180 win->main_window = XmCreateMainWindow(shell, win->name, args, n_args);

,並執行「P *取勝」的命令在那裏,輸出應該是有啓發性。應該被初始化的「贏」結構的成員可能是空的或垃圾的,這應該會讓你走。

+0

你可以看看我添加到我的問題更新的輸出? – systemoutprintln 2010-02-26 21:54:13

1

這可能是一個長鏡頭,但你嘗試過「拆解」在回溯那些內存地址?如果你對裝配有一些基本的瞭解,那麼你可能會以這種方式辨別出某種不合適的地方。正如幾個人提到的,你可能在某處訪問了一個錯誤的指針。有時候程序集會揭示哪個指針的地址是不正確的。這是我過去的正確方向。