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
use crate::lldb_manager::{LldbManager, ThreadInfo};
use crate::error::IncodeResult;
use crate::tools::{Tool, ToolResponse};
use std::collections::HashMap;
use serde_json::{json, Value};
use async_trait::async_trait;
use tracing::{debug, error};

pub fn list_threads(
    lldb_manager: &LldbManager,
    arguments: HashMap<String, Value>,
) -> IncodeResult<Value> {
    debug!("Thread Management: list_threads called with args: {:?}", arguments);
    
    let include_details = arguments.get("include_details")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
        
    let filter_state = arguments.get("filter_state")
        .and_then(|v| v.as_str());
    
    match lldb_manager.list_threads() {
        Ok(threads) => {
            debug!("Found {} threads", threads.len());
            
            // Apply state filter if specified
            let filtered_threads: Vec<&ThreadInfo> = if let Some(state) = filter_state {
                threads.iter()
                    .filter(|t| t.state.contains(state))
                    .collect()
            } else {
                threads.iter().collect()
            };
            
            let thread_list: Vec<Value> = filtered_threads.iter().map(|thread| {
                if include_details {
                    json!({
                        "thread_id": thread.thread_id,
                        "index": thread.index,
                        "name": thread.name,
                        "state": thread.state,
                        "stop_reason": thread.stop_reason,
                        "queue_name": thread.queue_name,
                        "frame_count": thread.frame_count,
                        "current_frame": thread.current_frame.as_ref().map(|frame| json!({
                            "index": frame.index,
                            "function_name": frame.function_name,
                            "file_path": frame.file_path,
                            "line_number": frame.line_number,
                            "address": format!("0x{:x}", frame.address)
                        }))
                    })
                } else {
                    json!({
                        "thread_id": thread.thread_id,
                        "index": thread.index,
                        "name": thread.name,
                        "state": thread.state
                    })
                }
            }).collect();
            
            Ok(json!({
                "success": true,
                "threads": thread_list,
                "total_count": filtered_threads.len(),
                "filter_applied": filter_state
            }))
        }
        Err(e) => {
            error!("Failed to list threads: {}", e);
            Ok(json!({
                "success": false,
                "error": e.to_string(),
                "threads": []
            }))
        }
    }
}

pub fn select_thread(
    lldb_manager: &mut LldbManager,
    arguments: HashMap<String, Value>,
) -> IncodeResult<Value> {
    debug!("Thread Management: select_thread called with args: {:?}", arguments);
    
    let thread_id = arguments.get("thread_id")
        .and_then(|v| v.as_u64())
        .ok_or_else(|| crate::error::IncodeError::invalid_parameter("thread_id is required and must be a number"))?
        as u32;
    
    let include_frames = arguments.get("include_frames")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
    
    match lldb_manager.select_thread(thread_id) {
        Ok(thread_info) => {
            debug!("Selected thread {}: {}", thread_id, thread_info.name.as_deref().unwrap_or("unnamed"));
            
            let mut result = json!({
                "success": true,
                "selected_thread": {
                    "thread_id": thread_info.thread_id,
                    "index": thread_info.index,
                    "name": thread_info.name,
                    "state": thread_info.state,
                    "stop_reason": thread_info.stop_reason,
                    "queue_name": thread_info.queue_name,
                    "frame_count": thread_info.frame_count
                }
            });
            
            if include_frames && thread_info.current_frame.is_some() {
                result["selected_thread"]["current_frame"] = json!({
                    "index": thread_info.current_frame.as_ref().unwrap().index,
                    "function_name": thread_info.current_frame.as_ref().unwrap().function_name,
                    "file_path": thread_info.current_frame.as_ref().unwrap().file_path,
                    "line_number": thread_info.current_frame.as_ref().unwrap().line_number,
                    "address": format!("0x{:x}", thread_info.current_frame.as_ref().unwrap().address)
                });
            }
            
            Ok(result)
        }
        Err(e) => {
            error!("Failed to select thread {}: {}", thread_id, e);
            Ok(json!({
                "success": false,
                "error": e.to_string(),
                "thread_id": thread_id
            }))
        }
    }
}

pub fn get_thread_info(
    lldb_manager: &LldbManager,
    arguments: HashMap<String, Value>,
) -> IncodeResult<Value> {
    debug!("Thread Management: get_thread_info called with args: {:?}", arguments);
    
    let thread_id = arguments.get("thread_id")
        .and_then(|v| v.as_u64())
        .ok_or_else(|| crate::error::IncodeError::invalid_parameter("thread_id is required and must be a number"))?
        as u32;
    
    let include_stack = arguments.get("include_stack")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
    
    let include_registers = arguments.get("include_registers")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
    
    // First, get all threads to find the requested one
    match lldb_manager.list_threads() {
        Ok(threads) => {
            if let Some(thread) = threads.iter().find(|t| t.thread_id == thread_id) {
                let mut result = json!({
                    "success": true,
                    "thread_info": {
                        "thread_id": thread.thread_id,
                        "index": thread.index,
                        "name": thread.name,
                        "state": thread.state,
                        "stop_reason": thread.stop_reason,
                        "queue_name": thread.queue_name,
                        "frame_count": thread.frame_count,
                        "current_frame": thread.current_frame.as_ref().map(|frame| json!({
                            "index": frame.index,
                            "function_name": frame.function_name,
                            "file_path": frame.file_path,
                            "line_number": frame.line_number,
                            "address": format!("0x{:x}", frame.address),
                            "is_inlined": frame.is_inlined
                        }))
                    }
                });
                
                // Add stack information if requested
                if include_stack {
                    // TODO: Implement stack frame enumeration
                    result["thread_info"]["stack_frames"] = json!([]);
                }
                
                // Add register information if requested
                if include_registers {
                    // TODO: Implement register reading for specific thread
                    result["thread_info"]["registers"] = json!({});
                }
                
                Ok(result)
            } else {
                Ok(json!({
                    "success": false,
                    "error": format!("Thread {} not found", thread_id),
                    "thread_id": thread_id
                }))
            }
        }
        Err(e) => {
            error!("Failed to get thread info for {}: {}", thread_id, e);
            Ok(json!({
                "success": false,
                "error": e.to_string(),
                "thread_id": thread_id
            }))
        }
    }
}

pub fn suspend_thread(
    _lldb_manager: &mut LldbManager,
    arguments: HashMap<String, Value>,
) -> IncodeResult<Value> {
    debug!("Thread Management: suspend_thread called with args: {:?}", arguments);
    
    let thread_id = arguments.get("thread_id")
        .and_then(|v| v.as_u64())
        .ok_or_else(|| crate::error::IncodeError::invalid_parameter("thread_id is required and must be a number"))?
        as u32;
    
    // TODO: Implement actual thread suspension
    debug!("Mock: Suspending thread {}", thread_id);
    
    Ok(json!({
        "success": true,
        "thread_id": thread_id,
        "status": "suspended",
        "message": format!("Thread {} suspended (mock implementation)", thread_id)
    }))
}

pub fn resume_thread(
    _lldb_manager: &mut LldbManager,
    arguments: HashMap<String, Value>,
) -> IncodeResult<Value> {
    debug!("Thread Management: resume_thread called with args: {:?}", arguments);
    
    let thread_id = arguments.get("thread_id")
        .and_then(|v| v.as_u64())
        .ok_or_else(|| crate::error::IncodeError::invalid_parameter("thread_id is required and must be a number"))?
        as u32;
    
    // TODO: Implement actual thread resumption
    debug!("Mock: Resuming thread {}", thread_id);
    
    Ok(json!({
        "success": true,
        "thread_id": thread_id,
        "status": "running",
        "message": format!("Thread {} resumed (mock implementation)", thread_id)
    }))
}

// Tool implementations for MCP protocol

pub struct ListThreadsTool;

