Sam Leffler | 19347f4 | 2022-09-13 00:42:47 +0000 | [diff] [blame] | 1 | #! /bin/bash |
| 2 | # |
| 3 | # Copyright 2022 Google LLC |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # CAmkES system image memory analyzer. |
| 18 | |
| 19 | # Analyze the CAmkES-generated capDL spec for memory use. |
| 20 | # By default the memory foottprint of each component is displayed. |
| 21 | # The -d option will give a breakdown by memory type: |
| 22 | # elf .text + .data |
| 23 | # bss .bss |
| 24 | # ipc_buffer CAmkES per-thread ipc_buffer's |
| 25 | # stack CAmkES per-thread stack |
| 26 | # bootinfo Bootinfo page passed by the rooteserver |
| 27 | # mmio MMIO region (backed by devivce memory) |
Sam Leffler | 19347f4 | 2022-09-13 00:42:47 +0000 | [diff] [blame] | 28 | # |
Sam Leffler | 315cedb | 2023-05-22 10:35:58 -0700 | [diff] [blame] | 29 | # Note mmio sections do not count against memory usage as they are |
| 30 | # allocated from dedicated memory that does not have physical |
Sam Leffler | 19347f4 | 2022-09-13 00:42:47 +0000 | [diff] [blame] | 31 | # memory backing. |
| 32 | # |
Sam Leffler | 19347f4 | 2022-09-13 00:42:47 +0000 | [diff] [blame] | 33 | # ROOTDIR must be set to the top of the shodan development tree |
| 34 | # (as done by build/setup.sh). |
| 35 | |
| 36 | # Usage: kmem [-d] |
| 37 | |
| 38 | if [[ -z "${ROOTDIR}" ]]; then |
| 39 | echo "Source build/setup.sh first" |
| 40 | exit 1 |
| 41 | fi |
| 42 | |
Sam Leffler | e6be932 | 2023-07-28 16:37:25 -0700 | [diff] [blame] | 43 | # NB: should always be set but default anyway |
| 44 | PLATFORM=${PLATFORM:-shodan} |
Sam Leffler | 19347f4 | 2022-09-13 00:42:47 +0000 | [diff] [blame] | 45 | |
| 46 | # Default is a summary of release build. |
Julian Mullings-Black | 68cf2ae | 2023-01-11 21:36:26 +0000 | [diff] [blame] | 47 | DETAILS="" |
Sam Leffler | 19347f4 | 2022-09-13 00:42:47 +0000 | [diff] [blame] | 48 | BUILD="release" |
Sam Leffler | 315cedb | 2023-05-22 10:35:58 -0700 | [diff] [blame] | 49 | KERNEL="--kernel" |
| 50 | VERBOSE="" |
Sam Leffler | 19347f4 | 2022-09-13 00:42:47 +0000 | [diff] [blame] | 51 | |
| 52 | function parseargv { |
Sam Leffler | 315cedb | 2023-05-22 10:35:58 -0700 | [diff] [blame] | 53 | local usage="Usage: kmem.sh [-h|--help] [-d|--details] [-D|--debug] [-R|--release] [-s|--summary] [-u|--user] [-v|--verbose]" |
| 54 | local args=$(getopt -o dDRsuv --long details,debug,release,summary,user,verbose,help -n kmem.sh -- "$@") |
Sam Leffler | 19347f4 | 2022-09-13 00:42:47 +0000 | [diff] [blame] | 55 | |
| 56 | set -- $args |
| 57 | |
| 58 | for i; do |
| 59 | case "$1" in |
| 60 | -d|--details) |
Julian Mullings-Black | 68cf2ae | 2023-01-11 21:36:26 +0000 | [diff] [blame] | 61 | DETAILS="--details" |
Sam Leffler | 19347f4 | 2022-09-13 00:42:47 +0000 | [diff] [blame] | 62 | shift |
| 63 | ;; |
| 64 | |
| 65 | -s|--summary) |
Julian Mullings-Black | 68cf2ae | 2023-01-11 21:36:26 +0000 | [diff] [blame] | 66 | DETAILS="" |
Sam Leffler | 19347f4 | 2022-09-13 00:42:47 +0000 | [diff] [blame] | 67 | shift |
| 68 | ;; |
| 69 | |
| 70 | -D|--debug) |
Sam Leffler | 5629337 | 2022-12-16 12:49:18 -0800 | [diff] [blame] | 71 | BUILD="debug" |
Sam Leffler | 19347f4 | 2022-09-13 00:42:47 +0000 | [diff] [blame] | 72 | shift |
| 73 | ;; |
| 74 | |
| 75 | -R|--release) |
| 76 | BUILD="release" |
| 77 | shift |
| 78 | ;; |
| 79 | |
Sam Leffler | 315cedb | 2023-05-22 10:35:58 -0700 | [diff] [blame] | 80 | -u|--user) |
| 81 | KERNEL="" |
| 82 | shift |
| 83 | ;; |
| 84 | |
| 85 | -v|--verbose) |
| 86 | VERBOSE="--verbose" |
| 87 | shift |
| 88 | ;; |
| 89 | |
Sam Leffler | 19347f4 | 2022-09-13 00:42:47 +0000 | [diff] [blame] | 90 | --) |
| 91 | shift |
| 92 | break |
| 93 | ;; |
| 94 | |
| 95 | -h|--help|*) |
| 96 | echo "$usage" >/dev/stderr |
| 97 | exit 1 |
| 98 | ;; |
| 99 | esac |
| 100 | done |
| 101 | } |
| 102 | |
| 103 | parseargv "$@" |
| 104 | |
Sam Leffler | e6be932 | 2023-07-28 16:37:25 -0700 | [diff] [blame] | 105 | CANTRIP_OUT="${ROOTDIR}/out/cantrip/${PLATFORM}/${BUILD}" |
Sam Leffler | 996c8dc | 2023-11-22 14:28:12 -0800 | [diff] [blame] | 106 | PYTHONPATH=${PYTHON_SHODAN_ENV}/lib/python3.11/site-packages/:${PYTHONPATH} |
Julian Mullings-Black | 68cf2ae | 2023-01-11 21:36:26 +0000 | [diff] [blame] | 107 | PYTHONPATH="${PYTHONPATH}:${ROOTDIR}/cantrip/projects/capdl/python-capdl-tool" |
Sam Leffler | 315cedb | 2023-05-22 10:35:58 -0700 | [diff] [blame] | 108 | exec python3 "${ROOTDIR}/cantrip/tools/seL4/kmem-tool/kmem.py" --object-state "${CANTRIP_OUT}/object-final.pickle" ${DETAILS} ${KERNEL} ${VERBOSE} |