membrowse 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. membrowse-0.0.1/CHANGELOG.md +43 -0
  2. membrowse-0.0.1/CLAUDE.md +328 -0
  3. membrowse-0.0.1/LICENSE +674 -0
  4. membrowse-0.0.1/MANIFEST.in +28 -0
  5. membrowse-0.0.1/PKG-INFO +267 -0
  6. membrowse-0.0.1/README.md +229 -0
  7. membrowse-0.0.1/membrowse/__init__.py +25 -0
  8. membrowse-0.0.1/membrowse/analysis/__init__.py +21 -0
  9. membrowse-0.0.1/membrowse/analysis/dwarf.py +798 -0
  10. membrowse-0.0.1/membrowse/analysis/mapper.py +138 -0
  11. membrowse-0.0.1/membrowse/analysis/sections.py +80 -0
  12. membrowse-0.0.1/membrowse/analysis/sources.py +232 -0
  13. membrowse-0.0.1/membrowse/analysis/symbols.py +160 -0
  14. membrowse-0.0.1/membrowse/api/__init__.py +13 -0
  15. membrowse-0.0.1/membrowse/api/client.py +164 -0
  16. membrowse-0.0.1/membrowse/cli.py +101 -0
  17. membrowse-0.0.1/membrowse/commands/__init__.py +1 -0
  18. membrowse-0.0.1/membrowse/commands/onboard.py +264 -0
  19. membrowse-0.0.1/membrowse/commands/report.py +350 -0
  20. membrowse-0.0.1/membrowse/core/__init__.py +35 -0
  21. membrowse-0.0.1/membrowse/core/analyzer.py +167 -0
  22. membrowse-0.0.1/membrowse/core/cli.py +118 -0
  23. membrowse-0.0.1/membrowse/core/exceptions.py +35 -0
  24. membrowse-0.0.1/membrowse/core/generator.py +126 -0
  25. membrowse-0.0.1/membrowse/core/models.py +77 -0
  26. membrowse-0.0.1/membrowse/linker/__init__.py +17 -0
  27. membrowse-0.0.1/membrowse/linker/cli.py +37 -0
  28. membrowse-0.0.1/membrowse/linker/elf_info.py +242 -0
  29. membrowse-0.0.1/membrowse/linker/parser.py +936 -0
  30. membrowse-0.0.1/membrowse/utils/__init__.py +1 -0
  31. membrowse-0.0.1/membrowse/utils/git.py +198 -0
  32. membrowse-0.0.1/membrowse.egg-info/SOURCES.txt +49 -0
  33. membrowse-0.0.1/onboard-action/requirements.txt +2 -0
  34. membrowse-0.0.1/pr-action/requirements.txt +2 -0
  35. membrowse-0.0.1/pyproject.toml +67 -0
  36. membrowse-0.0.1/scripts/membrowse +6 -0
  37. membrowse-0.0.1/setup.cfg +4 -0
  38. membrowse-0.0.1/setup.py +70 -0
  39. membrowse-0.0.1/tests/test_architecture_detection.py +282 -0
  40. membrowse-0.0.1/tests/test_base.py +38 -0
  41. membrowse-0.0.1/tests/test_dwarf_location_expression.py +134 -0
  42. membrowse-0.0.1/tests/test_hybrid_mapping.py +114 -0
  43. membrowse-0.0.1/tests/test_memory_analysis.py +584 -0
  44. membrowse-0.0.1/tests/test_memory_mapper.py +331 -0
  45. membrowse-0.0.1/tests/test_memory_regions.py +608 -0
  46. membrowse-0.0.1/tests/test_memory_regions_error_handling.py +622 -0
  47. membrowse-0.0.1/tests/test_memory_regions_unit.py +762 -0
  48. membrowse-0.0.1/tests/test_memory_report.py +226 -0
  49. membrowse-0.0.1/tests/test_micropython_firmware.py +816 -0
  50. membrowse-0.0.1/tests/test_source_file_mapping.py +239 -0
  51. membrowse-0.0.1/tests/test_static_variable_source_mapping.py +558 -0
  52. membrowse-0.0.1/tests/test_utils.py +132 -0
