incode 0.30.26038

InCode - MCP server for LLDB debugging automation
Documentation
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
// InCode Variable & Symbol Inspection Tools Test Suite
// 
// GRANULAR FEATURES TESTED:
// - F0035: get_variables - Get variables in current scope with types and values
// - F0036: get_global_variables - Get global variables and their values
// - F0037: evaluate_expression - Evaluate C/C++ expressions in current context
// - F0038: get_variable_info - Get detailed info about specific variable
// - F0039: set_variable - Modify variable value during debugging
// - F0040: lookup_symbol - Find symbol information by name
//
// Tests variable inspection with real LLDB integration using test_debuggee binary

// Import test setup utilities
mod test_setup;
use test_setup::{TestSession, TestMode};

#[tokio::test]
async fn test_f0035_get_variables_success() {
    // F0035: get_variables - Test getting variables in current scope
    println!("Testing F0035: get_variables");
    
    let mut session = match TestSession::new(TestMode::Normal) {
        Ok(s) => s,
        Err(e) => {
            println!("⚠️ F0035: Could not create test session: {}", e);
            return;
        }
    };
    
    match session.start() {
        Ok(pid) => {
            println!("✅ F0035: Test session started with PID {}", pid);
            
            // Set breakpoint in function with local variables
            let _ = session.lldb_manager().set_breakpoint("demonstrate_local_variables");
            let _ = session.lldb_manager().continue_execution();
            
            // Test getting variables in current scope
            let result = session.lldb_manager().get_variables(None, None);
            
            match result {
                Ok(variables) => {
                    println!("✅ F0035: get_variables succeeded, found {} variables", variables.len());
                    
                    for (i, var) in variables.iter().take(5).enumerate() {
                        println!("  Variable {}: {} = {} ({})", 
                               i + 1, var.name, var.value, var.var_type);
                    }
                    
                    // Look for expected local variables
                    let has_local_int = variables.iter().any(|v| v.name == "local_int");
                    let has_local_float = variables.iter().any(|v| v.name == "local_float");
                    
                    if has_local_int || has_local_float {
                        println!("✅ F0035: Found expected local variables");
                    }
                    
                    assert!(variables.len() > 0, "Should have at least one variable");
                }
                Err(e) => {
                    println!("⚠️ F0035: get_variables failed: {}", e);
                }
            }
        }
        Err(e) => {
            println!("⚠️ F0035: Could not start debugging session: {}", e);
        }
    }
    
    let _ = session.cleanup();
}

#[tokio::test]
async fn test_f0035_get_variables_with_filter() {
    // F0035: get_variables - Test getting variables with pattern filter
    println!("Testing F0035: get_variables with filter");
    
    let mut session = match TestSession::new(TestMode::Normal) {
        Ok(s) => s,
        Err(e) => {
            println!("⚠️ F0035: Could not create test session: {}", e);
            return;
        }
    };
    
    match session.start() {
        Ok(_pid) => {
            // Set breakpoint in function with various variables
            let _ = session.lldb_manager().set_breakpoint("showcase_variables");
            let _ = session.lldb_manager().continue_execution();
            
            // Test filtering variables by pattern
            let result = session.lldb_manager().get_variables(Some("local_*"), None);
            
            match result {
                Ok(filtered_vars) => {
                    println!("✅ F0035: get_variables with filter succeeded, found {} variables", filtered_vars.len());
                    
                    for var in &filtered_vars {
                        println!("  Filtered variable: {} = {}", var.name, var.value);
                        assert!(var.name.starts_with("local_"), 
                               "Filtered variable should match pattern");
                    }
                }
                Err(e) => {
                    println!("⚠️ F0035: get_variables with filter failed: {}", e);
                }
            }
        }
        Err(e) => {
            println!("⚠️ F0035: Could not start debugging session: {}", e);
        }
    }
    
    let _ = session.cleanup();
}

