Skip to content

Conversation

@AODtorusan
Copy link

Hi,

It would be nice if BridJ would not only extract the shared libraries, but also there companion files (files on which the library could depend). This would allow shipping/extracting additional debug symbols (pdb or gdb files) in the jar which would then be picked-up automatically by debugging tools.

For example extract both lib/win32/myLibrary.dll and lib/win32/myLibrary.pdb.

I'm willing to take a stab at this feature, but at first glance it would require some large changes in the Platform class. Any ideas or opinions on this?

Simon

@sarxos
Copy link
Contributor

sarxos commented Mar 20, 2014

Hi,

Wouldn't it be enough to change the method extracting *.dll or *.so from the JAR? I'm not very familiar with the native tools, but my assumption is that if gdb/pdb is extracted in the same directory as dll and has the same name (except the extension), the debugger will pick it automatically? I'm I correct? If this is the case, it should be enough to change this method from Platform.java:

static File extractEmbeddedLibraryResource(String name) throws IOException {
  // code
}

So it extract gdb/pdb files along with the library and put them in the same directory.

Just my two cents.

@AODtorusan
Copy link
Author

Hi

I experimented a bit, and I got a working solution. However its not every flexible as it has the hardcoded pdb and gdb extensions in them.

I basically added the extractEmbeddedResource method and changed the existing method extractEmbeddedLibraryResource to Platform.java:

static File extractEmbeddedResource(String libraryResource) throws IOException {
        InputStream in = getResourceAsStream(libraryResource);
        if (in == null) {
            File f = new File(libraryResource);
            if (!f.exists()) {
                f = new File(f.getName());
            }
            if (f.exists()) {
                return f.getCanonicalFile();
            } else {
                return null;
            }
        }
        int len;
        byte[] b = new byte[8196];
        String fileName = new File(libraryResource).getName();
        File libFile = new File(extractedLibrariesTempDir, fileName);
        OutputStream out = new BufferedOutputStream(new FileOutputStream(libFile));
        while ((len = in.read(b)) > 0) {
            out.write(b, 0, len);
        }
        out.close();
        in.close();

        addTemporaryExtractedLibraryFileToDeleteOnExit(libFile);
        addTemporaryExtractedLibraryFileToDeleteOnExit(libFile.getParentFile());

        return libFile;
    }

    static File extractEmbeddedLibraryResource(String name) throws IOException {
        String firstLibraryResource = null;

        List<String> libraryResources = getEmbeddedLibraryResource(name);
        if (BridJ.veryVerbose) {
            BridJ.info("Library resources for " + name + ": " + libraryResources);
        }               
        for (String libraryResource : libraryResources) {
            if (firstLibraryResource == null) {
                firstLibraryResource = libraryResource;
            }
            File libFile = extractEmbeddedResource(libraryResource);

            // Check if the resource was successfully extracted
            if (libFile != null) {
                // Try an extract debug symbols
                String basePath = libraryResource.substring(0, libraryResource.lastIndexOf('.'));
                extractEmbeddedResource(basePath + ".pdb");
                extractEmbeddedResource(basePath + ".gdb");

                // Return the extracted shared library
                return libFile;
            }
        }
        return null;
    }

I guess it should be decided if this is good enough, or a more flexible solution is required before I start making a pull request. Perhaps a similar method to getPossibleFileNames but then for possible companion extensions.

@ochafik ochafik added the BridJ label Apr 7, 2014
@ochafik
Copy link
Member

ochafik commented Apr 7, 2014

Hi @AODtorusan , @sarxos ,

Sorry for the delay!
This approach sounds good to me, happy to review any PR :-)
I'm unfamiliar with GDB and its debug symbol files, do you have pointers to docs on how to generate them? (just found a mention to .debug files in https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html)

Cheers

@AODtorusan
Copy link
Author

Hi @ochafik

I've only really used the visual studio debug files (as I generally develop in windows), but I generate the gcc symbol file similar to the cmake process here:

https://github.com/OPM/opm-benchmarks/blob/master/cmake/Modules/UseDebugSymbols.cmake

I've also converted this issue into a pull request implementing this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants