Update JDWP documentation (USB and JDWP protocol)
[barry.git] / tools / bjavadebug.cc
blob409a092a0a8efe233385c543bce1efdf19ea87a7
1 ///
2 /// \file bjavadebug.cc
3 ///
4 ///
6 /*
7 Copyright (C) 2008-2009, Nicolas VIVIEN
8 Copyright (C) 2005-2009, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
24 #include <barry/barry.h>
25 #include <iostream>
26 #include <vector>
27 #include <string>
28 #include <cstring>
29 #include <algorithm>
30 #include <getopt.h>
31 #include <fstream>
32 #include <string.h>
34 using namespace std;
35 using namespace Barry;
37 void Usage()
39 int major, minor;
40 const char *Version = Barry::Version(major, minor);
42 cerr
43 << "bjavadebug - Command line USB Blackberry Java Debugger\n"
44 << " Copyright 2008-2009, Nicolas VIVIEN.\n"
45 << " Copyright 2005-2009, Net Direct Inc. (http://www.netdirect.ca/)\n"
46 << " Using: " << Version << "\n"
47 << "\n"
48 << " -h This help\n"
49 << " -p pin PIN of device to talk with\n"
50 << " If only one device is plugged in, this flag is optional\n"
51 << " -P pass Simplistic method to specify device password\n"
52 << " -v Dump protocol data during operation\n"
53 << endl;
57 int main(int argc, char *argv[])
59 try {
61 uint32_t pin = 0;
62 bool data_dump = false;
63 string password;
64 vector<string> params;
65 string iconvCharset;
67 // process command line options
68 for(;;) {
69 int cmd = getopt(argc, argv, "hp:P:v");
70 if( cmd == -1 )
71 break;
73 switch( cmd )
75 case 'p': // Blackberry PIN
76 pin = strtoul(optarg, NULL, 16);
77 break;
79 case 'P': // Device password
80 password = optarg;
81 break;
83 case 'v': // data dump on
84 data_dump = true;
85 break;
87 case 'h': // help
88 default:
89 Usage();
90 return 0;
94 // Initialize the barry library. Must be called before
95 // anything else.
96 Barry::Init(data_dump);
98 // Probe the USB bus for Blackberry devices and display.
99 // If user has specified a PIN, search for it in the
100 // available device list here as well
101 Barry::Probe probe;
102 int activeDevice = probe.FindActive(pin);
103 if( activeDevice == -1 ) {
104 cerr << "No device selected, or PIN not found" << endl;
105 return 1;
108 // Create our controller object
109 Barry::Controller con(probe.Get(activeDevice));
110 Barry::Mode::JavaDebug javadebug(con);
113 // execute each mode that was turned on
115 javadebug.Open(password.c_str());
116 javadebug.Attach();
118 // ...Unit tests...
119 javadebug.Unknown01();
120 javadebug.Unknown02();
121 javadebug.Unknown03();
122 javadebug.Unknown04();
123 javadebug.Unknown05();
126 cout << "Java Modules List :" << endl;
127 JDModulesList list;
128 javadebug.GetModulesList(list);
129 cout << list;
133 cout << "Java Threads currently running :" << endl;
134 JDThreadsList list;
135 javadebug.GetThreadsList(list);
136 cout << list;
139 javadebug.Unknown06();
140 javadebug.Unknown07();
141 javadebug.Unknown08();
142 javadebug.Unknown09();
143 javadebug.Unknown10();
145 javadebug.Go();
147 for (int i=0; i<20; i++) {
148 int ret;
149 string msg;
150 ret = javadebug.GetConsoleMessage(msg);
152 if (ret < 0) {
153 int status;
154 javadebug.GetStatus(status);
156 else {
157 cout << "JVM message : " << msg << endl;
161 // End of session
162 javadebug.Detach();
164 catch( Usb::Error &ue) {
165 std::cout << endl; // flush any normal output first
166 std::cerr << "Usb::Error caught: " << ue.what() << endl;
167 return 1;
169 catch( Barry::Error &se ) {
170 std::cout << endl;
171 std::cerr << "Barry::Error caught: " << se.what() << endl;
172 return 1;
174 catch( std::exception &e ) {
175 std::cout << endl;
176 std::cerr << "std::exception caught: " << e.what() << endl;
177 return 1;
180 return 0;