Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello to everyone
I use ubuntu as an OS in Virtual Box(virtual machine). And my goal is to list all the pci devices that can be found in the system using c++ code. Here is a part of it:
C++
#include <stdio.h>
    #include <string.h>
    #include <sys/io.h>
    #include <stdlib.h>
    #define NOT_SPECIFIED "Not specified"

    int main()
    {
        int i, busid, devid;
        FILE * file = fopen("Pcibits.txt", "w+");
        for (busid = 0; busid < 256; busid++)
        {
            for (devid = 0; devid < 32; devid++)
            {
                unsigned int recvp, ven_id, dev_id, clid;
                unsigned int sendp =  (busid << 16) | (devid << 11) | ((unsigned int)0x80000000);
                outl(sendp, 0x0CF8);
                recvp = inl(0x0CFC);
                char* str = new char[32];
                sprintf(str, "%d", recvp);
                fwrite(str, sizeof(char), sizeof(str)/sizeof(char), file);
                fclose(file);
            }
        }
        return 0;
    }



But using gdb I struggle with the error like this:

C++
 Traceback (most recent call last): File "/usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19-gdb.py", line 63, in <module>
        from libstdcxx.v6.printers import register_libstdcxx_printers
    ImportError: No module named 'libstdcxx'
<b
    Program received signal SIGSEGV, Segmentation fault.
    0x000000000040075a in outl (__value=2256549650, __port=3320)
        at /usr/include/x86_64-linux-gnu/sys/io.h:125
    125   __asm__ __volatile__ ("outl %0,%w1": :"a" (__value), "Nd" (__port));




What is wrong with it? As I realize the program fails on the outl-step.
Please help!)
Posted
Updated 10-Nov-15 23:51pm
v2
Comments
Richard MacCutchan 11-Nov-15 5:31am    
ImportError: No module named 'libstdcxx'
The message is quite clear, and has nothing to do with the title of your question. Nor do I see the connection between your C++ code and your Python code.

From the outl man page[^]:
Quote:
You use ioperm(2) or alternatively iopl(2) to tell the kernel to allow the user space application to access the I/O ports in question. Failure to do this will cause the application to receive a segmentation fault.
 
Share this answer
 
To access ports from code you have to enable permissions for I/O access using ioperm() [^].
Consider also that there are some issues with io.h and asm.h (see[^]). It is highly recommended to compile the code with optimization on when using I/O macros.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900