|
| 1 | +/* |
| 2 | +Copyright 2021 The Skaffold Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Test utility to connect to a pydevd server to validate that it is working. |
| 18 | +// Protocol: https://github.com/fabioz/PyDev.Debugger/blob/main/_pydevd_bundle/pydevd_comm.py |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "bufio" |
| 23 | + "fmt" |
| 24 | + "log" |
| 25 | + "net" |
| 26 | + "net/url" |
| 27 | + "os" |
| 28 | + "strconv" |
| 29 | + "strings" |
| 30 | + "time" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + CMD_RUN = 101 |
| 35 | + CMD_LIST_THREADS = 102 |
| 36 | + CMD_THREAD_CREATE = 103 |
| 37 | + CMD_THREAD_KILL = 104 |
| 38 | + CMD_THREAD_RUN = 106 |
| 39 | + CMD_SET_BREAK = 111 |
| 40 | + CMD_WRITE_TO_CONSOLE = 116 |
| 41 | + CMD_VERSION = 501 |
| 42 | + CMD_RETURN = 502 |
| 43 | + CMD_ERROR = 901 |
| 44 | +) |
| 45 | + |
| 46 | +func main() { |
| 47 | + if len(os.Args) != 2 { |
| 48 | + fmt.Printf("Check that pydevd is running.\n") |
| 49 | + fmt.Printf("use: %s host:port\n", os.Args[0]) |
| 50 | + os.Exit(1) |
| 51 | + } |
| 52 | + |
| 53 | + var conn net.Conn |
| 54 | + for i := 0; i < 60; i++ { |
| 55 | + var err error |
| 56 | + conn, err = net.Dial("tcp", os.Args[1]) |
| 57 | + if err == nil { |
| 58 | + break |
| 59 | + } |
| 60 | + fmt.Printf("(sleeping) unable to connect to %s: %v\n", os.Args[1], err) |
| 61 | + time.Sleep(2 * time.Second) |
| 62 | + } |
| 63 | + |
| 64 | + pydb := newPydevdDebugConnection(conn) |
| 65 | + |
| 66 | + code, response := pydb.makeRequestWithResponse(CMD_VERSION, "pydevconnect") |
| 67 | + if code != CMD_VERSION { |
| 68 | + log.Fatalf("expected CMD_VERSION (%d) response (%q)", code, response) |
| 69 | + } |
| 70 | + if decoded, err := url.QueryUnescape(response); err != nil { |
| 71 | + log.Fatalf("CMD_VERSION response (%q): decoding error: %v", response, err) |
| 72 | + } else { |
| 73 | + fmt.Printf("version: %s", decoded) |
| 74 | + } |
| 75 | + |
| 76 | + pydb.makeRequest(CMD_RUN, "test") |
| 77 | +} |
| 78 | + |
| 79 | +type pydevdDebugConnection struct { |
| 80 | + conn net.Conn |
| 81 | + reader *bufio.Reader |
| 82 | + msgID int |
| 83 | +} |
| 84 | + |
| 85 | +func newPydevdDebugConnection(c net.Conn) *pydevdDebugConnection { |
| 86 | + return &pydevdDebugConnection{ |
| 87 | + conn: c, |
| 88 | + reader: bufio.NewReader(c), |
| 89 | + msgID: 1, |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func (c *pydevdDebugConnection) makeRequest(code int, arg string) { |
| 94 | + currMsgID := c.msgID |
| 95 | + c.msgID += 2 // outgoing requests should have odd msgID |
| 96 | + |
| 97 | + fmt.Printf("Making request: code=%d msgId=%d arg=%q\n", code, currMsgID, arg) |
| 98 | + fmt.Fprintf(c.conn, "%d\t%d\t%s\n", code, currMsgID, arg) |
| 99 | +} |
| 100 | + |
| 101 | +func (c *pydevdDebugConnection) makeRequestWithResponse(code int, arg string) (int, string) { |
| 102 | + currMsgID := c.msgID |
| 103 | + c.msgID += 2 // outgoing requests should have odd msgID |
| 104 | + |
| 105 | + fmt.Printf("Making request: code=%d msgId=%d arg=%q\n", code, currMsgID, arg) |
| 106 | + fmt.Fprintf(c.conn, "%d\t%d\t%s\n", code, currMsgID, arg) |
| 107 | + |
| 108 | + for { |
| 109 | + response, err := c.reader.ReadString('\n') |
| 110 | + if err != nil { |
| 111 | + log.Fatalf("error receiving response: %v", err) |
| 112 | + } |
| 113 | + fmt.Printf("Received response: %q\n", response) |
| 114 | + |
| 115 | + // check response |
| 116 | + tsv := strings.Split(response, "\t") |
| 117 | + if len(tsv) != 3 { |
| 118 | + log.Fatalf("invalid response: expecting three tab-separated components: %q", response) |
| 119 | + } |
| 120 | + |
| 121 | + code, err = strconv.Atoi(tsv[0]) |
| 122 | + if err != nil { |
| 123 | + log.Fatalf("could not parse response code: %q", tsv[0]) |
| 124 | + } |
| 125 | + |
| 126 | + responseID, err := strconv.Atoi(tsv[1]) |
| 127 | + if err != nil { |
| 128 | + log.Fatalf("could not parse response ID: %q", tsv[1]) |
| 129 | + } else if responseID == currMsgID { |
| 130 | + return code, tsv[2] |
| 131 | + } |
| 132 | + |
| 133 | + // handle commands sent to us |
| 134 | + switch code { |
| 135 | + case CMD_THREAD_CREATE: |
| 136 | + fmt.Printf("CMD_THREAD_CREATE: %s\n", tsv[2:]) |
| 137 | + |
| 138 | + default: |
| 139 | + log.Fatalf("Unknown/unhandled code %d: %q", code, tsv[2:]) |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments