ghostscope-protocol 0.1.1

Shared protocol definitions and serialization glue for GhostScope components.
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
//! Type information system for GhostScope
//!
//! This module defines a comprehensive type system for representing type information from DWARF.
//! These types preserve the full richness of debugging information needed for accurate
//! memory reading and data formatting.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;

/// Type information with full fidelity from DWARF debugging data
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TypeInfo {
    /// Base/primitive type (int, float, char, etc.)
    BaseType {
        name: String,
        size: u64,
        encoding: u16, // Store DwAte as u16 for serialization
    },

    /// Pointer type
    PointerType {
        target_type: Box<TypeInfo>,
        size: u64,
    },

    /// Array type
    ArrayType {
        element_type: Box<TypeInfo>,
        element_count: Option<u64>,
        total_size: Option<u64>,
    },

    /// Struct/class type
    StructType {
        name: String,
        size: u64,
        members: Vec<StructMember>,
    },

    /// Union type
    UnionType {
        name: String,
        size: u64,
        members: Vec<StructMember>,
    },

    /// Enum type
    EnumType {
        name: String,
        size: u64,
        base_type: Box<TypeInfo>,
        variants: Vec<EnumVariant>,
    },

    /// Typedef (type alias)
    TypedefType {
        name: String,
        underlying_type: Box<TypeInfo>,
    },

    /// Qualified type (const, volatile, restrict)
    QualifiedType {
        qualifier: TypeQualifier,
        underlying_type: Box<TypeInfo>,
    },

    /// Function type
    FunctionType {
        return_type: Option<Box<TypeInfo>>,
        parameters: Vec<TypeInfo>,
    },

    /// Bitfield type: a view over an underlying integer type with bit offset/size
    BitfieldType {
        underlying_type: Box<TypeInfo>,
        bit_offset: u8,
        bit_size: u8,
    },

    /// Unresolved or unknown type
    UnknownType { name: String },

    /// Optimized out type (variable was optimized away by compiler)
    OptimizedOut { name: String },
}

/// Struct/union member information
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StructMember {
    pub name: String,
    pub member_type: TypeInfo,
    pub offset: u64,
    pub bit_offset: Option<u8>,
    pub bit_size: Option<u8>,
}

/// Enum variant information
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EnumVariant {
    pub name: String,
    pub value: i64,
}

/// Type qualifiers
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TypeQualifier {
    Const,
    Volatile,
    Restrict,
}

impl TypeInfo {
    /// Get the size in bytes of this type
    pub fn size(&self) -> u64 {
        match self {
            TypeInfo::BaseType { size, .. } => *size,
            TypeInfo::PointerType { size, .. } => *size,
            TypeInfo::ArrayType { total_size, .. } => total_size.unwrap_or(0),
            TypeInfo::StructType { size, .. } => *size,
            TypeInfo::UnionType { size, .. } => *size,
            TypeInfo::EnumType { size, .. } => *size,
            TypeInfo::TypedefType {
                underlying_type, ..
            } => underlying_type.size(),
            TypeInfo::QualifiedType {
                underlying_type, ..
            } => underlying_type.size(),
            TypeInfo::FunctionType { .. } => 8, // Function pointer size
            TypeInfo::BitfieldType {
                underlying_type, ..
            } => underlying_type.size(),
            TypeInfo::UnknownType { .. } => 0,
            TypeInfo::OptimizedOut { .. } => 0, // Optimized out has no size
        }
    }

    /// Format base type with encoding information (shared utility)
    fn format_base_type(name: &str, size: u64, encoding: u16) -> (String, String) {
        let human_readable = if encoding == gimli::constants::DW_ATE_boolean.0 as u16 {
            "bool".to_string()
        } else if encoding == gimli::constants::DW_ATE_float.0 as u16 {
            match size {
                4 => "f32".to_string(),
                8 => "f64".to_string(),
                _ => format!("float{size}"),
            }
        } else if encoding == gimli::constants::DW_ATE_signed.0 as u16
            || encoding == gimli::constants::DW_ATE_signed_char.0 as u16
        {
            match size {
                1 => "i8".to_string(),
                2 => "i16".to_string(),
                4 => "i32".to_string(),
                8 => "i64".to_string(),
                _ => format!("i{}", size * 8),
            }
        } else if encoding == gimli::constants::DW_ATE_unsigned.0 as u16
            || encoding == gimli::constants::DW_ATE_unsigned_char.0 as u16
        {
            match size {
                1 => "u8".to_string(),
                2 => "u16".to_string(),
                4 => "u32".to_string(),
                8 => "u64".to_string(),
                _ => format!("u{}", size * 8),
            }
        } else {
            name.to_string()
        };

        let encoding_str = if encoding == gimli::constants::DW_ATE_signed.0 as u16 {
            "signed"
        } else if encoding == gimli::constants::DW_ATE_unsigned.0 as u16 {
            "unsigned"
        } else if encoding == gimli::constants::DW_ATE_float.0 as u16 {
            "float"
        } else if encoding == gimli::constants::DW_ATE_boolean.0 as u16 {
            "bool"
        } else if encoding == gimli::constants::DW_ATE_address.0 as u16 {
            "address"
        } else if encoding == gimli::constants::DW_ATE_signed_char.0 as u16 {
            "signed char"
        } else if encoding == gimli::constants::DW_ATE_unsigned_char.0 as u16 {
            "unsigned char"
        } else {
            "unknown"
        };

        (human_readable, encoding_str.to_string())
    }

