-
Notifications
You must be signed in to change notification settings - Fork 74
/
test_debugger.cpp
1451 lines (1261 loc) · 40.9 KB
/
test_debugger.cpp
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
/***************************************************************************
* Copyright (c) 2018, Martin Renou, Johan Mabille, Sylvain Corlay, and *
* Wolf Vollprecht *
* Copyright (c) 2018, QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include "doctest/doctest.h"
#include <algorithm>
#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <mutex>
#include <thread>
#include "xeus/xsystem.hpp"
#include "xeus_client.hpp"
#include "xeus-zmq/xzmq_context.hpp"
#include "pybind11/pybind11.h"
#include <stdio.h>
#ifdef _WIN32
#include <direct.h>
#define getcwd _getcwd
#else
#include <unistd.h>
#endif
/***********************************
* Should be moved in a utils file *
***********************************/
std::string get_current_working_directory()
{
char buff[FILENAME_MAX];
char* r = getcwd(buff, FILENAME_MAX);
// Avoids warning
(void*)r;
std::string current_dir(buff);
//current_dir += '/';
#ifdef _WIN32
std::replace(current_dir.begin(), current_dir.end(), '\\', '/');
#endif
return current_dir;
}
namespace nl = nlohmann;
using namespace std::chrono_literals;
/***********************
* Predefined messages *
***********************/
nl::json make_attach_request(int seq)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "attach"},
{"arguments", {
{"justMyCode", false},
{"cwd", get_current_working_directory()}
}}
};
return req;
}
nl::json make_init_request()
{
nl::json req = {
{"type", "request"},
{"seq", 1},
{"command", "initialize"},
{"arguments", {
{"cliendID", "vscode"},
{"clientName", "Visual Studio Code"},
{"adapterID", "python"},
{"pathFormat", "path"},
{"linesStartAt1", true},
{"columnsStartAt1", true},
{"supportsVariableType", true},
{"supportsVariablePaging", true},
{"supportsRunInTerminalRequest", false},
{"locale", "en-us"}
}}
};
return req;
}
nl::json make_disconnect_request(int seq)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "disconnect"},
{"arguments", {
{"restart", false},
{"terminateDebuggee", false}
}}
};
return req;
}
nl::json make_shutdown_request()
{
nl::json req = {
{"restart", false}
};
return req;
}
nl::json make_breakpoint_request(int seq, const std::string& path, int line_number)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "setBreakpoints"},
{"arguments", {
{"source", {
{"path", path}
}},
{"breakpoints",
nl::json::array({nl::json::object({{"line", line_number}})})
},
{"lines", {line_number}},
{"sourceModified", false}
}}
};
return req;
}
nl::json make_breakpoint_request(int seq, const std::string& path, int line_number1, int line_number2)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "setBreakpoints"},
{"arguments", {
{"source", {
{"path", path}
}},
{"breakpoints",
nl::json::array({nl::json::object({{"line", line_number1}}), nl::json::object({{"line", line_number2}})})
},
{"lines", {line_number1, line_number2}},
{"sourceModified", false}
}}
};
return req;
}
nl::json make_configuration_done_request(int seq)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "configurationDone"},
};
return req;
}
nl::json make_next_request(int seq, int thread_id)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "next"},
{"arguments", {
{"threadId", thread_id}
}}
};
return req;
}
nl::json make_continue_request(int seq, int thread_id)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "continue"},
{"arguments", {
{"threadId", thread_id}
}}
};
return req;
}
nl::json make_evaluate_request(int seq, const std::string& code)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "evaluate"},
{"arguments", {
{"expression", code}
}}
};
return req;
}
nl::json make_stacktrace_request(int seq, int thread_id)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "stackTrace"},
{"arguments", {
{"threadId", thread_id}
}}
};
return req;
}
nl::json make_scopes_request(int seq, int frame_id)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "scopes"},
{"arguments", {
{"frameId", frame_id}
}}
};
return req;
}
nl::json make_variables_request(int seq, int var_ref, int start = 0, int count = 0)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "variables"},
{"arguments", {
{"variablesReference", var_ref},
{"start", start},
{"count", count}
}}
};
return req;
}
nl::json make_execute_request(const std::string& code)
{
nl::json req = {
{"code", code},
};
return req;
}
nl::json make_dump_cell_request(int seq, const std::string& code)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "dumpCell"},
{"arguments", {
{"code", code}
}}
};
return req;
}
nl::json make_source_request(int seq, const std::string& path)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "source"},
{"arguments", {
{"source", {
{"path", path}
}}
}}
};
return req;
}
nl::json make_stepin_request(int seq, int thread_id)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "stepIn"},
{"arguments", {
{"threadId", thread_id}
}}
};
return req;
}
nl::json make_debug_info_request(int seq)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "debugInfo"}
};
return req;
}
nl::json make_inspect_variables_request(int seq)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "inspectVariables"}
};
return req;
}
nl::json make_rich_inspect_variables_request(int seq, const std::string& var_name)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "richInspectVariables"},
{"arguments", {
{"variableName", var_name},
}}
};
return req;
}
nl::json make_rich_inspect_variables_request(int seq, const std::string& var_name, int frame_id)
{
nl::json req = make_rich_inspect_variables_request(seq, var_name);
req["arguments"]["frameId"] = frame_id;
return req;
}
nl::json make_copy_to_globals_request(int seq, const std::string& src_var_name, const std::string& dst_var_name, int src_frame_id)
{
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "copyToGlobals"},
{"arguments", {
{"srcVariableName", src_var_name},
{"dstVariableName", dst_var_name},
{"srcFrameId", src_frame_id}
}}
};
return req;
}
nl::json make_exception_breakpoint_request(int seq)
{
nl::json except_option = {
{"path", nl::json::array({
nl::json::object({{"names", nl::json::array({"Python Exceptions"})}})
})},
{"breakMode", "always"}
};
nl::json options = nl::json::array({except_option, except_option});
nl::json req = {
{"type", "request"},
{"seq", seq},
{"command", "setExceptionBreakpoints"},
{"arguments", {
{"filters", {"raised", "uncaught"}},
{"exceptionOptions", options}
}}
};
return req;
}
/*******************
* debugger_client *
*******************/
class debugger_client
{
public:
debugger_client(xeus::xcontext& context,
const std::string& connection_file,
const std::string& log_file);
bool test_init();
bool test_disconnect();
bool test_attach();
bool test_external_set_breakpoints();
bool test_external_next_continue();
bool test_set_breakpoints();
bool test_set_exception_breakpoints();
bool test_source();
bool test_next_continue();
bool test_step_in();
bool test_stack_trace();
bool test_debug_info();
bool test_inspect_variables();
bool test_rich_inspect_variables();
bool test_variables();
bool test_copy_to_globals();
void start();
void shutdown();
void disconnect_debugger();
private:
nl::json attach();
nl::json set_external_breakpoints();
nl::json set_breakpoints();
nl::json set_exception_breakpoints();
std::string get_external_path();
void dump_external_file();
std::string make_code() const;
std::string make_external_code() const;
std::string make_external_invoker_code() const;
bool print_code_variable(const std::string& expected, int& seq);
void next(int& seq);
void continue_exec(int& seq);
bool next_continue_common();
xeus_logger_client m_client;
};
debugger_client::debugger_client(xeus::xcontext& context,
const std::string& connection_file,
const std::string& log_file)
: m_client(context, "debugger_client",
xeus::load_configuration(connection_file), log_file)
{
}
bool debugger_client::test_init()
{
m_client.send_on_control("debug_request", make_init_request());
nl::json rep = m_client.receive_on_control();
return rep["content"]["success"].get<bool>();
}
bool debugger_client::test_disconnect()
{
attach();
m_client.send_on_control("debug_request", make_disconnect_request(3));
nl::json rep = m_client.receive_on_control();
return rep["content"]["type"] == "response";
}
bool debugger_client::test_attach()
{
nl::json rep = attach();
return rep["content"]["success"].get<bool>();
}
bool debugger_client::test_external_set_breakpoints()
{
attach();
nl::json rep = set_external_breakpoints();
return rep["content"]["body"].size() != 0;
}
bool debugger_client::print_code_variable(const std::string& expected, int& seq)
{
m_client.send_on_control("debug_request", make_stacktrace_request(seq, 1));
++seq;
nl::json json1 = m_client.receive_on_control();
if(json1["content"]["body"]["stackFrames"].empty())
{
m_client.send_on_control("debug_request", make_stacktrace_request(seq, 1));
++seq;
json1 = m_client.receive_on_control();
}
int frame_id = json1["content"]["body"]["stackFrames"][0]["id"].get<int>();
m_client.send_on_control("debug_request", make_scopes_request(seq, frame_id));
++seq;
nl::json json2 = m_client.receive_on_control();
int variable_ref = json2["content"]["body"]["scopes"][0]["variablesReference"].get<int>();
m_client.send_on_control("debug_request", make_variables_request(seq, variable_ref));
++seq;
nl::json json3 = m_client.receive_on_control();
const auto& ar = json3["content"]["body"]["variables"];
bool var_found = false;
std::string name, value;
for(auto it = ar.begin(); it != ar.end() && !var_found; ++it)
{
auto d = *it;
name = d["name"];
if(name == "i")
{
var_found = true;
value = d["value"];
}
}
std::cout << "Variable " << name << " = " << value << std::endl;
return value == expected;
}
bool debugger_client::test_set_breakpoints()
{
attach();
nl::json rep = set_breakpoints();
return rep["content"]["body"].size() != 0;
}
bool debugger_client::test_set_exception_breakpoints()
{
attach();
nl::json reply = set_exception_breakpoints();
std::string code = "a = 3 * undef";
m_client.send_on_shell("execute_request", make_execute_request(code));
nl::json ev = m_client.wait_for_debug_event("stopped");
std::string reason = ev["content"]["body"]["reason"].get<std::string>();
return reason == "exception";
}
bool debugger_client::test_source()
{
attach();
std::string code = make_code();
m_client.send_on_control("debug_request", make_dump_cell_request(4, code));
nl::json reply = m_client.receive_on_control();
std::string path = reply["content"]["body"]["sourcePath"].get<std::string>();
m_client.send_on_control("debug_request", make_source_request(5, path));
nl::json rep = m_client.receive_on_control();
nl::json source = rep["content"]["body"]["content"];
bool res = source == code;
return res;
}
void debugger_client::next(int& seq)
{
m_client.send_on_control("debug_request", make_next_request(seq, 1));
m_client.receive_on_control();
++seq;
}
void debugger_client::continue_exec(int& seq)
{
m_client.send_on_control("debug_request", make_continue_request(seq, 1));
m_client.receive_on_control();
++seq;
}
bool debugger_client::next_continue_common()
{
nl::json ev = m_client.wait_for_debug_event("stopped");
// Code should have stopped line 3
int seq = ev["content"]["seq"].get<int>();
std::cout << "Thread hit a breakpoint line 3" << std::endl;
bool res = print_code_variable("4", seq);
next(seq);
m_client.wait_for_debug_event("stopped");
// Code should have stopped line 4
std::cout << "Thread is stopped line 4" << std::endl;
res = print_code_variable("8", seq) && res;
continue_exec(seq);
m_client.wait_for_debug_event("stopped");
// Code should have stopped line 5
std::cout << "Thread hit a breakpoint line 5" << std::endl;
res = print_code_variable("11", seq) && res;
continue_exec(seq);
nl::json rep = m_client.receive_on_shell();
return rep["content"]["status"] == "ok";
}
bool debugger_client::test_external_next_continue()
{
attach();
// We set 2 breakpoints on line 3 and 5 of external_code.py
set_external_breakpoints();
m_client.send_on_control("debug_request", make_configuration_done_request(5));
std::cout << "Configuration done sent" << std::endl;
m_client.receive_on_control();
std::cout << "Configuration done received" << std::endl;
m_client.send_on_shell("execute_request", make_execute_request(make_external_invoker_code()));
return next_continue_common();
}
bool debugger_client::test_next_continue()
{
attach();
// We set 2 breakpoints on line 2 and 4 of the first cell
set_breakpoints();
m_client.send_on_control("debug_request", make_configuration_done_request(5));
m_client.receive_on_control();
m_client.send_on_shell("execute_request", make_execute_request(make_code()));
return next_continue_common();
}
bool debugger_client::test_step_in()
{
attach();
m_client.send_on_control("debug_request", make_dump_cell_request(4, make_external_invoker_code()));
nl::json dump_res = m_client.receive_on_control();
std::string path = dump_res["content"]["body"]["sourcePath"].get<std::string>();
m_client.send_on_control("debug_request", make_breakpoint_request(4, path, 1, 2));
m_client.receive_on_control();
m_client.send_on_control("debug_request", make_configuration_done_request(5));
m_client.receive_on_control();
m_client.send_on_shell("execute_request", make_execute_request(make_external_code()));
m_client.receive_on_shell();
m_client.send_on_shell("execute_request", make_execute_request(make_external_invoker_code()));
nl::json ev = m_client.wait_for_debug_event("stopped");
int seq = ev["content"]["seq"].get<int>();
// Code should have stopped line 1
std::cout << "Thread hit a breakpoint line 1" << std::endl;
continue_exec(seq);
m_client.wait_for_debug_event("stopped");
// Code should have stopped line 2
std::cout << "Thread hit a breakpoint line 2" << std::endl;
m_client.send_on_control("debug_request", make_stepin_request(seq, 1));
m_client.receive_on_control();
m_client.wait_for_debug_event("stopped");
seq = ev["content"]["seq"].get<int>();
std::cout << "Thread has stepped in" << std::endl;
next(seq);
m_client.wait_for_debug_event("stopped");
bool res = print_code_variable("4", seq);
next(seq);
m_client.wait_for_debug_event("stopped");
res = print_code_variable("8", seq) && res;
continue_exec(seq);
nl::json rep = m_client.receive_on_shell();
res = rep["content"]["status"] == "ok" && res;
return res;
}
bool debugger_client::test_stack_trace()
{
attach();
// We set 2 breakpoints on line 2 and 4 of the first cell
set_breakpoints();
m_client.send_on_control("debug_request", make_configuration_done_request(5));
m_client.receive_on_control();
m_client.send_on_shell("execute_request", make_execute_request(make_code()));
nl::json ev = m_client.wait_for_debug_event("stopped");
int seq = 6;
m_client.send_on_control("debug_request", make_stacktrace_request(seq, 1));
++seq;
nl::json stackframes = m_client.receive_on_control();
bool res = stackframes["content"]["body"]["stackFrames"].size() != 0;
continue_exec(seq);
m_client.wait_for_debug_event("stopped");
continue_exec(seq);
return res;
}
bool debugger_client::test_debug_info()
{
attach();
m_client.send_on_control("debug_request", make_debug_info_request(4));
nl::json rep1 = m_client.receive_on_control();
bool res = rep1["content"]["body"]["breakpoints"].size() == 0;
set_breakpoints();
set_external_breakpoints();
m_client.send_on_control("debug_request", make_configuration_done_request(5));
m_client.receive_on_control();
m_client.send_on_control("debug_request", make_debug_info_request(12));
nl::json rep2 = m_client.receive_on_control();
nl::json bp_list = rep2["content"]["body"]["breakpoints"];
res = res && bp_list.size() == 2;
res = res && bp_list[0]["breakpoints"].size() == 2 && bp_list[1]["breakpoints"].size() == 2;
nl::json stopped_list = rep2["content"]["body"]["stoppedThreads"];
res = res && stopped_list.size() == 0;
m_client.send_on_shell("execute_request", make_execute_request(make_code()));
m_client.wait_for_debug_event("stopped");
m_client.send_on_control("debug_request", make_debug_info_request(14));
nl::json rep3 = m_client.receive_on_control();
nl::json stopped_list2 = rep3["content"]["body"]["stoppedThreads"];
int seq = 15;
next(seq);
m_client.wait_for_debug_event("stopped");
continue_exec(seq);
m_client.wait_for_debug_event("stopped");
continue_exec(seq);
nl::json rep4 = m_client.receive_on_shell();
bool res2 = rep4["content"]["status"] == "ok";
return res && res2 && stopped_list2[0] == 1;
}
bool debugger_client::test_inspect_variables()
{
attach();
std::string code = "i=4\nj=i+4\nk=j-3\n";
m_client.send_on_shell("execute_request", make_execute_request(code));
m_client.receive_on_shell();
m_client.send_on_control("debug_request", make_inspect_variables_request(0));
nl::json rep = m_client.receive_on_control();
nl::json vars = rep["content"]["body"]["variables"];
auto check_var = [&vars](const std::string& name, const std::string& value) {
auto x = std::find_if(vars.begin(), vars.end(), [&name](const nl::json& var) {
return var.is_object() && var.value("name", "") == name;
});
if (x == vars.end())
{
std::cout << "missing " << name << std::endl;
return false;
}
nl::json var = *x;
return var["value"] == value && var["variablesReference"] == 0;
};
bool res = check_var("i", "4") && check_var("j", "8") && check_var("k", "5");
return res;
}
std::string rich_inspect_class_def = R"RICH(
class Person:
def __init__(self, name="John Doe", address="Paris", picture=""):
self.name = name
self.address = address
self.picture = picture
def _repr_mimebundle_(self, include=None, exclude=None):
return {
"text/html": """<img src="{}">
<div><i class='fa-user fa'></i>: {}</div>
<div><i class='fa-map fa'></i>: {}</div>""".format(self.picture, self.name, self.address)
}
)RICH";
std::string rich_html = R"RICH(<img src="">
<div><i class='fa-user fa'></i>: James Smith</div>
<div><i class='fa-map fa'></i>: Boston</div>)RICH";
std::string rich_html2 = R"RICH(<img src="">
<div><i class='fa-user fa'></i>: John Smith</div>
<div><i class='fa-map fa'></i>: NYC</div>)RICH";
bool debugger_client::test_rich_inspect_variables()
{
bool res = false;
{
m_client.send_on_shell("execute_request", make_execute_request(rich_inspect_class_def));
m_client.receive_on_shell();
std::string code = "james = Person(\"James Smith\", \"Boston\")";
m_client.send_on_shell("execute_request", make_execute_request(code));
m_client.receive_on_shell();
m_client.send_on_control("debug_request", make_rich_inspect_variables_request(0, "james"));
nl::json rep = m_client.receive_on_control();
nl::json data = rep["content"]["body"]["data"];
std::string html = data["text/html"].get<std::string>();
std::string plain = data["text/plain"].get<std::string>();
res = html == rich_html && !plain.empty();
}
bool res2 = true;
{
attach();
std::string code = "john = Person(\"John Smith\", \"NYC\")\ni = 4\nj = 2";
m_client.send_on_shell("execute_request", make_execute_request(code));
m_client.receive_on_shell();
m_client.send_on_control("debug_request", make_dump_cell_request(12, code));
nl::json dump_res = m_client.receive_on_control();
std::string path = dump_res["content"]["body"]["sourcePath"].get<std::string>();
m_client.send_on_control("debug_request", make_breakpoint_request(12, path, 2));
m_client.receive_on_control();
m_client.send_on_shell("execute_request", make_execute_request(code));
nl::json ev = m_client.wait_for_debug_event("stopped");
int seq = 14;
m_client.send_on_control("debug_request", make_stacktrace_request(seq, 1));
nl::json stackframes = m_client.receive_on_control();
int frame_id = stackframes["content"]["body"]["stackFrames"][0]["id"].get<int>();
m_client.send_on_control("debug_request", make_rich_inspect_variables_request(seq, "john", frame_id));
nl::json rep = m_client.receive_on_control();
m_client.send_on_control("debug_request", make_continue_request(seq, 1));
m_client.receive_on_control();
m_client.receive_on_shell();
nl::json data = rep["content"]["body"]["data"];
std::string html = data["text/html"].get<std::string>();
std::string plain = data["text/plain"].get<std::string>();
res = html == rich_html2 && !plain.empty();
}
return res && res2;
}
bool debugger_client::test_variables()
{
std::string code = "i=4\nj=i+4\nk=j-3\ni=k+2\n";
attach();
{
m_client.send_on_control("debug_request", make_dump_cell_request(4, code));
nl::json res = m_client.receive_on_control();
std::string path = res["content"]["body"]["sourcePath"].get<std::string>();
m_client.send_on_control("debug_request", make_breakpoint_request(4, path, 4));
m_client.receive_on_control();
}
m_client.send_on_control("debug_request", make_configuration_done_request(5));
m_client.receive_on_control();
m_client.send_on_shell("execute_request", make_execute_request(code));
nl::json ev = m_client.wait_for_debug_event("stopped");
int seq = ev["content"]["seq"].get<int>();
m_client.send_on_control("debug_request", make_stacktrace_request(seq, 1));
++seq;
nl::json json1 = m_client.receive_on_control();
if(json1["content"]["body"]["stackFrames"].empty())
{
m_client.send_on_control("debug_request", make_stacktrace_request(seq, 1));
++seq;
json1 = m_client.receive_on_control();
}
int frame_id = json1["content"]["body"]["stackFrames"][0]["id"].get<int>();
m_client.send_on_control("debug_request", make_scopes_request(seq, frame_id));
++seq;
nl::json json2 = m_client.receive_on_control();
int variable_ref = json2["content"]["body"]["scopes"][0]["variablesReference"].get<int>();
m_client.send_on_control("debug_request", make_variables_request(seq, variable_ref, 1, 1));
++seq;
nl::json json3 = m_client.receive_on_control();
const auto& ar = json3["content"]["body"]["variables"];
bool res = ar.size() == 1u;
m_client.send_on_control("debug_request", make_continue_request(seq, 1));
m_client.receive_on_control();
return res;
}
bool debugger_client::test_copy_to_globals()
{
std::string local_var_name = "var";
std::string global_var_name = "var_copy";
std::string code = "from IPython.core.display import HTML\ndef my_test():\n\t" + local_var_name + " = HTML(\"<p>test content</p>\")\n\tpass\nmy_test()";
int seq = 12;
// Init debugger and set breakpoint
attach();
m_client.send_on_control("debug_request", make_dump_cell_request(seq, code));
++seq;
nl::json dump_res = m_client.receive_on_control();
std::string path = dump_res["content"]["body"]["sourcePath"].get<std::string>();
m_client.send_on_control("debug_request", make_breakpoint_request(seq, path, 4));
++seq;
m_client.receive_on_control();
// Execute code
m_client.send_on_shell("execute_request", make_execute_request(code));
// Get breakpoint event
nl::json ev = m_client.wait_for_debug_event("stopped");
m_client.send_on_control("debug_request", make_stacktrace_request(seq, 1));
++seq;
nl::json json1 = m_client.receive_on_control();
if(json1["content"]["body"]["stackFrames"].empty())
{
m_client.send_on_control("debug_request", make_stacktrace_request(seq, 1));
++seq;
json1 = m_client.receive_on_control();
}
// Get local frame id
int frame_id = json1["content"]["body"]["stackFrames"][0]["id"].get<int>();
// Copy the variable
m_client.send_on_control("debug_request", make_copy_to_globals_request(seq, local_var_name, global_var_name, frame_id));
++seq;
// Get the scopes
nl::json reply = m_client.receive_on_control();
m_client.send_on_control("debug_request", make_scopes_request(seq, frame_id));
++seq;
nl::json json2 = m_client.receive_on_control();
// Get the local variable
int variable_ref_local = json2["content"]["body"]["scopes"][0]["variablesReference"].get<int>();
m_client.send_on_control("debug_request", make_variables_request(seq, variable_ref_local));
++seq;
nl::json json3 = m_client.receive_on_control();
nl::json local_var = {};
for (auto &var: json3["content"]["body"]["variables"]){
if (var["evaluateName"] == local_var_name) {
local_var = var;
}
}
if (!local_var.contains("evaluateName")) {
std::cout << "Local variable \"" + local_var_name + "\" not found";
return false;
}
// Get the global variable (copy of the local variable)
nl::json global_scope = json2["content"]["body"]["scopes"].back();
int variable_ref_global = global_scope["variablesReference"].get<int>();
m_client.send_on_control("debug_request", make_variables_request(seq, variable_ref_global));
++seq;
nl::json json4 = m_client.receive_on_control();
nl::json global_var = {};
for (auto &var: json4["content"]["body"]["variables"]){
if (var["evaluateName"] == global_var_name) {
global_var = var;
}
}
if (!global_var.contains("evaluateName")) {
std::cout << "Global variable \"" + global_var_name + "\" not found";
return false;
}
// Compare local and global variable
return global_var["value"] == local_var["value"] && global_var["type"] == local_var["type"];
}
void debugger_client::start()
{
m_client.start();
}
void debugger_client::disconnect_debugger()
{
m_client.send_on_control("debug_request", make_disconnect_request(INT_MAX));
m_client.receive_on_control();
}
void debugger_client::shutdown()
{
if (m_client.kernel_dead)
{
throw std::runtime_error("Kernel is not alive. Cannot send shutdown request.");
}
m_client.stop_channels();
m_client.send_on_control("shutdown_request", make_shutdown_request());
m_client.receive_on_control();
}
nl::json debugger_client::attach()
{
m_client.send_on_control("debug_request", make_init_request());
nl::json rep = m_client.receive_on_control();
if (!rep["content"]["success"].get<bool>())
{
shutdown();
std::this_thread::sleep_for(2s);
throw std::runtime_error("Could not initialize debugger, exiting");
}