Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:32

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  Debugger test remote.
0005  *
0006  *  Copyright (c) 2016 Chris Johns (chrisj@rtems.org)
0007  *
0008  * Redistribution and use in source and binary forms, with or without
0009  * modification, are permitted provided that the following conditions
0010  * are met:
0011  * 1. Redistributions of source code must retain the above copyright
0012  *    notice, this list of conditions and the following disclaimer.
0013  * 2. Redistributions in binary form must reproduce the above copyright
0014  *    notice, this list of conditions and the following disclaimer in the
0015  *    documentation and/or other materials provided with the distribution.
0016  *
0017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0027  * POSSIBILITY OF SUCH DAMAGE.
0028  */
0029 
0030 #ifdef HAVE_CONFIG_H
0031 #include "config.h"
0032 #endif
0033 
0034 #include "tmacros.h"
0035 
0036 #include <errno.h>
0037 #include <stdlib.h>
0038 #include <unistd.h>
0039 
0040 #include <rtems/rtems-debugger.h>
0041 #include <rtems/debugger/rtems-debugger-server.h>
0042 #include <rtems/debugger/rtems-debugger-remote.h>
0043 
0044 #include "system.h"
0045 
0046 /**
0047  * Remote data.
0048  */
0049 typedef struct
0050 {
0051   int    connect_count;
0052   bool   connected;
0053   size_t out;
0054   size_t in;
0055 } rtems_debugger_remote_test;
0056 
0057 static const char* out[] =
0058 {
0059   "+",
0060   "xxxxx",
0061   "$x#aa",
0062 
0063   "$qSupported:multiprocess+;swbreak+;hwbreak+;qRelocInsn+;fork-events+;"
0064   "vfork-events+;exec-events+;vContSupported+;QThreadEvents+;no-resumed+#df",
0065 
0066   "$vMustReplyEmpty#3a",
0067   "+",
0068 
0069   "$Hgp0.0#ad",
0070   "+",
0071 
0072   "$qTStatus#49",
0073   "+",
0074 
0075   "$?#3f",
0076   "+",
0077 
0078   "$qfThreadInfo#bb",
0079   "+",
0080 
0081   "$qsThreadInfo#c8",
0082   "-",
0083   "+",
0084 
0085   "$qAttached:1#fa",
0086   "+",
0087 
0088   "$Hc-1#09",
0089   "+",
0090 
0091   "$qOffsets#4b",
0092   "+",
0093 
0094   "$g#67"
0095   "+",
0096 
0097   "$D;1#b0",
0098   "+"
0099 };
0100 
0101 static const char* in[] =
0102 {
0103   /*  0 */
0104   "-",
0105 
0106   /*  1 */
0107   "+",
0108   "$qSupported:PacketSize=4096;QNonStop-;multiprocess+;swbreak+;hwbreak-;"
0109   "qRelocInsn-;fork-events-;vfork-events-;exec-events-;vContSupported+;"
0110   "QThreadEvents-;no-resumed+#b3",
0111 
0112   /*  3 */
0113   "+",
0114   "$#00",
0115 
0116   /*  5 */
0117   "+",
0118   "$OK#9a",
0119 
0120   /*  7 */
0121   "+",
0122   "$#00",
0123 
0124   /*  9 */
0125   "+",
0126   "$T00thread:p1.0a010001;#23",
0127 
0128   /* 11 */
0129   "+",
0130   "**",
0131 
0132   /* 13 */
0133   "+",
0134   "$l#6c",
0135   "$l#6c",
0136 
0137   /* 16 */
0138   "+",
0139   "$1#31",
0140 
0141   /* 18 */
0142   "+",
0143   "$OK#9a",
0144 
0145   /* 20 */
0146   "+",
0147   "$#00",
0148 
0149   /* 22 */
0150   "+",
0151   "**",
0152 
0153   "+",
0154   "$OK#9a"
0155 };
0156 
0157 static int
0158 test_remote_begin(rtems_debugger_remote* remote, const char* device)
0159 {
0160   rtems_debugger_remote_test* test;
0161 
0162   rtems_debugger_printf("error: rtems-db: test remote: begin\n");
0163 
0164   rtems_debugger_lock();
0165 
0166   /*
0167    * Check the device.
0168    */
0169   rtems_test_assert(strcmp(device, "something") == 0);
0170 
0171   test = malloc(sizeof(rtems_debugger_remote_test));
0172   rtems_test_assert(test != NULL);
0173 
0174   remote->data = test;
0175 
0176   test->connect_count = 0;
0177   test->connected = false;
0178   test->out = 0;
0179   test->in = 0;
0180 
0181   rtems_debugger_unlock();
0182 
0183   return 0;
0184 }
0185 
0186 static int
0187 test_remote_end(rtems_debugger_remote* remote)
0188 {
0189   rtems_debugger_remote_test* test;
0190 
0191   rtems_debugger_printf("error: rtems-db: test remote: end\n");
0192 
0193   rtems_debugger_lock();
0194 
0195   rtems_test_assert(remote != NULL);
0196   rtems_test_assert(remote->data != NULL);
0197   test = (rtems_debugger_remote_test*) remote->data;
0198 
0199   test->connected = false;
0200 
0201   free(test);
0202 
0203   rtems_debugger_unlock();
0204 
0205   return 0;
0206 }
0207 
0208 static int
0209 test_remote_connect(rtems_debugger_remote* remote)
0210 {
0211   rtems_debugger_remote_test* test;
0212 
0213   rtems_test_assert(remote != NULL);
0214   rtems_test_assert(remote->data != NULL);
0215   test = (rtems_debugger_remote_test*) remote->data;
0216 
0217   if (test->connect_count > 0) {
0218     rtems_event_set out = 0;
0219     rtems_test_assert(rtems_event_receive(RTEMS_EVENT_1,
0220                                           RTEMS_EVENT_ALL | RTEMS_WAIT,
0221                                           RTEMS_NO_TIMEOUT,
0222                                           &out) == RTEMS_SUCCESSFUL);
0223   }
0224 
0225   rtems_debugger_printf("error: rtems-db: test remote: connect\n");
0226 
0227   ++test->connect_count;
0228   test->connected = true;
0229   test->out = 0;
0230   test->in = 0;
0231 
0232   return 0;
0233 }
0234 
0235 static int
0236 test_remote_disconnect(rtems_debugger_remote* remote)
0237 {
0238   rtems_debugger_remote_test* test;
0239 
0240   rtems_test_assert(remote != NULL);
0241   rtems_test_assert(remote->data != NULL);
0242   test = (rtems_debugger_remote_test*) remote->data;
0243 
0244   rtems_debugger_printf("rtems-db: test remote: disconnect host\n");
0245 
0246   rtems_debugger_lock();
0247 
0248   rtems_test_assert(test->connected == true);
0249 
0250   test->connected = false;
0251 
0252   rtems_debugger_unlock();
0253 
0254   return 0;
0255 }
0256 
0257 static bool
0258 test_remote_isconnected(rtems_debugger_remote* remote)
0259 {
0260   rtems_debugger_remote_test* test;
0261   bool                        isconnected;
0262   rtems_test_assert(remote != NULL);
0263   rtems_test_assert(remote->data != NULL);
0264   test = (rtems_debugger_remote_test*) remote->data;
0265   isconnected = test != NULL && test->connected;
0266   rtems_debugger_printf("rtems-db: test remote: isconnected: %s\n",
0267                         isconnected ? "connected" : "not-connected");
0268   return isconnected;
0269 }
0270 
0271 static void
0272 test_remote_print(const char* label, const char* buf, size_t size)
0273 {
0274   printf(" remote: %s: ", label);
0275   while (size-- > 0) {
0276     printf("%c", *buf++);
0277   }
0278   printf("\n");
0279 }
0280 
0281 static ssize_t
0282 test_remote_receive(rtems_debugger_remote* remote,
0283                     void*                  buf,
0284                     size_t                 nbytes)
0285 {
0286   rtems_debugger_remote_test* test;
0287   size_t                      len;
0288   rtems_test_assert(remote != NULL);
0289   rtems_test_assert(remote->data != NULL);
0290   test = (rtems_debugger_remote_test*) remote->data;
0291   rtems_test_assert(test->out < RTEMS_DEBUGGER_NUMOF(out));
0292   len = strlen(out[test->out]);
0293   printf(" remote: rx: message=%zu length=%zu\n", test->out, len);
0294   test_remote_print("rx", out[test->out], len);
0295   rtems_test_assert(len < nbytes);
0296   memcpy(buf, out[test->out++], len);
0297   return len;
0298 }
0299 
0300 static ssize_t
0301 test_remote_send(rtems_debugger_remote* remote,
0302                 const void*            buf,
0303                 size_t                 nbytes)
0304 {
0305   rtems_debugger_remote_test* test;
0306   size_t                      len;
0307   bool                        no_match;
0308   rtems_test_assert(remote != NULL);
0309   rtems_test_assert(remote->data != NULL);
0310   test = (rtems_debugger_remote_test*) remote->data;
0311   rtems_test_assert(test->in < RTEMS_DEBUGGER_NUMOF(in));
0312   len = strlen(in[test->in]);
0313   no_match = len == 2 && strcmp(in[test->in], "**") == 0;
0314   printf(" remote: tx: message=%zu length=%zu\n", test->in, len);
0315   if (!no_match)
0316     rtems_test_assert(len == nbytes);
0317   test_remote_print("tx", buf, nbytes);
0318   if (!no_match)
0319     rtems_test_assert(memcmp(buf, in[test->in], nbytes) == 0);
0320   test->in++;
0321   return nbytes;
0322 }
0323 
0324 static rtems_debugger_remote remote_test =
0325 {
0326   .name = "test",
0327   .begin = test_remote_begin,
0328   .end = test_remote_end,
0329   .connect = test_remote_connect,
0330   .disconnect = test_remote_disconnect,
0331   .isconnected = test_remote_isconnected,
0332   .read = test_remote_receive,
0333   .write = test_remote_send
0334 };
0335 
0336 int
0337 rtems_debugger_register_test_remote(void)
0338 {
0339   return rtems_debugger_remote_register(&remote_test);
0340 }