    /// Format qualifier string (shared utility)
    fn format_qualifier(qualifier: &TypeQualifier) -> &'static str {
        match qualifier {
            TypeQualifier::Const => "const",
            TypeQualifier::Volatile => "volatile",
            TypeQualifier::Restrict => "restrict",
        }
    }

    /// Get the type name for display
    pub fn type_name(&self) -> String {
        match self {
            TypeInfo::BaseType { name, .. } => name.clone(),
            TypeInfo::PointerType { target_type, .. } => {
                format!("{}*", target_type.type_name())
            }
            TypeInfo::ArrayType {
                element_type,
                element_count,
                ..
            } => {
                if let Some(count) = element_count {
                    format!("{}[{}]", element_type.type_name(), count)
                } else {
                    format!("{}[]", element_type.type_name())
                }
            }
            TypeInfo::StructType { name, .. } => format!("struct {name}"),
            TypeInfo::UnionType { name, .. } => format!("union {name}"),
            TypeInfo::EnumType { name, .. } => format!("enum {name}"),
            TypeInfo::TypedefType { name, .. } => name.clone(),
            TypeInfo::QualifiedType {
                qualifier,
                underlying_type,
            } => {
                format!(
                    "{} {}",
                    Self::format_qualifier(qualifier),
                    underlying_type.type_name()
                )
            }
            TypeInfo::FunctionType {
                return_type,
                parameters,
            } => {
                let return_str = return_type
                    .as_ref()
                    .map(|t| t.type_name())
                    .unwrap_or_else(|| "void".to_string());
                let param_str = parameters
                    .iter()
                    .map(|p| p.type_name())
                    .collect::<Vec<_>>()
                    .join(", ");
                format!("{return_str} ({param_str})")
            }
            TypeInfo::BitfieldType {
                underlying_type,
                bit_offset,
                bit_size,
            } => format!(
                "bitfield<{}:{}> {}",
                bit_offset,
                bit_size,
                underlying_type.type_name()
            ),
            TypeInfo::UnknownType { name } => name.clone(),
            TypeInfo::OptimizedOut { name } => format!("<optimized_out> {name}"),
        }
    }

    /// Check if this is a signed integer type
    pub fn is_signed_int(&self) -> bool {
        match self {
            TypeInfo::BaseType { encoding, .. } => {
                *encoding == gimli::constants::DW_ATE_signed.0 as u16
            }
            TypeInfo::TypedefType {
                underlying_type, ..
            } => underlying_type.is_signed_int(),
            TypeInfo::QualifiedType {
                underlying_type, ..
            } => underlying_type.is_signed_int(),
            TypeInfo::BitfieldType {
                underlying_type, ..
            } => underlying_type.is_signed_int(),
            TypeInfo::OptimizedOut { .. } => false,
            _ => false,
        }
    }

    /// Check if this is an unsigned integer type
    pub fn is_unsigned_int(&self) -> bool {
        match self {
            TypeInfo::BaseType { encoding, .. } => {
                *encoding == gimli::constants::DW_ATE_unsigned.0 as u16
            }
            TypeInfo::TypedefType {
                underlying_type, ..
            } => underlying_type.is_unsigned_int(),
            TypeInfo::QualifiedType {
                underlying_type, ..
            } => underlying_type.is_unsigned_int(),
            TypeInfo::BitfieldType {
                underlying_type, ..
            } => underlying_type.is_unsigned_int(),
            TypeInfo::OptimizedOut { .. } => false,
            _ => false,
        }
    }

    /// Check if this is a floating point type
    pub fn is_float(&self) -> bool {
        match self {
            TypeInfo::BaseType { encoding, .. } => {
                *encoding == gimli::constants::DW_ATE_float.0 as u16
            }
            TypeInfo::TypedefType {
                underlying_type, ..
            } => underlying_type.is_float(),
            TypeInfo::QualifiedType {
                underlying_type, ..
            } => underlying_type.is_float(),
            TypeInfo::OptimizedOut { .. } => false,
            _ => false,
        }
    }

    /// Check if this is a pointer type
    pub fn is_pointer(&self) -> bool {
        match self {
            TypeInfo::PointerType { .. } => true,
            TypeInfo::TypedefType {
                underlying_type, ..
            } => underlying_type.is_pointer(),
            TypeInfo::QualifiedType {
                underlying_type, ..
            } => underlying_type.is_pointer(),
            _ => false,
        }
    }

    /// Check if this is an array type
    pub fn is_array(&self) -> bool {
        match self {
            TypeInfo::ArrayType { .. } => true,
            TypeInfo::TypedefType {
                underlying_type, ..
            } => underlying_type.is_array(),
            TypeInfo::QualifiedType {
                underlying_type, ..
            } => underlying_type.is_array(),
            _ => false,
        }
    }

    /// Get the underlying type, skipping typedefs and qualifiers
    pub fn underlying_type(&self) -> &TypeInfo {
        match self {
            TypeInfo::TypedefType {
                underlying_type, ..
            } => underlying_type.underlying_type(),
            TypeInfo::QualifiedType {
                underlying_type, ..
            } => underlying_type.underlying_type(),
            TypeInfo::BitfieldType {
                underlying_type, ..
            } => underlying_type.underlying_type(),
            _ => self,
        }
    }

    /// Create a signed integer base type (compatibility method for compiler)
    pub fn signed_int(size: u64) -> Self {
        TypeInfo::BaseType {
            name: match size {
                1 => "i8",
                2 => "i16",
                4 => "i32",
                8 => "i64",
                _ => "iN",
            }
            .to_string(),
            size,
            encoding: gimli::constants::DW_ATE_signed.0 as u16,
        }
    }

    /// Create a floating point base type (compatibility method for compiler)
    pub fn float(size: u64) -> Self {
        TypeInfo::BaseType {
            name: match size {
                4 => "f32",
                8 => "f64",
                _ => "fN",
            }
            .to_string(),
            size,
            encoding: gimli::constants::DW_ATE_float.0 as u16,
        }
    }
}

