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