com.pdk.DisassembleElfAction.java Source code

Java tutorial

Introduction

Here is the source code for com.pdk.DisassembleElfAction.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pdk;

import com.peterswing.CommonLib;
import com.peterswing.advancedswing.jprogressbardialog.JProgressBarDialog;
import com.peterswing.advancedswing.jprogressbardialog.JProgressBarDialogEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.apache.commons.io.FileUtils;
import org.netbeans.api.project.Project;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.loaders.DataObject;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;
import org.openide.util.Utilities;

@ActionID(category = "PDK", id = "com.pdk.DisassembleElfAction")
@ActionRegistration(iconBase = "com/pdk/page_green.png", displayName = "#CTL_DisassembleElfAction")
@ActionReference(path = "Toolbars/PDK", position = 4)
@Messages("CTL_DisassembleElfAction=Disassemble Elf")
public final class DisassembleElfAction implements ActionListener, JProgressBarDialogEventListener {

    private boolean stop;
    Process process;
    StringBuilder lines;

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            Lookup context = Utilities.actionsGlobalContext();
            DataObject file = context.lookup(DataObject.class);
            if (file == null) {
                showList();
            } else {
                File fileObj = new File(file.getPrimaryFile().getPath());
                if (!fileObj.getName().endsWith(".o")) {
                    showList();
                } else {
                    File dir = new File(file.getFolder().getPrimaryFile().getPath());
                    Object[] options = { "Disasm", "Sections", "Cancel" };
                    int n = JOptionPane.showOptionDialog(null, "Please select", "Question",
                            JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,
                            options[2]);

                    if (n == 2) {
                        return;
                    }

                    String command2 = null;
                    if (n == 0) {
                        command2 = "i586-peter-elf-objdump -S";
                    } else if (n == 1) {
                        command2 = "i586-peter-elf-readelf -S";
                    }
                    if (command2 == null) {
                        return;
                    }
                    final String command = "/toolchain/bin/" + command2 + " " + fileObj.getName();

                    process = Runtime.getRuntime().exec(command, null, dir);
                    InputStreamReader isr = new InputStreamReader(process.getInputStream());
                    final BufferedReader br = new BufferedReader(isr, 1024 * 1024 * 50);
                    final JProgressBarDialog d = new JProgressBarDialog(new JFrame(),
                            "i586-peter-elf-objdump -S " + fileObj.getName(), true);

                    d.progressBar.setIndeterminate(true);
                    d.progressBar.setStringPainted(true);
                    d.progressBar.setString("Updating");
                    d.addCancelEventListener(this);
                    Thread longRunningThread = new Thread() {
                        public void run() {
                            try {
                                lines = new StringBuilder();
                                String line;
                                stop = false;
                                while (!stop && (line = br.readLine()) != null) {
                                    d.progressBar.setString(line);
                                    lines.append(line).append("\n");
                                }
                                DisassembleDialog disassembleDialog = new DisassembleDialog();
                                disassembleDialog.setTitle(command);
                                if (((DefaultComboBoxModel) disassembleDialog.enhancedTextArea1.fontComboBox
                                        .getModel()).getIndexOf("Monospaced") != -1) {
                                    disassembleDialog.enhancedTextArea1.fontComboBox.setSelectedItem("Monospaced");
                                }
                                if (((DefaultComboBoxModel) disassembleDialog.enhancedTextArea1.fontComboBox
                                        .getModel()).getIndexOf("Monospaced.plain") != -1) {
                                    disassembleDialog.enhancedTextArea1.fontComboBox
                                            .setSelectedItem("Monospaced.plain");
                                }
                                disassembleDialog.enhancedTextArea1.setText(lines.toString());
                                disassembleDialog.setVisible(true);
                            } catch (Exception ex) {
                                ModuleLib.log(CommonLib.printException(ex));
                            }
                        }
                    };
                    d.thread = longRunningThread;
                    d.setVisible(true);
                }
            }
        } catch (Exception ex) {
            ModuleLib.log(CommonLib.printException(ex));
        }
    }

    @Override
    public void cancelled() {
        process.destroy();
        stop = true;
        DisassembleDialog disassembleDialog = new DisassembleDialog();
        if (((DefaultComboBoxModel) disassembleDialog.enhancedTextArea1.fontComboBox.getModel())
                .getIndexOf("Monospaced") != -1) {
            disassembleDialog.enhancedTextArea1.fontComboBox.setSelectedItem("Monospaced");
        }
        if (((DefaultComboBoxModel) disassembleDialog.enhancedTextArea1.fontComboBox.getModel())
                .getIndexOf("Monospaced.plain") != -1) {
            disassembleDialog.enhancedTextArea1.fontComboBox.setSelectedItem("Monospaced.plain");
        }
        disassembleDialog.enhancedTextArea1.setText(lines.toString());
        disassembleDialog.setVisible(true);
    }

    private void showList() {
        Project project = NetbeansUtil.getPeterProject();
        File dir = new File(project.getProjectDirectory().getPath());
        Collection<File> files = FileUtils.listFiles(dir, new String[] { "o", "exe" }, true);
        if (files.isEmpty()) {
            return;
        }
        files.add(new File(dir.getAbsolutePath() + File.separator + "kernel/kernel"));
        Iterator<File> iter = files.iterator();
        while (iter.hasNext()) {
            File file = iter.next();
            String relativePath = file.getAbsolutePath().substring(dir.getAbsolutePath().length());
            if (relativePath.startsWith("/toolchain/")) {
                iter.remove();
            }
        }
        String[] arr = new String[files.size()];
        iter = files.iterator();
        int x = 0;
        while (iter.hasNext()) {
            File file = iter.next();
            String relativePath = file.getAbsolutePath().substring(dir.getAbsolutePath().length() + 1);
            arr[x] = relativePath;
            x++;
        }
        Arrays.sort(arr, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if (o1.startsWith("kernel/")) {
                    return -1;
                } else if (o1.startsWith("kernel/")) {
                    return 1;
                } else {
                    return o1.compareTo(o2);
                }
            }
        });

        String file = (String) JOptionPane.showInputDialog(null, null, "Please select an elf file",
                JOptionPane.QUESTION_MESSAGE, null, arr, arr[0]);
        if (file != null) {
            try {
                Object[] options = { "Disasm", "Sections", "Cancel" };
                int n = JOptionPane.showOptionDialog(null, "Please select", "Question",
                        JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]);

                if (n == 2) {
                    return;
                }
                String command2 = null;
                if (n == 0) {
                    command2 = "i586-peter-elf-objdump -S";
                } else if (n == 1) {
                    command2 = "i586-peter-elf-readelf -S";
                }
                if (command2 == null) {
                    return;
                }
                final String command = "/toolchain/bin/" + command2 + " " + file;

                process = Runtime.getRuntime().exec(command, null, dir);
                InputStreamReader isr = new InputStreamReader(process.getInputStream());
                final BufferedReader br = new BufferedReader(isr, 1024 * 1024 * 50);
                final JProgressBarDialog d = new JProgressBarDialog(new JFrame(),
                        "i586-peter-elf-objdump -S " + file, true);

                d.progressBar.setIndeterminate(true);
                d.progressBar.setStringPainted(true);
                d.progressBar.setString("Updating");
                d.addCancelEventListener(this);
                Thread longRunningThread = new Thread() {
                    public void run() {
                        try {
                            lines = new StringBuilder();
                            String line;
                            stop = false;
                            while (!stop && (line = br.readLine()) != null) {
                                d.progressBar.setString(line);
                                lines.append(line).append("\n");
                            }
                            DisassembleDialog disassembleDialog = new DisassembleDialog();
                            disassembleDialog.setTitle(command);
                            if (((DefaultComboBoxModel) disassembleDialog.enhancedTextArea1.fontComboBox.getModel())
                                    .getIndexOf("Monospaced") != -1) {
                                disassembleDialog.enhancedTextArea1.fontComboBox.setSelectedItem("Monospaced");
                            }
                            if (((DefaultComboBoxModel) disassembleDialog.enhancedTextArea1.fontComboBox.getModel())
                                    .getIndexOf("Monospaced.plain") != -1) {
                                disassembleDialog.enhancedTextArea1.fontComboBox
                                        .setSelectedItem("Monospaced.plain");
                            }
                            disassembleDialog.enhancedTextArea1.fontComboBox.setSelectedItem("Monospaced");
                            disassembleDialog.enhancedTextArea1.setText(lines.toString());
                            disassembleDialog.setVisible(true);
                        } catch (Exception ex) {
                            ModuleLib.log(CommonLib.printException(ex));
                        }
                    }
                };
                d.thread = longRunningThread;
                d.setVisible(true);
            } catch (Exception ex) {
                ModuleLib.log(CommonLib.printException(ex));
            }
        }
    }
}