/// Type cache for performance optimization
pub type TypeCache = HashMap<gimli::UnitOffset, Option<TypeInfo>>;

impl TypeInfo {}

impl fmt::Display for TypeInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TypeInfo::BaseType {
                name,
                size,
                encoding,
            } => {
                let (human_readable, _) = Self::format_base_type(name, *size, *encoding);
                write!(f, "{human_readable}")
            }
            TypeInfo::PointerType { target_type, .. } => {
                write!(f, "*{target_type}")
            }
            TypeInfo::ArrayType {
                element_type,
                element_count,
                ..
            } => match element_count {
                Some(n) => write!(f, "[{n} x {element_type}]"),
                None => write!(f, "[]{element_type}"),
            },
            TypeInfo::StructType { name, .. } => {
                if name.is_empty() {
                    write!(f, "struct")
                } else {
                    write!(f, "struct {name}")
                }
            }
            TypeInfo::UnionType { name, .. } => {
                if name.is_empty() {
                    write!(f, "union")
                } else {
                    write!(f, "union {name}")
                }
            }
            TypeInfo::EnumType { name, .. } => {
                if name.is_empty() {
                    write!(f, "enum")
                } else {
                    write!(f, "enum {name}")
                }
            }
            TypeInfo::BitfieldType {
                underlying_type,
                bit_offset,
                bit_size,
            } => {
                write!(f, "bitfield<{bit_offset}:{bit_size}> {underlying_type}")
            }
            TypeInfo::TypedefType {
                name,
                underlying_type,
            } => {
                if name.is_empty() {
                    write!(f, "{underlying_type}")
                } else {
                    write!(f, "{name}")
                }
            }
            TypeInfo::QualifiedType {
                qualifier,
                underlying_type,
            } => {
                write!(
                    f,
                    "{} {}",
                    Self::format_qualifier(qualifier),
                    underlying_type
                )
            }
            TypeInfo::FunctionType {
                return_type,
                parameters,
            } => {
                let params_str = parameters
                    .iter()
                    .map(|p| p.to_string())
                    .collect::<Vec<_>>()
                    .join(", ");
                match return_type {
                    Some(ret) => write!(f, "fn({params_str}) -> {ret}"),
                    None => write!(f, "fn({params_str})"),
                }
            }
            TypeInfo::UnknownType { name } => {
                if name.is_empty() {
                    write!(f, "unknown")
                } else {
                    write!(f, "{name}")
                }
            }
            TypeInfo::OptimizedOut { name } => {
                if name.is_empty() {
                    write!(f, "<optimized_out>")
                } else {
                    write!(f, "<optimized_out> {name}")
                }
            }
        }
    }
}