#[tokio::test]
async fn test_f0036_get_global_variables() {
    // F0036: get_global_variables - Test getting global variables
    println!("Testing F0036: get_global_variables");
    
    let mut session = match TestSession::new(TestMode::Normal) {
        Ok(s) => s,
        Err(e) => {
            println!("⚠️ F0036: Could not create test session: {}", e);
            return;
        }
    };
    
    match session.start() {
        Ok(pid) => {
            println!("✅ F0036: Test session started with PID {}", pid);
            
            // Set breakpoint to get process into a known state
            let _ = session.lldb_manager().set_breakpoint("main");
            let _ = session.lldb_manager().continue_execution();
            
            // Test getting global variables
            let result = session.lldb_manager().get_global_variables(None);
            
            match result {
                Ok(globals) => {
                    println!("✅ F0036: get_global_variables succeeded, found {} globals", globals.len());
                    
                    for (i, var) in globals.iter().take(5).enumerate() {
                        println!("  Global {}: {} = {} ({})", 
                               i + 1, var.name, var.value, var.var_type);
                    }
                    
                    // Look for expected global variables from test binary
                    let has_global_int = globals.iter().any(|v| v.name.contains("global_int"));
                    let has_global_string = globals.iter().any(|v| v.name.contains("global_string"));
                    
                    if has_global_int || has_global_string {
                        println!("✅ F0036: Found expected global variables");
                    }
                }
                Err(e) => {
                    println!("⚠️ F0036: get_global_variables failed: {}", e);
                }
            }
        }
        Err(e) => {
            println!("⚠️ F0036: Could not start debugging session: {}", e);
        }
    }
    
    let _ = session.cleanup();
}

#[tokio::test]
async fn test_f0036_get_global_variables_with_module_filter() {
    // F0036: get_global_variables - Test getting globals with module filter
    println!("Testing F0036: get_global_variables with module filter");
    
    let mut session = match TestSession::new(TestMode::Normal) {
        Ok(s) => s,
        Err(e) => {
            println!("⚠️ F0036: Could not create test session: {}", e);
            return;
        }
    };
    
    match session.start() {
        Ok(_pid) => {
            let _ = session.lldb_manager().set_breakpoint("main");
            let _ = session.lldb_manager().continue_execution();
            
            // Test filtering globals by module
            let result = session.lldb_manager().get_global_variables(Some("test_debuggee"));
            
            match result {
                Ok(module_globals) => {
                    println!("✅ F0036: get_global_variables with module filter succeeded, found {} globals", 
                           module_globals.len());
                    
                    for var in module_globals.iter().take(3) {
                        println!("  Module global: {} = {}", var.name, var.value);
                    }
                }
                Err(e) => {
                    println!("⚠️ F0036: get_global_variables with module filter failed: {}", e);
                }
            }
        }
        Err(e) => {
            println!("⚠️ F0036: Could not start debugging session: {}", e);
        }
    }
    
    let _ = session.cleanup();
}

#[tokio::test]
async fn test_f0037_evaluate_expression() {
    // F0037: evaluate_expression - Test evaluating C/C++ expressions
    println!("Testing F0037: evaluate_expression");
    
    let mut session = match TestSession::new(TestMode::Normal) {
        Ok(s) => s,
        Err(e) => {
            println!("⚠️ F0037: Could not create test session: {}", e);
            return;
        }
    };
    
    match session.start() {
        Ok(pid) => {
            println!("✅ F0037: Test session started with PID {}", pid);
            
            // Set breakpoint in function with local variables
            let _ = session.lldb_manager().set_breakpoint("demonstrate_local_variables");
            let _ = session.lldb_manager().continue_execution();
            
            // Test various expressions
            let expressions = vec![
                ("1 + 2", "simple arithmetic"),
                ("sizeof(int)", "sizeof operator"),
                ("local_int * 2", "variable arithmetic"),
                ("local_int > 0", "comparison"),
            ];
            
            for (expr, description) in expressions {
                let result = session.lldb_manager().evaluate_expression(expr);
                
                match result {
                    Ok(eval_result) => {
                        println!("✅ F0037: evaluate_expression succeeded for {}: {} = {}", 
                               description, expr, eval_result);
                        
                        assert!(!eval_result.is_empty());
                    }
                    Err(e) => {
                        println!("⚠️ F0037: evaluate_expression failed for {}: {}", description, e);
                    }
                }
            }
        }
        Err(e) => {
            println!("⚠️ F0037: Could not start debugging session: {}", e);
        }
    }
    
    let _ = session.cleanup();
}

#[tokio::test]
async fn test_f0037_evaluate_expression_invalid() {
    // F0037: evaluate_expression - Test error handling for invalid expressions
    println!("Testing F0037: evaluate_expression with invalid expression");
    
    let mut session = match TestSession::new(TestMode::Normal) {
        Ok(s) => s,
        Err(e) => {
            println!("⚠️ F0037: Could not create test session: {}", e);
            return;
        }
    };
    
    match session.start() {
        Ok(_pid) => {
            let _ = session.lldb_manager().set_breakpoint("main");
            let _ = session.lldb_manager().continue_execution();
            
            // Test invalid expression
            let result = session.lldb_manager().evaluate_expression("invalid_variable_name_12345");
            
            match result {
                Err(e) => {
                    println!("✅ F0037: Correctly handled invalid expression: {}", e);
                }
                Ok(eval_result) => {
                    println!("⚠️ F0037: evaluate_expression unexpectedly succeeded: invalid_variable_name_12345 = {}", 
                           eval_result);
                }
            }
        }
        Err(e) => {
            println!("⚠️ F0037: Could not start debugging session: {}", e);
        }
    }
    
    let _ = session.cleanup();
}

#[tokio::test]
async fn test_f0038_get_variable_info() {
    // F0038: get_variable_info - Test getting detailed variable information
    println!("Testing F0038: get_variable_info");
    
    let mut session = match TestSession::new(TestMode::Normal) {
        Ok(s) => s,
        Err(e) => {
            println!("⚠️ F0038: Could not create test session: {}", e);
            return;
        }
    };
    
    match session.start() {
        Ok(pid) => {
            println!("✅ F0038: Test session started with PID {}", pid);
            
            // Set breakpoint in function with local variables
            let _ = session.lldb_manager().set_breakpoint("demonstrate_local_variables");
            let _ = session.lldb_manager().continue_execution();
            
            // Test getting detailed info for specific variable
            let result = session.lldb_manager().get_variable_info("local_int");
            
            match result {
                Ok(var_info) => {
                    println!("✅ F0038: get_variable_info succeeded");
                    println!("  Name: {}", var_info.name);
                    println!("  Type: {}", var_info.var_type);
                    println!("  Value: {}", var_info.value);
                    println!("  Size: {} bytes", var_info.size);
                    println!("  Address: 0x{:x}", var_info.address);
                    println!("  Location: {}", var_info.location);
                    
                    assert_eq!(var_info.name, "local_int");
                    assert!(!var_info.var_type.is_empty());
                    assert!(!var_info.value.is_empty());
                    assert!(var_info.size > 0);
                    assert!(var_info.address > 0);
                }
                Err(e) => {
                    println!("⚠️ F0038: get_variable_info failed: {}", e);
                }
            }
        }
        Err(e) => {
            println!("⚠️ F0038: Could not start debugging session: {}", e);
        }
    }
    
    let _ = session.cleanup();
}

#[tokio::test]
async fn test_f0038_get_variable_info_nonexistent() {
    // F0038: get_variable_info - Test error handling for non-existent variable
    println!("Testing F0038: get_variable_info with non-existent variable");
    
    let mut session = match TestSession::new(TestMode::Normal) {
        Ok(s) => s,
        Err(e) => {
            println!("⚠️ F0038: Could not create test session: {}", e);
            return;
        }
    };
    
    match session.start() {
        Ok(_pid) => {
            let _ = session.lldb_manager().set_breakpoint("main");
            let _ = session.lldb_manager().continue_execution();
            
            // Test getting info for non-existent variable
            let result = session.lldb_manager().get_variable_info("nonexistent_variable_12345");
            
            match result {
                Err(e) => {
                    println!("✅ F0038: Correctly handled non-existent variable: {}", e);
                }
                Ok(var_info) => {
                    println!("⚠️ F0038: get_variable_info unexpectedly succeeded for non-existent variable: {}", 
                           var_info.name);
                }
            }
        }
        Err(e) => {
            println!("⚠️ F0038: Could not start debugging session: {}", e);
        }
    }
    
    let _ = session.cleanup();
}

