Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / drivers / trace / tr_rbug.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "util/u_format.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_simple_list.h"
33 #include "util/u_network.h"
34
35 #include "tgsi/tgsi_parse.h"
36
37 #include "tr_dump.h"
38 #include "tr_state.h"
39 #include "tr_buffer.h"
40 #include "tr_texture.h"
41
42 #include "rbug/rbug.h"
43
44 #include <errno.h>
45
46 #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
47 # define sleep Sleep
48 #elif defined(PIPE_OS_LINUX) || defined(PIPE_OS_APPLE)
49 void usleep(int);
50 # define sleep usleep
51 #else
52 # warning "No socket implementation"
53 #endif
54
55 #define U642VOID(x) ((void *)(unsigned long)(x))
56 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
57
58 struct trace_rbug
59 {
60 struct trace_screen *tr_scr;
61 struct rbug_connection *con;
62 pipe_thread thread;
63 boolean running;
64 };
65
66 PIPE_THREAD_ROUTINE(trace_rbug_thread, void_tr_rbug);
67
68
69 /**********************************************************
70 * Helper functions
71 */
72
73
74 static struct trace_context *
75 trace_rbug_get_context_locked(struct trace_screen *tr_scr, rbug_context_t ctx)
76 {
77 struct trace_context *tr_ctx = NULL;
78 struct tr_list *ptr;
79
80 foreach(ptr, &tr_scr->contexts) {
81 tr_ctx = (struct trace_context *)((char*)ptr - offsetof(struct trace_context, list));
82 if (ctx == VOID2U64(tr_ctx))
83 break;
84 tr_ctx = NULL;
85 }
86
87 return tr_ctx;
88 }
89
90 static struct trace_shader *
91 trace_rbug_get_shader_locked(struct trace_context *tr_ctx, rbug_shader_t shdr)
92 {
93 struct trace_shader *tr_shdr = NULL;
94 struct tr_list *ptr;
95
96 foreach(ptr, &tr_ctx->shaders) {
97 tr_shdr = (struct trace_shader *)((char*)ptr - offsetof(struct trace_shader, list));
98 if (shdr == VOID2U64(tr_shdr))
99 break;
100 tr_shdr = NULL;
101 }
102
103 return tr_shdr;
104 }
105
106 static void *
107 trace_shader_create_locked(struct pipe_context *pipe,
108 struct trace_shader *tr_shdr,
109 struct tgsi_token *tokens)
110 {
111 void *state = NULL;
112 struct pipe_shader_state pss = { 0 };
113 pss.tokens = tokens;
114
115 if (tr_shdr->type == TRACE_SHADER_FRAGMENT) {
116 state = pipe->create_fs_state(pipe, &pss);
117 } else if (tr_shdr->type == TRACE_SHADER_VERTEX) {
118 state = pipe->create_vs_state(pipe, &pss);
119 } else
120 assert(0);
121
122 return state;
123 }
124
125 static void
126 trace_shader_bind_locked(struct pipe_context *pipe,
127 struct trace_shader *tr_shdr,
128 void *state)
129 {
130 if (tr_shdr->type == TRACE_SHADER_FRAGMENT) {
131 pipe->bind_fs_state(pipe, state);
132 } else if (tr_shdr->type == TRACE_SHADER_VERTEX) {
133 pipe->bind_vs_state(pipe, state);
134 } else
135 assert(0);
136 }
137
138 static void
139 trace_shader_delete_locked(struct pipe_context *pipe,
140 struct trace_shader *tr_shdr,
141 void *state)
142 {
143 if (tr_shdr->type == TRACE_SHADER_FRAGMENT) {
144 pipe->delete_fs_state(pipe, state);
145 } else if (tr_shdr->type == TRACE_SHADER_VERTEX) {
146 pipe->delete_vs_state(pipe, state);
147 } else
148 assert(0);
149 }
150
151 /************************************************
152 * Request handler functions
153 */
154
155
156 static int
157 trace_rbug_texture_list(struct trace_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
158 {
159 struct trace_screen *tr_scr = tr_rbug->tr_scr;
160 struct trace_texture *tr_tex = NULL;
161 struct tr_list *ptr;
162 rbug_texture_t *texs;
163 int i = 0;
164
165 pipe_mutex_lock(tr_scr->list_mutex);
166 texs = MALLOC(tr_scr->num_textures * sizeof(rbug_texture_t));
167 foreach(ptr, &tr_scr->textures) {
168 tr_tex = (struct trace_texture *)((char*)ptr - offsetof(struct trace_texture, list));
169 texs[i++] = VOID2U64(tr_tex);
170 }
171 pipe_mutex_unlock(tr_scr->list_mutex);
172
173 rbug_send_texture_list_reply(tr_rbug->con, serial, texs, i, NULL);
174 FREE(texs);
175
176 return 0;
177 }
178
179 static int
180 trace_rbug_texture_info(struct trace_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
181 {
182 struct trace_screen *tr_scr = tr_rbug->tr_scr;
183 struct trace_texture *tr_tex = NULL;
184 struct rbug_proto_texture_info *gpti = (struct rbug_proto_texture_info *)header;
185 struct tr_list *ptr;
186 struct pipe_texture *t;
187
188 pipe_mutex_lock(tr_scr->list_mutex);
189 foreach(ptr, &tr_scr->textures) {
190 tr_tex = (struct trace_texture *)((char*)ptr - offsetof(struct trace_texture, list));
191 if (gpti->texture == VOID2U64(tr_tex))
192 break;
193 tr_tex = NULL;
194 }
195
196 if (!tr_tex) {
197 pipe_mutex_unlock(tr_scr->list_mutex);
198 return -ESRCH;
199 }
200
201 t = tr_tex->texture;
202 rbug_send_texture_info_reply(tr_rbug->con, serial,
203 t->target, t->format,
204 &t->width0, 1,
205 &t->height0, 1,
206 &t->depth0, 1,
207 util_format_get_blockwidth(t->format),
208 util_format_get_blockheight(t->format),
209 util_format_get_blocksize(t->format),
210 t->last_level,
211 t->nr_samples,
212 t->tex_usage,
213 NULL);
214
215 pipe_mutex_unlock(tr_scr->list_mutex);
216
217 return 0;
218 }
219
220 static int
221 trace_rbug_texture_read(struct trace_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
222 {
223 struct rbug_proto_texture_read *gptr = (struct rbug_proto_texture_read *)header;
224
225 struct trace_screen *tr_scr = tr_rbug->tr_scr;
226 struct trace_texture *tr_tex = NULL;
227 struct tr_list *ptr;
228
229 struct pipe_screen *screen = tr_scr->screen;
230 struct pipe_texture *tex;
231 struct pipe_transfer *t;
232
233 void *map;
234
235 pipe_mutex_lock(tr_scr->list_mutex);
236 foreach(ptr, &tr_scr->textures) {
237 tr_tex = (struct trace_texture *)((char*)ptr - offsetof(struct trace_texture, list));
238 if (gptr->texture == VOID2U64(tr_tex))
239 break;
240 tr_tex = NULL;
241 }
242
243 if (!tr_tex) {
244 pipe_mutex_unlock(tr_scr->list_mutex);
245 return -ESRCH;
246 }
247
248 tex = tr_tex->texture;
249 t = screen->get_tex_transfer(tr_scr->screen, tex,
250 gptr->face, gptr->level, gptr->zslice,
251 PIPE_TRANSFER_READ,
252 gptr->x, gptr->y, gptr->w, gptr->h);
253
254 map = screen->transfer_map(screen, t);
255
256 rbug_send_texture_read_reply(tr_rbug->con, serial,
257 t->texture->format,
258 util_format_get_blockwidth(t->texture->format),
259 util_format_get_blockheight(t->texture->format),
260 util_format_get_blocksize(t->texture->format),
261 (uint8_t*)map,
262 t->stride * util_format_get_nblocksy(t->texture->format, t->height),
263 t->stride,
264 NULL);
265
266 screen->transfer_unmap(screen, t);
267 screen->tex_transfer_destroy(t);
268
269 pipe_mutex_unlock(tr_scr->list_mutex);
270
271 return 0;
272 }
273
274 static int
275 trace_rbug_context_list(struct trace_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
276 {
277 struct trace_screen *tr_scr = tr_rbug->tr_scr;
278 struct tr_list *ptr;
279 struct trace_context *tr_ctx = NULL;
280 rbug_context_t *ctxs;
281 int i = 0;
282
283 pipe_mutex_lock(tr_scr->list_mutex);
284 ctxs = MALLOC(tr_scr->num_contexts * sizeof(rbug_context_t));
285 foreach(ptr, &tr_scr->contexts) {
286 tr_ctx = (struct trace_context *)((char*)ptr - offsetof(struct trace_context, list));
287 ctxs[i++] = VOID2U64(tr_ctx);
288 }
289 pipe_mutex_unlock(tr_scr->list_mutex);
290
291 rbug_send_context_list_reply(tr_rbug->con, serial, ctxs, i, NULL);
292 FREE(ctxs);
293
294 return 0;
295 }
296
297 static int
298 trace_rbug_context_info(struct trace_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
299 {
300 struct rbug_proto_context_info *info = (struct rbug_proto_context_info *)header;
301
302 struct trace_screen *tr_scr = tr_rbug->tr_scr;
303 struct trace_context *tr_ctx = NULL;
304 rbug_texture_t cbufs[PIPE_MAX_COLOR_BUFS];
305 rbug_texture_t texs[PIPE_MAX_SAMPLERS];
306 int i;
307
308 pipe_mutex_lock(tr_scr->list_mutex);
309 tr_ctx = trace_rbug_get_context_locked(tr_scr, info->context);
310
311 if (!tr_ctx) {
312 pipe_mutex_unlock(tr_scr->list_mutex);
313 return -ESRCH;
314 }
315
316 /* protect the pipe context */
317 pipe_mutex_lock(tr_ctx->draw_mutex);
318 trace_dump_call_lock();
319
320 for (i = 0; i < tr_ctx->curr.nr_cbufs; i++)
321 cbufs[i] = VOID2U64(tr_ctx->curr.cbufs[i]);
322
323 for (i = 0; i < tr_ctx->curr.num_texs; i++)
324 texs[i] = VOID2U64(tr_ctx->curr.tex[i]);
325
326 rbug_send_context_info_reply(tr_rbug->con, serial,
327 VOID2U64(tr_ctx->curr.vs), VOID2U64(tr_ctx->curr.fs),
328 texs, tr_ctx->curr.num_texs,
329 cbufs, tr_ctx->curr.nr_cbufs,
330 VOID2U64(tr_ctx->curr.zsbuf),
331 tr_ctx->draw_blocker, tr_ctx->draw_blocked, NULL);
332
333 trace_dump_call_unlock();
334 pipe_mutex_unlock(tr_ctx->draw_mutex);
335
336 pipe_mutex_unlock(tr_scr->list_mutex);
337
338 return 0;
339 }
340
341 static int
342 trace_rbug_context_draw_block(struct trace_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
343 {
344 struct rbug_proto_context_draw_block *block = (struct rbug_proto_context_draw_block *)header;
345
346 struct trace_screen *tr_scr = tr_rbug->tr_scr;
347 struct trace_context *tr_ctx = NULL;
348
349 pipe_mutex_lock(tr_scr->list_mutex);
350 tr_ctx = trace_rbug_get_context_locked(tr_scr, block->context);
351
352 if (!tr_ctx) {
353 pipe_mutex_unlock(tr_scr->list_mutex);
354 return -ESRCH;
355 }
356
357 pipe_mutex_lock(tr_ctx->draw_mutex);
358 tr_ctx->draw_blocker |= block->block;
359 pipe_mutex_unlock(tr_ctx->draw_mutex);
360
361 pipe_mutex_unlock(tr_scr->list_mutex);
362
363 return 0;
364 }
365
366 static int
367 trace_rbug_context_draw_step(struct trace_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
368 {
369 struct rbug_proto_context_draw_step *step = (struct rbug_proto_context_draw_step *)header;
370
371 struct trace_screen *tr_scr = tr_rbug->tr_scr;
372 struct trace_context *tr_ctx = NULL;
373
374 pipe_mutex_lock(tr_scr->list_mutex);
375 tr_ctx = trace_rbug_get_context_locked(tr_scr, step->context);
376
377 if (!tr_ctx) {
378 pipe_mutex_unlock(tr_scr->list_mutex);
379 return -ESRCH;
380 }
381
382 pipe_mutex_lock(tr_ctx->draw_mutex);
383 if (tr_ctx->draw_blocked & RBUG_BLOCK_RULE) {
384 if (step->step & RBUG_BLOCK_RULE)
385 tr_ctx->draw_blocked &= ~RBUG_BLOCK_MASK;
386 } else {
387 tr_ctx->draw_blocked &= ~step->step;
388 }
389 pipe_mutex_unlock(tr_ctx->draw_mutex);
390
391 #ifdef PIPE_THREAD_HAVE_CONDVAR
392 pipe_condvar_broadcast(tr_ctx->draw_cond);
393 #endif
394
395 pipe_mutex_unlock(tr_scr->list_mutex);
396
397 return 0;
398 }
399
400 static int
401 trace_rbug_context_draw_unblock(struct trace_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
402 {
403 struct rbug_proto_context_draw_unblock *unblock = (struct rbug_proto_context_draw_unblock *)header;
404
405 struct trace_screen *tr_scr = tr_rbug->tr_scr;
406 struct trace_context *tr_ctx = NULL;
407
408 pipe_mutex_lock(tr_scr->list_mutex);
409 tr_ctx = trace_rbug_get_context_locked(tr_scr, unblock->context);
410
411 if (!tr_ctx) {
412 pipe_mutex_unlock(tr_scr->list_mutex);
413 return -ESRCH;
414 }
415
416 pipe_mutex_lock(tr_ctx->draw_mutex);
417 if (tr_ctx->draw_blocked & RBUG_BLOCK_RULE) {
418 if (unblock->unblock & RBUG_BLOCK_RULE)
419 tr_ctx->draw_blocked &= ~RBUG_BLOCK_MASK;
420 } else {
421 tr_ctx->draw_blocked &= ~unblock->unblock;
422 }
423 tr_ctx->draw_blocker &= ~unblock->unblock;
424 pipe_mutex_unlock(tr_ctx->draw_mutex);
425
426 #ifdef PIPE_THREAD_HAVE_CONDVAR
427 pipe_condvar_broadcast(tr_ctx->draw_cond);
428 #endif
429
430 pipe_mutex_unlock(tr_scr->list_mutex);
431
432 return 0;
433 }
434
435 static int
436 trace_rbug_context_draw_rule(struct trace_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
437 {
438 struct rbug_proto_context_draw_rule *rule = (struct rbug_proto_context_draw_rule *)header;
439
440 struct trace_screen *tr_scr = tr_rbug->tr_scr;
441 struct trace_context *tr_ctx = NULL;
442
443 pipe_mutex_lock(tr_scr->list_mutex);
444 tr_ctx = trace_rbug_get_context_locked(tr_scr, rule->context);
445
446 if (!tr_ctx) {
447 pipe_mutex_unlock(tr_scr->list_mutex);
448 return -ESRCH;
449 }
450
451 pipe_mutex_lock(tr_ctx->draw_mutex);
452 tr_ctx->draw_rule.vs = U642VOID(rule->vertex);
453 tr_ctx->draw_rule.fs = U642VOID(rule->fragment);
454 tr_ctx->draw_rule.tex = U642VOID(rule->texture);
455 tr_ctx->draw_rule.surf = U642VOID(rule->surface);
456 tr_ctx->draw_rule.blocker = rule->block;
457 tr_ctx->draw_blocker |= RBUG_BLOCK_RULE;
458 pipe_mutex_unlock(tr_ctx->draw_mutex);
459
460 #ifdef PIPE_THREAD_HAVE_CONDVAR
461 pipe_condvar_broadcast(tr_ctx->draw_cond);
462 #endif
463
464 pipe_mutex_unlock(tr_scr->list_mutex);
465
466 return 0;
467 }
468
469 static int
470 trace_rbug_context_flush(struct trace_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
471 {
472 struct rbug_proto_context_flush *flush = (struct rbug_proto_context_flush *)header;
473
474 struct trace_screen *tr_scr = tr_rbug->tr_scr;
475 struct trace_context *tr_ctx = NULL;
476
477 pipe_mutex_lock(tr_scr->list_mutex);
478 tr_ctx = trace_rbug_get_context_locked(tr_scr, flush->context);
479
480 if (!tr_ctx) {
481 pipe_mutex_unlock(tr_scr->list_mutex);
482 return -ESRCH;
483 }
484
485 /* protect the pipe context */
486 trace_dump_call_lock();
487
488 tr_ctx->pipe->flush(tr_ctx->pipe, flush->flags, NULL);
489
490 trace_dump_call_unlock();
491 pipe_mutex_unlock(tr_scr->list_mutex);
492
493 return 0;
494 }
495
496 static int
497 trace_rbug_shader_list(struct trace_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
498 {
499 struct rbug_proto_shader_list *list = (struct rbug_proto_shader_list *)header;
500
501 struct trace_screen *tr_scr = tr_rbug->tr_scr;
502 struct trace_context *tr_ctx = NULL;
503 struct trace_shader *tr_shdr = NULL;
504 struct tr_list *ptr;
505 rbug_shader_t *shdrs;
506 int i = 0;
507
508 pipe_mutex_lock(tr_scr->list_mutex);
509 tr_ctx = trace_rbug_get_context_locked(tr_scr, list->context);
510
511 if (!tr_ctx) {
512 pipe_mutex_unlock(tr_scr->list_mutex);
513 return -ESRCH;
514 }
515
516 pipe_mutex_lock(tr_ctx->list_mutex);
517 shdrs = MALLOC(tr_ctx->num_shaders * sizeof(rbug_shader_t));
518 foreach(ptr, &tr_ctx->shaders) {
519 tr_shdr = (struct trace_shader *)((char*)ptr - offsetof(struct trace_shader, list));
520 shdrs[i++] = VOID2U64(tr_shdr);
521 }
522
523 pipe_mutex_unlock(tr_ctx->list_mutex);
524 pipe_mutex_unlock(tr_scr->list_mutex);
525
526 rbug_send_shader_list_reply(tr_rbug->con, serial, shdrs, i, NULL);
527 FREE(shdrs);
528
529 return 0;
530 }
531
532 static int
533 trace_rbug_shader_info(struct trace_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
534 {
535 struct rbug_proto_shader_info *info = (struct rbug_proto_shader_info *)header;
536
537 struct trace_screen *tr_scr = tr_rbug->tr_scr;
538 struct trace_context *tr_ctx = NULL;
539 struct trace_shader *tr_shdr = NULL;
540 unsigned original_len;
541 unsigned replaced_len;
542
543 pipe_mutex_lock(tr_scr->list_mutex);
544 tr_ctx = trace_rbug_get_context_locked(tr_scr, info->context);
545
546 if (!tr_ctx) {
547 pipe_mutex_unlock(tr_scr->list_mutex);
548 return -ESRCH;
549 }
550
551 pipe_mutex_lock(tr_ctx->list_mutex);
552
553 tr_shdr = trace_rbug_get_shader_locked(tr_ctx, info->shader);
554
555 if (!tr_shdr) {
556 pipe_mutex_unlock(tr_ctx->list_mutex);
557 pipe_mutex_unlock(tr_scr->list_mutex);
558 return -ESRCH;
559 }
560
561 /* just in case */
562 assert(sizeof(struct tgsi_token) == 4);
563
564 original_len = tgsi_num_tokens(tr_shdr->tokens);
565 if (tr_shdr->replaced_tokens)
566 replaced_len = tgsi_num_tokens(tr_shdr->replaced_tokens);
567 else
568 replaced_len = 0;
569
570 rbug_send_shader_info_reply(tr_rbug->con, serial,
571 (uint32_t*)tr_shdr->tokens, original_len,
572 (uint32_t*)tr_shdr->replaced_tokens, replaced_len,
573 tr_shdr->disabled,
574 NULL);
575
576 pipe_mutex_unlock(tr_ctx->list_mutex);
577 pipe_mutex_unlock(tr_scr->list_mutex);
578
579 return 0;
580 }
581
582 static int
583 trace_rbug_shader_disable(struct trace_rbug *tr_rbug, struct rbug_header *header)
584 {
585 struct rbug_proto_shader_disable *dis = (struct rbug_proto_shader_disable *)header;
586
587 struct trace_screen *tr_scr = tr_rbug->tr_scr;
588 struct trace_context *tr_ctx = NULL;
589 struct trace_shader *tr_shdr = NULL;
590
591 pipe_mutex_lock(tr_scr->list_mutex);
592 tr_ctx = trace_rbug_get_context_locked(tr_scr, dis->context);
593
594 if (!tr_ctx) {
595 pipe_mutex_unlock(tr_scr->list_mutex);
596 return -ESRCH;
597 }
598
599 pipe_mutex_lock(tr_ctx->list_mutex);
600
601 tr_shdr = trace_rbug_get_shader_locked(tr_ctx, dis->shader);
602
603 if (!tr_shdr) {
604 pipe_mutex_unlock(tr_ctx->list_mutex);
605 pipe_mutex_unlock(tr_scr->list_mutex);
606 return -ESRCH;
607 }
608
609 tr_shdr->disabled = dis->disable;
610
611 pipe_mutex_unlock(tr_ctx->list_mutex);
612 pipe_mutex_unlock(tr_scr->list_mutex);
613
614 return 0;
615 }
616
617 static int
618 trace_rbug_shader_replace(struct trace_rbug *tr_rbug, struct rbug_header *header)
619 {
620 struct rbug_proto_shader_replace *rep = (struct rbug_proto_shader_replace *)header;
621
622 struct trace_screen *tr_scr = tr_rbug->tr_scr;
623 struct trace_context *tr_ctx = NULL;
624 struct trace_shader *tr_shdr = NULL;
625 struct pipe_context *pipe = NULL;
626 void *state;
627
628 pipe_mutex_lock(tr_scr->list_mutex);
629 tr_ctx = trace_rbug_get_context_locked(tr_scr, rep->context);
630
631 if (!tr_ctx) {
632 pipe_mutex_unlock(tr_scr->list_mutex);
633 return -ESRCH;
634 }
635
636 pipe_mutex_lock(tr_ctx->list_mutex);
637
638 tr_shdr = trace_rbug_get_shader_locked(tr_ctx, rep->shader);
639
640 if (!tr_shdr) {
641 pipe_mutex_unlock(tr_ctx->list_mutex);
642 pipe_mutex_unlock(tr_scr->list_mutex);
643 return -ESRCH;
644 }
645
646 /* protect the pipe context */
647 trace_dump_call_lock();
648
649 pipe = tr_ctx->pipe;
650
651 /* remove old replaced shader */
652 if (tr_shdr->replaced) {
653 if (tr_ctx->curr.fs == tr_shdr || tr_ctx->curr.vs == tr_shdr)
654 trace_shader_bind_locked(pipe, tr_shdr, tr_shdr->state);
655
656 FREE(tr_shdr->replaced_tokens);
657 trace_shader_delete_locked(pipe, tr_shdr, tr_shdr->replaced);
658 tr_shdr->replaced = NULL;
659 tr_shdr->replaced_tokens = NULL;
660 }
661
662 /* empty inputs means restore old which we did above */
663 if (rep->tokens_len == 0)
664 goto out;
665
666 tr_shdr->replaced_tokens = tgsi_dup_tokens((struct tgsi_token *)rep->tokens);
667 if (!tr_shdr->replaced_tokens)
668 goto err;
669
670 state = trace_shader_create_locked(pipe, tr_shdr, tr_shdr->replaced_tokens);
671 if (!state)
672 goto err;
673
674 /* bind new shader if the shader is currently a bound */
675 if (tr_ctx->curr.fs == tr_shdr || tr_ctx->curr.vs == tr_shdr)
676 trace_shader_bind_locked(pipe, tr_shdr, state);
677
678 /* save state */
679 tr_shdr->replaced = state;
680
681 out:
682 trace_dump_call_unlock();
683 pipe_mutex_unlock(tr_ctx->list_mutex);
684 pipe_mutex_unlock(tr_scr->list_mutex);
685
686 return 0;
687
688 err:
689 FREE(tr_shdr->replaced_tokens);
690 tr_shdr->replaced = NULL;
691 tr_shdr->replaced_tokens = NULL;
692
693 trace_dump_call_unlock();
694 pipe_mutex_unlock(tr_ctx->list_mutex);
695 pipe_mutex_unlock(tr_scr->list_mutex);
696 return -EINVAL;
697 }
698
699 static boolean
700 trace_rbug_header(struct trace_rbug *tr_rbug, struct rbug_header *header, uint32_t serial)
701 {
702 int ret = 0;
703
704 switch(header->opcode) {
705 case RBUG_OP_PING:
706 rbug_send_ping_reply(tr_rbug->con, serial, NULL);
707 break;
708 case RBUG_OP_TEXTURE_LIST:
709 ret = trace_rbug_texture_list(tr_rbug, header, serial);
710 break;
711 case RBUG_OP_TEXTURE_INFO:
712 ret = trace_rbug_texture_info(tr_rbug, header, serial);
713 break;
714 case RBUG_OP_TEXTURE_READ:
715 ret = trace_rbug_texture_read(tr_rbug, header, serial);
716 break;
717 case RBUG_OP_CONTEXT_LIST:
718 ret = trace_rbug_context_list(tr_rbug, header, serial);
719 break;
720 case RBUG_OP_CONTEXT_INFO:
721 ret = trace_rbug_context_info(tr_rbug, header, serial);
722 break;
723 case RBUG_OP_CONTEXT_DRAW_BLOCK:
724 ret = trace_rbug_context_draw_block(tr_rbug, header, serial);
725 break;
726 case RBUG_OP_CONTEXT_DRAW_STEP:
727 ret = trace_rbug_context_draw_step(tr_rbug, header, serial);
728 break;
729 case RBUG_OP_CONTEXT_DRAW_UNBLOCK:
730 ret = trace_rbug_context_draw_unblock(tr_rbug, header, serial);
731 break;
732 case RBUG_OP_CONTEXT_DRAW_RULE:
733 ret = trace_rbug_context_draw_rule(tr_rbug, header, serial);
734 break;
735 case RBUG_OP_CONTEXT_FLUSH:
736 ret = trace_rbug_context_flush(tr_rbug, header, serial);
737 break;
738 case RBUG_OP_SHADER_LIST:
739 ret = trace_rbug_shader_list(tr_rbug, header, serial);
740 break;
741 case RBUG_OP_SHADER_INFO:
742 ret = trace_rbug_shader_info(tr_rbug, header, serial);
743 break;
744 case RBUG_OP_SHADER_DISABLE:
745 ret = trace_rbug_shader_disable(tr_rbug, header);
746 break;
747 case RBUG_OP_SHADER_REPLACE:
748 ret = trace_rbug_shader_replace(tr_rbug, header);
749 break;
750 default:
751 debug_printf("%s - unsupported opcode %u\n", __FUNCTION__, header->opcode);
752 ret = -ENOSYS;
753 break;
754 }
755 rbug_free_header(header);
756
757 if (ret)
758 rbug_send_error_reply(tr_rbug->con, serial, ret, NULL);
759
760 return TRUE;
761 }
762
763 static void
764 trace_rbug_con(struct trace_rbug *tr_rbug)
765 {
766 struct rbug_header *header;
767 uint32_t serial;
768
769 debug_printf("%s - connection received\n", __FUNCTION__);
770
771 while(tr_rbug->running) {
772 header = rbug_get_message(tr_rbug->con, &serial);
773 if (!header)
774 break;
775
776 if (!trace_rbug_header(tr_rbug, header, serial))
777 break;
778 }
779
780 debug_printf("%s - connection closed\n", __FUNCTION__);
781
782 rbug_disconnect(tr_rbug->con);
783 tr_rbug->con = NULL;
784 }
785
786 PIPE_THREAD_ROUTINE(trace_rbug_thread, void_tr_rbug)
787 {
788 struct trace_rbug *tr_rbug = void_tr_rbug;
789 uint16_t port = 13370;
790 int s = -1;
791 int c;
792
793 u_socket_init();
794
795 for (;port <= 13379 && s < 0; port++)
796 s = u_socket_listen_on_port(port);
797
798 if (s < 0) {
799 debug_printf("trace_rbug - failed to listen\n");
800 return NULL;
801 }
802
803 u_socket_block(s, false);
804
805 debug_printf("trace_rbug - remote debugging listening on port %u\n", --port);
806
807 while(tr_rbug->running) {
808 sleep(1);
809
810 c = u_socket_accept(s);
811 if (c < 0)
812 continue;
813
814 u_socket_block(c, true);
815 tr_rbug->con = rbug_from_socket(c);
816
817 trace_rbug_con(tr_rbug);
818
819 u_socket_close(c);
820 }
821
822 u_socket_close(s);
823
824 u_socket_stop();
825
826 return NULL;
827 }
828
829 /**********************************************************
830 *
831 */
832
833 struct trace_rbug *
834 trace_rbug_start(struct trace_screen *tr_scr)
835 {
836 struct trace_rbug *tr_rbug = CALLOC_STRUCT(trace_rbug);
837 if (!tr_rbug)
838 return NULL;
839
840 tr_rbug->tr_scr = tr_scr;
841 tr_rbug->running = TRUE;
842 tr_rbug->thread = pipe_thread_create(trace_rbug_thread, tr_rbug);
843
844 return tr_rbug;
845 }
846
847 void
848 trace_rbug_stop(struct trace_rbug *tr_rbug)
849 {
850 if (!tr_rbug)
851 return;
852
853 tr_rbug->running = false;
854 pipe_thread_wait(tr_rbug->thread);
855
856 FREE(tr_rbug);
857
858 return;
859 }
860
861 void
862 trace_rbug_notify_draw_blocked(struct trace_context *tr_ctx)
863 {
864 struct trace_screen *tr_scr = trace_screen(tr_ctx->base.screen);
865 struct trace_rbug *tr_rbug = tr_scr->rbug;
866
867 if (tr_rbug && tr_rbug->con)
868 rbug_send_context_draw_blocked(tr_rbug->con,
869 VOID2U64(tr_ctx), tr_ctx->draw_blocked, NULL);
870 }