1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

To remove the hardcoded "objdump" we had to lay

down some infrastructure.  This is base on ideas/patch
	from Chris Songer.  The idea is to define in the IToolsProvider
	the Objdump class and reuse it to implement IBinaryFile.getContents().
	Next step is to come up with ObjdumpEditor, to go this route
	will be more flexible.
This commit is contained in:
Alain Magloire 2004-01-20 21:13:28 +00:00
parent 14bf87311d
commit 2c72e31d52
10 changed files with 422 additions and 107 deletions

View file

@ -1,3 +1,23 @@
2004-01-20 Alain Magloire
To remove the hardcoded "objdump" we had to lay
down some infrastructure. This is base on ideas/patch
from Chris Songer. The idea is to define in the IToolsProvider
the Objdump class and reuse it to implement IBinaryFile.getContents().
Next step is to come up with ObjdumpEditor, to go this route
will be more flexible.
* model/org/eclipse/cdt/internal/core/model/Binary.java
* utils/org/eclipse/cdt/utils/CygwinToolsProvider.java
* utils/org/eclipse/cdt/utils/ToolsProvider.java
* utils/org/eclipse/cdt/utils/Objdump.java
* utils/org/eclipse/cdt/utils/IToolsProvider.java
* utils/org/eclipse/cdt/utils/coff/parser/CygwinPEParser.java
* utils/org/eclipse/cdt/utils/coff/parser/BinaryFile.java
* utils/org/eclipse/cdt/utils/elf/parser/BinaryFilejava
* utils/org/eclipse/cdt/utils/elf/parser/GNUElfParser.java
2004-01-19 John Camelon 2004-01-19 John Camelon
Updated CModelBuilder to access line number information from IASTOffsetableElement. Updated CModelBuilder to access line number information from IASTOffsetableElement.

View file

@ -5,6 +5,9 @@ package org.eclipse.cdt.internal.core.model;
* All Rights Reserved. * All Rights Reserved.
*/ */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -15,6 +18,7 @@ import org.eclipse.cdt.core.IBinaryParser.IBinaryShared;
import org.eclipse.cdt.core.IBinaryParser.ISymbol; import org.eclipse.cdt.core.IBinaryParser.ISymbol;
import org.eclipse.cdt.core.model.CModelException; import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.IBinary; import org.eclipse.cdt.core.model.IBinary;
import org.eclipse.cdt.core.model.IBuffer;
import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResource;
@ -271,4 +275,55 @@ public class Binary extends Openable implements IBinary {
//} //}
} }
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.model.Openable#openBuffer(org.eclipse.core.runtime.IProgressMonitor)
*/
protected IBuffer openBuffer(IProgressMonitor pm) throws CModelException {
// create buffer - translation units only use default buffer factory
BufferManager bufManager = getBufferManager();
IBuffer buffer = getBufferFactory().createBuffer(this);
if (buffer == null)
return null;
// set the buffer source
if (buffer.getCharacters() == null){
IBinaryFile bin = getBinaryFile();
if (bin != null) {
StringBuffer sb = new StringBuffer();
try {
BufferedReader stream = new BufferedReader(new InputStreamReader(bin.getContents()));
char[] buf = new char[512];
int len;
while ((len = stream.read(buf, 0, buf.length)) != -1) {
sb.append(buf, 0, len);
}
} catch (IOException e) {
// nothint.
}
buffer.setContents(sb.toString());
} else {
IResource file = this.getResource();
if (file != null && file.getType() == IResource.FILE) {
buffer.setContents(Util.getResourceContentsAsCharArray((IFile)file));
}
}
}
// add buffer to buffer cache
bufManager.addBuffer(buffer);
// listen to buffer changes
// buffer.addBufferChangedListener(this);
return buffer;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.model.Openable#hasBuffer()
*/
protected boolean hasBuffer() {
return true;
}
} }

View file

