# bjvmdebug.cc

Go to the documentation of this file.

    00001 ///
    00002 /// \file       bjvmdebug.cc
    00003 ///
    00004 ///
    00005
    00006 /*
    00007     Copyright (C) 2008-2009, Nicolas VIVIEN
    00008     Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
    00009
    00010     This program is free software; you can redistribute it and/or modify
    00011     it under the terms of the GNU General Public License as published by
    00012     the Free Software Foundation; either version 2 of the License, or
    00013     (at your option) any later version.
    00014
    00015     This program is distributed in the hope that it will be useful,
    00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00018
    00019     See the GNU General Public License in the COPYING file at the
    00020     root directory of this project for more details.
    00021 */
    00022
    00023
    00024 #include <barry/barry.h>
    00025 #include <iostream>
    00026 #include <vector>
    00027 #include <string>
    00028 #include <cstring>
    00029 #include <algorithm>
    00030 #include <getopt.h>
    00031 #include <fstream>
    00032 #include <string.h>
    00033 #include "i18n.h"
    00034
    00035 using namespace std;
    00036 using namespace Barry;
    00037
    00038 void Usage()
    00039 {
    00040    int major, minor;
    00041    const char *Version = Barry::Version(major, minor);
    00042
    00043    cerr
    00044    << "bjvmdebug - Command line USB Blackberry Java Debugger\n"
    00045    << "        Copyright 2008-2009, Nicolas VIVIEN.\n"
    00046    << "        Copyright 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)\n"
    00047    << "        Using: " << Version << "\n"
    00048    << "\n"
    00049    << "   -h        This help\n"
    00050    << "   -p pin    PIN of device to talk with\n"
    00051    << "             If only one device is plugged in, this flag is optional\n"
    00052    << "   -P pass   Simplistic method to specify device password\n"
    00053    << "   -v        Dump protocol data during operation\n"
    00054    << endl;
    00055 }
    00056
    00057
    00058 int main(int argc, char *argv[])
    00059 {
    00060         INIT_I18N(PACKAGE);
    00061
    00062         try {
    00063
    00064                 uint32_t pin = 0;
    00065                 bool data_dump = false;
    00066                 string password;
    00067                 vector<string> params;
    00068                 string iconvCharset;
    00069
    00070                 // process command line options
    00071                 for(;;) {
    00072                         int cmd = getopt(argc, argv, "hp:P:v");
    00073                         if( cmd == -1 )
    00074                                 break;
    00075
    00076                         switch( cmd )
    00077                         {
    00078                         case 'p':       // Blackberry PIN
    00079                                 pin = strtoul(optarg, NULL, 16);
    00080                                 break;
    00081
    00082                         case 'P':       // Device password
    00083                                 password = optarg;
    00084                                 break;
    00085
    00086                         case 'v':       // data dump on
    00087                                 data_dump = true;
    00088                                 break;
    00089
    00090                         case 'h':       // help
    00091                         default:
    00092                                 Usage();
    00093                                 return 0;
    00094                         }
    00095                 }
    00096
    00097                 // Initialize the barry library.  Must be called before
    00098                 // anything else.
    00099                 Barry::Init(data_dump);
    00100
    00101                 // Probe the USB bus for Blackberry devices and display.
    00102                 // If user has specified a PIN, search for it in the
    00103                 // available device list here as well
    00104                 Barry::Probe probe;
    00105                 int activeDevice = probe.FindActive(pin);
    00106                 if( activeDevice == -1 ) {
    00107                         cerr << "No device selected, or PIN not found" << endl;
    00108                         return 1;
    00109                 }
    00110
    00111                 // Create our controller object
    00112                 Barry::Controller con(probe.Get(activeDevice));
    00113                 Barry::Mode::JVMDebug jvmdebug(con);
    00114
    00115                 //
    00116                 // execute each mode that was turned on
    00117                 //
    00118                 jvmdebug.Open(password.c_str());
    00119                 jvmdebug.Attach();
    00120
    00121                 // ...Unit tests...
    00122                 jvmdebug.Unknown01();
    00123                 jvmdebug.Unknown02();
    00124                 jvmdebug.Unknown03();
    00125                 jvmdebug.Unknown04();
    00126                 jvmdebug.Unknown05();
    00127
    00128                 {
    00129                         cout << "Java Modules List :" << endl;
    00130                         JVMModulesList list;
    00131                         jvmdebug.GetModulesList(list);
    00132                         cout << list;
    00133                 }
    00134
    00135                 {
    00136                         cout << "Java Threads currently running :" << endl;
    00137                         JVMThreadsList list;
    00138                         jvmdebug.GetThreadsList(list);
    00139                         cout << list;
    00140                 }
    00141
    00142                 jvmdebug.Unknown06();
    00143                 jvmdebug.Unknown07();
    00144                 jvmdebug.Unknown08();
    00145                 jvmdebug.Unknown09();
    00146                 jvmdebug.Unknown10();
    00147
    00148                 jvmdebug.Go();
    00149
    00150                 for (int i=0; i<20; i++) {
    00151                         int ret;
    00152                         string msg;
    00153                         ret = jvmdebug.GetConsoleMessage(msg);
    00154
    00155                         if (ret < 0) {
    00156                                 int status;
    00157                                 jvmdebug.GetStatus(status);
    00158                         }
    00159                         else {
    00160                                 cout << "JVM message : " << msg << endl;
    00161                         }
    00162                 }
    00163
    00164                 // End of session
    00165                 jvmdebug.Detach();
    00166         }
    00167         catch( Usb::Error &ue) {
    00168                 std::cout << endl;      // flush any normal output first
    00169                 std::cerr << "Usb::Error caught: " << ue.what() << endl;
    00170                 return 1;
    00171         }
    00172         catch( Barry::Error &se ) {
    00173                 std::cout << endl;
    00174                 std::cerr << "Barry::Error caught: " << se.what() << endl;
    00175                 return 1;
    00176         }
    00177         catch( std::exception &e ) {
    00178                 std::cout << endl;
    00179                 std::cerr << "std::exception caught: " << e.what() << endl;
    00180                 return 1;
    00181         }
    00182
    00183         return 0;
    00184 }
    00185