Subversion Repositories HelenOS

Rev

Rev 2941 | Rev 2943 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2008 Jiri Svoboda
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup debug
  30.  * @{
  31.  */
  32. /** @file
  33.  */
  34.  
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <sys/types.h>
  38. #include <udebug.h>
  39.  
  40. #include <kernel/arch/context_offset.h>
  41.  
  42. #include "../../../cons.h"
  43. #include "../../../main.h"
  44. #include "../../../include/arch.h"
  45.  
  46. #define OPCODE_INT3     0xCC
  47.  
  48. void arch_breakpoint_add(uintptr_t addr)
  49. {
  50.     char brkp[1];
  51.     int rc;
  52.     breakpoint_t *brk;
  53.     int i;
  54.  
  55.     brk = NULL;
  56.     for (i = 0; i < MAX_BRKPTS; i++)
  57.         if (brk_list[i].set == 0) { brk = brk_list+i; break; }
  58.  
  59.     if (!brk) {
  60.         cons_printf("too many breakpoints\n");
  61.         return;
  62.     }
  63.  
  64.     rc = udebug_mem_read(app_phone, &brk->arch.back, addr, 1);
  65.     cons_printf("udebug_mem_read() -> %d\n", rc);
  66.     brkp[0] = OPCODE_INT3;
  67.     rc = udebug_mem_write(app_phone, brkp, addr, 1);
  68.     cons_printf("udebug_mem_write() -> %d\n", rc);
  69.  
  70.     brk->addr = addr;
  71.     brk->set = 1;
  72. }
  73.  
  74. static breakpoint_t *lifted_brkpt;
  75.  
  76. void arch_event_breakpoint(thash_t thread_hash)
  77. {
  78.     static istate_t istate;
  79.     int rc;
  80.  
  81.     rc = udebug_regs_read(app_phone, thread_hash, &istate);
  82. //  cons_printf("udebug_regs_read -> %d\n", rc);
  83. //  cons_printf("EIP was 0x%08x\n", istate.eip);
  84.     int brk_addr = istate.eip - 1;
  85.     int bi;
  86.     for (bi = 0; bi < MAX_BRKPTS; bi++) {
  87.         if (brk_list[bi].set && brk_list[bi].addr == brk_addr)
  88.             break;
  89.     }
  90.  
  91.     if (bi < MAX_BRKPTS) {
  92.         cons_printf("breakpoint %d hit\n", bi);
  93.  
  94.         istate.eip = brk_addr;
  95.         istate.eflags |= 0x0100; /* trap flag */
  96. //      cons_printf("setting EIP to 0x%08x\n", istate.eip);
  97.         rc = udebug_regs_write(app_phone, thread_hash, &istate);
  98.         if (rc < 0) { printf("error writing regs\n"); return; }
  99.             rc = udebug_mem_write(app_phone, &brk_list[bi].arch.back, brk_addr, 1);
  100.         if (rc < 0) { printf("error writing mem\n"); return; }
  101. //      cons_printf("udebug_mem_write(phone, 0x%x, 0x%02x, 1) -> %d\n", brk_addr, brk_list[bi].arch.back, rc);
  102.         lifted_brkpt = &brk_list[bi];
  103.  
  104.         breakpoint_hit();
  105.     } else {
  106.         cons_printf("unrecognized breakpoint at 0x%x\n", brk_addr);
  107.     }
  108. }
  109.  
  110. void arch_event_trap(dthread_t *dt)
  111. {
  112.     static istate_t istate;
  113.     unsigned char brkinstr[1];
  114.     int rc;
  115.  
  116. //  cons_printf("trap event\n");
  117.  
  118.     breakpoint_t *lb = lifted_brkpt;
  119.     if (lb) {
  120.         brkinstr[0] = OPCODE_INT3;
  121.         rc = udebug_mem_write(app_phone, brkinstr, lb->addr, 1);
  122. //      cons_printf("restore breakpoint -> %d\n", rc);
  123.         lifted_brkpt = NULL;
  124.     }
  125.  
  126.     if (!dt->arch.singlestep) {
  127.         rc = udebug_regs_read(app_phone, dt->hash, &istate);
  128. //      cons_printf("udebug_regs_read -> %d\n", rc);
  129.         istate.eflags &= ~0x0100; /* trap flag */
  130.         rc = udebug_regs_write(app_phone, dt->hash, &istate);
  131.     } else {
  132. //      printf("ss-hit\n");
  133.         singlestep_hit();
  134.     }
  135. }
  136.  
  137. void arch_dump_regs(thash_t thash)
  138. {
  139.     static istate_t istate;
  140.     int rc;
  141.  
  142.     printf("arch_dump_regs...\n");
  143.  
  144.     rc = udebug_regs_read(app_phone, thash, &istate);
  145.     if (rc < 0) { cons_printf("Error reading regs\n"); return; }
  146.  
  147.     cons_printf(
  148.         "eip:%08x eflags:%08x eax:%08x ebx:%08x ecx:%08x edx:%08x\n"
  149.         "esi:%08x edi:%08x cs:%04x ds:%04x es:%04x fs:%04x gs:%04x\n",
  150.         istate.eip, istate.eflags, istate.eax, istate.ebx,
  151.         istate.ecx, istate.edx, istate.esi, istate.edi, istate.cs,
  152.         istate.ds, istate.es, istate.fs, istate.gs);
  153. }
  154.  
  155. void arch_set_singlestep(dthread_t *dt, int enable)
  156. {
  157.     static istate_t istate;
  158.     int rc;
  159.  
  160.     rc = udebug_regs_read(app_phone, dt->hash, &istate);
  161.     if (rc < 0) { printf("regs read failed\n"); return; }
  162.  
  163.     if (enable) istate.eflags |= 0x0100; /* trap flag */
  164.     else istate.eflags &= ~0x0100; /* trap flag */
  165.  
  166.     rc = udebug_regs_write(app_phone, dt->hash, &istate);  
  167.     if (rc < 0) { printf("regs write failed\n"); return; }
  168.  
  169.     dt->arch.singlestep = enable;
  170. }
  171.  
  172. /** @}
  173.  */
  174.