@ -0,0 +1,53 @@
/**********************************************************************
* Copyright (c) 2002,2003 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* QNX Software Systems - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.utils;
import java.io.IOException;
import org.eclipse.cdt.core.ICExtension;
import org.eclipse.cdt.core.ICExtensionReference;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
/*
* CygwinToolsProvider
*/
public class CygwinToolsProvider extends ToolsProvider implements ICygwinToolsProvider {
public CygwinToolsProvider(ICExtension cextension) {
super(cextension);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.utils.ICygwinToolsProvider#getCygPath()
*/
public CygPath getCygPath() {
IPath cygPathPath = getCygPathPath();
CygPath cygpath = null;
if (cygPathPath != null && !cygPathPath.isEmpty()) {
try {
cygpath = new CygPath(cygPathPath.toOSString());
} catch (IOException e1) {
}
}
return cygpath;
}
public IPath getCygPathPath() {
ICExtensionReference ref = getExtensionReference();
String value = ref.getExtensionData("cygpath"); //$NON-NLS-1
if (value == null || value.length() == 0) {
value = "cygpath"; //$NON-NLS-1
}
return new Path(value);
}
}

View file

@ -16,8 +16,24 @@ import org.eclipse.core.runtime.IPath;
*/ */
public interface IToolsProvider { public interface IToolsProvider {
/**
* Return Addr2Line for this executable
* @param path, the executable
* @return
*/
Addr2line getAddr2Line(IPath path); Addr2line getAddr2Line(IPath path);
/**
* Return CPPFilt to translate mangle names
* @return
*/
CPPFilt getCPPFilt(); CPPFilt getCPPFilt();
/**
* Return Objdump for this executable
* @param path, the executable
* @return
*/
Objdump getObjdump(IPath path);
} }

View file

@ -0,0 +1,80 @@
/**********************************************************************
* Copyright (c) 2002,2003 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* QNX Software Systems - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.eclipse.cdt.utils.spawner.ProcessFactory;
/*
* Objdump
*/
public class Objdump {
String[] args;
public Objdump(String command, String param, String file) throws IOException {
String[] params;
if (param == null || param.length() == 0) {
params = new String[0];
} else {
// FIXME: This is wrong we have to check for quoted strings.
List list = new ArrayList();
StringTokenizer st = new StringTokenizer(param);
while (st.hasMoreTokens()) {
list.add(st.nextToken());
}
params = new String[list.size()];
list.toArray(params);
}
init(command, params, file);
}
public Objdump(String command, String[] params, String file) throws IOException {
init(command, params, file);
}
public Objdump(String file) throws IOException {
this("objdump", new String[0], file);
}
void init(String command, String[] params, String file) throws IOException {
if (params == null || params.length == 0) {
args = new String[] { command, "-C", "-x", "-S", file };
} else {
args = new String[params.length + 1];
args[0] = command;
System.arraycopy(params, 0, args, 1, params.length);
}
}
public byte[] getOutput() throws IOException {
Process objdump = ProcessFactory.getFactory().exec(args);
StringBuffer buffer = new StringBuffer();
BufferedReader stdout = new BufferedReader(new InputStreamReader(objdump.getInputStream()));
char[] buf = new char[512];
int len;
while ((len = stdout.read(buf, 0, buf.length)) != -1) {
buffer.append(buf, 0, len);
}
stdout.close();
objdump.destroy();
return buffer.toString().getBytes();
}
public void dispose() {
}
}

View file

@ -0,0 +1,117 @@
/**********************************************************************
* Copyright (c) 2002,2003 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* QNX Software Systems - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.utils;
import java.io.IOException;
import org.eclipse.cdt.core.ICExtension;
import org.eclipse.cdt.core.ICExtensionReference;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
/*
* ToolsProvider
*/
public class ToolsProvider implements IToolsProvider {
ICExtension cextension;
public ToolsProvider(ICExtension cext) {
cextension = cext;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.utils.IToolsProvider#getAddr2Line(org.eclipse.core.runtime.IPath)
*/
public Addr2line getAddr2Line(IPath path) {
IPath addr2LinePath = getAddr2LinePath();
Addr2line addr2line = null;
if (addr2LinePath != null && !addr2LinePath.isEmpty()) {
try {
addr2line = new Addr2line(addr2LinePath.toOSString(), path.toOSString());
} catch (IOException e1) {
}
}
return addr2line;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.utils.IToolsProvider#getCPPFilt()
*/
public CPPFilt getCPPFilt() {
IPath cppFiltPath = getCPPFiltPath();
CPPFilt cppfilt = null;
if (cppFiltPath != null && ! cppFiltPath.isEmpty()) {
try {
cppfilt = new CPPFilt(cppFiltPath.toOSString());
} catch (IOException e2) {
}
}
return cppfilt;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.utils.IToolsProvider#getObjdump(org.eclipse.core.runtime.IPath)
*/
public Objdump getObjdump(IPath path) {
IPath objdumpPath = getObjdumpPath();
String objdumpArgs = getObjdumpArgs();
Objdump objdump = null;
if (objdumpPath != null && !objdumpPath.isEmpty()) {
try {
objdump = new Objdump(objdumpPath.toOSString(), objdumpArgs, path.toOSString());
} catch (IOException e1) {
}
}
return objdump;
}
ICExtensionReference getExtensionReference() {
return cextension.getExtensionReference();
}
IPath getAddr2LinePath() {
ICExtensionReference ref = getExtensionReference();
String value = ref.getExtensionData("addr2line"); //$NON-NLS-1
if (value == null || value.length() == 0) {
value = "addr2line"; //$NON-NLS-1
}
return new Path(value);
}
IPath getObjdumpPath() {
ICExtensionReference ref = getExtensionReference();
String value = ref.getExtensionData("objdump"); //$NON-NLS-1
if (value == null || value.length() == 0) {
value = "objdump"; //$NON-NLS-1
}
return new Path(value);
}
String getObjdumpArgs() {
ICExtensionReference ref = getExtensionReference();
String value = ref.getExtensionData("objdumpArgs"); //$NON-NLS-1
if (value == null || value.length() == 0) {
value = ""; //$NON-NLS-1
}
return value;
}
IPath getCPPFiltPath() {
ICExtensionReference ref = getExtensionReference();
String value = ref.getExtensionData("c++filt"); //$NON-NLS-1
if (value == null || value.length() == 0) {
value = "c++filt"; //$NON-NLS-1
}
return new Path(value);
}
}

View file

@ -20,6 +20,7 @@ import org.eclipse.cdt.utils.Addr2line;
import org.eclipse.cdt.utils.CPPFilt; import org.eclipse.cdt.utils.CPPFilt;
import org.eclipse.cdt.utils.CygPath; import org.eclipse.cdt.utils.CygPath;
import org.eclipse.cdt.utils.ICygwinToolsProvider; import org.eclipse.cdt.utils.ICygwinToolsProvider;
import org.eclipse.cdt.utils.Objdump;
import org.eclipse.cdt.utils.coff.PE.Attribute; import org.eclipse.cdt.utils.coff.PE.Attribute;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.PlatformObject; import org.eclipse.core.runtime.PlatformObject;
@ -59,6 +60,13 @@ public abstract class BinaryFile extends PlatformObject implements IBinaryFile {
return null; return null;
} }
public Objdump getObjdump() {
if (toolsProvider != null) {
return toolsProvider.getObjdump(path);
}
return null;
}
/** /**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryFile#getFile() * @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryFile#getFile()
*/ */
@ -77,9 +85,19 @@ public abstract class BinaryFile extends PlatformObject implements IBinaryFile {
public InputStream getContents() { public InputStream getContents() {
InputStream stream = null; InputStream stream = null;
if (path != null) { if (path != null) {
try { Objdump objdump = getObjdump();
stream = new FileInputStream(path.toFile()); if (objdump != null) {
} catch (IOException e) { try {
byte[] contents = objdump.getOutput();
stream = new ByteArrayInputStream(contents);
} catch (IOException e) {
// Nothing
}
} else {
try {
stream = new FileInputStream(path.toFile());
} catch (IOException e) {
}
} }
} }
if (stream == null) { if (stream == null) {

View file

@ -13,13 +13,13 @@ package org.eclipse.cdt.utils.coff.parser;
import java.io.IOException; import java.io.IOException;
import org.eclipse.cdt.core.IBinaryParser; import org.eclipse.cdt.core.IBinaryParser;
import org.eclipse.cdt.core.ICExtensionReference;
import org.eclipse.cdt.utils.Addr2line; import org.eclipse.cdt.utils.Addr2line;
import org.eclipse.cdt.utils.CPPFilt; import org.eclipse.cdt.utils.CPPFilt;
import org.eclipse.cdt.utils.CygPath; import org.eclipse.cdt.utils.CygPath;
import org.eclipse.cdt.utils.CygwinToolsProvider;
import org.eclipse.cdt.utils.ICygwinToolsProvider; import org.eclipse.cdt.utils.ICygwinToolsProvider;
import org.eclipse.cdt.utils.Objdump;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
/** /**
*/ */
@ -43,66 +43,31 @@ public class CygwinPEParser extends PEParser implements IBinaryParser, ICygwinTo
return "Cygwin PE"; return "Cygwin PE";
} }
public IPath getAddr2LinePath() { /* (non-Javadoc)
ICExtensionReference ref = getExtensionReference(); * @see org.eclipse.cdt.utils.ICygwinToolsProvider#getCygPath()
String value = ref.getExtensionData("addr2line"); //$NON-NLS-1 */
if (value == null || value.length() == 0) {
value = "addr2line"; //$NON-NLS-1
}
return new Path(value);
}
public IPath getCPPFiltPath() {
ICExtensionReference ref = getExtensionReference();
String value = ref.getExtensionData("c++filt"); //$NON-NLS-1
if (value == null || value.length() == 0) {
value = "c++filt"; //$NON-NLS-1
}
return new Path(value);
}
public IPath getCygPathPath() {
ICExtensionReference ref = getExtensionReference();
String value = ref.getExtensionData("cygpath"); //$NON-NLS-1
if (value == null || value.length() == 0) {
value = "cygpath"; //$NON-NLS-1
}
return new Path(value);
}
public Addr2line getAddr2Line(IPath execFile) {
IPath addr2LinePath = getAddr2LinePath();
Addr2line addr2line = null;
if (addr2LinePath != null && !addr2LinePath.isEmpty()) {
try {
addr2line = new Addr2line(addr2LinePath.toOSString(), execFile.toOSString());
} catch (IOException e1) {
}
}
return addr2line;
}
public CPPFilt getCPPFilt() {
IPath cppFiltPath = getCPPFiltPath();
CPPFilt cppfilt = null;
if (cppFiltPath != null && ! cppFiltPath.isEmpty()) {
try {
cppfilt = new CPPFilt(cppFiltPath.toOSString());
} catch (IOException e2) {
}
}
return cppfilt;
}
public CygPath getCygPath() { public CygPath getCygPath() {
IPath cygPathPath = getCygPathPath(); return new CygwinToolsProvider(this).getCygPath();
CygPath cygpath = null; }
if (cygPathPath != null && !cygPathPath.isEmpty()) {
try { /* (non-Javadoc)
cygpath = new CygPath(cygPathPath.toOSString()); * @see org.eclipse.cdt.utils.IToolsProvider#getAddr2Line(org.eclipse.core.runtime.IPath)
} catch (IOException e1) { */
} public Addr2line getAddr2Line(IPath path) {
} return new CygwinToolsProvider(this).getAddr2Line(path);
return cygpath; }
/* (non-Javadoc)
* @see org.eclipse.cdt.utils.IToolsProvider#getCPPFilt()
*/
public CPPFilt getCPPFilt() {
return new CygwinToolsProvider(this).getCPPFilt();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.utils.IToolsProvider#getObjdump(org.eclipse.core.runtime.IPath)
*/
public Objdump getObjdump(IPath path) {
return new CygwinToolsProvider(this).getObjdump(path);
} }
} }

View file

@ -16,9 +16,10 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.eclipse.cdt.core.IBinaryParser.IBinaryFile; import org.eclipse.cdt.core.IBinaryParser.IBinaryFile;
import org.eclipse.cdt.utils.*;
import org.eclipse.cdt.utils.Addr2line; import org.eclipse.cdt.utils.Addr2line;
import org.eclipse.cdt.utils.CPPFilt; import org.eclipse.cdt.utils.CPPFilt;
import org.eclipse.cdt.utils.IToolsProvider;
import org.eclipse.cdt.utils.Objdump;
import org.eclipse.cdt.utils.elf.Elf.Attribute; import org.eclipse.cdt.utils.elf.Elf.Attribute;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.PlatformObject; import org.eclipse.core.runtime.PlatformObject;
@ -52,6 +53,12 @@ public abstract class BinaryFile extends PlatformObject implements IBinaryFile {
return null; return null;
} }
public Objdump getObjdump() {
if (toolsProvider != null) {
return toolsProvider.getObjdump(path);
}
return null;
}
/** /**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryFile#getFile() * @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryFile#getFile()
@ -71,9 +78,19 @@ public abstract class BinaryFile extends PlatformObject implements IBinaryFile {
public InputStream getContents() { public InputStream getContents() {
InputStream stream = null; InputStream stream = null;
if (path != null) { if (path != null) {
try { Objdump objdump = getObjdump();
stream = new FileInputStream(path.toFile()); if (objdump != null) {
} catch (IOException e) { try {
byte[] contents = objdump.getOutput();
stream = new ByteArrayInputStream(contents);
} catch (IOException e) {
// Nothing
}
} else {
try {
stream = new FileInputStream(path.toFile());
} catch (IOException e) {
}
} }
} }
if (stream == null) { if (stream == null) {

View file

@ -13,17 +13,18 @@ package org.eclipse.cdt.utils.elf.parser;
import java.io.IOException; import java.io.IOException;
import org.eclipse.cdt.core.IBinaryParser; import org.eclipse.cdt.core.IBinaryParser;
import org.eclipse.cdt.core.ICExtensionReference;
import org.eclipse.cdt.utils.*;
import org.eclipse.cdt.utils.Addr2line; import org.eclipse.cdt.utils.Addr2line;
import org.eclipse.cdt.utils.CPPFilt; import org.eclipse.cdt.utils.CPPFilt;
import org.eclipse.cdt.utils.IToolsProvider;
import org.eclipse.cdt.utils.Objdump;
import org.eclipse.cdt.utils.ToolsProvider;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
/** /**
*/ */
public class GNUElfParser extends ElfParser implements IBinaryParser, IToolsProvider { public class GNUElfParser extends ElfParser implements IBinaryParser, IToolsProvider {
/** /**
* @see org.eclipse.cdt.core.model.IBinaryParser#getBinary(IPath) * @see org.eclipse.cdt.core.model.IBinaryParser#getBinary(IPath)
*/ */
@ -42,46 +43,19 @@ public class GNUElfParser extends ElfParser implements IBinaryParser, IToolsProv
return "GNU ELF"; return "GNU ELF";
} }
public IPath getAddr2LinePath() {
ICExtensionReference ref = getExtensionReference();
String value = ref.getExtensionData("addr2line"); //$NON-NLS-1
if (value == null || value.length() == 0) {
value = "addr2line"; //$NON-NLS-1
}
return new Path(value);
}
public IPath getCPPFiltPath() {
ICExtensionReference ref = getExtensionReference();
String value = ref.getExtensionData("c++filt"); //$NON-NLS-1
if (value == null || value.length() == 0) {
value = "c++filt"; //$NON-NLS-1
}
return new Path(value);
}
public Addr2line getAddr2Line(IPath path) { public Addr2line getAddr2Line(IPath path) {
IPath addr2LinePath = getAddr2LinePath(); ToolsProvider provider = new ToolsProvider(this);
Addr2line addr2line = null; return provider.getAddr2Line(path);
if (addr2LinePath != null && !addr2LinePath.isEmpty()) { }
try {
addr2line = new Addr2line(addr2LinePath.toOSString(), path.toOSString()); public Objdump getObjdump(IPath path) {
} catch (IOException e1) { ToolsProvider provider = new ToolsProvider(this);
} return provider.getObjdump(path);
}
return addr2line;
} }
public CPPFilt getCPPFilt() { public CPPFilt getCPPFilt() {
IPath cppFiltPath = getCPPFiltPath(); return new ToolsProvider(this).getCPPFilt();
CPPFilt cppfilt = null;
if (cppFiltPath != null && ! cppFiltPath.isEmpty()) {
try {
cppfilt = new CPPFilt(cppFiltPath.toOSString());
} catch (IOException e2) {
}
}
return cppfilt;
} }
} }