The short answer is: if you are rated in the aircraft, then both you and the instructor can log PIC time.
Logging PIC is different than acting as PIC. The only thing grey about when you can and can not log PIC time is peoples' understanding of the rules. The requirements to act as PIC are spelled out in FAR 61.3 and 61.31. The requirements to log PIC are spelled out in FAR 61.51.
The person who is acting as PIC is the one who is responsible for the safety of the flight. Only one person can act as PIC, and that person should be decided before the flight. One or more persons can log PIC time for a flight, including persons who may not be qualified to act as PIC.
The conditions under which you can "log" PIC time are spelled out in FAR 61.51. In summary, if you satisfy one of the following conditions, you can log PIC time:
1) You are the sole manipulator of the controls in an aircraft for which you are rated.
2) You are the sole occupant of the aircraft.
3) You are acting as the PIC in an aircraft when more than one pilot is required either by the aircraft type certificate, or by the rules under which the flight is conducted (and you have a private or higher certificate).
4) You are a flight instructor giving dual instruction.
5) You are an ATP acting as PIC and giving instruction on an operation requiring an ATP.
On a dual flight in which the student is rated in the aircraft, both the student and the instructor can log PIC time. The student because of condition 1), and the instructor because of condition 4). This is true even if the student is not qualified to act as PIC. This could be the case when
* the student does not have a current flight review,
* they do not have a valid medical certificate,
* the flight is conducted under IFR but the student does not have an instrument rating (e.g., instruction toward an instrument rating), or
* the flight is in an aircraft that requires a FAR 61.31 endorsement to act as PIC (e.g., a high performance or complex aircraft) that the student does not have.
Condition 2) is what allows student pilots to log PIC time while in solo flight. Student pilots, or pilots not rated in the aircraft in which they are receiving instruction can not log PIC time in dual flight because they do not satisfy all of Condition 1). The ability to log solo time as PIC time was added in 1997 and is retroactive for any time logged before then.
Also consider the case of one pilot flying under the hood, with another pilot as safety pilot. If the safety pilot is qualified to act as PIC and agrees to do so before the flight, both pilots could log PIC time. The pilot flying under the hood because of condition 1), and the safety pilot because of condition 3). Condition 3) is satisfied because the flight rules under which the simulated instrument flight is conducted (FAR 91.109) requires two pilots, and the safety pilot is qualified to act as PIC and has agreed to do so.
Note that there are cases where a pilot could be qualified to act as a safety pilot, but not qualified to act as PIC. For example, if the flight is conducted on an IFR flight plan, but the safety pilot is not instrument rated, then only the pilot (who is instrument rated) can log PIC time. To act as a safety pilot, the pilot need only have the appropriate category and class ratings (FAR 91.109).
I have also heard of cases where flight schools have three people in the plane logging PIC time. A pilot under the hood, a safety pilot "acting" as PIC and a CFI in the back seat giving "instruction". The pilot logs PIC under condition 1), the safety pilot under condition 3) and the CFI under condition 4). FAR 91.109 states that an aircraft to be used for flight instruction must have dual controls, or throwover controls if it is used for instrument instruction, but it does not explicitly state that the CFI must be at the other control station. While a strict reading of the FARs makes such a flight technically legal, I have heard that the FAA frowns on such practices and has told flight schools that do this to stop. After all, what kind of "instruction" is the CFI going to be giving from the back seat.