This file is indexed.

/usr/share/slsh/sldbsock.sl is in slsh 2.3.0-2ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
% Copyright (C) 2012-2014 John E. Davis
%
% This file is part of the S-Lang Library and may be distributed under the
% terms of the GNU General Public License.  See the file COPYING for
% more information.
%---------------------------------------------------------------------------
% This file implements a socket-based wrapper around sldbcore for
% debugging a separate process.  Programs that wish to use this for
% debugging should something like
%
%    autoload ("sldb", "sldbcore");
%
% in a startup file.  Then instead of using `require` or `evalfile` to
% load a file, use sldb("name-of-file") to load the file to be
% debugged.  Then in another window, run
%
%    sldb --pid <pid-of-process-to-be-debugged>
%
% Do not byte-compile this file.  It contains both the client and
% server code.
require ("sldbcore");
require ("socket");

% Simple line-based protocol:

private variable SLDB_SOCKET_PREFIX	= "/tmp/.sldb_";

private variable OK_CMD_RECEIVED	= 200;
private variable OK_CONT_XFER		= 201;
private variable OK_XFER_RECEIVED	= 202;
private variable ERR_UNKNOWN_CMD	= 301;

private variable Debug_Mode = 0;

private define debug_output ()
{
   variable args = __pop_args (_NARGS);
   if (Debug_Mode)
     {
	variable fp = stderr;
	() = fprintf (fp, __push_args (args));
	() = fflush (fp);
     }
}

private define make_socket_name (pid)
{
   return "$SLDB_SOCKET_PREFIX$pid"$;
}

private define do_write (fp, data)
{
   variable nbytes = fwrite (data, fp);
   if ((bstrlen (data) != nbytes)
       or (-1 == fflush (fp)))
     throw WriteError;

   debug_output ("** WROTE: %s\n", data);
}

private define do_fgets (fp)
{
   variable buf;
   if (-1 == fgets (&buf, fp))
     throw ReadError;

   debug_output ("** READ: %s\n", buf);
   return buf;
}

private define receive_response (fp)
{
   variable line = do_fgets (fp);
   return atoi (line);
}

private define send_response (fp, rsp, txt)
{
   rsp = sprintf ("%03d %s\n", rsp, txt);
   debug_output ("Sending Response:<%s>\n", rsp);
   do_write (fp, rsp);
}

private define send_cmd (fp, cmd)
{
   debug_output ("Sending CMD:<%s>\n", cmd);
   do_write (fp, cmd);
   return receive_response (fp);
}

private define receive_data (fp)
{
   variable data = "";
   forever
     {
	variable line = do_fgets (fp);
	if (line[0] == '.')
	  {
	     if (line == ".\n")
	       break;

	     line = substr (line, 2, -1);
	  }
	data = strcat (data, line);
     }
   debug_output ("** Received:<%s>\n", data);

   send_response (fp, OK_XFER_RECEIVED, "Ok transfer received");
   return data;
}

private define send_data (fp, data)
{
   debug_output ("** Sending:<%s>\n", data);

   variable lines = strchop (data, '\n', 0);
   variable num = length (lines);
   if (data[-1] == '\n')
     num--;

   _for (0, num-1, 1)
     {
	variable i = ();
	variable line = lines[i];
	if (line[0] == '.')
	  line = strcat (".", line, "\n");
	else
	  line = strcat (line, "\n");

	do_write (fp, line);
     }
   do_write (fp, ".\n");
   return receive_response (fp);
}

%

#ifnexists __SLDB_CLIENT__
% Server Routines

private variable Server_Socket_fd = NULL;
private variable Server_Socket_fp = NULL;

private define vmessage_method ()
{
   variable args = __pop_args (_NARGS);
   variable buf = sprintf (__push_args (args));
   debug_output ("Entering vmessage_method to send <%s>\r\n", buf);
   variable fp = Server_Socket_fp;

   variable status = send_cmd (fp, "MESSAGE\n");
   if (status != OK_CONT_XFER)
     throw IOError, "Client returned $status to MESSAGE command";

   status = send_data (fp, buf);
   if (status != OK_XFER_RECEIVED)
     throw IOError, "Expected OK_XFER_RECEIVED, got $status"$;

   debug_output ("Leaving vmessage_method\r\n");
}

private define open_file_at_linenum (file, linenum)
{
   if (path_extname (file) == ".slc")
     file = path_sans_extname (file) + ".sl";

   variable fp = fopen (file, "r");
   if (fp == NULL)
     {
	vmessage_method ("Unable to open %s\n", file);
	return NULL;
     }
   if (linenum == 1)
     return fp;

   foreach (fp) using ("line")
     {
	variable line = ();
	linenum--;
	if (linenum == 1)
	  break;
     }
   return fp;
}

private define list_method (file, linemin, linemax)
{
   variable n = linemax - linemin + 1;
   foreach (open_file_at_linenum (file, linemin))
     {
	variable line = ();
	vmessage_method ("%d\t%s", linemin, line);
	linemin++;
	if (linemin > linemax)
	  break;
     }
}

private define read_input_method (prompt, default_cmd)
{
   variable fp = Server_Socket_fp;

   do
     {
	variable status = send_cmd (fp, "INPUT\n");
	if (status != OK_CONT_XFER)
	  throw IOError, "Expected OK_CONT_XFER, got $status"$;

	status = send_data (fp, prompt);

	if (status != OK_XFER_RECEIVED)
	  throw IOError, "Expected OK_XFER_RECEIVED, got $status"$;

	variable input = receive_data (fp);
	if (input == "\n")
	  input = default_cmd;
     }
   while ((input == "") or (input == NULL));
   return input;
}

private define quit_method ()
{
   variable fp = Server_Socket_fp;
   () = send_cmd (fp, "QUIT\n");
   exit (0);
}

private define exit_method ()
{
   variable fp = Server_Socket_fp;
   () = send_cmd (fp, "EXIT\n");
}

private define initialize_server ()
{
   if (Server_Socket_fd != NULL)
     return;

   variable s = socket (PF_UNIX, SOCK_STREAM, 0);
   variable name = make_socket_name (getpid ());
   bind (s, name);
   () = chmod (name, S_IRUSR|S_IWUSR);
   listen (s, 1);
   Server_Socket_fd = accept (s);
   Server_Socket_fp = fdopen (Server_Socket_fd, "r+");
   if (Server_Socket_fd == NULL)
     throw OpenError, errno_string (errno);

   vmessage_method ("Welcome to sldb");
}

define sldb_initialize ()
{
   initialize_server ();
   variable m = sldb_methods ();
   m.list = &list_method;
   m.vmessage = &vmessage_method;
   m.read_input = &read_input_method;
   m.quit = &quit_method;
   m.pprint = NULL;
   m.exit = &exit_method;
}
#endif				       % ! __SLDB_CLIENT__

#ifexists __SLDB_CLIENT__
% Client Routines

private define read_command (s)
{
   variable line = strtrim (do_fgets (s));
   return line;
}

private define handle_message_cmd (s)
{
   send_response (s, OK_CONT_XFER, "Ok continue transfer");
   variable msg = receive_data (s);

   () = fputs (msg, stdout);
   () = fflush (stdout);
}

private define handle_input_cmd (s)
{
   send_response (s, OK_CONT_XFER, "Ok continue transfer");
   variable prompt = receive_data (s);

   prompt = strtrim (prompt, "\n");
   forever
     {
	try
	  {
	     variable line = slsh_readline (prompt);
	  }
	catch UserBreakError: continue;
	if (line != NULL)
	  break;
     }
   send_data (s, line);
}

private define handle_unknown_cmd (s)
{
   send_response (s, ERR_UNKNOWN_CMD, "Error Unknown Command");
}

define sldbsock_attach (pid)
{
   variable name = make_socket_name (pid);
   variable s = socket (PF_UNIX, SOCK_STREAM, 0);
   variable e;
   try (e)
     {
	connect (s, name);
     }
   catch SocketError:
     {
	() = fprintf (stderr, "Unable to connect to socket %s\n", name);
	() = fprintf (stderr, "Is the remote process ready to be debugged?\n");
	return -1;
     }

   variable fp = fdopen (s, "r+");
   forever
     {
	variable cmd = read_command (fp);
	if (cmd == "MESSAGE")
	  {
	     handle_message_cmd (fp);
	     continue;
	  }
	if (cmd == "INPUT")
	  {
	     handle_input_cmd (fp);
	     continue;
	  }
	if (cmd == "QUIT")
	  {
	     send_response (fp, OK_CMD_RECEIVED, "Ok Cmd Received");
	     return 0;
	  }
	if (cmd == "EXIT")
	  {
	     send_response (fp, OK_CMD_RECEIVED, "Ok Cmd Received");
	     return 0;
	  }
	handle_unknown_cmd (fp, cmd);
     }
}

#endif

provide ("sldbsock");