Index: src/target/armv4_5.c
===================================================================
--- src/target/armv4_5.c	(revision 884)
+++ src/target/armv4_5.c	(working copy)
@@ -477,6 +477,8 @@
 	int exit_breakpoint_size = 0;
 	int i;
 	int retval = ERROR_OK;
+	int bkpt_found;
+	
 	LOG_DEBUG("Running algorithm");
 	
 	if (armv4_5_algorithm_info->common_magic != ARMV4_5_COMMON_MAGIC)
@@ -544,10 +546,20 @@
 		armv4_5->core_cache->reg_list[ARMV4_5_CPSR].valid = 1;
 	}
 
-	if ((retval = breakpoint_add(target, exit_point, exit_breakpoint_size, BKPT_HARD)) != ERROR_OK)
+	/* disable any existing breakpoints/watchpoints
+	 * they will be restored when the target is resumed in a normal context */
+	break_watchpoint_disable(target);
+	
+	/* attempt to find existing breakpoint at exit eddress */
+	bkpt_found = breakpoint_enable(target, exit_point);
+	
+	if (bkpt_found == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
 	{
-		LOG_ERROR("can't add breakpoint to finish algorithm execution");
-		return ERROR_TARGET_FAILURE;
+		if ((retval = breakpoint_add(target, exit_point, exit_breakpoint_size, BKPT_HARD)) != ERROR_OK)
+		{
+			LOG_ERROR("can't add breakpoint to finish algorithm execution");
+			return ERROR_TARGET_FAILURE;
+		}
 	}
 	
 	target_resume(target, 0, entry_point, 1, 1);
@@ -583,7 +595,9 @@
 			buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32)); 
 	}
 	
-	breakpoint_remove(target, exit_point);
+	/* delete added breakpoint, if not already used */
+	if (bkpt_found == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
+		breakpoint_remove(target, exit_point);
 	
 	for (i = 0; i < num_mem_params; i++)
 	{
Index: src/target/armv7m.c
===================================================================
--- src/target/armv7m.c	(revision 884)
+++ src/target/armv7m.c	(working copy)
@@ -300,6 +300,7 @@
 	u32 pc;
 	int i;
 	u32 context[ARMV7NUMCOREREGS];
+	int bkpt_found;
 	
 	if (armv7m_algorithm_info->common_magic != ARMV7M_COMMON_MAGIC)
 	{
@@ -355,16 +356,26 @@
 		armv7m->core_cache->reg_list[ARMV7M_CONTROL].dirty = 1;
 		armv7m->core_cache->reg_list[ARMV7M_CONTROL].valid = 1;
 	}
+
+	/* disable any existing breakpoints/watchpoints
+	 * they will be restored when the target is resumed in a normal context */
+	break_watchpoint_disable(target);
 	
-	/* ARMV7M always runs in Thumb state */
-	if ((retval = breakpoint_add(target, exit_point, 2, BKPT_SOFT)) != ERROR_OK)
+	/* attempt to find existing breakpoint at exit eddress */
+	bkpt_found = breakpoint_enable(target, exit_point);
+	
+	if (bkpt_found == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
 	{
-		LOG_ERROR("can't add breakpoint to finish algorithm execution");
-		return ERROR_TARGET_FAILURE;
+		/* ARMV7M always runs in Thumb state */
+		if ((retval = breakpoint_add(target, exit_point, 2, BKPT_SOFT)) != ERROR_OK)
+		{
+			LOG_ERROR("can't add breakpoint to finish algorithm execution");
+			return ERROR_TARGET_FAILURE;
+		}
 	}
 	
 	/* This code relies on the target specific  resume() and  poll()->debug_entry() 
-	sequence to write register values to the processor and the read them back */
+	 * sequence to write register values to the processor and the read them back */
 	target_resume(target, 0, entry_point, 1, 1);
 	target_poll(target);
 	
@@ -393,7 +404,9 @@
 		}
 	}
 	
-	breakpoint_remove(target, exit_point);
+	/* delete added breakpoint, if not already used */
+	if (bkpt_found == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
+		breakpoint_remove(target, exit_point);
 	
 	/* Read memory values to mem_params[] */
 	for (i = 0; i < num_mem_params; i++)
Index: src/target/breakpoints.c
===================================================================
--- src/target/breakpoints.c	(revision 884)
+++ src/target/breakpoints.c	(working copy)
@@ -246,3 +246,75 @@
 	
 	return ERROR_OK;
 }
+
+int break_watchpoint_disable(target_t *target)
+{
+	breakpoint_t *breakpoint = target->breakpoints;
+	watchpoint_t *watchpoint = target->watchpoints;
+	int retval;
+	
+	/* disable breakpoints/watchpoints but do not delete them */
+	
+	while (breakpoint)
+	{
+		if ((retval = target->type->remove_breakpoint(target, breakpoint)) != ERROR_OK)
+		{
+			switch (retval)
+			{
+				case ERROR_TARGET_NOT_HALTED:
+					LOG_INFO("can't disable breakpoint while target is running");
+					return retval;
+				default:
+					LOG_ERROR("unknown error");
+					exit(-1);
+			}
+		}
+		breakpoint = breakpoint->next;
+	}
+	
+	while (watchpoint)
+	{
+		if ((retval = target->type->remove_watchpoint(target, watchpoint)) != ERROR_OK)
+		{
+			switch (retval)
+			{
+				case ERROR_TARGET_NOT_HALTED:
+					LOG_INFO("can't disable watchpoint while target is running");
+					return retval;
+				default:
+					LOG_ERROR("unknown error");
+					exit(-1);
+			}
+		}
+		watchpoint = watchpoint->next;
+	}
+	
+	return ERROR_OK;
+}
+
+int breakpoint_enable(target_t *target, int address)
+{
+	int retval;
+	breakpoint_t *breakpoint;
+	
+	/* attempt to find breakpoint */
+	breakpoint = breakpoint_find(target, address);
+	
+	if (breakpoint == NULL)
+		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
+	
+	if ((retval = target->type->add_breakpoint(target, breakpoint)) != ERROR_OK)
+	{
+		switch (retval)
+		{
+			case ERROR_TARGET_NOT_HALTED:
+				LOG_INFO("can't enable breakpoint while target is running");
+				return retval;
+			default:
+				LOG_ERROR("unknown error");
+				exit(-1);
+		}
+	}
+	
+	return ERROR_OK;
+}
Index: src/target/breakpoints.h
===================================================================
--- src/target/breakpoints.h	(revision 884)
+++ src/target/breakpoints.h	(working copy)
@@ -65,6 +65,8 @@
 extern breakpoint_t* breakpoint_find(struct target_s *target, u32 address);
 extern int watchpoint_add(struct target_s *target, u32 address, u32 length, enum watchpoint_rw rw, u32 value, u32 mask);
 extern int watchpoint_remove(struct target_s *target, u32 address);
+extern int break_watchpoint_disable(struct target_s *target);
+extern int breakpoint_enable(struct target_s *target, int address);
 
 #endif /* BREAKPOINTS_H */
 