#[async_trait]
impl Tool for ListThreadsTool {
    fn name(&self) -> &'static str {
        "list_threads"
    }
    
    fn description(&self) -> &'static str {
        "List all threads with IDs and states"
    }
    
    fn parameters(&self) -> Value {
        json!({
            "include_details": {
                "type": "boolean",
                "description": "Include detailed thread information including frames",
                "default": false
            },
            "filter_state": {
                "type": "string",
                "description": "Filter threads by state (stopped, running, etc.)"
            }
        })
    }
    
    async fn execute(&self, arguments: HashMap<String, Value>, manager: &mut LldbManager) -> IncodeResult<ToolResponse> {
        match list_threads(manager, arguments) {
            Ok(result) => Ok(ToolResponse::Success(result.to_string())),
            Err(e) => Ok(ToolResponse::Error(e.to_string())),
        }
    }
}

pub struct SelectThreadTool;

#[async_trait]
impl Tool for SelectThreadTool {
    fn name(&self) -> &'static str {
        "select_thread"
    }
    
    fn description(&self) -> &'static str {
        "Switch to specific thread for debugging"
    }
    
    fn parameters(&self) -> Value {
        json!({
            "thread_id": {
                "type": "number",
                "description": "Thread ID to select"
            },
            "include_frames": {
                "type": "boolean",
                "description": "Include current frame information",
                "default": false
            }
        })
    }
    
    async fn execute(&self, arguments: HashMap<String, Value>, manager: &mut LldbManager) -> IncodeResult<ToolResponse> {
        match select_thread(manager, arguments) {
            Ok(result) => Ok(ToolResponse::Success(result.to_string())),
            Err(e) => Ok(ToolResponse::Error(e.to_string())),
        }
    }
}

pub struct GetThreadInfoTool;

#[async_trait]
impl Tool for GetThreadInfoTool {
    fn name(&self) -> &'static str {
        "get_thread_info"
    }
    
    fn description(&self) -> &'static str {
        "Get thread details (state, stack, registers)"
    }
    
    fn parameters(&self) -> Value {
        json!({
            "thread_id": {
                "type": "number",
                "description": "Thread ID to get info for"
            },
            "include_stack": {
                "type": "boolean",
                "description": "Include stack frame information",
                "default": false
            },
            "include_registers": {
                "type": "boolean",
                "description": "Include register information",
                "default": false
            }
        })
    }
    
    async fn execute(&self, arguments: HashMap<String, Value>, manager: &mut LldbManager) -> IncodeResult<ToolResponse> {
        match get_thread_info(manager, arguments) {
            Ok(result) => Ok(ToolResponse::Success(result.to_string())),
            Err(e) => Ok(ToolResponse::Error(e.to_string())),
        }
    }
}

pub struct SuspendThreadTool;

#[async_trait]
impl Tool for SuspendThreadTool {
    fn name(&self) -> &'static str {
        "suspend_thread"
    }
    
    fn description(&self) -> &'static str {
        "Suspend specific thread execution"
    }
    
    fn parameters(&self) -> Value {
        json!({
            "thread_id": {
                "type": "number",
                "description": "Thread ID to suspend"
            }
        })
    }
    
    async fn execute(&self, arguments: HashMap<String, Value>, manager: &mut LldbManager) -> IncodeResult<ToolResponse> {
        match suspend_thread(manager, arguments) {
            Ok(result) => Ok(ToolResponse::Success(result.to_string())),
            Err(e) => Ok(ToolResponse::Error(e.to_string())),
        }
    }
}

pub struct ResumeThreadTool;

#[async_trait]
impl Tool for ResumeThreadTool {
    fn name(&self) -> &'static str {
        "resume_thread"
    }
    
    fn description(&self) -> &'static str {
        "Resume suspended thread"
    }
    
    fn parameters(&self) -> Value {
        json!({
            "thread_id": {
                "type": "number",
                "description": "Thread ID to resume"
            }
        })
    }
    
    async fn execute(&self, arguments: HashMap<String, Value>, manager: &mut LldbManager) -> IncodeResult<ToolResponse> {
        match resume_thread(manager, arguments) {
            Ok(result) => Ok(ToolResponse::Success(result.to_string())),
            Err(e) => Ok(ToolResponse::Error(e.to_string())),
        }
    }
}