Contiki 3.x
shell-memdebug.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Shell commands for memory debugging
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  * Joakim Eriksson <joakime@sics.se>
39  */
40 
41 #include "contiki.h"
42 #include "shell-memdebug.h"
43 
44 #include <stdio.h>
45 #include <string.h>
46 
47 /*---------------------------------------------------------------------------*/
48 PROCESS(shell_poke_process, "poke");
49 SHELL_COMMAND(poke_command,
50  "poke",
51  "poke <address> <byte>: write byte <byte> to address <address>",
52  &shell_poke_process);
53 PROCESS(shell_peek_process, "peek");
54 SHELL_COMMAND(peek_command,
55  "peek",
56  "peek <address>: read a byte from address <address>",
57  &shell_peek_process);
58 /*---------------------------------------------------------------------------*/
59 PROCESS_THREAD(shell_poke_process, ev, data)
60 {
61  uint8_t *address;
62  uint8_t byte;
63  const char *args, *next;
64 
65  PROCESS_BEGIN();
66 
67  args = data;
68 
69  if(args == NULL) {
70  shell_output_str(&poke_command, "usage 0", "");
71  PROCESS_EXIT();
72  }
73 
74  address = (uint8_t *)(int)shell_strtolong(args, &next);
75  if(next == args) {
76  shell_output_str(&poke_command, "usage 1", "");
77  PROCESS_EXIT();
78  }
79 
80  args = next;
81  byte = shell_strtolong(args, &next);
82  if(next == args) {
83  shell_output_str(&poke_command, "usage 2", "");
84  PROCESS_EXIT();
85  }
86 
87  printf("address %p byte 0x%02x\n", address, byte);
88 
89  *address = byte;
90 
91  PROCESS_END();
92 }
93 /*---------------------------------------------------------------------------*/
94 PROCESS_THREAD(shell_peek_process, ev, data)
95 {
96  uint8_t *address;
97  const char *args, *next;
98  char buf[32];
99 
100  PROCESS_BEGIN();
101 
102  args = data;
103 
104  if(args == NULL) {
105  shell_output_str(&peek_command, "usage 0", "");
106  PROCESS_EXIT();
107  }
108 
109  address = (uint8_t *)(int)shell_strtolong(args, &next);
110  if(next == args) {
111  shell_output_str(&peek_command, "usage 1", "");
112  PROCESS_EXIT();
113  }
114 
115  snprintf(buf, sizeof(buf), "0x%02x", *address);
116 
117  shell_output_str(&peek_command, buf, "");
118 
119  PROCESS_END();
120 }
121 /*---------------------------------------------------------------------------*/
122 void
123 shell_memdebug_init(void)
124 {
125  shell_register_command(&poke_command);
126  shell_register_command(&peek_command);
127 }
128 /*---------------------------------------------------------------------------*/
void shell_output_str(struct shell_command *c, char *text1, const char *text2)
Output strings from a shell command.
Definition: shell.c:383
#define PROCESS_EXIT()
Exit the currently running process.
Definition: process.h:200
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define NULL
The null pointer.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
unsigned long shell_strtolong(const char *str, const char **retstr)
Convert a string to a number.
Definition: shell.c:521
void shell_register_command(struct shell_command *c)
Register a command with the shell.
Definition: shell.c:413
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
Shell commands for memory debugging
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219