2015-12-21 36 views
0

中的分段錯誤我想將u3-工具(http://u3-tool.sourceforge.net/)的輸出轉換爲變量。 沒有重定向流的結果是:u3工具在ba​​sh管道中不起作用| C

$ echo "pass" | sudo -S sh -c "u3-tool -i /dev/sdc" 
Total device size: 1.88 GB (2022703104 bytes) 
Segmentation fault 

,但如果我的管道重定向到一個文件:echo "pass" | sudo -S sh -c "u3-tool -i /dev/sdc > /tmp/u3info 2>&1",然後我得到的只有第二行Segmentation fault

有沒有人有想法? 「stdout」中的「Total device size:1.88 GB(2022703104 bytes)」是什麼?

什麼是語言C的Segmentation fail?我如何解決它?

敬意和聖誕快樂!

使用是Linux Mint的17.3

+0

u3-tool-Fix:https://github.com/marcusrugger/u3-tool/pull/2/files – user4742258

回答

0

Segmentation fault消息被你的shell印刷,而不是你的程序,當this signal產生的,而不是抓住了,被一些程序,它試圖訪問之外是什麼內存分配給它。

現在,至於爲什麼你只在你的輸出得到段錯誤,我認爲有兩件事情發生在這裏:

  • 當你調用使用sh -c一個shell命令,你實際上調用命令在一個新的shell中。 seg錯誤消息來自新shell,而不是您正在使用的那個,並且當您將stdout和stderr與2>&1組合在一起時,那麼您也將獲得新shell的stderr。其中包含段錯誤消息。如果您直接在您已經使用的shell(即echo "pass" | sudo -S u3-tool -i /dev/sdc > /tmp/u3info 2>&1)中直接運行命令,那麼重定向應該像您期望的那樣工作。
  • 在輸出行完全緩衝並刷新到文件之前,seg故障很可能發生,請參閱this stackoverflow question。要回答爲什麼輸出行顯示在終端窗口中,而不是重定向到文件時,我必須誠實地說,我不知道爲什麼發生這種情況,但我可以通過一個簡單的程序在我的機器上驗證相同的行爲:

    #include <stdio.h>                
    
    void main (void)                 
    {                    
        char *p;   /* uninitialised pointer */        
        printf("hello\n"); /* print a line */          
        p[7] = 'a';  /* seg fault! */           
    } 
    

    這裏是當我編譯和測試它會發生什麼:

    enyquist$ gcc segfault.c -o segfault 
    enyquist$ ./segfault 
    hello 
    Segmentation fault 
    enyquist$ ./segfault > file 
    Segmentation fault 
    enyquist$ cat file 
    enyquist$ 
    

    來說明這個問題,我們實際上可以解決這個問題的代碼,以確保完整的郵件被刷新,做任何事情之前印刷否則,像這樣:

    #include <stdio.h>                
    
    void main (void)                 
    {                    
        char *p;   /* uninitialised pointer */        
        printf("hello\n"); /* print a line */ 
        fflush(stdout); /* flush stdout stream */          
        p[7] = 'a';  /* seg fault! */           
    } 
    

    和輸出....

    enyquist$ gcc segfault.c -o segfault 
    enyquist$ ./segfault 
    hello 
    Segmentation fault 
    enyquist$ ./segfault > file 
    Segmentation fault 
    enyquist$ cat file 
    hello 
    enyquist$ 
    

因此,要總結,如果修改您的shell命令一樣,我建議那我想重定向會像你期望的那樣,但沒有別的表現就像你期望的那樣,因爲你在某個地方遇到了seg故障,所以你需要找出是什麼原因造成的!

+0

爲什麼這不會像你期望的那樣工作(har har)?請訪問:http://askubuntu.com/a/470387 我不希望在日誌中出現'Segmentation fault',但我的重定向只會捕獲此消息,而不會顯示其他輸出,例如Total device size! – user4742258

+0

我的錯誤,我忽略了'-S'來sudo。你是對的,那會像你期望的那樣工作,我會從答案中解決這個問題。 –

+0

目前我沒有得到'Segmentation fault'消息,但是用命令'echo「pass」| sudo -S u3-tool -i/dev/sdc>/tmp/u3info 2>&1'我沒有在文件中獲得標準輸出。該文件目前爲空。 – user4742258

0

分段錯誤是指程序嘗試訪問內存時未被清除訪問的錯誤。當程序超出分配的數組或內存空間的邊界時,或者當它試圖訪問從分配的內存中初始化的元素時,會發生這種情況。

您只會在重定向後獲得第二行,因爲它打印在stderr上,而這是錯誤輸出在linux上。

至於錯誤,如果您確定該程序的參數是正確的,那麼如果您無法訪問源代碼,則唯一可以做的事情是向開發人員發放一張票。

+0

但是用命令'2&1',我應該把pipe stderr和stdout寫到日誌裏。我已經從這裏檢查了源代碼:http://archive.ubuntu.com/ubuntu/pool/universe/u/u3-tool/u3-tool_0.3-1.1.dsc但我沒有發現錯誤: 'printf(「Total device size:」); \t print_human_size(1 * U3_SECTOR_SIZE * device_properties.device_size); \t printf(「(%llu bytes)\ n」,1ll * U3_SECTOR_SIZE * device_properties.device_size);' – user4742258

+0

您可以用valgrind啓動程序並將日誌放在那裏? – loginn

+0

由於valgrind是工具庫,我不知道該怎麼做。 – user4742258