#[tokio::test]
async fn test_f0039_set_variable() {
    // F0039: set_variable - Test modifying variable value during debugging
    println!("Testing F0039: set_variable");
    
    let mut session = match TestSession::new(TestMode::Normal) {
        Ok(s) => s,
        Err(e) => {
            println!("⚠️ F0039: Could not create test session: {}", e);
            return;
        }
    };
    
    match session.start() {
        Ok(pid) => {
            println!("✅ F0039: Test session started with PID {}", pid);
            
            // Set breakpoint in function with modifiable variables
            let _ = session.lldb_manager().set_breakpoint("demonstrate_variable_modifications");
            let _ = session.lldb_manager().continue_execution();
            
            // Get original variable value
            let _original_value = match session.lldb_manager().evaluate_expression("modification_test") {
                Ok(result) => {
                    println!("Original value of modification_test: {}", result);
                    result
                }
                Err(e) => {
                    println!("⚠️ F0039: Could not get original value: {}", e);
                    return;
                }
            };
            
            // Test setting variable to new value
            let new_value = "999";
            let result = session.lldb_manager().set_variable("modification_test", new_value);
            
            match result {
                Ok(success) => {
                    if !success.is_empty() {
                        println!("✅ F0039: set_variable succeeded");
                        
                        // Verify the change
                        match session.lldb_manager().evaluate_expression("modification_test") {
                            Ok(new_result) => {
                                println!("New value of modification_test: {}", new_result);
                                if new_result.contains(new_value) {
                                    println!("✅ F0039: Variable modification verified");
                                } else {
                                    println!("⚠️ F0039: Variable modification not reflected");
                                }
                            }
                            Err(e) => {
                                println!("⚠️ F0039: Could not verify variable change: {}", e);
                            }
                        }
                    } else {
                        println!("⚠️ F0039: set_variable reported failure");
                    }
                }
                Err(e) => {
                    println!("⚠️ F0039: set_variable failed: {}", e);
                }
            }
        }
        Err(e) => {
            println!("⚠️ F0039: Could not start debugging session: {}", e);
        }
    }
    
    let _ = session.cleanup();
}

#[tokio::test]
async fn test_f0040_lookup_symbol() {
    // F0040: lookup_symbol - Test finding symbol information by name
    println!("Testing F0040: lookup_symbol");
    
    let mut session = match TestSession::new(TestMode::Normal) {
        Ok(s) => s,
        Err(e) => {
            println!("⚠️ F0040: Could not create test session: {}", e);
            return;
        }
    };
    
    match session.start() {
        Ok(pid) => {
            println!("✅ F0040: Test session started with PID {}", pid);
            
            // Test looking up function symbols
            let symbols_to_lookup = vec!["main", "showcase_variables", "printf"];
            
            for symbol_name in symbols_to_lookup {
                let result = session.lldb_manager().lookup_symbol(symbol_name);
                
                match result {
                    Ok(symbol_info) => {
                        println!("✅ F0040: lookup_symbol succeeded for '{}'", symbol_name);
                        println!("  Name: {}", symbol_info.name);
                        println!("  Type: {}", symbol_info.symbol_type);
                        println!("  Address: 0x{:x}", symbol_info.address);
                        println!("  Size: {}", symbol_info.size);
                        println!("  Module: {}", symbol_info.module.unwrap_or_else(|| "N/A".to_string()));
                        
                        assert_eq!(symbol_info.name, symbol_name);
                        assert!(!symbol_info.symbol_type.is_empty());
                        assert!(symbol_info.address > 0);
                    }
                    Err(e) => {
                        println!("⚠️ F0040: lookup_symbol failed for '{}': {}", symbol_name, e);
                    }
                }
            }
        }
        Err(e) => {
            println!("⚠️ F0040: Could not start debugging session: {}", e);
        }
    }
    
    let _ = session.cleanup();
}

