/
backend.py
executable file
·1013 lines (851 loc) · 32.2 KB
/
backend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
A server that provides a graphical user interface to the gnu debugger (gdb).
https://github.com/cs01/gdbgui
"""
import argparse
import binascii
from distutils.spawn import find_executable
from flask import (
Flask,
session,
request,
Response,
render_template,
jsonify,
redirect,
abort,
)
from flask_socketio import SocketIO, emit
from flask_compress import Compress
from functools import wraps
import json
import logging
import os
import platform
import pygdbmi
from pygments.lexers import get_lexer_for_filename
from pygdbmi.gdbcontroller import NoGdbProcessError
import re
import signal
import shlex
import sys
import socket
import traceback
import webbrowser
from werkzeug.security import pbkdf2_hex
pyinstaller_env_var_base_dir = "_MEIPASS"
pyinstaller_base_dir = getattr(sys, "_MEIPASS", None)
using_pyinstaller = pyinstaller_base_dir is not None
if using_pyinstaller:
BASE_PATH = pyinstaller_base_dir
else:
BASE_PATH = os.path.dirname(os.path.realpath(__file__))
PARENTDIR = os.path.dirname(BASE_PATH)
sys.path.append(PARENTDIR)
from gdbgui import htmllistformatter, __version__ # noqa
from gdbgui.statemanager import StateManager # noqa
try:
from gdbgui.SSLify import SSLify, get_ssl_context # noqa
except ImportError:
print("Warning: Optional SSL support is not available")
def get_ssl_context(private_key, certificate): # noqa
return None
USING_WINDOWS = os.name == "nt"
TEMPLATE_DIR = os.path.join(BASE_PATH, "templates")
GDBGUI_PREF_DIR = os.path.join(os.path.expanduser("~"), ".gdbgui")
STATIC_DIR = os.path.join(BASE_PATH, "static")
DEFAULT_HOST = "127.0.0.1"
DEFAULT_PORT = 5000
IS_A_TTY = sys.stdout.isatty()
DEFAULT_GDB_EXECUTABLE = "gdb"
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
class ColorFormatter(logging.Formatter):
def format(self, record):
color = "\033[1;0m"
if not USING_WINDOWS and sys.stdout.isatty():
if record.levelname == "WARNING":
color = "\33[93m" # yellow
elif record.levelname == "ERROR":
color = "\033[1;41m"
return "{color}{levelname}\033[1;0m - {msg}".format(color=color, **vars(record))
formatter = ColorFormatter()
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
# create dictionary of signal names
SIGNAL_NAME_TO_OBJ = {}
for n in dir(signal):
if n.startswith("SIG") and "_" not in n:
SIGNAL_NAME_TO_OBJ[n.upper()] = getattr(signal, n)
# Create flask application and add some configuration keys to be used in various callbacks
app = Flask(__name__, template_folder=TEMPLATE_DIR, static_folder=STATIC_DIR)
Compress(
app
) # add gzip compression to Flask. see https://github.com/libwilliam/flask-compress
app.config["initial_binary_and_args"] = []
app.config["gdb_path"] = DEFAULT_GDB_EXECUTABLE
app.config["gdb_cmd_file"] = None
app.config["show_gdbgui_upgrades"] = True
app.config["TEMPLATES_AUTO_RELOAD"] = True
app.config["LLDB"] = False # assume false, okay to change later
app.config["project_home"] = None
app.config["remap_sources"] = {}
app.config["rr"] = False
app.secret_key = binascii.hexlify(os.urandom(24)).decode("utf-8")
@app.before_request
def csrf_protect_all_post_and_cross_origin_requests():
"""returns None upon success"""
success = None
if is_cross_origin(request):
logger.warning("Received cross origin request. Aborting")
abort(403)
if request.method in ["POST", "PUT"]:
token = session.get("csrf_token")
if token == request.form.get("csrf_token"):
return success
elif token == request.environ.get("HTTP_X_CSRFTOKEN"):
return success
else:
logger.warning("Received invalid csrf token. Aborting")
abort(403)
def is_cross_origin(request):
"""Compare headers HOST and ORIGIN. Remove protocol prefix from ORIGIN, then
compare. Return true if they are not equal
example HTTP_HOST: '127.0.0.1:5000'
example HTTP_ORIGIN: 'http://127.0.0.1:5000'
"""
origin = request.environ.get("HTTP_ORIGIN")
host = request.environ.get("HTTP_HOST")
if origin is None:
# origin is sometimes omitted by the browser when origin and host are equal
return False
if origin.startswith("http://"):
origin = origin.replace("http://", "")
elif origin.startswith("https://"):
origin = origin.replace("https://", "")
return host != origin
def csrf_protect(f):
"""A decorator to add csrf protection by validing the X_CSRFTOKEN
field in request header"""
@wraps(f)
def wrapper(*args, **kwargs):
token = session.get("csrf_token", None)
if token is None or token != request.environ.get("HTTP_X_CSRFTOKEN"):
logger.warning("Received invalid csrf token. Aborting")
abort(403)
# call original request handler
return f(*args, **kwargs)
return wrapper
def add_csrf_token_to_session():
if "csrf_token" not in session:
session["csrf_token"] = binascii.hexlify(os.urandom(20)).decode("utf-8")
socketio = SocketIO()
_state = StateManager(app.config)
def setup_backend(
serve=True,
host=DEFAULT_HOST,
port=DEFAULT_PORT,
debug=False,
open_browser=True,
browsername=None,
testing=False,
private_key=None,
certificate=None,
LLDB=False,
):
"""Run the server of the gdb gui"""
app.config["LLDB"] = LLDB
kwargs = {}
ssl_context = get_ssl_context(private_key, certificate)
if ssl_context:
# got valid ssl context
# force everything through https
SSLify(app)
# pass ssl_context to flask
kwargs["ssl_context"] = ssl_context
url = "%s:%s" % (host, port)
if kwargs.get("ssl_context"):
protocol = "https://"
url_with_prefix = "https://" + url
else:
protocol = "http://"
url_with_prefix = "http://" + url
if debug:
async_mode = "eventlet"
else:
async_mode = "gevent"
socketio.server_options["async_mode"] = async_mode
try:
socketio.init_app(app)
except Exception:
print(
'failed to initialize socketio app with async mode "%s". Continuing with async mode "threading".'
% async_mode
)
socketio.server_options["async_mode"] = "threading"
socketio.init_app(app)
if testing is False:
if host == DEFAULT_HOST:
url = (DEFAULT_HOST, port)
else:
try:
url = (socket.gethostbyname(socket.gethostname()), port)
except Exception:
url = (host, port)
if open_browser is True and debug is False:
browsertext = repr(browsername) if browsername else "default browser"
args = (browsertext,) + url
text = ("Opening gdbgui with %s at " + protocol + "%s:%d") % args
print(colorize(text))
b = webbrowser.get(browsername) if browsername else webbrowser
b.open(url_with_prefix)
else:
print(colorize("View gdbgui at %s%s:%d" % (protocol, url[0], url[1])))
print("exit gdbgui by pressing CTRL+C")
try:
socketio.run(
app,
debug=debug,
port=int(port),
host=host,
extra_files=get_extra_files(),
**kwargs
)
except KeyboardInterrupt:
# Process was interrupted by ctrl+c on keyboard, show message
pass
def verify_gdb_exists(gdb_path):
if find_executable(gdb_path) is None:
pygdbmi.printcolor.print_red(
'gdb executable "%s" was not found. Verify the executable exists, or that it is a directory on your $PATH environment variable.'
% gdb_path
)
if USING_WINDOWS:
print(
'Install gdb (package name "mingw32-gdb") using MinGW (https://sourceforge.net/projects/mingw/files/Installer/mingw-get-setup.exe/download), then ensure gdb is on your "Path" environement variable: Control Panel > System Properties > Environment Variables > System Variables > Path'
)
else:
print('try "sudo apt-get install gdb" for Linux or "brew install gdb"')
sys.exit(1)
elif "lldb" in gdb_path.lower() and "lldb-mi" not in app.config["gdb_path"].lower():
pygdbmi.printcolor.print_red(
'gdbgui cannot use the standard lldb executable. You must use an executable with "lldb-mi" in its name.'
)
sys.exit(1)
def dbprint(*args):
"""print only if app.debug is truthy"""
if app and app.debug:
if USING_WINDOWS:
print("DEBUG: " + " ".join(args))
else:
CYELLOW2 = "\33[93m"
NORMAL = "\033[0m"
print(CYELLOW2 + "DEBUG: " + " ".join(args) + NORMAL)
def colorize(text):
if IS_A_TTY and not USING_WINDOWS:
return "\033[1;32m" + text + "\x1b[0m"
else:
return text
@socketio.on("connect", namespace="/gdb_listener")
def client_connected():
if is_cross_origin(request):
logger.warning("Received cross origin request. Aborting")
abort(403)
csrf_token = request.args.get("csrf_token")
if csrf_token is None:
logger.warning("Recieved invalid csrf token")
emit("server_error", {"message": "Recieved invalid csrf token"})
return
elif csrf_token != session.get("csrf_token"):
# this can happen fairly often, so log debug message, not warning
logger.debug(
"Recieved invalid csrf token %s (expected %s)"
% (csrf_token, str(session.get("csrf_token")))
)
emit(
"server_error", {"message": "Session expired. Please refresh this webpage."}
)
return
# see if user wants to connect to existing gdb pid
desired_gdbpid = int(request.args.get("gdbpid", 0))
payload = _state.connect_client(request.sid, desired_gdbpid)
logger.info(
'Client websocket connected in async mode "%s", id %s'
% (socketio.async_mode, request.sid)
)
# tell the client browser tab which gdb pid is a dedicated to it
emit("gdb_pid", payload)
# Make sure there is a reader thread reading. One thread reads all instances.
if _state.gdb_reader_thread is None:
_state.gdb_reader_thread = socketio.start_background_task(
target=read_and_forward_gdb_output
)
logger.info("Created background thread to read gdb responses")
@socketio.on("run_gdb_command", namespace="/gdb_listener")
def run_gdb_command(message):
"""
Endpoint for a websocket route.
Runs a gdb command.
Responds only if an error occurs when trying to write the command to
gdb
"""
controller = _state.get_controller_from_client_id(request.sid)
if controller is not None:
try:
# the command (string) or commands (list) to run
cmd = message["cmd"]
controller.write(cmd, read_response=False)
except Exception:
err = traceback.format_exc()
logger.error(err)
emit("error_running_gdb_command", {"message": err})
else:
emit("error_running_gdb_command", {"message": "gdb is not running"})
def send_msg_to_clients(client_ids, msg, error=False):
"""Send message to all clients"""
if error:
stream = "stderr"
else:
stream = "stdout"
response = [{"message": None, "type": "console", "payload": msg, "stream": stream}]
for client_id in client_ids:
logger.info("emiting message to websocket client id " + client_id)
socketio.emit(
"gdb_response", response, namespace="/gdb_listener", room=client_id
)
@app.route("/remove_gdb_controller", methods=["POST"])
def remove_gdb_controller():
gdbpid = int(request.form.get("gdbpid"))
orphaned_client_ids = _state.remove_gdb_controller_by_pid(gdbpid)
num_removed = len(orphaned_client_ids)
send_msg_to_clients(
orphaned_client_ids,
"The underlying gdb process has been killed. This tab will no longer function as expected.",
error=True,
)
msg = "removed %d gdb controller(s) with pid %d" % (num_removed, gdbpid)
if num_removed:
return jsonify({"message": msg})
else:
return jsonify({"message": msg}), 500
@socketio.on("disconnect", namespace="/gdb_listener")
def client_disconnected():
"""do nothing if client disconnects"""
_state.disconnect_client(request.sid)
logger.info("Client websocket disconnected, id %s" % (request.sid))
@socketio.on("Client disconnected")
def test_disconnect():
print("Client websocket disconnected", request.sid)
def read_and_forward_gdb_output():
"""A task that runs on a different thread, and emits websocket messages
of gdb responses"""
while True:
socketio.sleep(0.05)
controllers_to_remove = []
controller_items = _state.controller_to_client_ids.items()
for controller, client_ids in controller_items:
try:
try:
response = controller.get_gdb_response(
timeout_sec=0, raise_error_on_timeout=False
)
except NoGdbProcessError:
response = None
send_msg_to_clients(
client_ids,
"The underlying gdb process has been killed. This tab will no longer function as expected.",
error=True,
)
controllers_to_remove.append(controller)
if response:
for client_id in client_ids:
logger.info(
"emiting message to websocket client id " + client_id
)
socketio.emit(
"gdb_response",
response,
namespace="/gdb_listener",
room=client_id,
)
else:
# there was no queued response from gdb, not a problem
pass
except Exception:
logger.error(traceback.format_exc())
for controller in controllers_to_remove:
_state.remove_gdb_controller(controller)
def server_error(obj):
return jsonify(obj), 500
def client_error(obj):
return jsonify(obj), 400
def get_extra_files():
"""returns a list of files that should be watched by the Flask server
when in debug mode to trigger a reload of the server
"""
FILES_TO_SKIP = ["src/gdbgui.js"]
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
extra_dirs = [THIS_DIR]
extra_files = []
for extra_dir in extra_dirs:
for dirname, _, files in os.walk(extra_dir):
for filename in files:
filepath = os.path.join(dirname, filename)
if os.path.isfile(filepath) and filepath not in extra_files:
for skipfile in FILES_TO_SKIP:
if skipfile not in filepath:
extra_files.append(filepath)
return extra_files
def credentials_are_valid(username, password):
user_credentials = app.config.get("gdbgui_auth_user_credentials")
if user_credentials is None:
return False
elif len(user_credentials) < 2:
return False
return user_credentials[0] == username and user_credentials[1] == password
def authenticate(f):
@wraps(f)
def wrapper(*args, **kwargs):
if app.config.get("gdbgui_auth_user_credentials") is not None:
auth = request.authorization
if (
not auth
or not auth.username
or not auth.password
or not credentials_are_valid(auth.username, auth.password)
):
return Response(
"You must log in to continue.",
401,
{"WWW-Authenticate": 'Basic realm="gdbgui_login"'},
)
return f(*args, **kwargs)
return wrapper
@app.route("/", methods=["GET"])
@authenticate
def gdbgui():
"""Render the main gdbgui interface"""
interpreter = "lldb" if app.config["LLDB"] else "gdb"
gdbpid = request.args.get("gdbpid", 0)
add_csrf_token_to_session()
THEMES = ["monokai", "light"]
# fmt: off
initial_data = {
"csrf_token": session["csrf_token"],
"gdbgui_version": __version__,
"gdbpid": gdbpid,
"interpreter": interpreter,
"initial_binary_and_args": app.config["initial_binary_and_args"],
"p": pbkdf2_hex(str(app.config.get("l")), "Feo8CJol")
if app.config.get("l")
else "",
"project_home": app.config["project_home"],
"remap_sources": app.config["remap_sources"],
"rr": app.config["rr"],
"show_gdbgui_upgrades": app.config["show_gdbgui_upgrades"],
"themes": THEMES,
"signals": SIGNAL_NAME_TO_OBJ,
"using_windows": USING_WINDOWS,
}
# fmt: on
return render_template(
"gdbgui.html",
version=__version__,
debug=app.debug,
interpreter=interpreter,
initial_data=initial_data,
themes=THEMES,
)
@app.route("/send_signal_to_pid", methods=["POST"])
def send_signal_to_pid():
signal_name = request.form.get("signal_name", "").upper()
pid_str = str(request.form.get("pid"))
try:
pid_int = int(pid_str)
except ValueError:
return (
jsonify(
{
"message": "The pid %s cannot be converted to an integer. Signal %s was not sent."
% (pid_str, signal_name)
}
),
400,
)
if signal_name not in SIGNAL_NAME_TO_OBJ:
raise ValueError("no such signal %s" % signal_name)
signal_value = int(SIGNAL_NAME_TO_OBJ[signal_name])
try:
os.kill(pid_int, signal_value)
except Exception:
return (
jsonify(
{
"message": "Process could not be killed. Is %s an active PID?"
% pid_int
}
),
400,
)
return jsonify(
{
"message": "sent signal %s (%s) to process id %s"
% (signal_name, signal_value, pid_str)
}
)
@app.route("/dashboard", methods=["GET"])
@authenticate
def dashboard():
add_csrf_token_to_session()
"""display a dashboard with a list of all running gdb processes
and ability to kill them, or open a new tab to work with that
GdbController instance"""
return render_template(
"dashboard.html",
processes=_state.get_dashboard_data(),
csrf_token=session["csrf_token"],
)
@app.route("/shutdown", methods=["GET"])
@authenticate
def shutdown_webview():
add_csrf_token_to_session()
return render_template(
"donate.html", debug=app.debug, csrf_token=session["csrf_token"]
)
@app.route("/help")
def help():
return redirect("https://github.com/cs01/gdbgui/blob/master/HELP.md")
@app.route("/_shutdown", methods=["POST"])
def _shutdown():
try:
_state.exit_all_gdb_processes()
except Exception:
logger.error("failed to exit gdb subprocces")
logger.error(traceback.format_exc())
pid = os.getpid()
if app.debug:
os.kill(pid, signal.SIGINT)
else:
socketio.stop()
return jsonify({})
@app.route("/get_last_modified_unix_sec", methods=["GET"])
@csrf_protect
def get_last_modified_unix_sec():
"""Get last modified unix time for a given file"""
path = request.args.get("path")
if path and os.path.isfile(path):
try:
last_modified = os.path.getmtime(path)
return jsonify({"path": path, "last_modified_unix_sec": last_modified})
except Exception as e:
return client_error({"message": "%s" % e, "path": path})
else:
return client_error({"message": "File not found: %s" % path, "path": path})
@app.route("/read_file", methods=["GET"])
@csrf_protect
def read_file():
"""Read a file and return its contents as an array"""
path = request.args.get("path")
start_line = int(request.args.get("start_line"))
end_line = int(request.args.get("end_line"))
start_line = max(1, start_line) # make sure it's not negative
try:
highlight = json.loads(request.args.get("highlight", "true"))
except Exception as e:
if app.debug:
print("Raising exception since debug is on")
raise e
else:
highlight = (
True
) # highlight argument was invalid for some reason, default to true
if path and os.path.isfile(path):
try:
last_modified = os.path.getmtime(path)
with open(path, "r") as f:
raw_source_code_list = f.read().split("\n")
num_lines_in_file = len(raw_source_code_list)
end_line = min(
num_lines_in_file, end_line
) # make sure we don't try to go too far
# if leading lines are '', then the lexer will strip them out, but we want
# to preserve blank lines. Insert a space whenever we find a blank line.
for i in range((start_line - 1), (end_line)):
if raw_source_code_list[i] == "":
raw_source_code_list[i] = " "
raw_source_code_lines_of_interest = raw_source_code_list[
(start_line - 1) : (end_line)
]
try:
lexer = get_lexer_for_filename(path)
except Exception:
lexer = None
if lexer and highlight:
highlighted = True
# convert string into tokens
tokens = lexer.get_tokens("\n".join(raw_source_code_lines_of_interest))
# format tokens into nice, marked up list of html
formatter = (
htmllistformatter.HtmlListFormatter()
) # Don't add newlines after each line
source_code = formatter.get_marked_up_list(tokens)
else:
highlighted = False
source_code = raw_source_code_lines_of_interest
return jsonify(
{
"source_code_array": source_code,
"path": path,
"last_modified_unix_sec": last_modified,
"highlighted": highlighted,
"start_line": start_line,
"end_line": end_line,
"num_lines_in_file": num_lines_in_file,
}
)
except Exception as e:
return client_error({"message": "%s" % e})
else:
return client_error({"message": "File not found: %s" % path})
def get_gdbgui_auth_user_credentials(auth_file, user, password):
if auth_file and (user or password):
print("Cannot supply auth file and username/password")
exit(1)
if auth_file:
if os.path.isfile(auth_file):
with open(auth_file, "r") as authFile:
data = authFile.read()
split_file_contents = data.split("\n")
if len(split_file_contents) < 2:
print(
'Auth file "%s" requires username on first line and password on second line'
% auth_file
)
exit(1)
return split_file_contents
else:
print('Auth file "%s" for HTTP Basic auth not found' % auth_file)
exit(1)
elif user and password:
return [user, password]
else:
return None
def initialize_preferences():
if not os.path.exists(GDBGUI_PREF_DIR):
os.makedirs(GDBGUI_PREF_DIR)
app.config["l"] = None
if os.path.exists(os.path.join(GDBGUI_PREF_DIR, "license")):
with open(os.path.join(GDBGUI_PREF_DIR, "license")) as f:
app.config["l"] = f.read().strip()
def save_license(license):
with open(os.path.join(GDBGUI_PREF_DIR, "license"), "w") as f:
f.write(license)
app.config["l"] = license
print("saved license information")
def get_parser():
parser = argparse.ArgumentParser(description=__doc__)
gdb_group = parser.add_argument_group(title="gdb settings")
args_group = parser.add_mutually_exclusive_group()
network = parser.add_argument_group(title="gdbgui network settings")
security = parser.add_argument_group(title="security settings")
other = parser.add_argument_group(title="other settings")
gdb_group.add_argument(
"-g",
"--gdb",
help="Path to debugger. Default: %s" % DEFAULT_GDB_EXECUTABLE,
default=DEFAULT_GDB_EXECUTABLE,
)
gdb_group.add_argument(
"--gdb-args",
help=(
"Arguments passed directly to gdb when gdb is invoked. "
'For example,--gdb-args="--nx --tty=/dev/ttys002"'
),
default="",
)
gdb_group.add_argument(
"--rr",
action="store_true",
help=(
"Use `rr replay` instead of gdb. Replays last recording by default. "
"Replay arbitrary recording by passing recorded directory as an argument. "
"i.e. gdbgui /recorded/dir --rr. See http://rr-project.org/."
),
)
network.add_argument(
"-p",
"--port",
help="The port on which gdbgui will be hosted. Default: %s" % DEFAULT_PORT,
default=DEFAULT_PORT,
)
network.add_argument(
"--host",
help="The host ip address on which gdbgui serve. Default: %s" % DEFAULT_HOST,
default=DEFAULT_HOST,
)
network.add_argument(
"-r",
"--remote",
help="Shortcut to set host to 0.0.0.0 and suppress browser from opening. This allows remote access "
"to gdbgui and is useful when running on a remote machine that you want to view/debug from your local "
"browser, or let someone else debug your application remotely.",
action="store_true",
)
security.add_argument(
"--auth-file",
help="Require authentication before accessing gdbgui in the browser. "
"Specify a file that contains the HTTP Basic auth username and password separate by newline. ",
)
security.add_argument("--user", help="Username when authenticating")
security.add_argument("--password", help="Password when authenticating")
security.add_argument(
"--key",
default=None,
help="SSL private key. "
"Generate with:"
"openssl req -newkey rsa:2048 -nodes -keyout host.key -x509 -days 365 -out host.cert",
)
# https://www.digitalocean.com/community/tutorials/openssl-essentials-working-with-ssl-certificates-private-keys-and-csrs
security.add_argument(
"--cert",
default=None,
help="SSL certificate. "
"Generate with:"
"openssl req -newkey rsa:2048 -nodes -keyout host.key -x509 -days 365 -out host.cert",
)
# https://www.digitalocean.com/community/tutorials/openssl-essentials-working-with-ssl-certificates-private-keys-and-csrs
other.add_argument(
"--remap-sources",
"-m",
help=(
"Replace compile-time source paths to local source paths. "
"Pass valid JSON key/value pairs."
'i.e. --remap-sources=\'{"/buildmachine": "/home/chad"}\''
),
)
other.add_argument(
"--project",
help='Set the project directory. When viewing the "folders" pane, paths are shown relative to this directory.',
)
other.add_argument("-v", "--version", help="Print version", action="store_true")
other.add_argument(
"--hide-gdbgui-upgrades",
help="Hide messages regarding newer version of gdbgui. Default: False.",
action="store_true",
)
other.add_argument(
"-n",
"--no-browser",
help="By default, the browser will open with gdbgui. Pass this flag so the browser does not open.",
action="store_true",
)
other.add_argument(
"-b",
"--browser",
help="Use the given browser executable instead of the system default.",
default=None,
)
other.add_argument("--license", help="Store gdbgui ad-free license key.")
other.add_argument(
"--debug",
help="The debug flag of this Flask application. "
"Pass this flag when debugging gdbgui itself to automatically reload the server when changes are detected",
action="store_true",
)
args_group.add_argument(
"cmd",
nargs="?",
type=lambda prog: [prog],
help="The executable file and any arguments to pass to it."
" To pass flags to the binary, wrap in quotes, or use --args instead."
" Example: gdbgui ./mybinary [other-gdbgui-args...]"
" Example: gdbgui './mybinary myarg -flag1 -flag2' [other gdbgui args...]",
default=[],
)
args_group.add_argument(
"--args",
nargs=argparse.REMAINDER,
help="Specify the executable file and any arguments to pass to it. All arguments are"
" taken literally, so if used, this must be the last argument"
" passed to gdbgui."
" Example: gdbgui [...] --args ./mybinary myarg -flag1 -flag2",
default=[],
)
return parser
def main():
"""Entry point from command line"""
parser = get_parser()
args = parser.parse_args()
if args.debug:
logger.setLevel(logging.NOTSET)
initialize_preferences()
if args.version:
print(__version__)
return
cmd = args.cmd or args.args
if args.no_browser and args.browser:
print("Cannot specify no-browser and browser. Must specify one or the other.")
exit(1)
app.config["initial_binary_and_args"] = cmd
app.config["gdb_args"] = shlex.split(args.gdb_args)
app.config["rr"] = args.rr
app.config["gdb_path"] = args.gdb
app.config["show_gdbgui_upgrades"] = not args.hide_gdbgui_upgrades
app.config["gdbgui_auth_user_credentials"] = get_gdbgui_auth_user_credentials(
args.auth_file, args.user, args.password
)
app.config["project_home"] = args.project
if args.remap_sources:
try:
app.config["remap_sources"] = json.loads(args.remap_sources)
except json.decoder.JSONDecodeError as e:
print(
"The '--remap-sources' argument must be valid JSON. See gdbgui --help."
)
print(e)
exit(1)
if args.license:
print("saving license information")
save_license(args.license)
verify_gdb_exists(app.config["gdb_path"])
if args.remote:
args.host = "0.0.0.0"
args.no_browser = True
if app.config["gdbgui_auth_user_credentials"] is None:
print(
"Warning: authentication is recommended when serving on a publicly "
"accessible IP address. See gdbgui --help."
)
if warn_startup_with_shell_off(platform.platform().lower(), args.gdb_args):
logger.warning(
"You may need to set startup-with-shell off when running on a mac. i.e.\n"
" gdbgui --gdb-args='--init-eval-command=\"set startup-with-shell off\"'\n"
"see http://stackoverflow.com/questions/39702871/gdb-kind-of-doesnt-work-on-macos-sierra\n"
"and https://sourceware.org/gdb/onlinedocs/gdb/Starting.html"
)
setup_backend(
serve=True,
host=args.host,
port=int(args.port),
debug=bool(args.debug),
open_browser=(not args.no_browser),
browsername=args.browser,
private_key=args.key,
certificate=args.cert,
)
def warn_startup_with_shell_off(platform, gdb_args):
"""return True if user may need to turn shell off
if mac OS version is 16 (sierra) or higher, may need to set shell off due