@@ -0,0 +1,43 @@
1
+ # Changelog
2
+
3
+ All notable changes to MemBrowse will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.0.1] - 2024-10-22
9
+
10
+ ### Added
11
+ - Initial PyPI release with core functionality
12
+ - PyPI packaging support for easy installation via `pip install membrowse`
13
+ - Unified `membrowse` CLI with `report` and `onboard` subcommands
14
+ - ELF file analysis with DWARF debug information processing
15
+ - Linker script parsing with multi-architecture support
16
+ - Memory region extraction and validation
17
+ - Symbol-to-source file mapping
18
+ - GitHub Actions integration (pr-action and onboard-action)
19
+ - Support for ARM, Xtensa (ESP32), RISC-V, and other architectures
20
+ - API client for uploading reports to MemBrowse platform
21
+ - Comprehensive package metadata in `pyproject.toml`
22
+ - Modern Python packaging configuration following PEP 621
23
+ - MANIFEST.in for proper source distribution
24
+ - PyPI classifiers for better discoverability
25
+ - Project URLs (homepage, documentation, issues, changelog)
26
+
27
+ ### Features
28
+ - Architecture-agnostic analysis relying on DWARF format
29
+ - Intelligent linker script parsing with expression evaluation
30
+ - Hierarchical memory region support
31
+ - Source file resolution from debug symbols
32
+ - Optional `--skip-line-program` flag for faster processing
33
+ - Local mode (JSON output) and upload mode (cloud integration)
34
+ - Automatic Git metadata detection in GitHub Actions
35
+
36
+ ### Supported Platforms
37
+ - STM32 and ARM Cortex-M microcontrollers
38
+ - ESP32 and ESP8266 (Xtensa architecture)
39
+ - Nordic nRF series
40
+ - RISC-V embedded targets
41
+ - Any platform using ELF files and GNU LD linker scripts
42
+
43
+ [0.0.1]: https://github.com/membrowse/membrowse-action/releases/tag/v0.0.1
@@ -0,0 +1,328 @@
1
+ # CLAUDE.md
2
+
3
+ This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4
+
5
+ ## Project Overview
6
+
7
+ MemBrowse GitHub Actions is a collection of GitHub Actions for analyzing memory usage in embedded firmware and uploading reports to the MemBrowse SaaS platform. The repository contains two main actions:
8
+
9
+ - **pr-action**: Analyzes memory usage in pull requests and push events
10
+ - **onboard-action**: Performs historical analysis across multiple commits for onboarding
11
+
12
+ ## Command-Line Interface
13
+
14
+ ### Unified CLI (`membrowse`)
15
+
16
+ MemBrowse provides a single `membrowse` command with subcommands for different operations:
17
+
18
+ #### `membrowse report` - Generate Memory Footprint Reports
19
+
20
+ Core analysis tool that generates memory footprint reports from ELF files and linker scripts.
21
+
22
+ **Local Mode (Default)** - Generate JSON report without Git metadata or uploading:
23
+ ```bash
24
+ # Minimal usage - outputs JSON to stdout (quiet mode, only errors shown)
25
+ membrowse report firmware.elf "linker.ld"
26
+
27
+ # Save to file
28
+ membrowse report firmware.elf "linker.ld" > report.json
29
+
30
+ # With verbose output to see progress
31
+ membrowse report firmware.elf "linker.ld" --verbose > report.json
32
+
33
+ # Multiple linker scripts
34
+ membrowse report firmware.elf "mem.ld sections.ld"
35
+ ```
36
+
37
+ **Note**: By default, only errors are shown. Use `--verbose` to see progress messages.
38
+
39
+ **Upload Mode** - Upload report to MemBrowse platform:
40
+ ```bash
41
+ # Upload without Git metadata
42
+ membrowse report firmware.elf "linker.ld" --upload \
43
+ --api-key "$API_KEY" \
44
+ --target-name "esp32" \
45
+ --api-url "https://membrowse.appspot.com/api/upload"
46
+
47
+ # Upload with Git metadata (manual)
48
+ membrowse report firmware.elf "linker.ld" --upload \
49
+ --api-key "$API_KEY" \
50
+ --target-name "stm32f4" \
51
+ --api-url "https://membrowse.appspot.com/api/upload" \
52
+ --commit-sha "abc123" \
53
+ --branch-name "main" \
54
+ --repo-name "my-repo"
55
+ ```
56
+
57
+ **GitHub Actions Mode** - Automatically detect Git metadata:
58
+ ```bash
59
+ # Auto-detects Git metadata from GitHub environment
60
+ membrowse report firmware.elf "linker.ld" --github \
61
+ --target-name "esp32" \
62
+ --api-key "$API_KEY" \
63
+ --api-url "https://membrowse.appspot.com/api/upload"
64
+ ```
65
+
66
+ **Modes:**
67
+ - **Local mode (default)**: Generates JSON report and outputs to stdout
68
+ - **Upload mode**: Uploads report to MemBrowse platform (requires `--upload` flag)
69
+ - **GitHub mode**: Auto-detects Git metadata and uploads (requires `--github` flag)
70
+
71
+ **Key Features:**
72
+ - Does NOT execute `git` commands unless `--github` flag is used
73
+ - Target name only required when uploading
74
+ - All Git metadata is optional
75
+
76
+ #### `membrowse onboard` - Historical Analysis
77
+
78
+ Analyzes memory footprints across multiple historical commits and uploads them to the MemBrowse platform:
79
+ ```bash
80
+ # Analyze last 50 commits and upload to MemBrowse
81
+ membrowse onboard 50 "make clean && make" build/firmware.elf "linker.ld" \
82
+ stm32f4 "$API_KEY" https://membrowse.appspot.com/api/upload
83
+
84
+ # ESP-IDF project (API URL is optional, defaults to https://membrowse.appspot.com/api/upload)
85
+ membrowse onboard 25 "idf.py build" build/firmware.elf \
86
+ "build/esp-idf/esp32/esp32.project.ld" esp32 "$API_KEY"
87
+ ```
88
+
89
+ ### Python CLI (Low-level)
90
+
91
+ Direct Python interface for advanced usage:
92
+
93
+ ```bash
94
+ # Generate memory report with maximum coverage (default)
95
+ python -m membrowse.core.cli --elf-path firmware.elf --output report.json
96
+
97
+ # With memory regions
98
+ python -m membrowse.core.cli --elf-path firmware.elf --memory-regions regions.json --output report.json
99
+
100
+ # Fast mode: skip line program processing (88% coverage on ARM, 24% faster)
101
+ python -m membrowse.core.cli --elf-path firmware.elf --output report.json --skip-line-program
102
+
103
+ # Verbose output with performance metrics
104
+ python -m membrowse.core.cli --elf-path firmware.elf --output report.json --verbose
105
+ ```
106
+
107
+ ### Performance Options
108
+
109
+ #### --skip-line-program flag
110
+
111
+ Skips DWARF line program processing for faster analysis at the cost of reduced source file coverage.
112
+
113
+ **When to use:**
114
+ - ✅ Build speed is critical (CI/CD, iterative development)
115
+ - ✅ 88% coverage acceptable (ARM) or 65% (ESP32)
116
+ - ✅ Large firmware (>10MB) with slow processing
117
+
118
+ **When to avoid:**
119
+ - ❌ Need 100% symbol coverage
120
+ - ❌ Processing time not a concern (<10s)
121
+ - ❌ Detailed profiling of compiler-optimized code
122
+
123
+ **Performance impact:**
124
+ - ARM Cortex-M (STM32): 9.3s → 7.1s (24% faster), 97% → 88% coverage
125
+ - Xtensa (ESP32): 30.1s → 20.8s (31% faster), 76% → 65% coverage
126
+
127
+ See `SKIP_LINE_PROGRAM_SUMMARY.md` for detailed analysis.
128
+
129
+ ## Testing
130
+
131
+ ### Run Tests
132
+ ```bash
133
+ python -m pytest tests/
134
+ ```
135
+
136
+ ### Run Specific Tests
137
+ ```bash
138
+ # Run a specific test file
139
+ python -m pytest tests/test_memory_regions.py -v
140
+
141
+ # Run a specific test class
142
+ python -m pytest tests/test_memory_analysis.py::TestMemoryAnalysis -v
143
+
144
+ # Run with verbose output and stop on first failure
145
+ python -m pytest tests/ -v -x
146
+ ```
147
+
148
+ ### Prerequisites
149
+ Install required system dependencies:
150
+ ```bash
151
+ # Install C compiler (required for tests)
152
+ sudo apt-get install gcc # Standard GCC
153
+ # OR for ARM targets
154
+ sudo apt-get install gcc-arm-none-eabi # ARM cross-compiler
155
+
156
+ # Python dependencies are in requirements.txt files
157
+ pip install -r onboard-action/requirements.txt
158
+ pip install -r pr-action/requirements.txt
159
+ ```
160
+
161
+ ### Test Structure
162
+ - Tests use mock ELF files and linker scripts in `tests/` directory
163
+ - Integration tests validate the complete `collect_report.sh` workflow
164
+ - Memory region parsing tests verify linker script parsing accuracy
165
+ - ELF analysis tests validate symbol extraction and architecture detection
166
+
167
+ ## Architecture Overview
168
+
169
+ ### Package Structure
170
+ The codebase is organized as a proper Python package:
171
+
172
+ ```
173
+ membrowse/ # Main Python package
174
+ ├── __init__.py # Public API exports
175
+
176
+ ├── core/ # Core coordination
177
+ │ ├── __init__.py
178
+ │ ├── cli.py # CLI interface
179
+ │ ├── generator.py # Memory report generation
180
+ │ ├── analyzer.py # Main ELF analysis coordination
181
+ │ ├── models.py # Data classes (MemoryRegion, Symbol, etc.)
182
+ │ └── exceptions.py # Exception hierarchy
183
+
184
+ ├── analysis/ # Analysis components
185
+ │ ├── __init__.py
186
+ │ ├── dwarf.py # DWARF debug information processing
187
+ │ ├── sources.py # Source file resolution
188
+ │ ├── symbols.py # ELF symbol extraction
189
+ │ ├── sections.py # ELF section analysis
190
+ │ └── mapper.py # Section-to-region mapping
191
+
192
+ ├── linker/ # Linker script parsing
193
+ │ ├── __init__.py
194
+ │ ├── parser.py # Linker script parser (library)
195
+ │ ├── cli.py # Linker parser CLI
196
+ │ └── elf_info.py # ELF architecture detection
197
+
198
+ ├── api/ # API client
199
+ │ ├── __init__.py
200
+ │ └── client.py # Report upload to MemBrowse
201
+
202
+ ├── cli.py # Main CLI entry point
203
+
204
+ ├── commands/ # CLI subcommands
205
+ │ ├── __init__.py
206
+ │ ├── report.py # 'report' subcommand
207
+ │ └── onboard.py # 'onboard' subcommand
208
+
209
+ ├── utils/ # Utilities
210
+ │ ├── __init__.py
211
+ │ └── git.py # Git metadata detection
212
+
213
+ scripts/ # Shell wrappers
214
+ └── membrowse # Main CLI wrapper (calls membrowse.cli)
215
+ ```
216
+
217
+ ### CLI Architecture
218
+
219
+ **`membrowse` command** - Unified CLI interface:
220
+ - Single entry point with subcommands (`report`, `onboard`)
221
+ - Python-based with proper argument parsing and error handling
222
+ - Shell wrapper provides seamless installation via setup.py
223
+
224
+ **`membrowse report`** - Core analysis command:
225
+ - Parses linker scripts to extract memory regions
226
+ - Analyzes ELF files to generate memory footprint reports
227
+ - Outputs JSON to stdout (default) or uploads to platform
228
+ - `--github` flag enables automatic Git metadata detection from GitHub environment
229
+ - Does NOT execute `git` commands unless `--github` flag is used
230
+
231
+ **`membrowse onboard`** - Historical analysis command:
232
+ - Iterates through N commits, checking out each one
233
+ - Builds firmware at each commit
234
+ - Automatically extracts Git metadata for each commit
235
+ - Uploads memory footprint reports with full commit context
236
+
237
+ ### Action Structure
238
+ Each action (`pr-action/`, `onboard-action/`) contains:
239
+ - `action.yml`: GitHub Actions definition
240
+ - `entrypoint.sh`: Action-specific entry point that calls scripts
241
+ - `requirements.txt`: Python dependencies
242
+
243
+ ### Key Processing Flow
244
+ 1. **Architecture Detection**: `linker/elf_info.py` analyzes ELF files to determine target architecture (ARM, Xtensa, RISC-V, etc.)
245
+ 2. **Linker Script Parsing**: `linker/parser.py` parses GNU LD linker scripts using architecture-specific strategies
246
+ 3. **Memory Analysis**: The modular analysis system combines ELF analysis with memory regions to generate comprehensive reports
247
+ 4. **Report Upload**: `api/client.py` sends reports to MemBrowse platform (optional)
248
+
249
+ ### Advanced Features
250
+ - **DWARF Debug Info**: Extracts source file mappings from debug symbols (prioritizes definition locations over declarations)
251
+ - **Multi-Architecture Support**: Handles different embedded platforms (STM32, ESP32, Nordic, etc.)
252
+ - **Expression Evaluation**: Safely evaluates linker script expressions and variables
253
+ - **Hierarchical Memory Regions**: Supports parent-child memory region relationships
254
+
255
+ ## Architecture-Specific Parsing
256
+
257
+ The system automatically detects target architecture from ELF files and applies appropriate parsing strategies:
258
+
259
+ - **ESP32/ESP8266**: Handles Xtensa-specific linker script patterns and variables
260
+ - **STM32/ARM**: Processes standard ARM Cortex-M memory layouts
261
+ - **Nordic nRF**: Supports SoftDevice and bootloader-aware memory regions
262
+ - **RISC-V**: Handles QEMU and embedded RISC-V targets
263
+
264
+ ## Memory Region Validation
265
+
266
+ The parser includes intelligent validation that:
267
+ - Detects hierarchical memory relationships (e.g., FLASH parent with FLASH_APP child)
268
+ - Validates region overlaps and containment
269
+ - Provides architecture-specific default variables
270
+ - Handles complex linker script expressions with variable substitution
271
+
272
+ ## Development Commands
273
+
274
+ ### Code Quality
275
+ ```bash
276
+ # Run pylint on membrowse package
277
+ pylint membrowse/
278
+
279
+ # Run pylint on tests
280
+ pylint tests/
281
+
282
+ # Check all Python code with scores
283
+ pylint membrowse/ tests/ --score=yes
284
+ ```
285
+
286
+ ### Manual Testing
287
+ ```bash
288
+ # Test linker script parsing
289
+ python -m membrowse.linker.cli path/to/linker.ld
290
+
291
+ # Test ELF analysis (Python CLI)
292
+ python -m membrowse.core.cli --elf-path firmware.elf --memory-regions regions.json --output report.json
293
+
294
+ # Test report command - local mode (no Git, no upload)
295
+ membrowse report firmware.elf "linker1.ld linker2.ld" > report.json
296
+
297
+ # Test report command - upload mode
298
+ membrowse report firmware.elf "linker.ld" --upload \
299
+ --api-key "$API_KEY" \
300
+ --target-name "stm32f4" \
301
+ --api-url "https://membrowse.appspot.com/api/upload" \
302
+ --commit-sha "$(git rev-parse HEAD)" \
303
+ --branch-name "$(git branch --show-current)"
304
+
305
+ # Test report command - GitHub mode (auto-detects Git metadata and uploads)
306
+ membrowse report firmware.elf "linker.ld" --github \
307
+ --target-name "esp32" \
308
+ --api-key "$API_KEY"
309
+
310
+ # Test onboard command (analyzes and uploads historical commits)
311
+ membrowse onboard 10 "make build" build/firmware.elf "src/linker.ld" \
312
+ stm32f4 "$API_KEY" https://membrowse.appspot.com/api/upload
313
+ ```
314
+
315
+ ## Common Patterns
316
+
317
+ ### Linker Script Support
318
+ The system handles various linker script formats:
319
+ - Standard GNU LD syntax with ORIGIN/LENGTH
320
+ - ESP-IDF style without parenthetical attributes
321
+ - Variable-based expressions and DEFINED() conditionals
322
+ - Architecture-specific memory layouts
323
+
324
+ ### Error Handling
325
+ - Graceful degradation when DWARF debug info is unavailable
326
+ - Fallback strategies for unsupported linker script patterns
327
+ - Comprehensive logging for debugging parsing issues
328
+ - Validation warnings for unusual memory configurations