#[tokio::test]
async fn test_f0040_lookup_symbol_nonexistent() {
    // F0040: lookup_symbol - Test error handling for non-existent symbol
    println!("Testing F0040: lookup_symbol with non-existent symbol");
    
    let mut session = match TestSession::new(TestMode::Normal) {
        Ok(s) => s,
        Err(e) => {
            println!("⚠️ F0040: Could not create test session: {}", e);
            return;
        }
    };
    
    match session.start() {
        Ok(_pid) => {
            // Test looking up non-existent symbol
            let result = session.lldb_manager().lookup_symbol("nonexistent_symbol_12345");
            
            match result {
                Err(e) => {
                    println!("✅ F0040: Correctly handled non-existent symbol: {}", e);
                }
                Ok(symbol_info) => {
                    println!("⚠️ F0040: lookup_symbol unexpectedly succeeded for non-existent symbol: {}", 
                           symbol_info.name);
                }
            }
        }
        Err(e) => {
            println!("⚠️ F0040: Could not start debugging session: {}", e);
        }
    }
    
    let _ = session.cleanup();
}

#[tokio::test]
async fn test_variable_inspection_workflow() {
    // Integration test: Complete variable inspection workflow
    println!("Testing variable inspection workflow integration");
    
    let mut session = match TestSession::new(TestMode::Normal) {
        Ok(s) => s,
        Err(e) => {
            println!("⚠️ Workflow: Could not create test session: {}", e);
            return;
        }
    };
    
    match session.start() {
        Ok(pid) => {
            println!("✅ Workflow: Test session started with PID {}", pid);
            
            // Step 1: Get to variable-rich function
            let _ = session.lldb_manager().set_breakpoint("function_with_parameters");
            let _ = session.lldb_manager().continue_execution();
            
            // Step 2: List all local variables
            match session.lldb_manager().get_variables(None, None) {
                Ok(locals) => {
                    println!("✅ Workflow: Found {} local variables", locals.len());
                    
                    if !locals.is_empty() {
                        let first_var = &locals[0];
                        
                        // Step 3: Get detailed info for first variable
                        match session.lldb_manager().get_variable_info(&first_var.name) {
                            Ok(var_info) => {
                                println!("✅ Workflow: Got detailed info for variable '{}'", var_info.name);
                                println!("  Type: {}, Size: {} bytes, Address: 0x{:x}", 
                                       var_info.var_type, var_info.size, var_info.address);
                            }
                            Err(e) => {
                                println!("⚠️ Workflow: Failed to get variable info: {}", e);
                            }
                        }
                        
                        // Step 4: Try to modify variable if it's modifiable
                        if first_var.var_type.contains("int") {
                            match session.lldb_manager().set_variable(&first_var.name, "42") {
                                Ok(success) => {
                                    if !success.is_empty() {
                                        println!("✅ Workflow: Successfully modified variable '{}'", first_var.name);
                                    } else {
                                        println!("⚠️ Workflow: Variable modification reported failure");
                                    }
                                }
                                Err(e) => {
                                    println!("⚠️ Workflow: Variable modification failed: {}", e);
                                }
                            }
                        }
                    }
                }
                Err(e) => {
                    println!("⚠️ Workflow: Failed to get local variables: {}", e);
                }
            }
            
            // Step 5: Get global variables
            match session.lldb_manager().get_global_variables(None) {
                Ok(globals) => {
                    println!("✅ Workflow: Found {} global variables", globals.len());
                }
                Err(e) => {
                    println!("⚠️ Workflow: Failed to get global variables: {}", e);
                }
            }
            
            // Step 6: Evaluate some expressions
            let expressions = vec!["sizeof(int)", "1 + 1"];
            for expr in expressions {
                match session.lldb_manager().evaluate_expression(expr) {
                    Ok(result) => {
                        println!("✅ Workflow: Expression '{}' = {}", expr, result);
                    }
                    Err(e) => {
                        println!("⚠️ Workflow: Expression '{}' failed: {}", expr, e);
                    }
                }
            }
            
            // Step 7: Lookup function symbols
            let symbols = ["main", "showcase_variables"];
            for symbol in symbols {
                match session.lldb_manager().lookup_symbol(symbol) {
                    Ok(symbol_info) => {
                        println!("✅ Workflow: Found symbol '{}' at 0x{:x}", symbol, symbol_info.address);
                    }
                    Err(e) => {
                        println!("⚠️ Workflow: Symbol lookup for '{}' failed: {}", symbol, e);
                    }
                }
            }
            
            println!("✅ Workflow: Complete variable inspection workflow tested");
        }
        Err(e) => {
            println!("⚠️ Workflow: Could not start debugging session: {}", e);
        }
    }
    
    let _ = session.cleanup();
}