1 /*
2 * Copyright (c) 2016 Intel Corporation
3 * Copyright (c) 2023 Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/logging/log.h>
9 LOG_MODULE_DECLARE(net_shell);
10
11 #include "net_shell_private.h"
12
13 #if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC)
allocs_cb(struct net_pkt * pkt,struct net_buf * buf,const char * func_alloc,int line_alloc,const char * func_free,int line_free,bool in_use,void * user_data)14 static void allocs_cb(struct net_pkt *pkt,
15 struct net_buf *buf,
16 const char *func_alloc,
17 int line_alloc,
18 const char *func_free,
19 int line_free,
20 bool in_use,
21 void *user_data)
22 {
23 struct net_shell_user_data *data = user_data;
24 const struct shell *sh = data->sh;
25 const char *str;
26
27 if (in_use) {
28 str = "used";
29 } else {
30 if (func_alloc) {
31 str = "free";
32 } else {
33 str = "avail";
34 }
35 }
36
37 if (buf) {
38 goto buf;
39 }
40
41 if (func_alloc) {
42 if (in_use) {
43 PR("%p/%ld\t%5s\t%5s\t%s():%d\n",
44 pkt, atomic_get(&pkt->atomic_ref), str,
45 net_pkt_slab2str(pkt->slab),
46 func_alloc, line_alloc);
47 } else {
48 PR("%p\t%5s\t%5s\t%s():%d -> %s():%d\n",
49 pkt, str, net_pkt_slab2str(pkt->slab),
50 func_alloc, line_alloc, func_free,
51 line_free);
52 }
53 }
54
55 return;
56 buf:
57 if (func_alloc) {
58 struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
59
60 if (in_use) {
61 PR("%p/%d\t%5s\t%5s\t%s():%d\n",
62 buf, buf->ref,
63 str, net_pkt_pool2str(pool), func_alloc,
64 line_alloc);
65 } else {
66 PR("%p\t%5s\t%5s\t%s():%d -> %s():%d\n",
67 buf, str, net_pkt_pool2str(pool),
68 func_alloc, line_alloc, func_free,
69 line_free);
70 }
71 }
72 }
73 #endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC */
74
cmd_net_allocs(const struct shell * sh,size_t argc,char * argv[])75 static int cmd_net_allocs(const struct shell *sh, size_t argc, char *argv[])
76 {
77 #if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC)
78 struct net_shell_user_data user_data;
79 #endif
80
81 ARG_UNUSED(argc);
82 ARG_UNUSED(argv);
83
84 #if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC)
85 user_data.sh = sh;
86
87 PR("Network memory allocations\n\n");
88 PR("memory\t\tStatus\tPool\tFunction alloc -> freed\n");
89 net_pkt_allocs_foreach(allocs_cb, &user_data);
90 #else
91 PR_INFO("Set %s to enable %s support.\n",
92 "CONFIG_NET_DEBUG_NET_PKT_ALLOC", "net_pkt allocation");
93 #endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC */
94
95 return 0;
96 }
97
98 SHELL_SUBCMD_ADD((net), allocs, NULL,
99 "Print network memory allocations.",
100 cmd_net_allocs, 1, 0);
101