starlark/environment/module_dump.rs
1/*
2 * Copyright 2018 The Starlark in Rust Authors.
3 * Copyright (c) Facebook, Inc. and its affiliates.
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
18use std::fmt::Write;
19
20use crate::environment::FrozenModule;
21use crate::eval::compiler::def::FrozenDef;
22use crate::values::FrozenHeapRef;
23use crate::values::FrozenValueTyped;
24
25impl FrozenModule {
26    /// Print a lot of module internals for debugging.
27    pub fn dump_debug(&self) -> String {
28        let mut w = String::new();
29
30        writeln!(w, "Eval duration: {:.3}s", self.eval_duration.as_secs_f64()).unwrap();
31        writeln!(w, "Heap stats:").unwrap();
32        w.push_str(&self.frozen_heap().dump_debug());
33
34        for (name, value) in self.all_items() {
35            // TODO(nga): this prints public, private and imported symbols.
36            //   We only care about public and private symbols, but no imported.
37            writeln!(w).unwrap();
38            writeln!(w, "{} = {}", name, value).unwrap();
39            if let Some(def) = FrozenValueTyped::<FrozenDef>::new(value) {
40                def.dump_debug()
41                    .lines()
42                    .for_each(|line| writeln!(w, "  {}", line).unwrap());
43            }
44        }
45        w
46    }
47}
48
49impl FrozenHeapRef {
50    fn dump_debug(&self) -> String {
51        let mut w = String::new();
52        writeln!(w, "Allocated bytes: {}", self.allocated_bytes()).unwrap();
53        writeln!(w, "Available bytes: {}", self.available_bytes()).unwrap();
54        w
55    }
56}