~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to tools/debugger/gdbsx/gx/gx_utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009, Mukesh Rathor, Oracle Corp.  All rights reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public
 
6
 * License v2 as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public
 
14
 * License along with this program; if not, write to the
 
15
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
16
 * Boston, MA 021110-1307, USA.
 
17
 */
 
18
 
 
19
#include <stdio.h>
 
20
#include <stdint.h>
 
21
#include <stdarg.h>
 
22
 
 
23
#include "gx.h"
 
24
 
 
25
 
 
26
void
 
27
gxprt(const char *fmt, ...)
 
28
{
 
29
    char buf[2048];
 
30
    va_list args;
 
31
 
 
32
    va_start(args, fmt);
 
33
    (void)vsnprintf(buf, sizeof(buf), fmt, args);
 
34
    va_end(args);
 
35
    fprintf(stderr, "%s", buf);
 
36
    fflush(stderr);
 
37
}
 
38
 
 
39
int
 
40
gx_fromhex(int a)
 
41
{
 
42
    if (a >= '0' && a <= '9')
 
43
        return a - '0';
 
44
    else if (a >= 'a' && a <= 'f')
 
45
        return a - 'a' + 10;
 
46
    else
 
47
        gxprt("Reply contains invalid hex digit");
 
48
    return 0;
 
49
}
 
50
 
 
51
int
 
52
gx_tohex(int nib)
 
53
{
 
54
    if (nib < 10)
 
55
        return '0' + nib;
 
56
    else
 
57
        return 'a' + nib - 10;
 
58
}
 
59
 
 
60
 
 
61
void
 
62
gx_convert_int_to_ascii(char *from, char *to, int n)
 
63
{
 
64
    int nib;
 
65
    int ch;
 
66
    while (n--) {
 
67
        ch = *from++;
 
68
        nib = ((ch & 0xf0) >> 4) & 0x0f;
 
69
        *to++ = gx_tohex(nib);
 
70
        nib = ch & 0x0f;
 
71
        *to++ = gx_tohex(nib);
 
72
    }
 
73
    *to = 0;
 
74
}
 
75
 
 
76
/* input: "70676433206431" output: "pgd3 d1" n == 7 */
 
77
void
 
78
gx_convert_ascii_to_int(char *from, char *to, int n)
 
79
{
 
80
    int nib1, nib2;
 
81
    while (n--) {
 
82
        nib1 = gx_fromhex(*from++);
 
83
        nib2 = gx_fromhex(*from++);
 
84
        *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
 
85
    }
 
86
    *to = 0;
 
87
}
 
88
 
 
89
void
 
90
gx_decode_zZ_packet(char *from, uint64_t *mem_addr_ptr)
 
91
{
 
92
    int i = 0;
 
93
    char ch;
 
94
    *mem_addr_ptr = 0;
 
95
 
 
96
    while ((ch=from[i++]) != ',') {
 
97
        *mem_addr_ptr = *mem_addr_ptr << 4;
 
98
        *mem_addr_ptr |= gx_fromhex(ch) & 0x0f;
 
99
    }
 
100
}
 
101
 
 
102
/* Eg: mc0267d3a,1\0 : from points to char after 'm' */
 
103
void
 
104
gx_decode_m_packet(char *from, uint64_t *mem_addr_ptr, int *len_ptr)
 
105
{
 
106
    int i = 0, j = 0;
 
107
    char ch;
 
108
    *mem_addr_ptr = *len_ptr = 0;
 
109
 
 
110
    while ((ch=from[i++]) != ',') {
 
111
        *mem_addr_ptr = *mem_addr_ptr << 4;
 
112
        *mem_addr_ptr |= gx_fromhex(ch) & 0x0f;
 
113
    }
 
114
    for (j = 0; j < 4; j++) {
 
115
        if ((ch=from[i++]) == 0)
 
116
            break;
 
117
        *len_ptr = *len_ptr << 4;
 
118
        *len_ptr |= gx_fromhex(ch) & 0x0f;
 
119
    }
 
120
}
 
121
 
 
122
/*
 
123
 * Decode M pkt as in: Mc0267d3a,1:cc\0 where c0267d3a is the guest addr
 
124
 * from points to char after 'M'
 
125
 * Returns: address of byte after ":", ie, addr of cc in buf
 
126
 */
 
127
char *
 
128
gx_decode_M_packet(char *from, uint64_t *mem_addr_ptr, int *len_ptr)
 
129
{
 
130
    int i = 0;
 
131
    char ch;
 
132
 
 
133
    *mem_addr_ptr = *len_ptr = 0;
 
134
 
 
135
    while ((ch=from[i++]) != ',') {
 
136
        *mem_addr_ptr = *mem_addr_ptr << 4;
 
137
        *mem_addr_ptr |= gx_fromhex(ch) & 0x0f;
 
138
    }
 
139
    while ((ch = from[i++]) != ':') {
 
140
        *len_ptr = *len_ptr << 4;
 
141
        *len_ptr |= gx_fromhex(ch) & 0x0f;
 
142
    }
 
143
    return(&from[i]);
 
144
}
 
145